<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shivam Sinha on Medium]]></title>
        <description><![CDATA[Stories by Shivam Sinha on Medium]]></description>
        <link>https://medium.com/@sinhashivam.10?source=rss-b6a45c05a54a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LwYHYFSX6-nlAxG0</url>
            <title>Stories by Shivam Sinha on Medium</title>
            <link>https://medium.com/@sinhashivam.10?source=rss-b6a45c05a54a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 03:15:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sinhashivam.10/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building up a Facial Detection App with the help of Tensorflow.js]]></title>
            <link>https://medium.com/spidernitt/building-up-a-facial-detection-app-with-the-help-of-tensorflow-js-122fcdc13b2a?source=rss-b6a45c05a54a------2</link>
            <guid isPermaLink="false">https://medium.com/p/122fcdc13b2a</guid>
            <category><![CDATA[facedetection]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tensorflowjs]]></category>
            <category><![CDATA[spiders]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Shivam Sinha]]></dc:creator>
            <pubDate>Wed, 08 Dec 2021 12:31:07 GMT</pubDate>
            <atom:updated>2021-12-08T12:31:07.734Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Building a Facial Detection App with Tensorflow.js</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrBuponCt2tn5s1yQxee7g.jpeg" /></figure><p>Have you ever wondered how your smartphone’s camera can quickly detect your face and give those highlighted boxes? How does it detect faces, and what’s the working principle behind them?</p><p>Well, in this article, we’ll get to the insights of Facial Detection and create our very own Facial Detection App. Let’s start with the basics…</p><p><strong>What is Facial Detection?</strong></p><p>Facial Detection is an artificial intelligence (AI) based computer technology used to find and identify human faces in digital images. It just means that a system can identify a human face present in an image or video.</p><p>Now let’s look at how it works.</p><p><strong>How does Facial Detection works?</strong></p><p>In principle, face detection applications use machine learning techniques and algorithms to detect human faces within larger images. These larger images might contain numerous objects that aren’t faces such as landscapes, buildings, and other parts of humans (e.g., legs, shoulders, and arms).<br>There are some steps that how to face detection operates, which are as follows-<br>Firstly the image is obtained, and it is converted from RGB(Red-Green-Blue) to Grayscale, as it’s easier to detect the objects in grayscale compatibility.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*gIFnYHuOMd35lykxAQF-PA.png" /><figcaption>Converting RGB image to Grayscale.</figcaption></figure><p>The next step is to use the <a href="https://en.wikipedia.org/wiki/Haar-like_feature">Haar<strong>-</strong>Like</a> features algorithm, which the Viola-Jones object detection framework use for face detection propose. This algorithm is used to find the human faces’ location in a frame or image. All human faces share some universal properties of the human face. The eyes region is darker than its neighbor pixels, and the nose region is brighter than the eye region. It is also used for feature selection for an object in an image. Edge detection, line detection, and center detection are used for detecting eyes, nose, mouth, etc., in the picture. It is used to select the essential features in an image and extract these features for face detection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_OKWD4hQTCAlpsTdiAF8SA.jpeg" /><figcaption>Haar-like features are depicted for face detection.</figcaption></figure><p>The next step is to give the coordinates of <em>x, y, w, h,</em> which makes a rectangle box in the picture to show the location of the face, or we can say that to show the region of interest in the image. After this, it can make a rectangle box anywhere it detects the face.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*X00jr_rVYEXautai3W9eag.png" /><figcaption>Face detection of an image</figcaption></figure><p>Well, that sums up the working, and now it’s time to make our very own version of the face detection app with a live webcam in the browser.</p><p><strong>Providing Access to the webcam</strong></p><p>Firstly, we need to give access to your webcam in the browser. We’ll start with creating a basic <em>index.html</em> file, where access to the webcam will be provided with the help of the video tag.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/789e089d46c84f97d4697e6811fdb71b/href">https://medium.com/media/789e089d46c84f97d4697e6811fdb71b/href</a></iframe><p>Now, let’s create another file called <em>script.js,</em> where we’ll write the Javascript code and include it in <em>index.html</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb1195907096a4205692bf25e7187579/href">https://medium.com/media/cb1195907096a4205692bf25e7187579/href</a></iframe><p>Here we are accessing the video tag element and storing it in the variable video. The function <em>accessCamera</em>, calls the <em>getUserMedia</em> API, and we specify the video dimensions(I have taken 500 px width and 400px height, you can choose the dimensions as per your wish). <br>The <em>getUserMedia</em> function returns a promise with the <em>MediaStream</em> object, which we assign to the video element’s <em>srcObject</em> property. Finally, we call the <em>accessCamera</em> function.<br>Now, to run this, we can either use <a href="https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer">live server</a> extension or can directly open the <em>index.html</em> file. The browser will ask permission to access the camera, allow it, and you should see the webcam feed on the screen.</p><p><strong>Executing the Face Detection</strong></p><p>For this, we’ll be using <a href="https://github.com/tensorflow/tfjs-models/tree/master/blazeface"><em>Blazeface</em></a> model from the Simple Face Detection model in <a href="https://www.tensorflow.org/js/models"><em>tensorflow.js</em></a><em>. Blazeface </em>is a lightweight model used for detecting faces in images.</p><p>Now, from the <em>Blazeface</em> model we’ll be picking up the following script tags and adding them to our <em>index.html</em> file:</p><pre>&lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow/tfjs&quot;&lt;/script&gt;</pre><pre>&lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow-models/blazeface&quot;&gt;&lt;/script&gt;</pre><p>Let’s head back to our <em>script.js</em> file, where we’ll create a variable named <em>model</em> that’ll load the <em>Blazeface</em> model inside it.</p><pre>let model;</pre><pre>model = blazeface.load();</pre><p>Now, we’ll create a function <em>detectFaces</em> which would perform the facial detection and give us the necessary output.</p><pre>const detectFaces = async () =&gt; {<br>  const prediction = await model.estimateFaces(video, false);   <br>};</pre><p>Since <em>blazeface.load()</em> is an asynchronous function, it takes some time to load, but at the same time, <em>detectFaces also </em>get called up, so to avoid this, we will first wait for the video feed to load from the webcam. Once that’s completed, we’ll wait for the model to load then call up the <em>detectFaces</em>. On loading the video feed, there is an event listener called <em>loadeddata</em>, which we’ll be using for this purpose.</p><p>Now, our script.js file should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0810f99946f15b1d0c6afdb634d1e64/href">https://medium.com/media/c0810f99946f15b1d0c6afdb634d1e64/href</a></iframe><p>The prediction will list the objects where each object corresponds to a detected face. Inside each object, we’ll find the top left and bottom-right coordinates, also the six face landmarks corresponding to eyes, ears, nose, and mouth.<br>Now, let’s use these coordinates to display the results in the rectangular box. We must first iterate through the predictions array and draw a rectangle or points for the face landmarks. We’ll be using HTML canvas for this, and we’ll match the width and height the same as the video feed. Since we don’t need to display the contents of the video tag anymore, we’ll set its display property to none.</p><p>This will be our final <em>index.html</em> file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf9e701a1d839aef14fc88378de96e8e/href">https://medium.com/media/cf9e701a1d839aef14fc88378de96e8e/href</a></iframe><p>Next, we need to create a variable for the canvas object and create a 2D context. After that, we need to draw the current video feed onto the canvas, followed by a rectangle to detect the face, which can be done with the help of <em>drawImage</em> and <em>rect</em> functions of HTML canvas.</p><p>Since <em>detectFaces</em> get called up only once, so to see the output in the video, we need to keep calling it. We’ll use the <em>setInterval</em> method and call it every 40 milliseconds to get the output of around 24 frames per second.</p><p>Here’s the final <em>script.js</em> code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80b8bd9a6451eb7d3c1d28902bfc5f1a/href">https://medium.com/media/80b8bd9a6451eb7d3c1d28902bfc5f1a/href</a></iframe><p>Here’s the live <a href="https://steven4869.github.io/Face-Detect/">demo</a> version to see the final result. It may take up a few seconds when it loads for the first time.</p><p><strong>Conclusion</strong></p><p>Well, that’s all it takes to create a facial detection app.</p><p>We have used <em>tensorflow.js</em>, a great tool to run ML models in browsers, and come up with many other useful pre-trained models.</p><p>Happy Coding!</p><p><strong>References</strong></p><p><strong>Tensorflow.js — </strong><a href="https://www.tensorflow.org/js">Home</a>, <a href="https://github.com/tensorflow/tfjs-models/tree/master/blazeface">Blazeface Model</a></p><p><strong>Blogs</strong> — <a href="https://towardsdatascience.com/face-detection-for-beginners-e58e8f21aad9">Face Detection for Beginners</a></p><p><strong>Youtube — </strong><a href="https://www.youtube.com/watch?v=GItCuyP0G_k">Facial Detection with tensorflow.js by </a><a href="https://javascript.plainenglish.io/face-detection-in-the-browser-using-tensorflow-js-facb2304ed91">Adarsh Menon</a></p><p><em>This article is published as a part of ‘</em><strong><em>JavaScripted’</em></strong><em> under </em><a href="https://medium.com/spidernitt"><em>Spider Research and Development Club, NIT Trichy</em></a><em> on a Web Wednesday!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=122fcdc13b2a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spidernitt/building-up-a-facial-detection-app-with-the-help-of-tensorflow-js-122fcdc13b2a">Building up a Facial Detection App with the help of Tensorflow.js</a> was originally published in <a href="https://medium.com/spidernitt">Spider R&amp;D</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>