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.

Webcam feed on NodeJs environment

See original GitHub issue

Hi,

I would like to perform live face detection in a nodejs environment but I don’t quiet see how to do that.

Initially, I was capturing frames from my webcam using opencv4nodejs:

cap = new cv.VideoCapture(videoDeviceId);
(...)
_frame = cap.read();

But then I would have to find a way to convert _frame to a Canvas, right ? At least if I’m using a monkey path like this:

const { Canvas, Image, ImageData } = canvas
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })

Do I have other options ? Would it be possible to use a monkey patch with some opencv4nodejs’ elements ?

I saw this line in README.md:

Alternatively you can simply construct your own tensors from image data and pass tensors as inputs to the API.

Okay, but how can I achieve that ? If I look at detectAllFaces’ signature, I can see the following:

detectAllFaces(input: TNetInput, options?: FaceDetectionOptions): DetectAllFacesTask

And if I look further, I can see that TNetInput can be a TNetInputArg, which can be a TResolvedNetInput, which can be a tf.Tensor3D or tf.Tensor4D.

So I would have to retrieve data from _frame and use them to instantiate a tf.Tensor3D ?

Thanks !

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
mamackercommented, Jun 9, 2019

In case someone comes along after me looking for exactly the code needed. As a repo: https://github.com/mamacker/faceme

import * as tfjs from '@tensorflow/tfjs-node';
import * as faceapi from 'face-api.js';
import * as fs from 'fs';
import * as cv from 'opencv';

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

let globalFrame = null;
let lastFrame = null;
function processFrame() {
  if (globalFrame == null) return;
  if (lastFrame == globalFrame) return;

  lastFrame = globalFrame;
  let tFrame = faceapi.tf.tensor3d(globalFrame, [480, 640, 3])
  faceapi.detectAllFaces(tFrame, faceDetectionOptions).then((faces) => {
    console.log(faces);
  });
}
setInterval(processFrame, 100);

async function run() {
  await faceDetectionNet.loadFromDisk('./weights');
  let video = '/dev/video0';
  let cap = new cv.VideoCapture(video);
  let capture = () => {
    cap.read(function(err, frame) {
      if (frame.width() > 0) {
        let data = new Uint8Array(frame.getData().buffer);
        globalFrame = data;
      }

      setTimeout(capture, 0);
    });
  }
  capture();
}

run()
1reaction
msvargascommented, Jan 24, 2020

Thank you @mamacker for the code 🙇‍♂️ Thanks to you I was able to remove canvas from my project 🙇‍♂️

Full code: https://github.com/whyboris/extract-faces-node

I’m using sharp for image loading (as I need other image manipulations later)

Relevant piece:

const sharp = require('sharp');
const imgBuffer: Buffer = await sharp('./some/img/path.jpg').toBuffer();
const imgTensor = tf.node.decodeJpeg(imgBuffer);
const detections = await faceapi.detectAllFaces(imgTensor);

This the correct mode, using decodeJpeg and most faster

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access Webcam with NodeJS - Medium
Want to create a Web application to stream webcams, capture and upload images in browsers? You can use a JavaScript webcam library and...
Read more >
Show webcam feed, capture images and save them locally in ...
I implemented the image capture part using a third-party npm module called 'node-webcam': const nodeWebCam = require('node-webcam'); const ...
Read more >
How to Integrate Real-time Webcam on a Web Page Using ...
Step 3 - Setting up Index.html · Step 4 - Referencing Script. · Step 5 - Integrating Webcam on the Web Page With...
Read more >
Tutorial: Stream live with Media Services by using Node.js and ...
Examine the TypeScript code for live streaming · Start using the Media Services SDK for Node. · Setting the longRunningOperationUpdateIntervalMs.
Read more >
How to access webcam and take photo with JavaScript
webcam -easy.js a JavaScript module I built for accessing webcam stream, it can allows us to capture a picture from the webcam. Below...
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