<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Allan To</title>
    <description>The latest articles on DEV Community by Allan To (@allantoo).</description>
    <link>https://dev.to/allantoo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1034491%2Fcb37d7c5-3d20-45f7-b918-578fb279d42b.jpeg</url>
      <title>DEV Community: Allan To</title>
      <link>https://dev.to/allantoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/allantoo"/>
    <language>en</language>
    <item>
      <title>Detect Human Faces Using Python ( Part 2 )</title>
      <dc:creator>Allan To</dc:creator>
      <pubDate>Fri, 07 Apr 2023 08:44:14 +0000</pubDate>
      <link>https://dev.to/allantoo/detect-human-faces-using-python-part-2--5cp5</link>
      <guid>https://dev.to/allantoo/detect-human-faces-using-python-part-2--5cp5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Welcome my readers!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This post is a continuation from my previous post. This post is all about how to actually implement openCV and write a simple application to detect human faces in any given pictures &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick re-cap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Face detection is a computer technology that enables machines to identify and locate human faces in digital images. The technique can be applied in various fields such as security systems, photo editing, entertainment, and social media. OpenCV is a popular open-source computer vision library that provides tools for face detection, image processing, and machine learning.&lt;/p&gt;

&lt;p&gt;Ok. Before we are going to the coding part, here are some requirements that you need to install&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install &lt;strong&gt;Python&lt;/strong&gt; (&lt;a href="https://www.python.org/downloads/"&gt;https://www.python.org/downloads/&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install OpenCV &lt;br&gt;
&lt;code&gt;pip install opencv-python&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose an image ( note the absolute path of the image for reference )&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After you have installed all of the requirements, here is a complete application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import cv2

face_cascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") #Note the change

img = cv2.imread("yourpathtoimage")

gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces=face_cascade.detectMultiScale(gray_img, scaleFactor=1.05,minNeighbors=5)

for x, y, w, h in faces:
 img=cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

resized=cv2.resize(img,(int(img.shape[1]/3), int(img.shape[0]/3))) 

cv2.imshow("Deteced-face",resized)

cv2.waitKey(0)

cv2.destroyAllWindows()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is the simplest application to detect human faces in python. Let's break this block of code down for further understanding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Importing the Required Libraries:&lt;/strong&gt;&lt;br&gt;
Open a Python file and import the required libraries as shown below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import cv2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loading the Pre-Trained Face Detection Classifier:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;OpenCV provides a pre-trained face detection classifier, which is a XML file containing features of the face. Download the classifier from the OpenCV website: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml"&gt;https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save the file in the same directory as your Python file. Now, load the classifier as shown below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loading the Image:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Load the image you want to detect faces from using the cv2.imread() function as shown below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;img = cv2.imread('image.jpg')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Converting the Image to Grayscale:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Convert the loaded image to grayscale using the cv2.cvtColor() function as shown below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Detecting Faces:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, we can detect faces in the grayscale image using the detectMultiScale() function of the face_cascade object as shown below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, the scaleFactor, minNeighbors, and minSize parameters are used to adjust the sensitivity and accuracy of the face detection algorithm. The &lt;em&gt;detectMultiScale()&lt;/em&gt; function returns a list of rectangular coordinates (x, y, w, h) of each detected face.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Displaying the Image with Detected Faces&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, we need to display the original image with the detected faces. We can use the&lt;code&gt;cv2.imshow()&lt;/code&gt;function to display the image and the &lt;code&gt;cv2.waitKey()&lt;/code&gt; function to wait for a keyboard event before closing the window.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cv2.imshow('Detected Faces', img)&lt;br&gt;
cv2.waitKey(0)&lt;br&gt;
cv2.destroyAllWindows()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This code will display the original image with rectangles drawn around each detected face. The window will stay open until a key is pressed, after which it will close.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Running that app and we will have this: *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FX-YPzKm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wiofanh2a60t396nrrvd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FX-YPzKm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wiofanh2a60t396nrrvd.png" alt="Image description" width="424" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tips for fixing bugs as you go through&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;pip : the term 'pip' is not recognized as the name of a cmdlet, function, script file, or operable program. check the spelling of the name, or if a path was included, verify that the path is correct and try again.  &lt;/p&gt;

&lt;p&gt;_Follow this YT video and you can fix it: _&lt;a href="https://www.youtube.com/watch?v=xdj0mGmuNjc"&gt;https://www.youtube.com/watch?v=xdj0mGmuNjc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings &amp;gt; Manage App Execution Aliases. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Go to App Execution Aliases and turn off Python alias&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape&lt;/p&gt;

&lt;p&gt;This error occurs when you use the backslash instead of forward slash in the path for the image. So just use forward slash. :)&lt;/p&gt;

