#VSCode – Let’s do some #FaceRecognition with 20 lines in #Python (7/N)

Buy Me A Coffee

Hi!

I’m writing a series of posts about how to control a drone with Python and 20 lines of code, and once I reach to the point to read the camera feed, I’ve added a face detection sample. However this time I didn’t use the face_recognition python package I’ve used in this series, I performed the face detection using OpenCV and Haar Cascades. So, let’s explain a little what’s this.

Let me start quoting an amazing article “Face Detection using Haar Cascades” (see references)

Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001. It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.

OpenCV comes with a trainer as well as detector. If you want to train your own classifier for any object like car, planes etc. you can use OpenCV to create one. Its full details are given here: Cascade Classifier Training.

And here we come to the cool part, OpenCV already contains many pre-trained classifiers for face, eyes, smile etc. Those XML files are stored in opencv/data/haarcascades/ folder (see references).

opencv github haar cascades files

Let’s take a look at a really [20 lines] sample code for face detection:

  • Line 6, we use OpenCV to load the haar cascade classifier to detect faces
  • Lines 9-20, main app
  • Lines 10-12, open a frame from the camera, transform the frame to a gray color scaled image and use the face cascade detector to find faces
  • Lines 14-15, iterate thought detected faces and draw a frame
  • Lines 17-20, display the webcam image with the detected faces, and stop the app when ESC key is pressed

And a live sample using a drone camera instead of an USB Camera

Bonus. Viola Jones Face Detection and tracking explained video

# Bruno Capuano
# detect faces using haar cascades from https://github.com/opencv/opencv/tree/master/data/haarcascades
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (top, right, bottom, left) in faces:
cv2.rectangle(frame,(top,right),(top+bottom,right+left),(0,0,255),2)
cv2.imshow('Face Detection',frame)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()

This is a long video, however is an amazing entry point to understand how the Viola Jones algorithm works.

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Resources

2 comments

  1. Great sequence of articles. Liked them a lot!

    For this version, the code does not seem to work anymore with new versions. I expect the “detectMultisScale”expect different arguments.

    Error:
    Traceback (most recent call last):
    File “D:/Documenten/_Python_progs/07-Face_detection_with_Haar_Cascades_and_OpenCV.py”, line 14, in
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wwma2wne\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function ‘cv::CascadeClassifier::detectMultiScale’

    Like

    1. I tested and it works. The !empty() error is usually attached to a non valid frame to analyze. can you check that you have a valid frame from your camera ?

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.