COMPUTER VISION & PHYSICS

AI SPEED CAMERA

Measuring vehicle speed using single-camera calibration and object detection.

1. The "Why": Low-Cost Traffic Analysis

Traditional speed guns (LIDAR/Radar) are expensive and active emitters. I wanted to build a passive, low-cost system that could analyze traffic flow and speed using nothing but a standard webcam and computer vision.

The core challenge is translating pixels to meters. By calibrating the camera against known landmarks on the road, we can accurately calculate the real-world distance a vehicle travels between video frames.

2. The "How": Perspective Transformation

Tech Stack

  • Camera: 1080p Webcam (Fixed Position)
  • Detection: YOLOv8 (Real-time Object Detection)
  • Tracking: DeepSORT (Multi-Object Tracking)
  • Math: OpenCV (Perspective Warp / Homography)

Calibration Logic

To measure speed, we need to map the 2D road image to a "bird's eye view" where pixels represent consistent distances.

SOURCE VIEW Angled Camera Feed Trapezoidal ROI
↓ Perspective Transform Matrix (M)
WARPED VIEW Top-Down "Bird's Eye" Rectangular ROI
↓ Scale Factor
REAL WORLD 100px = 5 meters

We select four points on the road forming a rectangle in the real world (e.g., lane markings). OpenCV calculates a transformation matrix that warps this trapezoid into a rectangle. In this warped view, we can measure the pixel distance between two points and multiply by a known scale factor (meters per pixel) to get the physical distance.

Speed Calculation Pipeline

Frame Capture YOLO Detection (Car) Object Tracker ID Transform Centroid Calculate Δ Distance / Δ Time Speed (km/h)
FRAME T1 Car at Point A Time: 0.0s
FRAME T2 Car at Point B Time: 0.5s
↓ Calculation
SPEED Distance (A→B) / 0.5s

The system tracks the center of the car. As the car moves across the calibrated zone, we calculate the distance traveled every few frames. By dividing this distance by the time elapsed (based on the video framerate), we get the instantaneous speed. A moving average filter smooths out the noise to provide a stable reading.