-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
41 lines (33 loc) · 1.21 KB
/
features.py
File metadata and controls
41 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
from collections import deque
class FeatureExtractor:
def __init__(self, window_size=5):
self.prev_areas = deque(maxlen=window_size)
self.prev_directions = deque(maxlen=window_size)
def extract(self, bbox, magnitude, angle):
x, y, w, h = bbox
# Feature 1: Bounding box area
area = w * h
self.prev_areas.append(area)
# Feature 2: Area change rate
if len(self.prev_areas) > 1:
area_change = self.prev_areas[-1] - self.prev_areas[-2]
else:
area_change = 0
# Feature 3: Average motion magnitude inside bounding box
mag_roi = magnitude[y:y+h, x:x+w]
avg_magnitude = np.mean(mag_roi) if mag_roi.size > 0 else 0
# Feature 4: Direction consistency
ang_roi = angle[y:y+h, x:x+w]
mean_direction = np.mean(ang_roi) if ang_roi.size > 0 else 0
self.prev_directions.append(mean_direction)
if len(self.prev_directions) > 1:
direction_variance = np.var(self.prev_directions)
else:
direction_variance = 0
return [
area,
area_change,
avg_magnitude,
direction_variance
]