MOTION DETECTION & TRACKING
Here’s the high-level concept for motion detection
and speed estimation:
• The camera provides a sequence of image
frames. Each frame is compared to an average
of past frames to detect differences.
• Analysis methods from the Open Source
Computer Vision library (Open CV) are used to
blur and expand the differences, for example
to make a blob from moving legs merge with a
blob from a moving torso.
• The ndContours method is used to outline
the blobs, and then to simplify matters,
bounding rectangles that surround each blob
are computed. So for each frame, there is set
of rectangles that represent changing portions
of the image. By correlating the rectangles
from one frame to the next, we can track
moving objects.
If a car is moving across the image from left
to right, first a smaller rectangle is seen on the
left side of the frame where the car enters the
camera view. As more of the car comes into
view, the rectangle expands in width, as there
is a difference of the car image to what was an
empty street. The leading edge of the rectangle,
the right side, is seen at larger values of the x
coordinate which is a count of pixels from the
left frame edge. The difference in the x position
of the leading edge from frame to frame is an
approximate measure of the car’s speed.
For the most part the frames are evenly spaced
in time. However, there are occasionally lost
frames, resulting in the time interval between
frames being 2x or more than normal. So a call
to time.perf_counter() is a better measure of
time than a count of frames.
SPEED CALCULATION
The essence of speed tracking is to calculate
the difference in x position per difference in time
(dx/dt) using the current frame versus the prior
one, and to analyze the sequence of dx/dt values
versus x for the moving object across the frame.
• The dx/dt values tend to vary in a regular way
for a car that moves at steady speed along a
street that may be angled slightly across the
view, or is gently curved.
TIME REQUIRED:
2–8 Hours
DIFFICULTY:
Intermediate
COST:
$130–$160
MATERIALS
» Raspberry Pi 4B single-board computer
(SBC), 8GB RAM or other Linux system
» Raspberry Pi Camera Module V2 or webcam
for a Linux system
» MicroSD card, 32GB or larger
» Aluminum heatsinks for Pi 4B from pishop.us
» Ventilated case for Pi 4B such as the PiShop
HighPi case, pishop.us, with my ventilated
replacement cover at printables.com/
model/178257
» Flex cable for Pi Camera, 200mm or suitable
length for your installation
» Camera case and mounting brackets You
can 3D print my window shade camera case,
printables.com/model/172019, or tripod-
mounted case, printables.com/model/107623,
or search Thingiverse and Printables for other
cases.
TOOLS
» 3D printer (optional) for DIY cases and/or
brackets
» Computer for setting up the Pi, not needed after
setup
» Project code Download the Python code file
piMotion.py
from the project page at makezine.
com/go/pi-cam-speed-trap.
Oscar Roselló
Adobe Stock-Umi G Design
A
Two kids on motorbikes, clocked at 24.8mph by the Pi
Cam Speed Trap.
77
make.co
ED HUME lives in Austin, Texas,
and is well known in the “live steam”
hobby as the author of A Climax
Class A Live Steam Locomotive Model
and various magazine articles on
metalworking.
M83_076-83_PiCamSpeedTrap_F1.indd 77M83_076-83_PiCamSpeedTrap_F1.indd 77 10/10/22 4:16 PM10/10/22 4:16 PM