Face Detection using RaspberryPi and OpenCV

Face detection is getting important in many places. Knowing the importance and relevance,Almost all major industry vendors are doing something or other in this field.

Amazon – Amazon rekognition
Google – Google vision
Microsoft – Face API

 

Open source players
OpenCV
OpenFace

Most of these use deep neural network to detect faces.

I made an attempt to make use of OpenCV and Haarcascade calssifier

I split the content into two topics:

Required items and setting up
Running and results

Required items and setting up

Raspberry Pi with Camera connection kit
OpenCV libarary
Pre-trained classifier files for face and eyes
Putty for connecting Raspi to your laptop (or Monitor and keyboard to work on Raspi directly)

Camera connection kit:
Camera module need to connected to raspi using flex cable

Setting up the environment raspberry pi :

Required libraries need to be installed by executing below commands

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install python-opencv
sudo apt-get install opencv
sudo apt-get install python-matplotlib
view raw pi_opencv hosted with ❤ by GitHub

Camera need to be enabled in raspi-config

Connection setup :

Confirm camera is working or not by executing this command

sudo raspistill -o filename.jpg

Setting the coding environment:

Python code is used to build the face detection which uses Haar cascade classifier (pre -trained)

import io
import picamera
import cv2
import numpy
#Create a memory stream so photos doesn't need to be saved in a file
stream = io.BytesIO()
#Get the picture (low resolution, so it should be quite fast)
#Here you can also specify other parameters (e.g.:rotate the image)
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.capture(stream, format='jpeg')
#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
#Now creates an OpenCV image
img = cv2.imdecode(buff, 1)

 

Once we get the image, move on to classifier

#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/home/pi/Desktop/opencv/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/home/pi/Desktop/opencv/haarcascade_eye.xml')
#Convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print "Found "+str(len(faces))+" face(s)"
#Draw a rectangle around every found face
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]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
#Save the result2 image
cv2.imwrite('result2.jpg',img)

 

Now run the python code, `sudo python facedetect.py`

Result image will be stored in the file.

 

After identifying /detecting the face, we can compare with known face DB.

Face Recognition can be used in numerous ways as we can provide contextual information to the user

1. Preferred treatment to customer (loyal or otherwise) depending on the customer profile
2. Warning to the user if he enters dangerZone or unauthorized zone
3. Can give specific treatment in case of senior citizens or kids

References
http://docs.opencv.org/…
https://www.hackster.io/…
http://www.bogotobogo.com/…

If one does not have camera , still he can run this program using the stock images from gallery ( in stead of live source)

Tweaked code for that case

For object recognition, cascade classifier files need to be generated by training the negative image
http://docs.opencv.org/2.4.13.2/doc/user_guide/ug_traincascade.html
http://docs.opencv.org/trunk/dc/d88/tutorial_traincascade.html

Tweaked code for that case

Tags:, ,
One Comment

Add a Comment

Your email address will not be published. Required fields are marked *