Real‑Time Pan/Tilt Face Tracking on a Raspberry Pi – Practical Guide
With a modest investment of time and $25 in parts, you can turn a Raspberry Pi into a lightweight, real‑time pan/tilt webcam that follows faces. By combining OpenCV face detection, two hobby servos, and the servoblaster driver, the Pi can keep the camera centered on a subject at a frame rate faster than one move per second.
Step 1: Acquire the Hardware
What you’ll need:
- Raspberry Pi – Model A or B will work; the newer Model A (or Pi Zero) is fully sufficient.
- Pan/tilt bracket (many free designs exist on Thingiverse)
- Two hobby servos (e.g., SG90 or MG995)
- GPIO ribbon cable
- Pi‑compatible webcam (I used a Logitech C210)
Assuming you already own a Pi and a webcam, the extra parts cost roughly $25.
Step 2: Prepare Your Raspberry Pi
Use the official Raspbian OS (hard‑float) and keep it updated. Overclocking to 800 MHz can give you the extra CPU headroom you need for real‑time detection, but be aware it may reduce stability.
Install OpenCV for Python:
sudo apt-get install python-opencv
Download and install servoblaster, the servo driver from Richard Hirst:
git clone https://github.com/richard-hirst/servoblaster.git cd servoblaster make install_autostart
Optional: Configure a timeout so the driver stops sending pulses after a second of inactivity. Add this line to /etc/modules:
servoblaster idle_timeout=1000
Start the driver:
sudo modprobe servoblaster
Camera tweaks: Thanks to Gmoto for the insight that adjusting the uvcvideo module fixes latency issues. Run these commands once per boot or add them to /etc/modules:
rmmod uvcvideo modprobe uvcvideo nodrop=1 timeout=5000 quirks=0x80
Step 3: Assemble the Rig
Follow the bracket instructions, mount the servos, and attach the webcam to the top of the bracket (tape works fine). Connect the camera to the Pi’s USB port; a powered USB hub is optional but can help if the camera draws more current.
Step 4: Connect the Servos
Servoblaster maps servo‑0 to GPIO 4 and servo‑1 to GPIO 17. The typical servo wires are:
- Red – +5 V
- Brown/Black – GND
- Control – signal line
Use the ribbon cable to tie the control wires to the correct GPIO pins. The code treats servo‑0 as pan (left‑right) and servo‑1 as tilt (up‑down).
Powering the servos: Small hobby servos can often be powered directly from the Pi’s 5 V GPIO pins, but larger or more demanding models may require an external 5 V–6 V supply. Connect the servo +5 V and GND to the external supply, then tie the external GND to a Pi GPIO ground pin.
Step 5: Run the Tracking Program
Download the provided PiFace.py script, place it on the Pi, and execute:
cd /path/to/PiFace.py python PiFace.py
OpenCV runs face detection at 320×240 resolution, searching for right profile, left profile, and frontal faces. The servos adjust at a rate faster than once per second, keeping the camera centered on the detected face.
Check the demo videos linked in the original article for reference.
Manufacturing process
- How to Read Temperature with a DS18B20 on Raspberry Pi 2
- Professional Raspberry Pi Temperature Monitoring with DS18B20
- Measuring Temperature on Raspberry Pi with Maxim 1‑Wire Sensors and DS2482 I2C Bridge
- Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
- Integrating Sensor Data into a Raspberry Pi: A Hands‑On Guide
- Advanced Raspberry Pi Table Tennis Ball Tracking with OpenCV
- Integrating the Acconeer A111 Pulsed Radar with a Raspberry Pi: A Practical Guide
- Step-by-Step Guide to Deploy a RAK 831 LoRa Gateway on Raspberry Pi 3
- Build a LoRa Gateway with the RAK831 Module on Raspberry Pi 3 – Step‑by‑Step Guide
- Walbi: A Stable Biped Robot Built with Arduino and LX-16A Servos