Simple face detection using haar feature-based cascade classifier with OpenCV implemented in python
You are probably here for simple code that can be used for face detection using OpenCV, implemented in python, right? OpenCV features both training and detection. Here, we will concern with detection only. Our goal is to detect or find faces present in an image.
Pre-requisite: basic knowledge of python.
What is Haar Cascade?
Haar cascade is a classifier, it is used to detect objects in an image for which it has been trained. Haar cascade is stored in some source files, mainly as
.xml. They are loaded and applied to images to find for matches.
What is classifiers?
classifiers are those who classify something into several classes or groups on the basis of similarity and dissimilarity between them.
Let’s make a simple face detector. Jump to FUll Code
Step 1. Import OpenCV library.
Step 2. Load the cascade classifier from an XML source file, which is stored in
./haarcascades/haarcascade_frontalface.xml, in my case. There are several default cascades comes with OpenCV in the folder
opencv/data/haarcascades .you can download other cascade files according to your needs, available at OpenCV GitHub, or you can train your own. I have used one of the default haar cascade frontal face classifier source files.
Step 3. Now, Load the test image file in which the faces to be found. In my case, the image
group1.jpg is stored in
test_data folder inside
images folder. The
cv2.imread(), used to read the image data, have two parameters, the first parameter is
image file path and the second one is
type , type=0 for grayscale & 1 for RGB image.
Step 4. Convert it to a grayscale image. For detecting faces using
.detectMultiScale() method, a grayscaled image to be passed as its first parameter. On the other hand, we are working with the RGB image. So, It must be converted to grayscale first.
Step 5. Detection of multiple faces. Multiple faces are being detected using
.detectMultiScale() the first parameter is the image data using matrix form, of type CV_8U,
scaleFactor specifies how much the image size is reduced at each image scale, must be greater than 1.
minNeighbors specifies how many neighbors each candidate rectangle should have to retain it. It returns detected faces as a list of rectangles each of which contains four integers say
(x,y,w,h) , x: x coordinate of upper-left corner, y: y coordinate of upper-left corner, w: width of the rectangle, h: height of the rectangle.
Step 6. Drawing rectangle around all the detected faces. using
.rectangle() , which requires five parameters,
coordinate of upper-left corner: in the form of tuple i.e. (x,y),
coordinate of the lower-right corner: in the form of tuple i.e. (x,y),
BGR color: in the form of tuple i.e. (B, G, R),
stroke: width of the line.
for(x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(200,130,0),2)
Step 7. Showing the image in a window using
.imshow() and saving the image as a file using
Step 8. Wait for a keypress using
.waitKey(0) and closing all opened window using
The full source code is given below. If you copy and paste it, please be cautious of your cascade path, test image path and path where result image to be saved.
import cv2 #loading the cascade classifier face_cascade=cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface.xml')#place your cascade file path #loading the test image img=cv2.imread("images/test_data/group1.jpg",1)#place you test image file path #converting it into grayscale image gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #multiple face detection faces=face_cascade.detectMultiScale(gray,scaleFactor=1.8,minNeighbors=1) #drawing rectangles around detected faces for(x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(200,130,0),2) #showing and saving the image cv2.imshow("image",img) cv2.imwrite("images/results/result1.jpg",img)#place your output image file path #wait for a key press cv2.waitKey(0) cv2.destroyAllWindows()
- Github respository (to be added soon).
We would love to listen from you in the comment section below. One more important task for you is to find me out in the test images as well as in the resulting images. haha! 🙂