3

i tried the following two ways o fetch image from localserver in node js in face-api.js a and output of the following codes are commemnted

should i missing something or i need to try different way please help..

1st try

   var image = fs.readFileSync(path);
            console.log('Exists ' + typeof (image)) // Exists object


            const image = await faceapi.fetchImage(image)
                .then(res =>{console.log(res)})
                .catch(e=> console.log("Error e "+e)) //Error e Type Error: Only absolute URLs are supported

i wanted to fetch images from loccal server and train the model but i'm not able to fetch the image. the full error is below:-

(node:25194) UnhandledPromiseRejectionWarning: TypeError: Only absolute URLs are supported
    at getNodeRequestOptions (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/node-fetch/lib/index.js:1299:9)
    at /home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/node-fetch/lib/index.js:1404:19
    at new Promise (<anonymous>)
    at fetch (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/node-fetch/lib/index.js:1401:9)
    at Object.<anonymous> (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tfjs-image-recognition-base/build/commonjs/dom/fetchOrThrow.js:12:42)
    at step (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:136:27)
    at Object.next (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:117:57)
    at /home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:106:16)

ps i already checked this face-api.js load image file from disk

Edit:

Repository
        -temp
          image.png
        server.js  
        -Router
          router.js <- here i'm having the below router code

router.post('/upload-file-face',  (req, res) => {
    console.log("Helllo");
    Promise.all([
      faceapi.nets.faceRecognitionNet.loadFromDisk('./weights'),
      faceapi.nets.faceLandmark68Net.loadFromDisk('./weights'),
      faceapi.nets.ssdMobilenetv1.loadFromDisk('./weights'),
    ]).then(async () => {
    tempUpload(req,res, async(err) =>{
        console.log(req.files);
        if(err) {
            return res.end("Error uploading file." + err);
        } 
        var location = "./"+req.files[0].destination
        var imgFile = req.files[0].filename;
        if(!fs.existsSync(fspath.join(location,imgFile))) {
         console.log("Not exists")
      }else{
        console.log('Exists')  //yes Exists
      }

      var pp =fspath.join(location,imgFile);
      console.log("path : "+pp)
        const image =  faceapi.fetchImage(pp)
            .then(res =>{console.log(res)})
            .catch(e=> console.log("Error e " +e)) // Error e TypeError: Only absolute URLs are supported

    res.send("hello");
  })
    })
})
Milind Modi
  • 321
  • 1
  • 4
  • 12

2 Answers2

0

According to the docs the faceapi.fetchImage accepts an path, not filecontent/buffer.

So, instead of doing var image = fs.readFileSync(path) you should just do:

try{
    const image = await faceapi.fetchImage(path)
} catch (e){
    console.log("Error e "+e)
}
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35
  • Note that in your example code `path` would only be an online resource (or one served by your local server), not a local file on the HD. – Boris Yakubchik Jan 20 '20 at 17:59
0

fetchImage will not work with a local resource, because "fetch" is for the web.

You'll want to npm install canvas and then simply do this:

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

See another discussion: https://stackoverflow.com/a/59828665/5017391

Boris Yakubchik
  • 3,861
  • 3
  • 34
  • 41