Detecting faces from the webcam in JavaScript

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

As we looked at the face detection API yesterday, let's see how we can make it like the barcode from the webcam example.
We will be doing this one a little bit differently, due to the high drawing rate, we will draw everything on the canvas, which makes it easier to draw the detection.
The end goal for today is cool face detection from a webcam input.

For the first part, let's look at how we can capture the video input, and write it to a canvas.
For our HTML we can use the following:
<canvas></canvas>
Now let's introduce the basic onload to call an asynchronous function again.
window.onload = () => {
detect();
};
Inside our detect function, we can have a play with getting the canvas and writing the video stream to it. But first, let's define all our variables.
async function detect() {
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
const faceDetector = new FaceDetector({fastMode: true});
const mediaStream = await navigator.mediaDevices.getUserMedia({
video: {facingMode: 'environment'}
});
const video = document.createElement('video');
video.srcObject = mediaStream;
video.autoplay = true;
video.onloadedmetadata = () => {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
};
}
However this will not write the actual video yet, this will need to be based on the loop to write every change.
So let's add the loop already.
(function renderLoop() {
requestAnimationFrame(renderLoop);
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
})();
That will give us the webcam output on the screen.

So now that we have the webcam on the canvas, let's look at how we can implement our face detection.
We will change our loop to call the render function.
(function renderLoop() {
requestAnimationFrame(renderLoop);
render();
})();
This render function in return will call the face detector API.
function render() {
faceDetector
.detect(video)
.then(faces => {
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
})
.catch(console.error);
}
Now let's draw an outline for each face we find.
context.strokeStyle = '#FFFF00';
context.lineWidth = 5;
faces.forEach(face => {
const {top, left, width, height} = face.boundingBox;
context.beginPath();
context.rect(left, top, width, height);
context.stroke();
});
And it should give us a result like this.

If you have the flag enabled in Chrome.
Open Chrome, and type the following address: chrome://flags, in there enable the #enable-experimental-web-platform-features.
You should be able to try out this Codepen as well.
This API is unfortunately not publicly available, so browser support can't be provided at this stage. However, is a very cool one to look out for!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter