Eastern Ave Speed Camera
Detects passing vehicles and computes their speed by the distance traveled between two consecutive image frames
Refresh browser for updates
Set-up
Runs on a Raspberry Pi 4 computer with 5 megapixel camera module
Code is written in Python using Picamera and OpenCV libraries
View the code on Github: https://github.com/ryanraba/pimotion/blob/master/monitor.py
Algorithm
An image frame is recorded every ~0.75 seconds
The frame is convolved with a gaussian (kernel size 31) to blur small details
The blurred frame is subtracted from the previous to produce a difference
The absolute value of the difference image is thresholded to produce a binary (black/white) image of changes over/under threshold
A dilation function expands the area of the pixels over threshold
A contour function finds the outline of pixel groups over threshold
The area of each contour is computed and the largest saved
A bounding rectangle is placed over the original image at the location of the saved contour
Center position of the bounding rectangle and time of image recording are saved
If the subsequent frame also produces a large bounding rectangle, the position delta is divided by the time delta of the two images and converted to units of mph
Limitations
The real-world distance corresponding to position delta of the bounding rectangles is a wild estimate, this needs to be calibrated, and until then the speed calculation is also a wild estimate
A filter is used to throw out small contours and slow moving deltas, this is meant to avoid picking up people walking, but may end up missing some vehicles
The image frames are heavily cropped to avoid people's yards
The algorithm does not account for 3-D projection on to a 2-D surface, so it is limited to perpendicular movement with a fixed offset for depth between right and left lanes of the road