Convert File to HTMLImageElement

Viewed 666

I have pretty hard task to solve, so i am building face recognition and face detection needs HTMLImageElement as argument, but problem is that i can not install node-canvas to polyfill it so i need another way to change file sent from client to HTMLImageElement, is there any way to do it? and also to polyfill canvas. here is my code

    const imgBuffer = await sharp(path.resolve(__dirname, '..', 'queryImages', file.filename))/*.toBuffer()*/;
    //const imgTensor = faceapi.tf.tensor3d(new Uint8Array(imgBuffer), [480,640,3]);
    const detections = await faceapi.detectAllFaces(imgBuffer);

and error

(node:3620) UnhandledPromiseRejectionWarning: Error: toNetInput - expected media to be of type HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | tf.Tensor3D, or to be an element id

1 Answers

i guess you took a ready trained model from tensorflow.js and they don't run with node.

If you want to use the example with Node, then you need to prepare the image with tensorflow beforehand:

const tf = require('@tensorflow/tfjs-node');
const util = require('util');
const fs = require('fs');

const readImg = util.promisify(fs.readFile);

const img = await readImg('./<pathToYourImage>/<yourImage>.jpg');
const adaptedImg = tf.node.decodeImage(img, 3);
const model = await <someModel>.load();
const predictions = await model.detect(adaptedImg);
Related