Skip to main content

An automatic self-portrait camera using Raspberry Pi

title


Combining the Pi, Camera Module, Python and OpenCV for a fun demo

Computer vision is an exciting field that is becoming increasingly accessible through advances in both hardware and software. In days past it would have been inconceivable to fit the computing power required into a family home, let alone something as small as the Raspberry Pi.

As part of the Raspberry Pi Day hosted by RS, celebrating the launch of the Raspberry Pi 3, and four years of the Raspberry Pi Foundation, we decided to build something to show off the Pi performing face detection.

This post covers the hardware and software elements of the project, including the use of Python OpenCV libraries, doing the heavy lifting for the face detection.

Ingredients

title

It was proposed that the system should incorporate the following:

  • Raspberry Pi

  • Raspberry Pi Camera Module

  • Tripod

  • WiFi capability (we used a USB adaptor since we were using a Pi 2, though a Pi 3 would not require this!)

  • Ethernet port for networking flexibility

  • USB port

  • Touch screen

  • LEDs for illumination and status indication

  • Loudspeaker and amplifier for audio output

To house all of the above, a laser cut enclosure was designed and made. Aesthetically, there was a nod to vintage cameras, with the use of a wooden tripod and a cuboid camera design, though modernised with thick colourless and fluorescent acrylic sheet.

title

A 'soft power off' button was included and connected to two GPIO pins on the Pi. This presents a simple way of safely shutting down the system without the need for a graphical user interface, keyboard or mouse. Though it is possible to simply remove the power from the Pi, this can cause corruption of the SD card, loss of data or even the need to reinstall the OS. Not ideal!

With all of the above parts assembled, we had a camera-equipped system ready to be programmed. Below is an overview of how the software on the camera functions:

  1. Display live feed from camera and wait until face detected

  2. Once face detected, draw yellow box around face and begin countdown timer

  3. Count down and illuminate LEDs 1 to 3

  4. Play audio file, take photo, play another audio file

  5. Add overlay to still photo and save

  6. Inhibit face detection for short period

  7. Return to start

The small screen on the camera housing provides real-time video feedback to facilitate photo composure, as well as including an overlay, showing what will be covered by the image border added after the photo is taken.

Since we wanted to display the photos taken on a separate screen, a secondary Pi was set up and employed to run a slide show, with a simple shell script copying the photos from the main Pi.

For reliability, particularly in locations with unknown infrastructure such as the venue for the Pi Day, a standalone wireless access point was set up. This provided a private network to which both Pis could connect and use to remotely sync files.

Snake Eyes

title

The software running on our Pi uses OpenCV. Started at Intel in 1999, OpenCV supports a wide variety of programming languages, and here we are using the Python API, OpenCV-Python. This is a powerful solution as it allows the computationally intensive code to be written in C/C++ and it's functionality accessed from the comparatively friendly Python language.

Face detection in OpenCV is performed using Haar Cascades, which are somewhat beyond the scope of this post to describe in detail, particularly as openCV.org does so very well.

The video from the Raspberry Pi camera module is shown on screen and processed by the Python script, with a yellow box drawn around any faces detected. Once this occurs, a countdown timer starts with indicator LEDs on the front panel illuminating in sequence. Once all LEDs are lit, an audio file plays “say cheese!”, the camera takes a photo and a shutter sound is played.

title

Once the image is captured, a graphics overlay, or border is added, before being saved to the SD card and synced across to the second, display Pi. This overlay can be modified according to the event and further demonstrates the flexibility provided by a Pi-powered system such as this.

Summary

title

It was fantastic to be part of the Pi Day, learning about the capability of the new Pi 3 board whilst being among some of the inspirational makers and engineers involved with the Raspberry Pi scene. Our camera was put through it's paces with plenty of faces detected and photos taken.

Both the Python code and laser design files are provided on Github should you wish to use, or modify them for your own purposes.

It is worth taking a moment to consider the complexity of the image processing that is taking place on the Pi, enabling fun projects such as this. It is all too easy to overlook or forget this as processing power becomes increasingly ubiquitous and ultimately affordable.

Due to the huge user base of the Pi, with now over 8 million units sold, this is not only an affordable solution, but a polished one, with a stable Linux environment and peripherals like the Raspberry Pi Camera Module making it easier than ever to add real-time image capture and processing to your projects.

We look forward to seeing what inventive computer vision applications come about with the arrival of the Pi 3!

maker, hacker, doer
DesignSpark Electrical Logolinkedin