<?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 Grégoire Marabout on Medium]]></title>
        <description><![CDATA[Stories by Grégoire Marabout on Medium]]></description>
        <link>https://medium.com/@gmarabout?source=rss-663582e3acef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yNX2ad7bPvKe4RfT4uISSg.jpeg</url>
            <title>Stories by Grégoire Marabout on Medium</title>
            <link>https://medium.com/@gmarabout?source=rss-663582e3acef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 19:37:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gmarabout/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[Face detection with Taipy and OpenCV]]></title>
            <link>https://medium.com/@gmarabout/face-detection-with-taipy-and-opencv-c3ef31ab7d62?source=rss-663582e3acef------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3ef31ab7d62</guid>
            <category><![CDATA[face-detection-app]]></category>
            <category><![CDATA[taipy]]></category>
            <category><![CDATA[opencv-python]]></category>
            <dc:creator><![CDATA[Grégoire Marabout]]></dc:creator>
            <pubDate>Fri, 10 Feb 2023 17:01:34 GMT</pubDate>
            <atom:updated>2023-02-11T20:11:21.787Z</atom:updated>
            <content:encoded><![CDATA[<h3>Tutorial: How build a face detection app using Taipy and OpenCV</h3><p>In this tutorial, I will demonstrate how to use Taipy and OpenCV together to make a web app for detecting faces from a photo.</p><p>At the end of the tutorial, you will have a <em>single page</em> web app where you can upload a photo. The photo will be displayed with rectangles around detected faces.</p><p>The promise is to take less than 50 lines of code for it!</p><p>When finished, this is how it will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*cDO7uu2o1D3GwxEdx6l7VA.png" /></figure><p>Before we start coding, let me say a word or two about Taipy and OpenCV.</p><p><strong>What is Taipy?</strong> Taipy is an open source application builder for Python. Using Taipy, it takes 5 minutes to build a small web app in Python. Taipy is provided with a bunch of predefined visual components like buttons, images, tables, charts.</p><p>As it is pure Python, you can use any additional Python library. In our case, we will use opencv-python to run a face detection algorithm on photos.</p><p>To install Taipy:</p><pre>foo@bar:~$ pip install taipy</pre><p><strong>What is OpenCV?</strong> OpenCV is a widely used library for computer vision and image processing. It’s written in C++ but it comes with a pretty nice Python library, and that’s what we are going to use in this tutorial.</p><p>To install OpenCV for Python:</p><pre>foo@bar:~$ pip install opencv-python</pre><p>Now that you know everything you need to know about Taipy and OpenCV, let’s get started!</p><p>We shall start by creating an empty Python main.py file. In this file, we shall import the Taipy GUI library as follow:</p><pre>from taipy import Gui</pre><p>Good.</p><p>Now, we need to define the page content. The application will actually be very simple: we need an “image” component for the photo, an “upload” button, and a label to display the number of faces detected in the picture.</p><p>Fortunately, Taipy already provides those components out-of-the-box.</p><p>The upload button will be a file_selector , the image file be displayed in a image component, and the text will be displayed in a simple text component.</p><p>Let&#39;s create the Taipy page content in a page variable:</p><pre>from taipy import Gui<br><br>page = &quot;&quot;&quot;<br>&lt;|{selected_file}|file_selector|label=Upload File|extensions=.jpg,.gif,.png|&gt;<br>&lt;|{image}|image|width=300px|height=300px|&gt;<br>&lt;|{number_of_faces_detected} face(s) detected|&gt;<br>&quot;&quot;&quot;</pre><p>If you haven&#39;t used Taipy before, just know that a component is declared with the syntax &lt;|{binding}|component_name|options...&gt; . When no component_name is given, Taipy will just create a text.</p><p>We now need to declare the variables used in the Taipy components for data binding:</p><pre>selected_file = None         # the file selected by the Taipy &quot;selector&quot;<br>image = None                 # The image we will work on<br>number_of_faces_detected = 0 # The number of detected faces</pre><p>Now that we have all the stuff we need for Taipy, let’s add an entry point to run the Taipy app:</p><pre>if __name__ == &quot;__main__&quot;:<br>    Gui(page=page).run(dark_mode=False, port=8080)</pre><p>Now you can run the Taipy app by running this python script:</p><pre>foo@bar:~$ python main.py</pre><p>This should automatically open the default web browser to http://127.0.0.1:8080 and displays this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*w9CSK_IIeylmqFyglQDMAA.png" /></figure><p>If you click on the &quot;Upload&quot; button, the file selector opens and you can select a jpg/gif/png file from your computer.</p><p>That’s cool, but it does not do much…</p><p>What we want now is to get the uploaded image and pass it to a &quot;face detector&quot; to detect faces in it.</p><p>This is when OpenCV comes into play, and especially the <em>Haar cascade classifier</em> from OpenCV.<br> An Haar classifier is a machine learning object detection program that identifies objects in an image and video. Objects can be anything we trained it to detect: faces, ears, eyes, etc.</p><p>Here we will use it just for face detection.</p><p>Fortunately, OpenCV provides us with a haarcascade_frontalface_default.xml which is a pre-trained face detection model! Let&#39;s use this model with the following code:</p><pre>import cv2<br><br>face_cascade = cv2.CascadeClassifier(&quot;haarcascade_frontalface_default.xml&quot;)</pre><p>Now, we can detect objects (faces) from an image using:</p><pre>objects = face_cascade.detectMultiScale(an_image)</pre><p>The only thing we need to keep in mind is thatface_detector actually expects the image to be gray scaled… That won&#39;t be a problem since OpenCV has convenient functions for color conversion.</p><p>Let’s write a function to load the uploaded image and use the face_cascade to detect faces in it:</p><pre>def process_image(state):<br>    img = cv2.imread(state.selected_file, cv2.IMREAD_UNCHANGED)<br>    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<br>    faces = face_cascade.detectMultiScale(gray)<br>    # Draw a rectangle around faces<br>    for (x, y, w, h) in faces:<br>        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 5)<br>    state.number_of_faces_detected = len(faces)</pre><ul><li>the function takes a single state argument. The <em>state</em> is a Taipy object that contains current values for Taipy variables. This argument will be given by Taipy as we will use this function in the &quot;selector&quot; callback.</li><li>cv2.imread() reads the image from the given path.</li><li>cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) creates a new gray image from the original one.</li><li>we use detectMultiscale on the gray image to detect faces. The function returns a list of all the detected faces, given by a rectangle for each of them.</li><li>For each faces detected, the for loop will use the face coordinate to draw a rectangle using the opencv cv2.rectangle API.</li><li>Then we update the number_of_faces_detected state variable so that our label displays the number of faces detected.</li></ul><p>We need to slightly update the page string to add our new process_image() function as anon_action callback in the file_selector :</p><pre>page = &quot;&quot;&quot;<br>&lt;|{selected_file}|file_selector|label=Upload File|on_action=process_image|extensions=.jpg,.gif,.png|&gt;<br>&lt;|{image}|image|width=300px|height=300px|&gt;<br>&lt;|{number_of_faces_detected} face(s) detected|&gt;<br>&quot;&quot;&quot;</pre><p>And we are done! The application is now complete.</p><p>And, as promised, it takes less than 50 lines of code, as you can see in the complete final version below:</p><pre>from taipy import Gui<br>import cv2<br><br>face_cascade = cv2.CascadeClassifier(&quot;haarcascade_frontalface_default.xml&quot;)<br><br>number_of_faces_detected = 0<br>selected_file = None<br>image = None<br><br><br>def process_image(state):<br>    img = cv2.imread(state.selected_file, cv2.IMREAD_UNCHANGED)<br>    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<br>    faces = face_cascade.detectMultiScale(gray)<br>    state.number_of_faces_detected = len(faces)<br>    # Draw a rectangle around faces<br>    for (x, y, w, h) in faces:<br>        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 5)<br><br>    state.image = cv2.imencode(&quot;.jpg&quot;, img)[1].tobytes()<br><br><br>page = &quot;&quot;&quot;<br>&lt;|{selected_file}|file_selector|label=Upload File|on_action=process_image|extensions=.jpg,.gif,.png|&gt;<br><br>&lt;|{image}|image|width=300px|height=300px|&gt;<br><br>&lt;|{number_of_faces_detected} face(s) detected|&gt;<br>&quot;&quot;&quot;<br><br><br>if __name__ == &quot;__main__&quot;:<br>    Gui(page=page).run(dark_mode=False, port=8080)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*l99Q6tZhBmpH8icQpyCEcw.png" /></figure><p>I hope you found this tutorial useful.</p><p>The code of this demo can be freely downloaded <a href="https://github.com/gmarabout/taipy-face-dectection/">from Github</a>.</p><p>Any feedbacks, comments or improvements are welcome 🤗.</p><h4>Further Reading</h4><ul><li>Documentation: <a href="https://docs.taipy.io/en/latest/">Taipy</a></li><li>Documentation: <a href="https://docs.opencv.org/4.x/index.html">OpenCV</a></li><li>Paper: <a href="https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf">Rapid Object Detection using a Boosted Cascade of Simple Features</a></li><li>Article: <a href="https://towardsdatascience.com/computer-vision-detecting-objects-using-haar-cascade-classifier-4585472829a9">Computer Vision with Haar Cascade Classifiers</a></li></ul><p><em>If you’re interested in connecting, follow me on </em><a href="https://www.linkedin.com/in/gmarabout/"><em>Linkedin</em></a><em>, </em><a href="https://github.com/gmarabout"><em>Github</em></a><em>, and </em><a href="https://medium.com/@gmarabout"><em>Medium</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3ef31ab7d62" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>