tryolabs / norfair

Lightweight Python library for adding real-time multi-object tracking to any detector.
https://tryolabs.github.io/norfair/
BSD 3-Clause "New" or "Revised" License
2.34k stars 237 forks source link

Compute vehicle speed/velocity #285

Closed pg37013 closed 5 months ago

pg37013 commented 7 months ago

Having this:

for frame in video:
    detections = model(frame)
    tracked_objects = tracker.update(detections=detections)
    for object in tracked_objects:
        velocities = object.estimate_velocity

How can I convert the velocities in km/h?

Currently I'm doing the following np.linalg.norm(obj.estimate_velocity,1). However the results does not have sense... The results are being to close to 0.

aguscas commented 7 months ago

Hello @pg37013 . Assuming that your Detection instances in your detections list have the pixel coordinates of its points, then the estimated velocity returned by object.estimate_velocity will be in pixels/frame (so, how many pixels in the horizontal and vertical directions does the tracked point moves between consecutive frames).

To convert it to km/h, you will need some reference length relating pixels to kilometers (for example, if you know the real size of the objects you detected, you can use that). Secondly, you also need the FPS of the video to relate the time between consecutive frames and an hour.

Please, if you need more help or guidance, don't hesitate to ask.

pg37013 commented 7 months ago

Hello @aguscas

So, suppose that I have velocities = object.estimate_velocity. How can I convert that to km/h?

If I know the real size of an object, lets say: width= x m and height=y m and the fps variable. How can I make de conversion?

aguscas commented 7 months ago

So, you know the width in meters (x_meters m), and also you know it in pixels (x_pixels pixels), and similarly for the height. width = x_meters m = x_pixels pixels height = y_meters m = y_pixels pixels

Now, let's relate the velocity units meters/second with pixels/frame : pixels/frame = (pixels/meters) * (meters/second) * (second/frame) = (pixels/meters) * (1/fps) * (meters/second)

So the velocity can be expressed as velocity = velocity_in_pixels_per_frame * pixels/frame = velocity_in_pixels_per_frame * (pixels/meters) * (1/fps) * (meters/second)

So the velocity in meters per second is: velocity_in_meters_per_second = velocity_in_pixels_per_frame * (pixels/meters) * (1/fps)

So in particular, looking in the horizontal and in the vertical directions:

horizontal_velocity_in_meters_per_second = horizontal_velocity_pixels_per_frame * (x_pixels/x_meters) * (1/fps) vertical_velocity_in_meters_per_second = vertical_velocity_pixels_per_frame * (y_pixels/y_meters) * (1/fps)

In summary, just compute: object.estimate_velocity @ np.diag( [(x_pixels/x_meters), (y_pixels/y_meters)] ) /fps

If this answer is not clear enough, please tell me

aguscas commented 5 months ago

Will close this issue due to inactivity. Feel free to create another one if this was still unclear