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: reference frame used for lane detection
Lane boundary detection (greyscale + Canny edges)
Output: annotated frames with vehicle detection and speed overlay
| Detection output | Alt view |
|---|---|
![]() |
![]() |
Raw per-lane split clips (pre-processed, from assets/samples/):
| Lane 1 | Lane 2 | Lane 3 |
|---|---|---|
![]() |
![]() |
![]() |
| 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 4 | Lane 5 | Lane 6 |
|---|---|---|
![]() |
![]() |
![]() |
All lanes combined: full highway view with speed overlay
- Lane detection — uses Canny edge detection and contour analysis on a reference image to find lane boundaries
- Video splitting — splits the input video into per-lane video streams
- Speed calculation — tracks vehicle centroids across two detection lines, computes km/h using pixel-to-km calibration and frame timing
- Flagging — saves snapshots of vehicles exceeding the speed limit
- Language: Python 3
- Vision: OpenCV (
cv2) - Parallelism: Python
threading(one thread per lane)
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
pip install -r requirements.txtpython src/speed_detect.py \
--input-image assets/input-image.jpg \
--input-video assets/input.mp4 \
--output-dir output \
--speed-limit 100| 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/
├── 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
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.
















