The function estimates distance in a mp4 video. I tested it on dashcam videos from Youtube.
def estimate_distance(bbox_width):
focal_length = 1000 # Example focal length in pixels
known_width = 2.0 # Known width of the vehicle in meters
distance = (known_width * focal_length) / bbox_width
return distance
The logic behind this distance estimation is processing of video frame by frame. The frame window is considered as the camera and distance in pixels is estimated from the objects in the video that move frame by frame.
When using the function in image callback or process video functions:
bbox_width = x2 - x1
distance = estimate_distance(bbox_width)
The images were taken from UDACITY.