Skip to content

About

Real-time vehicle speed detection from highway footage using OpenCV, Canny edge detection, and per-lane threading

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

20 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

speedvision

Real-time vehicle speed detection from highway camera footage using OpenCV.

speedvision processes a highway video, automatically detects lane boundaries, splits the feed by lane, tracks vehicles across detection lines, and calculates their speed in km/h. Vehicles exceeding the speed limit are flagged and their images saved.

Input / Output

Input: reference frame used for lane detection

Input reference frame

Lane boundary detection (greyscale + Canny edges)

Greyscale edge detection

Output: annotated frames with vehicle detection and speed overlay

Detection output Alt view
Output Output alt

Raw per-lane split clips (pre-processed, from assets/samples/):

Lane 1 Lane 2 Lane 3
Lane 1 Lane 2 Lane 3
Lane 4 Lane 5 Lane 6
Lane 4 Lane 5 Lane 6

Full pipeline output: annotated per-lane clips with speed overlay (from assets/output/):

Lane 1 Lane 2 Lane 3
Lane 1 Lane 2 Lane 3
Lane 4 Lane 5 Lane 6
Lane 4 Lane 5 Lane 6

All lanes combined: full highway view with speed overlay

Full highway output


How It Works

  1. Lane detection — uses Canny edge detection and contour analysis on a reference image to find lane boundaries
  2. Video splitting — splits the input video into per-lane video streams
  3. Speed calculation — tracks vehicle centroids across two detection lines, computes km/h using pixel-to-km calibration and frame timing
  4. Flagging — saves snapshots of vehicles exceeding the speed limit

Tech Stack

  • Language: Python 3
  • Vision: OpenCV (cv2)
  • Parallelism: Python threading (one thread per lane)

Project Structure

speedvision/
├── src/
│   └── speed_detect.py     # Full pipeline: lane detection, splitting, speed calc
├── assets/
│   ├── input-image.jpg     # Reference frame for lane detection
│   ├── input.mp4           # Sample input highway video
│   ├── gray.jpeg           # Greyscale edge detection output
│   ├── output.jpeg         # Sample annotated frame
│   ├── output1.jpeg        # Sample annotated frame (alt)
│   ├── full_output.gif     # All lanes combined with speed overlay
│   ├── output/             # Per-lane annotated GIFs (with speed overlay)
│   │   ├── output1.gif
│   │   ├── output2.gif
│   │   ├── output3.gif
│   │   ├── output4.gif
│   │   ├── output5.gif
│   │   └── output6.gif
│   └── samples/            # Raw per-lane split GIFs (pre-speed detection)
│       ├── output1.gif
│       ├── output2.gif
│       ├── output3.gif
│       ├── output4.gif
│       ├── output5.gif
│       └── output6.gif
├── requirements.txt
└── LICENSE

Setup

pip install -r requirements.txt

Usage

python src/speed_detect.py \
  --input-image assets/input-image.jpg \
  --input-video assets/input.mp4 \
  --output-dir output \
  --speed-limit 100

All options

Flag Default Description
--input-image assets/input-image.jpg Reference frame for lane detection
--input-video assets/input.mp4 Highway video to process
--output-dir output Directory for split/final videos and images
--speed-limit 100 Speed limit in km/h; faster vehicles are flagged
--start-line 100 Start detection line x-coordinate
--stop-line 400 Stop detection line x-coordinate

Output Structure

output/
├── split/
│   ├── output1.avi         # Per-lane split video (one per lane)
│   ├── output2.avi
│   ├── ...
│   └── output6.avi
└── final/
    ├── output1.avi         # Annotated output with speed overlay (one per lane)
    ├── output2.avi
    ├── ...
    ├── output6.avi
    └── image/
        ├── 1/              # Vehicle snapshots for lane 1
        ├── 2/              # Vehicle snapshots for lane 2
        ├── ...
        ├── 6/              # Vehicle snapshots for lane 6
        └── overspeed/      # Snapshots of vehicles over the speed limit

Calibration

The default km_per_pix = 0.0035 / lane_height ratio is calibrated for standard highway footage. Adjust this constant in src/speed_detect.py for different camera heights or zoom levels.

License

MIT

About

Real-time vehicle speed detection from highway footage using OpenCV, Canny edge detection, and per-lane threading

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages