Skip to playerSkip to main content
Unlock the world of facial recognition! In this comprehensive tutorial, I guide you through the process of building a "Python Face Detective" tool. We'll be using two industry-standard libraries: OpenCV for real-time detection and Face_Recognition for identifying and comparing individuals.

I walk you through the entire coding process in Jupyter Lab, starting from basic installations to advanced techniques like face encoding and calculating "face match percentages" using Euclidean distance. You'll learn how to handle color space conversions (BGR to RGB), draw bounding boxes around faces, and troubleshoot common issues like accessory interference (hijabs or glasses) and side-view detection limits.

Check the script on my Patreon:
https://www.patreon.com/posts/158217968

Original YouTube Tutorial: https://youtu.be/SycAW1ScYAE

Connected Videos in this Series:
* How to Install Python & Set Environment Variables:
https://www.dailymotion.com/video/xa8ony2
* How to Install Jupyter Lab (Jupyter Notebook) :
https://www.dailymotion.com/video/xa9nxf8

Source/Model Resources:
* Face_Recognition Library (Dlib-based): https://github.com/ageitgey/face_recognition
* OpenCV Python Documentation: https://opencv.org/

Video Details:
* Original Publish Date: April 24, 2024
* Focus: Python / OpenCV / Facial Recognition / Computer Vision
* Test Environment: Python 3.9 on Windows 11

Follow lordcaocao2025 on Dailymotion for more deep dives into computer vision, AI-driven automation, and technical Python tutorials!

---
Connect with me:
📺 YouTube: https://www.youtube.com/@CaoCao2025
📱 TikTok: https://www.tiktok.com/@caocao20250
💎 Patreon: https://www.patreon.com/cw/Caocao2025

