Python Face Recognition

Usman Tariq
3 min readApr 6, 2022

--

Face Detection using python
Facial Recognition using openCV | Image source: Freepik

Python can be affectively used for face recognition search in images & videos. We will be requiring Open Source Computer Vision (openCV) library to process our code.

First off, we need to install openCV. We can install openCV using Visual Studio Code & Atom by typing in the following command on the terminal of your choice of editor;
For MacOS/Linux Users: python3.9 -m pip install opencv-python
For Windows Users: py -m pip install opencv-python

Note: I wrote “3.9” after python because i’m using python3.9. Hence, if you are using 3.8 version of python, you should type in “python3.8 -m pip install opencv-python”

Next, we need to download “haarcascade_frontalface_default.xml” from githib

Now, we will jump straight to the code. I will explain the code step by step.
Face recognition online code is also accessible on my github.

Execute the above code on your terminal. If your screen shows the image below, congrats, you have successfully made a face detection code!
Notice the green rectangle detecting the face.

Image source: Freepik

Keep in mind, logic is the fundamental of coding. Try to make logic in your mind as you read the code and it will enhance your understanding.

Important points from the code

Here are some of the important points from the code:
a) In line 5 of the code, we are accessing the haarcascade file by writing the path. Make sure the file is in the same folder as your code or else, give the complete path to the file.
b) In line 7, the parathesis should contain the name of the image you are working on. My image name was “sample_image.jpg”
c) In line 14, scaleFactor = 1.05 means 5% of the image. The lower the scaleFactor, the more detailed the image detection.
d) In line 15, minNeighbors is the minimum neighbors to search around the window.
e) In line 20, we are making a rectangle that will show up on the face. (x,y) & (x+w, y+h) are the two opposite (diagonal) corners of the rectangle.(0, 255, 0) is the color code (green color) & 5 is the thickness of the rectangle.
f) Line 23 is important if we want our program to show the image result on screen. Without this step, the image won’t show up without giving any error and you might be wondering what you missed.
g) In line 24, cv2.waitKey(0) means the picture will stay on the screen until you press any key. It can be changed as desired. cv2.waitKey(1000) means the image will show up for 1000 ms (1s).

Well, that’s pretty much it. If you need any help with the code, please feel free to reach out. Happy coding!

Interesting Facts about Facial Recognition

Face Recognition code is an extremely useful tool being used worldwide. Many real life applications are based out on this code. It is, indeed, helping the world grow smarter, safer and automated. Some of the interesting uses of face recognition are:

Unlock Phones: Ever wondered how you look at your iPhone and it unlocks instantly? Now you know! Almost all the flagship smartphones are using facial recognition technology to unlock them.

Forensic Investigations: Facial recognition can automatically recognize faces in security footages or videos, hence, aiding investigations.

Validate ID: Facial recognition can aid validate identity of the user on ATMs, to facilitate secure transaction and recognize VIP faces in events, etc.

Prevent Retail Crime: Yes, face recognition can prevent retail crime! Through machine learning, it can identify a criminal as soon as he enters the premises and is seen on the camera.

Tracing Missing Persons: Facial recognition can help trace missing persons — like victims of human trafficking.

--

--