justadudewhohacks / face-api.js

JavaScript API for face detection and face recognition in the browser and nodejs with tensorflow.js
MIT License
16.75k stars 3.73k forks source link

Node js real time #604

Open tonxxd opened 4 years ago

tonxxd commented 4 years ago

Hello, thank you very much for this library! I'm trying to run a small face detection program in node js. It takes as input a frame read from the webcam and output the detection object. I see that the browser version does realtime detection while with the node version it takes around 350ms per frame which is very low compared.

I took some measures of just the prediction step

console.time("detect")
const detections = await faceapi.detectAllFaces(frame)
console.timeEnd("detect")

detect: 442.732ms detect: 363.256ms detect: 365.847ms detect: 338.513ms detect: 334.374ms detect: 340.412ms detect: 324.549ms detect: 333.849ms detect: 327.226ms detect: 325.315ms detect: 322.517ms detect: 322.398ms

I am already importing the tfjs-node library and I am running the program on a 2,6 GHz Intel Core i7 quad-core 16gb ram MacBook.

I just want to know if that frame rate is common or I am facing slow performances.

Thank you

ChrisDalley commented 4 years ago

@tonxxd - I'm at pretty much the same point as you and coming up against this issue running tfjs in Node on a Rasp Pi 4. The best performance I can get at the moment is around 550-600ms....

Detecting Faces: 595.007ms Detecting Faces: 588.997ms Detecting Faces: 549.516ms Detecting Faces: 616.711ms Detecting Faces: 580.152ms Detecting Faces: 562.429ms Detecting Faces: 540.243ms Detecting Faces: 551.98ms Detecting Faces: 532.101ms Detecting Faces: 578.383ms

From reading a number of other issues, here are some things to check.

    "@tensorflow/tfjs-core": "1.7.0",
    "@tensorflow/tfjs-node": "1.7.0",
    "@tensorflow/tfjs-node-gpu": "1.7.0",
    "face-api.js": "0.22.2",
tonxxd commented 4 years ago

Hi @ChrisDalley thanks for the quick response!

I tried the tiny face detector and it gives much higher performances (~ 60ms per frame). Changing to node-gpu didn't increase performances (strange(?)).

Anyway, what I don't understand is why the node version is slower than the browser one when tensorflow has access to the Native C modules in the node env. I have the exact same app in python (with tensorflow for python and opencv-python) and I get real-time detections without issues.

ChrisDalley commented 4 years ago

I'm honestly not too sure on the difference between Python and the node implementation, it's my first time touching this library today. I'm going to do some more digging over the next few days and see what I can figure out - even getting down to 60ms for me would be better than where it's at right now!

Maybe @justadudewhohacks knows more about the difference between Python + Node / where we are going wrong performance wise?

tonxxd commented 4 years ago

@ChrisDalley yes but that was on my MacBook, since I plan to run the app on fewer resources I am looking for a better strategy as you

chanduthedev commented 4 years ago

@ChrisDalley could you able to get any improvement? I am also facing same issue in my MacBook. It is taking ~7.5 sec for each image which is quite a lot in these days. I used GPU as well, but not much difference. Please help if you got any breakthrough.

Thanks in advance.