#Python #FacialRecognition #OpenCV #ComputerVision #AITutorial #Coding #lordcaocao2025
Transcript
00:00Hello guys! Welcome back with me, Chow Chow 2025.
00:04Today we're turning you into a Python face detective. Buckle up because we're diving into
00:10the fascinating world of facial recognition, marking, and comparison. No prior experience
00:17is needed, just your curiosity and coding spirit. We'll be using two powerful libraries that is
00:24OpenCV for real-time face detection and face turn for identifying those faces like a pro.
00:30Imagine building apps that unlock doors with your smile, tag friends and photos automatically,
00:36or even create fun face, whopping filters, swapping filters. We'll be using two powerful libraries
00:44that is OpenCV and face recognition for identifying those faces like a pro.
00:51Imagine building apps that unlock doors with your smile, tag friends in photos automatically,
00:58or even create fun face, swapping filters. Before we even start it, make sure you already
01:06have Python installed. In this example, I use Python 3.9. I suggest you use JupyterLab for the editor,
01:14but you could use any editor that you want. I already have video about how to install Python and JupyterLab,
01:22link in the description. Now open your JupyterLab and create your empty Python notebook. What you need
01:30to do first is to install the library that we need using pip. So create a new block and add
01:36this line,
01:37pip install OpenCV Python, pip install face recognition. Remember, unless you do this in Google Colab,
01:47you only need to this once. The next time you do this, you could simply remove this line.
01:52You could also install the library in command prompt if you wish to do so.
02:03Now let all done, let's start coding and detect those faces. Think of it like a magic eye puzzle,
02:10but way cooler. Let's import all the library that we need, like cv2, face recognition,
02:17matplot.ly, pipeplot, and numpy. We use cd2 for drawing purpose, face recognition for detecting and
02:25comparing face mathplot.ly, pipeplot for making the table, and numpy for a math calculation.
02:33Next, add a new code block. This time is to read the image that are identified, and the image that
02:39we going to
02:40compare using cv2imread. You could add a parameter of exact location, or you could add the name if you locate
02:49the image in the same directory of your notebook. In my example, I add the picture in the same
02:55location, so I simply put the file name. Now if you want, you could show the picture that are being
03:02read, so you feel confidence that you get the right image. We could use matplot.ly, pipeplot to show
03:09multiple images that are being read by cv2. We create a figure first with one row and two column, then
03:16we
03:17add the subplot for each cell. We show the image with figure I am show. Also, we add title for
03:23each image,
03:24and remove the axis, since it's not a math graph. You notice that both picture has a strange color,
03:31that because OpenCV library uses the blue-green-red, or BGR layout, instead of the conventional use of
03:38RGB, or red-green-blue layout. If you want it to revert to RGB model, you need to convert its
03:47color with
03:47CDT color. Here is the example. As you could see, the picture now changing like the original. Now we have
03:56the original image, let's start comparing faces. Let's take it up a notch. Can you imagine comparing two
04:04faces to see if they belong to the same person? We'll use distance metrics to measure the similarity
04:11between faces, just like comparing fingerprint. Before we could compare, we first must detect
04:17faces on the picture. To do that, we could use face encodings. This will return a list of 128
04:26dimensional face encodings on all the faces in this picture. We will select the first face they found,
04:33by putting index 0 on the right. Now let's compare the face between first image and second image.
04:41We use face recognition compare faces. The first parameter is a list of known face encodings,
04:48the second parameter is the face encoding that you want to check, and the last parameter is the
04:54tolerance. Tolerance default value is 0.6 and it should work best for regular face. Remember that the
05:03lower the tolerance, the more strict the result is. If the image you compare is somehow blurry or using
05:11accessories that hide face, you should increase the tolerance. The result only gave you information
05:24of whether it is the same face or not according to your tolerance setting. But if you want to know
05:30how
05:30much confidence the model think that one picture looked like the other, you could use face distances.
05:36This function will compare a list of known image to image that you want to check and gave Euclidean
05:43distance for each comparison face. The distance tells you how similar the faces are. The distance tells
05:51you how similar the faces are. You could use this result as the baseline of tolerance. Like tolerance,
05:58the lower the number of face distance, the lower the number of face distance, the more similar the two faces
06:02is.
06:03I simplify this by making face match percentage that use formula 1 minus face distances and multiply the result with
06:13100.
06:17Of course the result only show you text because we don't do any drawing here. Actually we could finish here
06:24if we only want to compare faces, but for bonuses I will tell you how to show the result in
06:30the pictures.
06:32First we will create a text variable and put the value according to the result. Same face or different face.
06:40Now we will check where is the face on those two pictures using face locations. If you have more than
06:48one face in the picture,
06:49you could change the second param into the number of face you want to check.
06:53The last param is to select the model. HAWG is faster and good for CPU. CNN is slower but detect
07:01face better.
07:02You must use CUDA for this and that mean using GPU that support CUDA.
07:10Face locations will return a list of tuples of found face locations in top, right, bottom, left order.
07:19After that we will draw a square on every face that are found using CD2 rectangle. After that we will
07:26add to the first image of whether the face is the same or different using CD2, put text above the
07:33rectangle.
07:34Now let's try run it for the final result.
07:40Okay, the result is quite rough, but I think it is pretty good for you to get started. You could
07:46modify the script to whatever you want.
07:48Before we close, let's try another few images and let's learn about a few weaknesses of this program that I
07:54need to share.
07:55Now go to the top and let's try another images.
07:59Click restart the kernel and run all cell to play it top to bottom so we don't need to play
08:05each block manually.
08:18Here is an example when you have different faces.
08:35Now you already see many examples on the opening video, but here I will show you a problem that you
08:42could find.
08:42First is a picture of a woman that has accessories on their face like hijab or glasses.
08:50Using hijab, the test results still return true, but the confidence level is almost at the limit of the tolerance.
08:58The face distance is 0.58, only 0.2 difference with the tolerance.
09:05But when using glasses and with position not looking to the front, the face distance is 0.61 over the
09:13tolerance limit by 0.1.
09:15If you want, you could simply change the tolerance to 0.65 to accommodate this, but it depends on how
09:23important is the use case of the program.
09:30The next example is when you found more than one faces in the picture.
09:38You could see here that the face that being compared is the man on the right.
09:43So having a visualization is also pretty important, so you know who's being compared.
09:49The solution for this ideally is to iterate every face, but it will be for another video.
09:55For now, I'm simply going to show you which part of the code that can select the face.
10:06The first one is when you're doing face encodings. Just simply select the index of the image, in this case
10:141.
10:19Next for visualization is to select the second face. That is when I equal 2.
10:26And here are the result.
10:29Okay, now you can see that the one who's being compared is the girl.
10:34Now you see it. I will revert back the script to check the first image.
10:50The other weakness of the face encoding is that it will not able to detect face that
10:56looking side view like more than 45 degree. It simply will not consider a face and will trigger
11:02an error. You could add error handler when the program not found any faces.
11:08Congratulations! You're now starting to become Python face detective.
11:13You've unlocked the power of face detection, marking, recognition, and comparison.
11:20Remember the possibilities are endless. Go build cool projects, experiment with different algorithms,
11:27problems, and don't forget to share your creations.
11:32Okay guys, I think that is for today's video. Thank you for watching. Don't forget to like,
11:38share, and comment. See you again on the next episode. And have a nice day. Don't forget to subscribe.
Comments

Recommended