Abrar Hossain
Abrar Hossain

Reputation: 2702

face-api.js load image file from disk

I tried the following to load an image from disk using face-api.js:

faceapi.fetchImage(path.resolve(INPUT_DIR, 'input1.jpg');

It throws the following error: Error: fetch - missing fetch implementation for nodejs environment

Is there any other way of load image from disk and displaying using nodejs?

Upvotes: 2

Views: 4187

Answers (2)

Boris Yakubchik
Boris Yakubchik

Reputation: 4453

The documentation recommends you use the canvas to do something like this:

const canvas = require('canvas');
faceapi.env.monkeyPatch({ Canvas, Image })
const img = await canvas.loadImage('./img.jpg');
const detections = await faceapi.detectSingleFace(img);

fetchImage will not work with local files:

faceapi.fetchImage as the name implies uses fetch under the hood, thus it doesn't work with filepaths to local files.

Upvotes: 4

Shadab Faiz
Shadab Faiz

Reputation: 2508

The reason it fails because you need to implements the fetch function for it to work. FetchImage should be used when you are trying to retrieve the image from online. If you are using the image from local disk, then do this:

  1. Load the modules from local disk.

    await faceapi.nets.ssdMobilenetv1.loadFromDisk(path.join(__dirname, 'models'));
    

    // for me, I have put all the modules in src/models/

  2. Load the canvas

  3. Pass the canvas and modules option (if any)

    let detectionresult = awaitfaceapi.detectAllFaces(canvas, this.getSSNMobileOptions())
    

Upvotes: 2

Related Questions