<?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 Mritul on Medium]]></title>
        <description><![CDATA[Stories by Mritul on Medium]]></description>
        <link>https://medium.com/@mritulsenth?source=rss-a29dfdf2c274------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6JfhuWQ2h7yymp4GzR049A.jpeg</url>
            <title>Stories by Mritul on Medium</title>
            <link>https://medium.com/@mritulsenth?source=rss-a29dfdf2c274------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:07:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mritulsenth/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[Make your own blink detection application using Python]]></title>
            <link>https://medium.com/@mritulsenth/make-your-own-blink-detection-application-using-python-0a0a87e81b6a?source=rss-a29dfdf2c274------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a0a87e81b6a</guid>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Mritul]]></dc:creator>
            <pubDate>Fri, 03 May 2024 05:39:49 GMT</pubDate>
            <atom:updated>2024-05-03T05:44:12.610Z</atom:updated>
            <content:encoded><![CDATA[<p>A human eye on average blinks up to 20 times a minute. But, this rate drops to less than half while we look at our computer and smartphone screens. This reduced rate of blinking leads to consequences like vision changes and dry eyes. We will build this application to combat the issue and help the users maintain good eye health.</p><p><strong>Here is how the application would look like…</strong></p><figure><img alt="Demo of the application startup" src="https://cdn-images-1.medium.com/max/871/1*L5zRHlmqMYutBzB7gtQqLw.png" /><figcaption><strong>The initial startup lets the user choose the duration to check for blinks</strong></figcaption></figure><p>Once the duration is entered, the camera opens up and if a face is detected the monitoring begins, and if the user hasn’t blinked within the duration entered above, the application gives a beep reminding the user to blink.</p><p>If the user has blinked in that duration then the application simply ignores and does not give a beep.</p><p>And voila! If u set a duration of 10 seconds then the application takes care that you blink at least once every 10 seconds 🥳.</p><p><strong>Now that we’re all set, let’s dive into building the application!</strong></p><p>The libraries we’ll be using are :</p><ul><li><a href="https://docs.python.org/3/library/tkinter.html"><em>tkinter</em> </a>— For the GUI to get the duration from the user</li><li><a href="https://github.com/google/mediapipe/blob/master/docs/getting_started/python.md"><em>mediapipe</em></a><em> — </em>To detect the facial landmarks specifically the eye borders in our application</li><li><em>threading — </em>Default Python library to run a thread in the background to detect blinks</li><li><em>winsound — </em><strong>Note that this is a library with support only for Windows and you can use any other library of your choice for generating the beep sound</strong></li></ul><p><strong>The folder structure</strong></p><p>We’ll keep the folder organization simple. Have a folder named <strong>src </strong>inside your project directory and two files <strong>main.py </strong>and <strong>gui.py </strong>in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ZacJpag7VI0p0rpDEvgyVA.png" /></figure><p><strong>Part 1 — The GUI</strong></p><p>We’re going to make the GUI using <strong>Tkinter</strong> inside of our <strong>gui.py</strong> module file.</p><p>Start off by importing <strong>tkinter </strong>and write a function <strong>getDuration() </strong>which we’ll be importing inside of our <strong>main.py.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*1S3kr8DREdUEyKhdmTcyWg.png" /></figure><p>Now, let’s give a default duration of 10 seconds (you’ll find out why I’ve made duration global later in this article) and initialize the tkinter object as <strong>root </strong>and provide configurations like the dimensions, background color, and title.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*QsoGfMCIHQzowXqsWolGuQ.png" /></figure><p>Once that is done, let’s work on the text and input textbox to be displayed inside the window and position them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*ZvupYVSNNJVzeSWUcpTWOg.png" /></figure><p>And finally, we define a function <strong>getValueAndClose() </strong>which we provide as the command to the <strong>Proceed button </strong>we will be creating. This function takes the value entered in the textbox and assigns it to the global variable <strong>duration </strong>we earlier created and destroys the tkinter window and returns the value of <strong>duration.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*eiy8jnOVoGoQEbV5FVpR5g.png" /></figure><h4>Part 2 — Eye landmarking and blink detection</h4><p>Now that we have the duration to check for blinks, let’s implement the core logic for the application.</p><p>Head to the <strong>main.py </strong>file and start importing the necessary libraries and the <strong>getDuration() </strong>method we had defined in the <strong>gui.py </strong>module and get the duration we will be working with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*YHnrfM1UX3v6S0pKztcftQ.png" /></figure><p>Let’s create a few variables that we’ll be using to keep track of the number of blinks and trigger the beep accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*E1_SorlCUO9T4PeMsXcXIw.png" /></figure><p>The list <strong>blink_count_list </strong>is used to monitor if we had blinked within the duration and the logic of how we use it will be covered later in the article so hold tight!</p><p>An important step in the application is creating a thread that runs in the background. To make things simple, we just run a function, an infinite loop that holds the logic, in the background as the main application runs which sees if the user has blinked in the given duration. <strong>That is basically what the thread is doing! </strong>Let’s now see how we create the thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWPcunYgK6XXjqoRI7B1MQ.png" /></figure><p>The <strong>stop_threads</strong> is a global variable that helps us destroy the thread whenever required and we’ll soon be doing that in the further pieces of code.</p><p>So what the thread is doing is it checks the last element in the blink_count_list and after a “<strong>duration ”</strong>amount of seconds, it again checks if the blink_count_list has been updated with a new increased blink count. If the blink count remains the same after “<strong>duration” </strong>seconds then a beep is triggered. Now all we got to do is initialize a thread that runs the function we just defined and start it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*ol6vvf-u7f2sIlPmTt61tw.png" /></figure><p>Perfect! We’ll now be dealing with the fun part which is to analyze the facial features and landmark the eye contours and use something called the <strong>Eye Aspect Ratio (EAR) </strong>to record the blinks. Mediapipe is a machine-learning framework developed by Google that lets us perform different actions like facial detection, facial landmarking, and much more.</p><p>Let’s begin by initialising the <strong>FaceMesh</strong> object from the <strong>mediapipe </strong>library and also the <strong>VideoCapture</strong> object to get the camera input using <strong>OpenCV.</strong></p><p>To be able to display the frames we should use the function <strong>cv2.imshow(). </strong>Go down further to see how it is done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*QgbV4_BhHJZtH9308wIeSQ.png" /></figure><p>Now we run an infinite loop to continuously capture the video frames and analyze them. Note that we have to convert the frames from BGR to RGB because OpenCV returns BGR frames but Mediapipe can only process RGB frames.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*jv6IXWs8j9VEoscnW86OcA.png" /></figure><p>The <strong>multi_face_landmarks </strong>is the tuple that holds the facial landmarks, which coordinate the eye, mouth, and so on are at.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*L6Q_1yruIJ4Pnk4Fz_06GQ.png" /></figure><p>So now, <strong>facial_landmarks.landmark[i] </strong>will give a dictionary with the <strong>x,y, and z </strong>coordinates of that landmark. Here is the <a href="https://github.com/tensorflow/tfjs-models/blob/838611c02f51159afdd77469ce67f0e26b7bbb23/face-landmarks-detection/src/mediapipe-facemesh/keypoints.ts">link</a> to all the different landmarks with their indices mapped. Feel free to be creative and find other use cases with this information. In the comment above I’ve mentioned the indices that we’ll be using for this application. Now we have to use these indices to get the coordinates of that point on the face and later get the distances between them.</p><p>Let us get the coordinates of the top, bottom, left and right ends of each eye. The image illustrates the 8 points mentioned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*y-q1jB2Tas30moZve3ZEJw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*BZN6OWjmKKPMIwj6U7iX-g.png" /></figure><p>Now we find the vertical and horizontal distances between the extremes for each eye and the ratio of the vertical distance to the horizontal distance gives us the <strong>Eye Aspect Ratio (EAR). </strong>So when the ear drops below a threshold we can consider it a blink.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*JJcMGhXNZaWBhcjfWVJRdg.png" /></figure><p><strong>Note that the EAR threshold for different persons could be different. I’ve used an average value 0.3 here. Feel free to maybe implement a calibration to set an EAR threshold.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*euEFT7fwsn-wNQPyg35TwA.png" /></figure><p>We can now write the logic for a blink. Remember we declared three variables <strong>blinks, counter, </strong>and <strong>blink_count_list </strong>earlier? We will be modifying them here. Every time a blink occurs, the blinks variable is incremented and appended to the <strong>blink_count_list </strong>and also the first element of the list is popped as it is not needed anyways since we only use the last element of the list for checking for blinks (that thread we earlier started). This prevents the list from infinitely increasing in size.</p><p>And the <strong>counter </strong>variable is used to implement the logic that prevents one blink resulting in several different blinks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*DUblVHY0LSafLkp7RrVXmg.png" /></figure><p><strong>Note that we’ve been writing all the logic above inside of the for loop we earlier declared.</strong></p><p>We then come outside of the for loop and display the openCV frames and also listen to a keypress of <strong>Q </strong>to exit the application (the while loop is broken).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*l-POyEYusSYBejoQz-alOg.png" /></figure><p>And finally, we exit the while loop where the openCV frames are displayed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*v-Z35sm5AS_VBtuJgqdrEg.png" /></figure><p>We can now safely destroy the windows opened by OpenCV and release the VideoCapture object we instantiated and stop the thread by setting the global variable <strong>stop_threads </strong>to <strong>True.</strong></p><p>When the join() method is invoked, the current thread stops its execution and the thread goes into the wait state.</p><p><strong>And that brings us to a close….</strong></p><p>Feel free to check out the Github <a href="https://github.com/broken-myth/blinkr">repository</a> for the project and tweak it the way you wish to bring out something different 😁.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a0a87e81b6a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>