Simple Head Tracking with OpenCV

Posted on 02/02/2019 by Animabot
Modified on: 03/02/2019
Press to mark as completed

I’m currently working a bit on Animabot’s software (Raspberry Pi side). I developed a small application for the Head Tracking which will be used on the robot. The application is developed in Python and using TkInter for the GUI. The software is using the standard Face Detection from OpenCV.

The program checks for face to detect, then provide the Head center position regarding the center of the video. It also sends the position trough UART is order to link the Pi to another board if needed (in my case, to the STM32F4). Or through socket if you want to use the info internally in the Pi. The output coordinates are filtered to have a more accurate position and avoid too much jitter.


The interface also allows to change the detection settings (video cropping, Face HitBox size, etc..) These parameters are directly in the GUI so it is easy to find the right settings. I will maybe add a save/load feature for the parameters later if needed. But for now you can tune these parameters in real-time, then change their default values in the code.


The output coordinates (on UART and socket) as the following format : <%+0.3d|%+0.3d>. This means, that from the above screenshot, the output value is : <+034|-021>

This application is based on Python 2.7 and OpenCV 3. To use it you need to install the following packages : PIL, tkinter, PySerial and numpy. For now I’m working on windows, so I didn’t tried it on the Pi yet… this means I can’t help you for packages installation… However, I suggest you to install PIP to install packages, this would be much easier. Do not forget to place haarcascade_frontalface_default.xml in the same folder as the Python file.

Of course I will not use this application for the Pi as it would be too heavy to process smoothly, I will make a lighter version without GUI and without video feedback and only focus on the algorithm and UART output.

Below you can find the application’s code. Please be indulgent as I’m still learning Python… I’m sure it can be greatly improved (if you have corrections/suggestions, please tell me  ) but at least it is as clear as possible and it works fine ! The code contains enough comments to understand it if you know how to program.

>>>  Source code available here <<<

LikedLike this to see more

Spread the word

Flag this post

Thanks for helping to keep our community civil!

Notify staff privately
It's Spam
This post is an advertisement, or vandalism. It is not useful or relevant to the current topic.

You flagged this as spam. Undo flag.Flag Post
You have to be logged in to chat

Our chat is a buzzing-with-life place where our community members join to discuss and foster great conversations.

Whether it's about their latest robotics project, questions about electronics, debates about the industry, or just talking between friends.

You have to be logged in to chat