speed check

#2
by rahul7star - opened

can it detect car speed in video ?

Ultralytics org

Hi @rahul7star .
Yes , ultralytics library already provides a way to measure car speed. This solution is built on top of object detection and tracking - it tracks objects across video frames and computes speed from their displacement over time.

Here is an example code snippet:

import cv2
from ultralytics import solutions

cap = cv2.VideoCapture("video.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)

estimator = solutions.SpeedEstimator(
    model="yolo11n.pt",
    fps=fps,
    meter_per_pixel=0.05,  # scene scale, calibrate to your camera setup
)

while cap.isOpened():
    success, frame = cap.read()
    if not success:
        break
    results = estimator(frame)  # annotated frame available as results.plot_im

As you can see - speeds are estimates, accuracy depends on calibrating meter_per_pixel to your camera configuration.
Here, meter_per_pixel is the scene scale. It defines how many real-world meters one pixel covers on the road surface. In the provided code snippet 0.05 means 1 pixel β‰ˆ 5 cm, so a typical car 4.5 m long would span 90 pixels in the frame.

The value is determined by camera height, angle, distance to the road, and focal length together, so the practical way to set it is to calibrate from a known reference in your footage:

 meter_per_pixel = real_length_in_meters / length_in_pixels

For example, a lane width of 3.5 m that measures 70 px in the frame gives 3.5 / 70 = 0.05.

Camera positioning is very important:

  • Prefer a side or high-angle view perpendicular to traffic flow. Displacement is measured in 2D image space, so motion directly toward/away from the camera is underestimated.
  • Keep the measured zone roughly at a constant distance from the camera; with strong perspective (objects shrinking into the distance), a single meter_per_pixel value is only accurate in one region of the frame.

You can find full documentation and quite nice video regarding your question here: https://docs.ultralytics.com/guides/speed-estimation#what-is-speed-estimation.

Sign up or log in to comment