Face Detection on Raspberry Pi

We have realized a simple haarcascade-based face detection with openCV. Images are acquired from a Basler dart camera. You can use that example also for any Basler 2D cameras.

Hardware license: Solderpad 0.51


Face detection using PyPylon

This is a simple example of running face detection with OpenCV on images acquired from a Basler camera. Please ensure you have pylon, PyPylon and OpenCV installed. See the guide here on how to set up the required software on your system.

Face Detection with a Basler Dart Camera and OpenCV on Raspberry Pi B+

With the launch of Imaginghub Basler is releasing a fully functional Python 3 wrapper for the Pylon C++ API called PyPylon.
In this demo I would like to give you an impression of the simplicity of writing an image processing application straight forward with Python. Furthermore I will run the project on an ultra-low budget setup like the Raspberry Pi to give you a feeling of how easy and affordable embedded vison can be. So let’s dive right in!

Hardware setup

Basically there are only two things needed for the basic setup. One the one side you need an Basler camera to be able to use the Python Wrapper. And on the other side some sort of single-board-computer.
As we are talking about an embedded setup I suggest one of the dart USB3 Vison cameras which are perfect for a low-budget setup like this.
You can choose most of the available SBCs out there. It just has to have an ARMv7 architecture because PyPylon is only released therefore.

Pi setup

I will use Raspbian Jessie as operating system, but any Linux distribution should work equally. Just make sure that you use at least a 16GB SD-Card because we need a lot of disk storage for compiling.
After initial setup I recommend setting the Pi to ”boot only to command line” to save some resources. You can do this under “sudo raspi-config” and then “boot_behaviour”.
From now on you can do every command of this guide via SSH as well so the choice is up to you. For that you will only need the IP Address by writing “ip addr show”.
For this setup I recommend Python 3.5 which already is fully compatible with PyPylon.

Follow this guide to get PyPylon and OpenCV up and running: From Zero to Image

Writing an easy Face Recognition sample

Optional Step: Jupyter Notebook

A very smart way of writing Python code is using Jupyter Notebook. It lets you connect to a Web-based Python IDE from any machine in your network. So when you’re not working directly on the Pi, this is great way to write your first samples very straight forward.

Here is a good tutorial for getting it done.

Getting an Image with PyPylon

When you are familiar with Pylons C++ API you will feel very familiar with the Python Wrapper. All functions are the same but only in Python typical syntax. When you are searching for more advanced features, just check out the detailed C++ Programmers-guide.

Grabbing an image only requires very few steps. I recommend just using this short demo as a first impression and using the sample code afterwards.

First the InstantCamera has to be initialized:

import pypylon.pylon as py
icam = py.InstantCamera(py.TlFactory.GetInstance().CreateFirstDevice())

Then just open the Camera and grab an image:

img = icam.GrabOne(4000)

Now you’ve got the image as an object. To get it as an numpy array for OpenCV just do the following:

img = img.Array

You can change each camera parameter within PyPylon, but it’s a lot easier to create a profile in Pylon Viewer and load it like this:


One important setting I would like to mention is the PixelFormat. The Dart supports RGB8 output, which makes the handling in OpenCV a lot easier. Safe this setting in the profile or set it within Python by:

icam.PixelFormat = "RGB8"

If you want to see the picture in Python you can use for example Matplotlibs pylab class and just write

sudo apt-get install python-matplotlib
sudo apt-get install python3-matplotlib
import matplotlib.pyplot as plt
import cv2
plt.imshow(cv2.cvtColor(img, cv2.COLOR_RGB2BGR))

The color-conversion is needed, because plt expects the image to be in BGR format.




Haar Cascades Face Recognition sample

With OpenCV face recognition gets pretty simple with the Haar Cascade class. If you want to understand the used algorithm I recommend the OpenCV docs site and this video.
You will need an Haar Cascade XML file, which contains the parameters of all kinds of faces. You can find a ton of them for different kinds of objects in the internet or even create your own. First import it with this code:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

In some cases the classifier doesn't load succefully from the file. In these cases you should add the whole path (f.e. '/opt/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_default.xml'). Now you can run the face_cascade function. It will return a list of faces which we print into the image for illustration.

faces = face_cascade.detectMultiScale(img, 1.3, 5)
for (x,y,w,h) in faces:

Now just show the edited image:

plt.imshow(cv2.cvtColor(img, cv2.COLOR_RGB2BGR))


Sample Code

As I mentioned I recommend using Jupyter Notebook for your first PyPylon approaches. I will provide not only a Python but also a Jupyter Notebook code sample. Therefore click on the "Code" tab.



Does this project pique your interest?
Login or register on Imaginghub to join or follow this project.