&lt;p&gt;Hope you find something interesting here and thanks for your reading :)&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>opencv</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Detect human faces using Python</title>
      <dc:creator>Allan To</dc:creator>
      <pubDate>Mon, 27 Feb 2023 05:35:31 +0000</pubDate>
      <link>https://dev.to/allantoo/detect-human-faces-using-python-1b22</link>
      <guid>https://dev.to/allantoo/detect-human-faces-using-python-1b22</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;SOME GENERAL INSIGHTS ABOUT THIS POST&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
This post will cover all of the fundamental knowledge about AI in general as well as the library and language recommended to create a detect human faces application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ARTIFICIAL INTELLIGENCE IN A NUTSHELL&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Artificial intelligence has made significant advancements in computer vision, or the capacity of computers to comprehend and interpret images and movies, in recent years. Face detection, which is the act of identifying and locating human faces in an image or video, is one typical use of computer vision. This article will discuss how to utilize Python's artificial intelligence library to recognize human faces.&lt;/p&gt;

&lt;p&gt;Let's first talk about the idea of artificial intelligence. Artificial intelligence is the capacity of machines to carry out operations that ordinarily require human intelligence, such as comprehending speech, recognize natural language, and forming judgements based on facts. Machine learning is a branch of artificial intelligence that deals with the process of teaching a computer system to make predictions based on data&lt;/p&gt;

&lt;p&gt;Using a variety of algorithms, models, and methodologies, machine learning enables a computer to learn and enhance its performance over time. In order to find patterns in the data and create predictions based on those patterns, these algorithms employ statistical techniques.&lt;/p&gt;

&lt;p&gt;Image and video analysis can be solved using machine learning, which is a potent technique. Machine learning techniques can be used to recognize faces, track motion, and identify objects in the field of computer vision.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;SO WHAT IS HUMAN FACE DETECTION?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The detection of human faces in an image is a classic machine learning problem that may be solved using a number of well-liked algorithms. The Viola-Jones algorithm, which was created in 2001 by Paul Viola and Michael Jones, is one of the most frequently used algorithms. The eyes, nose, and mouth are among the traits that this programme looks for using a method known as Haar cascades.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;WHAT IS OPENCV ( PYTHON LIBRARY ) AND WHY SHOULD WE USE IT&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A well-liked open-source Python package for computer vision and image processing is called OpenCV (Open Source Computer Vision). It provides a variety of capabilities, including as face detection, for image and video analysis.&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;p&gt;Accuracy: OpenCV's face detection algorithm has been thoroughly tested on a wide range of images and is incredibly accurate.&lt;/p&gt;

&lt;p&gt;Performance: For real-time applications, OpenCV offers quick speed thanks to its high level of optimization.&lt;/p&gt;

&lt;p&gt;Simple and intuitive API provided by OpenCV makes it simple to use and comprehend.&lt;/p&gt;

&lt;p&gt;Cross-platform: OpenCV can be used on Windows, macOS, and Linux as well as other operating systems because it is cross-platform.&lt;/p&gt;

&lt;p&gt;Strong developer community: OpenCV has a sizable and active developer community, making it simple to obtain assistance and support.&lt;/p&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;p&gt;Training: Although OpenCV offers pre-trained models for face identification, it can be difficult to train your own models.&lt;/p&gt;

&lt;p&gt;False positives: On occasion, the face identification algorithm in OpenCV may mistakenly recognise non-faces as faces.&lt;/p&gt;

&lt;p&gt;Difficult configuration: Setting up OpenCV needs a process that can be challenging for beginners.&lt;/p&gt;

&lt;p&gt;Restricted application: The face detection method in OpenCV is only effective on frontal faces; it may not be as effective on side profiles or other positions.&lt;/p&gt;

