Face Detection with OpenCV (Raspberry Pi and Picamera)

Thanks to OpenCV, now face detection is becoming a easy task and you can find this technology in many photography apps or social networks.

I am also new in the field, but I have created a sample program in python which will work with Raspberry Pi and Picamera.

This program will do a simple task: take a picture, and detect face in the picture.

I did not add any validation check as this is my first experiment on face detection.

Source code can be found in my git repository is https://github.com/sunnycyk/pi-facedetection

I will explain how my code work:

1.  Take a picture with picamera [doc] (line 16-17) and save the captured image to a filename call “myface.jpg

2. use OpenCV api function imread [doc]to read the file “myface.jpg” into a image object. (line 19)

3. line 21 will call function detect_faces

4. function detect_faces will first create a CascadeClassifier Object [doc] .  Classifier is a object detector which is trained with a few hundred sample views of particular object.  The word “cascade” in the classifier name means that the resultant classifier consists of several simpler classifiers (stages) that are applied subsequently to a region of interest until at some stage the candidate is rejected or all the stages are passed.  For more detail, please check the documentation in OpenCV, but basically we will provide a trained file that has data of a object that we want to detect.  

The train file for front face was actually located at OpenCV directory under /data/haarcascades/haarcascade_frontalface_alt.xml since I only wanted to a simple face detection.

5. Method detectMulitScale will return detected faces

6. Once we obtains all detected face, the program will draw a blue rectangle box around the face and write to a image named “detect.jpg” or if no face is detected, the program will just print out “No Face Detected” Message.

Here is the sample photo of me with detection:

detect

Next I will work with video mode.

 

Read More