Face and Movement Tracking Pan-Tilt System with Raspberry Pi and OpenCV

Hey Luka,

Absolutely. Take the time to get the main python script adapted for normal servo motors but you can definitely do it this way.

Come take a look at the GitHub page here, you should be able to figure out how to do it from there - GitHub - pageauc/face-track-demo: Raspberry Pi python PiCamera, OpenCV Face and Motion Tracking using pan/tilt assembly via servo controller or gpiozero pwm.

Kind regards,

Hi Tim,

I have recently got started with Raspberry Pi and am still quite new to it. I have played with your open cv face tracking and object recognition. For a project I would like to be able to track an object (specifically a ball) using a similar method to the face tracking. I am unsure how to implement this and thought you might be able to help!


1 Like

Heyya James,

Here is a great education on tracking a Ball using OpenCV and Python - Ball Detection Using OpenCV in Python - YouTube

If you utilise that you will be able to adjust the pan-tilt script I have here and adapt if for your use :blush: Then you will be pan-tilt tracking a ball all day.

Kind regards,

Hi, incredible guide. I learnt a lot. Newbie question if I may. Is there any way to autocenter pimoroni hat when the program is initiated. All the time the hat is started looking at the ceiling.
Many thanks! Keep up the good work!


There are some lines early in the code that should do what you’re after:

# Default Pan/Tilt for the camera in degrees. I have set it up to roughly point at my face location when it starts the code.
# Camera range is from 0 to 180. Alter the values below to determine the starting point for your pan and tilt.
cam_pan = 40
cam_tilt = 20

Have you changed these to see if you can get it doing what you’re after? Hope this works! :slight_smile:

1 Like

Thank you so much!
It worked beautifully!
May I ask some help? I am trying to make tracking of an object, but I have been investigating your other tutorial about object classification. The issue is I don´t know how to extract the x,y,z,h info from the detected object in : result, objectInfo = getObjects(img, 0.45, 0.2,objects=[‘bird’]), so I can reuse your tracking function that works so well with the pan tilt pimoroni.
If you could kindly refer me to a function to extract this info, it would be much appreciated.

Many thanks!

Hi Tim, slowly learning my way around the raspberry pi and have had some good fun playing with your object detection tutorial and this face tracking one. I am currently trying to combine the two aforementioned codes in order to do some object tracking but I am not having much luck. Is there an easy way to do this?

Many thanks,

I hope I’m posting this question in the right place. I’ve been working on wearable camera tracking system for sport persons (like Osmo Pocket), which is a challenging environment. I have used OpenCV’s MOSSE algorithm with a Pi 3A+ for minimum processor and battery size/weight. It performed reasonably at 640x480 with a processing speed of 100fps, but struggles to track when the person targeted or camera wearer moves too quickly. Tracking performance is useless when frame size is increased to 1024x720 or the video is recorded.
The FOV and quality of 360° video cameras do not meet my requirements.
Do you have any suggestions for the most efficient CV library/code for detection and tracking, where the helmeted sports person and camera wearer are both moving at a range of 3-10m from each other?

1 Like

Hi Richard,

Sounds like quite the project!

I’d say something from OpenCV would be some of the fastest openly available tracking software, anything beyond this would be optimised for a specific product like the DJI range of tracking cameras, if you are running on the Pi 3 a bit more processing power might help, I’d check out the Oak-D Lite, it features an onboard video processing unit thats specialised at running vison models, and lets you pipe the data out via USB (along with a recording via UVC).

Let us know what you think!

Thanks for your comments Liam. I will look into the Oak-1 as it is more suitable for my application. Since there is a trade-off between hardware tracking performance and hardware size, I was hoping to improve performance with better code instead of hardware. I’m a Python novice, but will attempt threading on my Pi3A+, i.e. threads for capture, write, track, show, which will hopefully improve tracking performance.

Hi I need help in getting a object tracking project going.
This code does not load and gives and errors.


                            -D CMAKE_INSTALL_PREFIX=/usr/local \

                            -D INSTALL_PYTHON_EXAMPLES=ON \

                            -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.4.0/modules \

                            -D BUILD_EXAMPLES=ON ..

make -j $(nproc)