&lt;p&gt;Problems with dependencies: OpenCV needs dependencies like NumPy and Matplotlib, which might complicate installation and configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;SO WHY PYTHON?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python is a popular programming language used for various applications, including computer vision and image processing. Here are some reasons why Python is a good choice for detecting human faces:&lt;/p&gt;

&lt;p&gt;Large number of libraries: Python has a large number of libraries and frameworks for computer vision and image processing, including OpenCV, Dlib, and TensorFlow, which can make it easier to detect human faces.&lt;/p&gt;

&lt;p&gt;Easy to read and write: Python has a simple syntax and is easy to read and write, making it easier for developers to create and maintain code for detecting human faces.&lt;/p&gt;

&lt;p&gt;Cross-platform compatibility: Python is cross-platform and can run on various operating systems, including Windows, macOS, and Linux, making it accessible for developers on different platforms.&lt;/p&gt;

&lt;p&gt;Extensive community support: Python has a large and active community of developers, which means that you can find help and support easily.&lt;/p&gt;

&lt;p&gt;Integration with other languages: Python can be integrated with other languages such as C++ and Java, which can help developers optimize performance and increase the speed of the face detection process.&lt;/p&gt;

&lt;p&gt;Prototyping: Python is a great language for rapid prototyping, which is important when developing new face detection algorithms or improving existing ones.&lt;/p&gt;

&lt;p&gt;Overall, Python is a powerful programming language for detecting human faces, with a large number of libraries, cross-platform compatibility, and a strong community support. These factors make it an excellent choice for developers who want to create robust and accurate face detection systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;_OK. HERE IS HOW _&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install OpenCV: If you haven't already, install the OpenCV library by running the following command in your terminal: &lt;code&gt;pip install opencv-python.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Load the image: Use the OpenCV imread() function to load the image into memory. For example: &lt;code&gt;img = cv2.imread('image.jpg')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Convert the image to grayscale: Convert the image to grayscale using the OpenCV&lt;code&gt;cvtColor()&lt;/code&gt; function. This is necessary because the Viola-Jones algorithm requires grayscale images. For example: &lt;code&gt;gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Load the face detection model: Use the OpenCV &lt;code&gt;CascadeClassifier()&lt;/code&gt;function to load the pre-trained face detection model. For example: &lt;code&gt;face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml').&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Detect faces: Use the &lt;code&gt;detectMultiScale()&lt;/code&gt; method of the face detection model to detect human faces in the grayscale image. This method returns a list of rectangles that represent the bounding boxes of the detected faces. For example: f&lt;code&gt;aces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5).&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Draw bounding boxes: Use the OpenCV rectangle() function to draw a bounding box around each detected face. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Display the image: Use the OpenCV &lt;code&gt;imshow()&lt;/code&gt;function&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Other library implementation&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dlib:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import dlib
import cv2

detector = dlib.get_frontal_face_detector()
img = cv2.imread('test_image.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = detector(gray)

for face in faces:
    x, y, w, h = face.left(), face.top(), face.right(), face.bottom()
    cv2.rectangle(img, (x, y), (w, h), (255, 0, 0), 2)

cv2.imshow('img', img)
cv2.waitKey()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;TensorFlow&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import tensorflow as tf
import cv2

model = tf.keras.models.load_model('face_detection_model.h5')
img = cv2.imread('test_image.jpg')

input_data = cv2.resize(img, (224, 224))
input_data = input_data.reshape((1, 224, 224, 3))
input_data = input_data / 255.0

predictions = model.predict(input_data)

for box in predictions[0]:
    x, y, w, h = box
    x *= img.shape[1]
    y *= img.shape[0]
    w *= img.shape[1]
    h *= img.shape[0]
    cv2.rectangle(img, (int(x), int(y)), (int(w), int(h)), (255, 0, 0), 2)

cv2.imshow('img', img)
cv2.waitKey()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Helpful links for further study&lt;/strong&gt;&lt;br&gt;
Simple Application: &lt;a href="https://www.youtube.com/watch?v=5cg_yggtkso"&gt;https://www.youtube.com/watch?v=5cg_yggtkso&lt;/a&gt;&lt;br&gt;
Deep Applicaton: &lt;a href="https://www.youtube.com/watch?v=N_W4EYtsa10"&gt;https://www.youtube.com/watch?v=N_W4EYtsa10&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading this post! Hope you find something interest here&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
    </item>
  </channel>
</rss>
