Face Recognition with OpenCV and Raspberry Pi

After seeing a cool video of face recognition in action on a Raspberry Pi I knew I had to make this project.  This is how it went for me.

I’ll show you the web site instructions that I followed, the problems I encountered and their solutions and how I mounted the Raspberry Pi with a Pi Camera.


the project

This is not a detailed recipe for the project but rather references and my notes on the project.

This is not a beginner project.  No matter how detailed the instructions are, there will be gaps, and errors will occur along the way.  One really needs a basic understanding of Linux and Python and the skills to search for solutions for when things don’t work.

The project is relatively inexpensive.  The main equipment required are a Raspberry Pi 3 $35USD and a Pi camera 5MP $15USD.  You will also need a micro SD card,  5v 2.5A power supply, monitor with HDMI, keyboard, mouse, and internet connection.

The good news is the OpenCV software.  CV stands for Computer Vision.  We are standing on the shoulders of giants when we use the libraries developed by Intel and maintained by open source geniuses.  A few statements in our Python scripts do all the work!  OpenCV.org

web resources

First I must mention the source of my inspiration for this project – acrobotic.com This company also has excellent Arduino tutorial videos and projects.  https://acrobotic.com/blogs/projects/face-detection-using-computer-vision-on-raspberry-pi

Second I used this good tutorial at hackster.io by MJRoBot for the overall project.  He outlines the 3 steps (face detection, face learning and face recognition), explains some theory, some troubleshooting, lists some sources and most importantly gives us the 3 Python scripts that make it all happen.  He calls the next site to install OpenCV on the Pi.  https://www.hackster.io/mjrobot/real-time-face-recognition-an-end-to-end-project-a10826

The third resource that I used is at pyimagesearch.com by Adrian Rosebrock.  He gives us a detailed recipe (and video) to install OpenCV on a Raspberry Pi.  https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/

road blocks

I encountered some problems along the way.  I’ll describe them in case this may help someone else with the project.

compile errors

While trying to install OpenCV I got stopped by compile errors in both numpy and OpenCV.  The errors were random and inconsistent.  I tried various things like a new SD card with fresh Raspbian OS, virtual environments and no virtual environments, Python 2.7, Python 3, OpenCV 3.4.1, OpenCV 3.3.0 with no luck.  I tried putting the SD card in my version 1 Pi and it all compiled (after 24 hours!).  I had purchased a new Pi 3 because the project specified it and I felt the quad core processor would be beneficial.  I returned the Pi 3 for a new one.  Everything compiled.  With all the variables of software and hardware and configurations I can’t say for sure the first Pi 3 was defective but that is my suspicion.  Also I did not use the -j4 compile option or virtual environment for the successful software install.

path to cascade file missing

When it came to where I should put my project files the information was not clear.  I put the cascade file that I downloaded in a sub directory Cascades.  The recognition script failed with a lot of lines ending in something not found.  Eventually I found that I needed to use the cascade file included with OpenCV and put the path to the cascade file in the scripts.

faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')

camera cable disconnect

The Raspberry Pi and Pi camera is a sloppy pile of wires, bare system board and camera with a flimsy cable.  Holding the camera trying for face recognition is not good.  I bought a nice case to hold the Pi and camera and made a support to hold the case and point the cam at me.  The camera stopped being recognized by the Pi.  Turns out I dislodged a tiny connector between camera and camera circuit board during assembly.

camera capture file won’t play on Pi

I wanted to capture a short video of my face detection in action for this post.  Fortunately there are screen and camera capture apps for Linux including SimpleScreenRecorder and vokoscreen.  My captured camera files would not play correctly on the Pi.  The resulting video played back with VLC was a black screen with maybe a flash of my face.  Eventually I learned that the captured file played fine when played on another computer.  Didn’t try to figure out why.

a mount for the Raspberry Pi

Raspberry Pi with Camera setup
Raspberry Pi with Camera setup


Raspberry Pi case mount side view
Raspberry Pi case mount side view

a final note on recognition

It is fun and useful to have your computer recognize you and say hi or unlock your device.

But this technology will be the next assault on our privacy.  Government, police and business will collect this information, store it and aggregate it.  Presently there is little disclosure or regulation.  This technology is already used for vehicle license plate recognition with little oversight.

One useful book on this subject is Habeas Data: Privacy vs. the Rise of Surveillance Tech by Cyrus Farivar.




One Reply to “Face Recognition with OpenCV and Raspberry Pi”

Leave a Reply

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