Skip to main content

Speed Detection

· 3 min read
Scott Havird
Engineer

Video

Motivation

I live in a neighborhood with a 25mph speed limit. I've noticed that many people drive much faster than the speed limit. I wanted to capture how fast people were driving in my neighborhood. I set up a camera to capture the speed of cars driving by. I used a Raspberry Pi to capture the video and a Python script to detect the speed of the cars.

Setup

I used a Hikivision CCTV to capture the video. I used my macbook to connect to the CCTV and view the video. I also used that macbook to run the Python script to detect the speed of the cars.

Logic

I used the following Python script to detect the speed of the cars. I used the OpenCV library to detect the cars. I used the following formula to calculate the speed of the cars:

I used the following formula to calculate the distance between the camera and the car:

distance = (width of car in pixels * focal length) / width of car in real world

I used the following formula to calculate the focal length:

focal length = (width of car in pixels * distance) / width of car in real world

I used the following formula to calculate the width of the car in real world:

width of car in real world = (width of car in pixels * distance) / focal length

I used the following formula to calculate the width of the car in pixels:

width of car in pixels = (width of car in real world * focal length) / distance

Output

video

images

Images of the same car, wanted to train data :)

image of car folder structure image of car folder structure image of car folder structure image of car folder structure image of car folder structure image of car folder structure image of car folder structure

json data of car type and