jthomas / ai-speed-camera

Detect and record speeds for cars in video.
54 stars 5 forks source link

Tests

AI-Powered Speed Camera πŸš—πŸ“Έ

This repository contains the source for an AI-powered digital speed camera. It can detect vehicles in video files and calculate their speeds. It will produce an annotated version of the source video where cars are labelled with their speeds. The car speed statistics can also be exported to a CSV file.

AI Speed Camera Example

The project uses the Google Cloud Video AI API to track moving cars in video source files. API results from this service are used to calculate speeds based on a known fixed distance of video frame. FFMPEG is used to produce an annotated video file with cars labelled with their speeds.

If you want to learn more about this project - please see this blog post here: https://jamesthom.as/2022/05/ai-powered-speed-camera/

Requirements

Usage

TLDR - Run Demo

  1. Install requirements via poetry
    • poetry install
  2. Run as below
    # uses existing video with pre-generatated annotations from google cloud video ai api
    python -m ai_speed_camera --video sample_data/dene_road.mp4 --output dest.mp4 --annotations sample_data/output.json --distance 32

Process Source Video File with Cloud Video AI API

https://videointelligence.googleapis.com/v1/projects/<X>/locations/<REGION>/operations/<Y>
curl -X GET \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
https://videointelligence.googleapis.com/v1/projects/<X>/locations/<REGION>/operations/<Y> > results.json

Run Python Script To Annotate Video

python -m ai_speed_camera --video input.mp4 --output dest.mp4 --annotations results.json --distance 32

# try it out with pregenerated annotations
python -m ai_speed_camera --video sample_data/dene_road.mp4 --output dest.mp4 --annotations sample_data/output.json --distance 32

The Python script to calculate speeds and annotate the video file takes the following parameters (mandatory parameters in bold).

The Python script will produce the output below whilst running. It prints the number of cars detected in the AI API result set and the number of valid cars (in reference to minimum speeds and distances). A progress bar will be shown during the video processing stage with estimated time left.

INFO:root:Discovered 50 total cars in annotation response
INFO:root:Discovered 12 valid cars in annotation response: 6, 7, 11, 13, 14, 21, 24
INFO:root:Exporting valid car statistics to csv file
INFO:root:Processing source video file
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:00<00:00, 26.71it/s]

Limitations

Future Ideas