Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
أخبار العالم

How to implement pose estimation in a browser using your webcam

[ad_1]

Coding time

We are using two libraries here:

  • ml5.js for creating and running our ML model.
  • p5.js for getting the webcam video feed and displaying output in our browser.

I’ve added extensive documentation inside the code, explaining every single line. Here, we will discuss the main crux which is the majority of the code anyway.

Our code consists of two files:

  • poseNet_webcam.js, our JavaScript code.
  • index.html, the main page to show output.

PoseNet_webcam.js

  • function setup(). The first function that is executed and runs only once. We will do our initial setup in it.
  • function draw(). This function is called on repeat forever (unless you plan on closing the browser or pressing the power button).

createCanvas(width, height) is provided by p5 to create a box in the browser to show our output. Here, canvas has width: 640px and height: 480px.

createCapture(VIDEO) is used to capture a webcam feed and return a p5 element object, which we will name webcam_output. We set the webcam video to the same height and width of our canvas.

ml5.poseNet() creates a new PoseNet model, taking as input:

  • Our present webcam output.
  • A callback function, which is called when the model is successfully loaded. Inside our index.html file, we have created an HTML paragraph with an ID status showing the current status text to the user. We change that text to Model Loaded for the user to know, as the model takes a bit to load.

poseNet.on() is a trigger or event listener. Whenever the webcam gives a new image, it is given to the PoseNet model.

The moment pose is detected and output is ready. It calls function(results), where results is the final output of keypoints and scores given by the model.

We store this in our poses array, which is globally defined and can be used anywhere in our code. webcam_output.hide() hides the webcam output for now, as we will modify the images and show the image with detected points and lines later.

All we have left to do is to show the image with all the detection results stored in poses in the browser.

As we know, the draw() function runs in a loop forever. Inside this, we call the image() function to display our image (as we have our video image-by-image) in the canvas.

It takes five arguments:

  • input image. The image we want to display.
  • x position. The x-coordinate of the top-left corner of the image in respect to the canvas.
  • y position. The y-coordinate of the top-left corner of the image in respect to the canvas.
  • width. The width to draw the image.
  • height. The height to draw the image.

We then call drawKeyPoints() and drawSkeleton() to draw the dots and lines on the current image. draw() does this in an infinite loop, hence showing a continuous output to the user, which makes it look like a video.