@@ -42,20 +42,19 @@ def per_frame(self, identity: int) -> np.ndarray:
4242 # get centroids for all frames where this identity is present
4343 centroids = [convex_hulls [i ].centroid for i in indexes ]
4444
45- # convert to numpy array of x,y points of the centroids
46- points = np .asarray ([[p .x , p .y ] for p in centroids ])
45+ # get centroids for all frames where this identity is present
46+ centroid_centers = np .full ([self ._poses .num_frames , 2 ], np .nan , dtype = np .float32 )
47+ for i in indexes :
48+ centroid_centers [i , :] = np .asarray (convex_hulls [i ].centroid .xy ).squeeze ()
4749
48- if points .shape [0 ] > 1 :
49- # compute x,y velocities
50- # pass indexes so numpy can figure out spacing
51- v = np .gradient (points , indexes , axis = 0 )
50+ v = np .gradient (centroid_centers , axis = 0 )
5251
53- # compute direction of velocities
54- d = np .degrees (np .arctan2 (v [:, 1 ], v [:, 0 ]))
52+ # compute direction of velocities
53+ d = np .degrees (np .arctan2 (v [:, 1 ], v [:, 0 ]))
5554
56- # subtract animal bearing from orientation
57- # convert angle to range -180 to 180
58- values [ indexes ] = (((d - bearings [ indexes ] ) + 360 ) % 360 ) - 180
55+ # subtract animal bearing from orientation
56+ # convert angle to range -180 to 180
57+ values = (((d - bearings ) + 180 ) % 360 ) - 180
5958
6059 return {'centroid_velocity_dir' : values }
6160
@@ -92,18 +91,12 @@ def per_frame(self, identity: int) -> np.ndarray:
9291 indexes = np .arange (self ._poses .num_frames )[frame_valid == 1 ]
9392
9493 # get centroids for all frames where this identity is present
95- centroids = [convex_hulls [i ].centroid for i in indexes ]
96-
97- # convert to numpy array of x,y points of the centroids
98- points = np .asarray ([[p .x , p .y ] for p in centroids ])
99-
100- if points .shape [0 ] > 1 :
101- # compute x,y velocities
102- # pass indexes so numpy can figure out spacing
103- v = np .gradient (points , indexes , axis = 0 )
94+ centroid_centers = np .full ([self ._poses .num_frames , 2 ], np .nan , dtype = np .float32 )
95+ for i in indexes :
96+ centroid_centers [i , :] = np .asarray (convex_hulls [i ].centroid .xy ).squeeze ()
10497
105- # compute magnitude of velocities
106- values [ indexes ] = np .sqrt (
107- np .square ( v [:, 0 ]) + np . square ( v [:, 1 ])) * fps
98+ # get change over frames
99+ v = np .gradient ( centroid_centers , axis = 0 )
100+ values = np .linalg . norm ( v , axis = - 1 ) * fps * self . _pixel_scale
108101
109102 return {'centroid_velocity_mag' : values }
0 commit comments