norfair icon indicating copy to clipboard operation
norfair copied to clipboard

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

Open armine105 opened this issue 1 year ago • 1 comments

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

armine105 avatar Feb 01 '24 05:02 armine105

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

  • the relationship between a pixel and a meter (which might be different on each axis depending on the perspective and where your object is in the image)
  • the real time difference between consecutive frames in your video (typically is just the inverse of the fps, but if your video was in slow-motion or was a timelapse you would also need to contemplate that).

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

aguscas avatar Feb 01 '24 11:02 aguscas