This time I would like to use OpenCV to detect faces. There are various face detection methods, but this time we will use Haar cascade.
MacOS Mojave Python 3.7
Haar Casecade? From the facial features (Haar features), this is a classifier that determines whether or not a face is present. This classifier is called a Cascade (combined) classifier because it is made up of multiple classifiers combined for speed. The following black and white features are used as Haar features. Figure. Haar features (Image source: http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html)
I wonder why this black and white is ... Let's think about what the human face looks like.
If the human face is highly abstracted, it will be roughly as follows (maybe ... w) Figure. Highly abstracted face
From the above figure, don't you think that the eyes are arranged in "black and white" from the left, for example? Figure. Highly abstracted face white and black arrangement
The Haar feature represents this highly abstracted facial white and black arrangement. An arbitrary area is cut out from the input image, and if there are many of these features, it is judged to be a face. Figure. Image judged to be a face (there are many Haar features such as the eyes) (Image source: http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html)
This is just a rough explanation of the image, so if you want to know more details, please read the following paper. https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf
Now, I would like to recognize human faces using haar cascade. The image used is the following image (woman.jpg).
The executed code looks like this:
python
import cv2
import matplotlib.pyplot as plt
#Loading images(Image 1066x1600)
img = cv2.imread("woman.jpg ")
#Load face cascade classifier
face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")
#Make the image grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Perform face detection!
faces = face_cascade.detectMultiScale(gray)
#Since the position of the face is included in faces, read it with a for statement
for (x,y,w,h) in faces:
#Draw a rectangle at the face position
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),10)
#Change the order of colors
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#output
plt.imshow(img)
plt.show()
result
It's recognized properly! Next, I will explain only the points in the code.
python
#Load face cascade classifier
face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_default.xml")
In this part, we are loading the cascade classifier for classifying faces mentioned above. if
OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
If you see a message like this, you probably don't have the xml file for the Cascade Classifier, so download (or clone) it from: https://github.com/opencv/opencv
After downloading, put the data / haarcascades folder in the same folder as your python file (or ipynb) and run it.
python
for (x,y,w,h) in faces:
#Draw a rectangle at the face position
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),10)
The position of the face is output as (x, y, width, height). It looks like this:
The arguments of cv2.reactangle are as follows. cv2.reactangle (image, (upper left x, upper left y), (lower right x, lower right y), (color), line thickness)
The cascade classifier can detect not only the face but also the torso, lower body, and cats. I would like to write that article soon. If you have any comments or mistakes regarding the article, please comment.
Twitter I also send it on Twitter. Please follow me if you like ...! https://twitter.com/ryuji33722052
Recommended Posts