Face Tracking

Multi face tracking using the Raspberry Pi 3

Once you have completed the tutorial for Single image face detection with OpenCV. You should be ready for the next logical step which would be to pipe multiple images into OpenCV and track faces in real time.

You will need:

  1. A Raspberry Pi 3 loaded with OpenCV,Numpy and python
  2. A PiCamera and ribbon cable
  3. Wireless keyboard and mouse
  4. HDMI monitor

You should be all set, except for the line for the face_cascade variable. We’re going to grab a new xml file, which you can find here: haarcascade_frontalface_alt.xml file

The code:

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import io
import numpy

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
 
# allow the camera to warmup
time.sleep(0.1)
 
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
 # grab the raw NumPy array representing the image, then initialize the timestamp
 # and occupied/unoccupied text
 image = frame.array

 #Load a cascade file for detecting faces
 face_cascade = cv2.CascadeClassifier('/home/pi/faces.xml')

 #Convert to grayscale
 gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

 #Look for faces in the image using the loaded cascade file
 faces = face_cascade.detectMultiScale(gray, 1.1, 5)

 print "Found "+str(len(faces))+" face(s)"

 #Draw a rectangle around every found face
 for (x,y,w,h) in faces:
 cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)

 # show the frame
 cv2.imshow("Frame", image)
 key = cv2.waitKey(1) & 0xFF
 
 # clear the stream in preparation for the next frame
 rawCapture.truncate(0)
 
 # if the `q` key was pressed, break from the loop
 if key == ord("q"):
 break

You can adjust the image ratio from (640, 480) to (320, 240) pixels. This will increases the speed of the tracking though the pi will not be able to recognize as many faces farther from the camera.