question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: toNetInput - Problem with canvas.loadImage() + faceapi.detectAllFaces()

See original GitHub issue

Hey!

My name is Gabriel and I’m using face-api.js to apply FaceRecognition on my final essay, on my way to graduate.

With the objective of trying the face-api and understand how it works, I created the following test.js:

import faceapi from "face-api.js";

import * as canvas from 'canvas';
const { Canvas, Image, ImageData } = canvas;
faceapi.env.monkeyPatch({ Canvas, Image, ImageData });

const faceDetectionNet = faceapi.nets.ssdMobilenetv1;
const minConfidence = 0.5;
const faceDetectionOptions = new faceapi.SsdMobilenetv1Options({ minConfidence });

async function runRecognition() {    
    await faceDetectionNet.loadFromDisk('./weights');
    await faceapi.nets.faceLandmark68Net.loadFromDisk('./weights');
    await faceapi.nets.faceRecognitionNet.loadFromDisk('./weights');

    const REFERENCE_IMAGE = './bbt1.jpg';
    const QUERY_IMAGE = './bbt2.jpg';

    const referenceImage = await canvas.default.loadImage(REFERENCE_IMAGE);
    const queryImage = await canvas.default.loadImage(QUERY_IMAGE);

    const detections_ref = await faceapi.detectAllFaces(referenceImage, faceDetectionOptions)
        .withFaceLandmarks()
        .withFaceDescriptors();

    const detections_query = await faceapi.detectAllFaces(queryImage, faceDetectionOptions)
        .withFaceLandmarks()
        .withFaceDescriptors();

    const faceMatcher = new faceapi.FaceMatcher(detections_ref);

    detections_query.forEach(fd => {
        const bestMatch = faceMatcher.findBestMatch(fd.descriptor);
        console.log(bestMatch.toString());
    });
}

runRecognition();

But I wasn’t able to make it work, right now, I’m getting the following error: “UnhandledPromiseRejectionWarning: Error: toNetInput - expected media to be of type HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | tf.Tensor3D, or to be an element id at C:\Users\gabri\Desktop\TCC\face-api-teste\node_modules\face-api.js\build\commonjs\dom\toNetInput.js:38:35”

Does anyone have any idea of what the problem is?

Thanks for the help 😄

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
vladmandiccommented, Nov 18, 2020

what is the result and type of const referenceImage = await canvas.default.loadImage(REFERENCE_IMAGE); ?

if you’re not drawing on canvas in nodejs, you don’t actually need 3rd party libraries and monkey patches just to load image and use it in face-api - you can use native tfjs-node methods directly, take a look at https://github.com/vladmandic/face-api/blob/master/example/node.js, specifically ‘image()’ method.

0reactions
vladmandiccommented, Jun 6, 2022

@juancruzsosagab @eze-villegas this issue was successfully resolved which you can see if you read through it. and posting “me too” on an issue that was closed 1.5 years ago does not help. if you have a problem, document it and open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error with face-api - Error: toNetInput - expected media to be of ...
I did post an Issue to github Face-API page, and there I was able to found a solution, so, if anyone would like...
Read more >
face-api.js - Bountysource
js with 3 comments. Hi there, i bump into this issue whenever i run this code const detections = await faceapi.detectAllFaces(img).withFaceLandmarks().
Read more >
face-api.js
Detecting Faces. Detect all faces in an image. Returns Array<FaceDetection>: const detections = await faceapi.detectAllFaces(input).
Read more >
face-api.js.detectAllFaces JavaScript and Node.js ... - Tabnine
src/libs/FacialRecognition.js/undefined/drawFaceOutlines · drawFaceOutlines(imagePathOrUrl) { · await this.init() · const img = await canvas.loadImage( ...
Read more >
JavaScript API for face detection | Computer Vision library
Implement face-api.js with how-to, Q&A, fixes, code snippets. kandi ratings ... Error with face-api - Error: toNetInput - expected media to be of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found