[Python] Face detection by OpenCV (Haar Cascade)

Introduction

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.

environment

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. スクリーンショット 2020-03-24 16.45.21.png 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) スクリーンショット 2020-03-24 16.53.30.png 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? スクリーンショット 2020-03-24 17.01.59.png 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. スクリーンショット 2020-03-24 16.45.32.png 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

Face detection

Now, I would like to recognize human faces using haar cascade. The image used is the following image (woman.jpg). 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 スクリーンショット 2020-03-24 17.27.20.png

It's recognized properly! Next, I will explain only the points in the code.

Cascade classifier reading

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.

Rectangle drawing

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: スクリーンショット 2020-03-24 17.44.01.png

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) スクリーンショット 2020-03-24 17.47.45.png

At the end

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

[Python] Face detection by OpenCV (Haar Cascade)
Face detection with Python + OpenCV
Face detection with Python + OpenCV (rotation invariant)
Hello World and face detection with OpenCV 4.3 + Python
Face detection with Python + dlib
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Face detection summary in Python
Face detection with Haar Cascades
Anime face detection with OpenCV
Improve detection accuracy quickly by specifying parameters with openCV face detection
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Face detection using a cascade classifier
Save video frame by frame with Python OpenCV
Python dlib face detection and blink counter
Face detection by collecting images of Angers.
[python, openCV] base64 Face recognition with images
Python2.7 + CentOS7 + OpenCV3
Performance comparison of face detector with Python + OpenCV
I tried object detection using Python and OpenCV
OpenCV Samples (Python)
[Note] openCV + python
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
JPEG image generation by specifying quality with Python + OpenCV
Primality test by Python
python openCV installation (memo)
Visualization memo by Python
Communication processing by Python
Binarization with OpenCV / Python
Summary about Python3 + OpenCV3
Introduction to OpenCV (python)-(2)
OpenCV3 Python API list
Python OpenCV tutorial memo
Beamformer response by python
Python, OpenCV camera capture
OpenCV basic code (python)
OpenCV for Python beginners
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
A memo when face is detected with Python + OpenCV quickly
I tried face recognition from the video (OpenCV: python version)
Cut out frames from video by 1 second with Python + OpenCV