This project demonstrates near real-time face detection from a video camera on the Raspberry Pi. You can use the code in this project as a guide for integrating face detection into your own Raspberry Pi-based projects. To help with this, here's a quick explanation of some of the important files in the code:
  • CreepyPortrait.h - This file defines the internal state used by the application. You can modify the public members to change some of the functionality. Look for the explanation of each attribute in the comments. Also note main.cpp has two configuration functions, one for running on the Raspberry Pi and another for running on the PC--look at how each function sets the internal state like the delay in face detection, face detection buffer size, etc.
  • VideoSource.h & VideoSource.cpp - These files define a simple facade to wrap the openFrameworks webcam and Raspberry Pi camera video grabbers into a similar interface. If you're using the Raspberry Pi camera, be aware the addon to integrate it with openFrameworks is in an early beta stage and not yet part of the openFrameworks project. You can find the latest code for the Raspberry Pi camera addon at its github homepage. Special thanks to Jason Van Cleave for publishing this camera code!
  • VideoFaceDetector.h & VideoFaceDetector.cpp - These files define a class that uses the OpenCV addon to detect faces from frames in a video with a Haar feature-based cascade classifier. If you're running the face detection at a high rate, like more than once a second, you might notice noise from the center of the detected face slightly moving around. To smooth out this noise this class implements a buffer so previous frames are used to calculate an average detected face location. If you're running on a Raspberry Pi which can't detect faces as quickly this buffering isn't as necessary.
Here are a few more tips I learned if you plan to do face detection on the Raspberry Pi:
  • The size of video you're capturing will directly impact the performance of the face detection. The smaller the video the faster the face detection will run. I found 160x120 resolution video is a good trade-off of speed and quality.
  • Build your app to work with ~500-800 milliseconds of latency when detecting faces. To appear smooth my code runs the face detection every 2 seconds and then animates moving the model in the time it's waiting for the next face detection.
  • Watch out for memory consumption, especially if you're doing 3D rendering. You can quickly consume all the memory on the Pi with large models or textures. Scale assets down to as small a size as possible.
Have fun integrating face detection into your own Raspberry Pi project!

This guide was first published on Oct 15, 2013. It was last updated on Oct 15, 2013.
This page (Future Work) was last updated on Oct 22, 2020.