With some effort I have found that controlling two servo motors to allow a webcam to pan/tilt while tracking a face in real time using the raspberry pi not as impossible as it may at first seem. With some careful tweaking and code optimization I was able to allow the pi to keep up with two servos while running OpenCV face detection at 320×240 looking for a right profile,left profile, and frontal face and adjusting the servos faster than once per second.
Step 1: Acquire the hardware.
A raspberry pi — Model A will work fine, I have the original Model B which has the same specs as the new Model A (minus network).
A pan/tilt bracket
A GPIO Ribbin Cable
A Pi-Supported Webcam — I used a Logitech C210
Assuming you already have a raspberry pi and a webcam the additional hardware will run you about $25
Step 2: Get your raspberry pi ready.
Make sure you are using the Official RaspbianOS (the hard-float version) and that it is up to date.
You may want to overclock your raspberry pi. I did to 800mhz. The higher you go the faster the facial recognition will be, but the less stable your pi may be.
Install OpenCV for python: sudo apt-get install python-opencv
Get the wonderful servoblaster servo driver for the raspberry pi by Richard Hirst: here
You can download all the files as a zip archive and extract them to a folder somewhere on the pi.
To install the servo blaster driver open a terminal and CD into the directory where you extracted the servoblaster files
run the command: make install_autostart
You may want to make servoblaster time-out and stop sending signals to the servo after a second if it’s not being moved.
to do this add the following line to /etc/modules: servoblaster idle_timeout=1000
start servoblaster with the following command: sudo modprobe servoblaster
The next task is to get the camera functioning as expected:
First of all, thanks a lot to Gmoto for finding this and pointing it out, it was the last piece of the “pi” to get everything running smoothly; you have to adjust some parameters in the uvcvideo module to get everything running well.
Namely, run these commands:
modprobe uvcvideo nodrop=1 timeout=5000 quirks=0x80
You will need to run that every time you reboot if you plan to run the face tracking program, or alternatively add the parameters to /etc/modules like you did with the servoblaster time-out tweak.
Step 3: Put together your rig
Build the pan/tilt brackets as per the instructions and attach the servos.
Attach your camera to the top of the bracket (i just used tape) and plug it into your raspberry pi usb port.
I was able to power it without a usb hub, but you may want to get a powered usb hub and go through that.
Step 4: Connecting the servos
Servoblaster considers servo-0 to be whatever is connected to GPIO 4 and servo-1 is whatever is connected to GPIO-17.
Servos have three wires, one is red which is Vin/positive, one is brown or black which is ground/negative and the other is control.
using the ribbon cable (and in my case some connector wire jammed into the holes) connect the control wire for each servo to the correct pin. The code assumes that servo-0 will control the left-right movement and servo-1 will control the up-down movement of the camera; so connect them this way.
Now it would seem to be common sense that the Vin for the servos would come from the 5v pins from the GPIO and the ground for the servos would come from the ground pins of GPIO, but this did not work in my case because I used a larger servo for the base. The large servo pulled more power than the pi was willing so supply. I was, however, able to power my smaller tilt servo with no issue. Also, Richard Hirst who made servoblaster seems to imply that he can drive multiple small servos with the GPIO 5v. I have also learned that there are some fuses in my version of the pi that were later removed related to those power pins. My instinct tells me that you could power two smaller servos from those pins on a newer pi. If you cannot, this is what you will have to do:
You will need some kind of external power source which is able to handle a heavy 5v-6v load: I used the one built into an arduino, but any 5ish volt power source should do; the servos are rated for up to 6v. The 5v pin on a computer power supply, a 5v-6v wall charger, some batteries in parallel; whatever floats your boat. Once you have your external source just connect the positive and negative lines from the servos to the positive and negative side of your power source, then connect the ground (negative) from your external power source to a ground pin on the raspberry pi GPIO.
Step 5: Run the program
I have attached the python script to this article, it’s called PiFace.py to run it just CD to it’s location in terminal and type: python PiFace.py
Here are some videos of mine in action.
For more detail: Pan / Tilt face tracking with the raspberry pi
Current Project / Post can also be found using:
- face recognition tracking raspberry pi 3 and servos