Find the face in a picture — OpenCV example for beginners
To begin, we need to download the Haar cascade files from the following github link — https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
NOTE — Make sure that the downloaded XML file (haarcascade_frontalface_default.xml) and the input image is present in the directory from which the code is running.
About haarcascade_frontalface_default.xml file
This is basically a XML (extensible markup language) file containing generic information about features of a face. The following code uses this information to draw a rectangle around the detected face.
Let’s use this image to check the working of our code. Download it here.
THE CODE
First we will import all the required modules
import cv2
import numpy as np
import math
In next step, the image is extracted from its location and stored in ‘img’ variable
img=cv2.imread("pic1.jpg")
and then the cascade file is also loaded into ‘face_cascade’ variable
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Now come very important two lines of code-
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
The first line converts the image from one color space to another i.e from BGR to grayscale in this case.
The second line uses the CascadeClassifier::detectMultiScale() function which detects objects of different sizes in the input image and the detected objects are returned as a list of rectangles. More information on this module can be found here .
In the next 4 lines, we’re finding faces, their sizes, drawing rectangles, and noting the region of interest(ROI).
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
In above code, (x,y) are the starting indices of the rectangle and (w,h) are the width and height that when added to (x,y) form the rectangle. The second line draws rectangles on the input image.
Following code is used to display the image with the detected faces -
cv2.imshow("Image",img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
The first line displays the image in a new window named “Image”.
As seen, the last 8 lines of code are written inside an infinite loop (while 1:).
k = cv2.waitKey(30) & 0xff
if k == 27:
break
Above code displays the image until you press the “Esc” key (breaks the infinite loop).
Final Code
import cv2
import numpy as np
import math
img=cv2.imread("name_of_image.extension")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
while 1:
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.imshow("Image",img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cv2.destroyAllWindows()
Output of the program
As we can clearly see from the above output, it does not give perfect result.It left the guy in dark blue shirt and triange on guy which at back is not perfect.
So lets try our code with one more image.
Now run the code with this image. Make sure XML file, image and code are in same working directory.
After running the program we find that it again left the girl at right bottom of the picture.
In my personal opinian it is because of XML file we downloaded from github in the beginning. For more accurate results we have to modify that XML file so that it can capture face more accurately.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Please give your valuable feedback at pythoncript007@gmail.com
Please subcribe to Python Script — For more high quality article about Data Science/Machine Learning
New article will be published on Friday, Sunday and Tuesday.
Thank You
Python Script