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.39k stars 243 forks source link

To get a moving object's speed as single scalar value #292

Open armine105 opened 8 months ago

armine105 commented 8 months ago

Hello How are you? Thanks for contributing to this project. I want to get a moving object's speed as single scalar value. I see there is a velocity member in a tracked object. But it is a 2x2-D array as vector.

[ [39.56255496 -1.89730458] [40.79390178 -1.70833893] ] Of course, I used object bounding boxes as tracking module's input. How can I get an object's moving speed as single scalar value

aguscas commented 8 months ago

Hello! The TrackedObject.estimate_velocity is returning the horizontal and vertical velocity for each keypoint (in your case, for each corner of the bounding box) of your tracked object. If you want to give a single number, you should first average the velocities of both corners, and then take the norm of that final vector.

Here is an example of how to do that for a TrackedObject instance named obj:

import numpy as np

np.linalg.norm(obj.estimate_velocity.mean(axis=0))

Remember that this velocity is in pixels/frame units (so, how many pixels does the object move between consecutive frames). If you want to know the velocity in real units like meters/second, you will need to know

Once you have that information, you would have to convert the entries of obj.estimate_velocity accordingly before averaging.