Face Detection in 10 lines of code!
Face detection seems fascinating and it has been made very easy by OpenCV.
OpenCV is an open source computer vision library. In this article, I will show how you can use this library for face detection.
Note: This tutorial is for complete beginners. If you are familiar with python, you can simply use the codes and run them as per your convenience.
First step: download and install the OpenCV library
Search for “Command Prompt” App (or “Anaconda Prompt” if you have anaconda installed) and just type:
pip install opencv-contrib-python
And hit enter! This will download the whole package.
Error you might get: If it shows “ ‘pip’ is not recognized as an internal or external command”, it means you do not have Python installed. Go to Microsoft Store App and search “Python 3.x” (e.g. Python 3.8 or Python 3.9 or any version you would like) and install it. Now, try again the first step!
Second step: Try reading a local image using OpenCV
import cv2 as cv
img = cv.imread(r'C:\Users\Admin\face.jpg')
cv.imshow('Read image', img)
cv.waitKey(0)
Copy the above code in a new text document. In the 2nd line, inside the quotes, change the location and name of the image file you would like to read and save the file.
Most important: Rename the file and change the file name extension from “.txt” to “.py ” . For example: read_face.py
Now open the command prompt and run file by typing:
python read_face.py
and hit enter! A window will pop-up showing the image.
Press any key to close this window.
Error you might get: If it shows an error ‘No such file or directory’, make sure to change the directory in the command prompt using “cd”.
If you are not familiar with command prompt, just move your “detect_face.py” file to the current directory folder.
Here, as you can see in the command prompt that current directory folder is “Admin” in the C drive. So I have my “detect_face.py” file in “Admin” folder.
Till now if you are on the board, lets detect faces!
Step 3: Download the face haar cascade xml file
Traditional method: OpenCV has various haar cascades for detection. As we are detecting faces, we will download haar cascade for face detection. Go to below link:
and click on “haarcascade_frontalface_default.xml”. Now on the right side, click on “raw” . Now you will be able to see raw text. Select the whole text using Ctrl +A , copy and paste it in a new text document and save it.
Important: Rename and change the file name extension from “.txt” to “.xml”. For example: face_haar.xml
Shortcut method: you can simply click here and it will start downloading 😉 But make sure to rename the file.
Move this file to the location where “detect_face.py” file is saved.
Step 4: Detect faces!
Now create a new text document and copy-paste the above code. In the 2nd line, inside the quotes, change the location and name of the image file in which you want to detect face and save the file.
Again, rename and change the file name extension from “.txt” to “.py ” . For example: detect_face.py
Move this file to the location where “face_haar.xml” file is saved.
Now open the command prompt and run file by typing:
python detect_face.py
and hit enter! Two windows will pop-up one showing original image and other showing the same image green rectangle as detected face.
Press any key to close the windows.
Congratulations! 🎉 You have detected face with just 10 lines of code
Try images having multiple faces by changing the name and/or location of the image file in the code.
If it does not detect all faces or detect wrong objects as faces, play with the value of “minNeighbors” in the code by increasing or decreasing it (it is a positive integer).
face_rect = face_haar.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors = 3)
Happy coding!👨💻