Key Features • Usage • Setup • Credits • License
PiTracker leverages TensorFlow Lite and a lightweight neural network - Single Shot Detection - in order to deliver tracking at decent 3-4 FPS despite the RPi's weak CPU and GPU.
- Control the rover using your keyboard
- Detect objects using TensorFlow Lite (tflite)
- Track a specified object class (eg. person or bird) using a pan-tilt camera with Proportional Integral Derivative (PID) process control
usage: main.py
This produces a view from your RPi camera with bounding boxes of objects. Use "WASD" in the terminal to control the rover and the picamera should track any object you specify. Press "E" to exit.
There are two parts to the set-up: building the rover and setting up your Raspberry Pi.
You need the following hardware:
- Raspberry Pi 4B (at least 2GB recommended)
- Robot Car Chassis
- TT Motor + Wheel X 2
- 4xAA battery holder
- Acrylic board to hold everything together
- L298N Motor Driver (Used to control the motors)
- 5MP Camera for Raspberry Pi
- FFC Cable longer than 20cm (as you will need to move your camera around)
- SG90 Micro Servo X 2 (to pan/tilt camera)
- Mini breadboard
- Jumper wires
The above schematic is how to connect all our components together. It might seem a little complicated, but don't worry, it's actually not that complex! There are two parts to the diagram: the motors (the batteries, L298N motor driver and the two DC motors) and the servos (the breadboard and two servos)
- Connect the two DC motors to the L298N motor driver
- Connect GPIO BOARD pin 13 to the L298N in1 pin (Brown)
- Connect GPIO BOARD pin 11 to the L298N in2 pin (Orange)
- Connect GPIO BOARD pin 15 to the L298N en1 pin (Green)
- Connect GPIO BOARD pin 16 to the L298N in3 pin (Blue)
- Connect GPIO BOARD pin 18 to the L298N in4 pin (Purple)
- Connect GPIO BOARD pin 22 to the L298N en2 pin (White)
- Connect the batteries to the L298N
- Connect the L298N to a GPIO GND pin
The servos are much simpler, so just follow that part of the schematic diagram
To build the pan-tilt camera, you just need two servos - one for panning and the other for tilting. You can either buy a Pimoroni one, or just tape together two servos like I did below.
The supported software baseline is 64-bit Raspberry Pi OS Bookworm with Python 3.11. Configure and test the camera using the Raspberry Pi OS camera tools before starting PiTracker.
$ git clone https://github.com/jwnicholas/PiTracker.git
$ cd PiTracker/
Install the required system packages:
$ sudo apt update
$ sudo apt install -y python3-venv pigpio libgl1 libglib2.0-0
$ sudo systemctl enable --now pigpiod
Confirm that Python 3.11 is active:
$ python3 --version
Python 3.11.x
Create and activate a virtual environment:
$ python3 -m venv .venv
$ source .venv/bin/activate
Install the pinned runtime dependencies. The manifest includes current ARM wheels for OpenCV and TensorFlow Lite, so the old hand-installed wheels are no longer needed.
$ python -m pip install --upgrade pip
$ python -m pip install -r requirements-rpi.txt
It's likely that you will use different GPIO pins, so open up main.py and update the following lines:
# right motor
in1 = 13
in2 = 11
en1 = 15
# left motor
in3 = 16
in4 = 18
en2 = 22
# servos are using pigpio, which uses BCM numbering
# while the motors are using BOARD numbering
pan = 12
tilt = 13
Note that I am using two different modules for controlling GPIO pins: RPi.GPIO (for interfacing with the motor driver and the wheels) and pigpio (for controlling the servos). This is because pigpio is much more accurate than RPi.GPIO in maintaining pulse width modulation, hence preventing the servos from twitching too much.
Note that for the RPi.GPIO, I'm using BOARD numbering, while pigpio only allows me to use BCM numbering.
- Python - Everything is written in Python
- EdjeElectronic's repo on using Tensorflow Lite for object detection - I modified his
TFLite_detection_webcam.pyto fit into the PiTracker code - Adrian Rosebrock's guide on pan-tilt face tracking - his code formed the base for using PID processes for controlling the pan-tilt camera
This project is licensed under the MIT License - see the LICENSE.md file for details.