-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
35 lines (30 loc) · 1.41 KB
/
stats.py
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
import numpy as np
from scipy.spatial.distance import euclidean
import pandas as pd
class PlayerStats:
def __init__(self):
self.player_positions = {}
self.player_distances = {}
self.field_zones = {}
def update_position(self, track_id, position, frame_shape):
if track_id not in self.player_positions:
self.player_positions[track_id] = []
self.player_distances[track_id] = 0
self.field_zones[track_id] = np.zeros((5, 5))
if self.player_positions[track_id]:
last_position = self.player_positions[track_id][-1]
distance = euclidean(last_position, position)
self.player_distances[track_id] += distance
self.player_positions[track_id].append(position)
zone_x, zone_y = int(position[0] // (frame_shape[1] / 5)), int(position[1] // (frame_shape[0] / 5))
self.field_zones[track_id][zone_y, zone_x] += 1
def get_stats(self):
stats = []
for track_id in self.player_positions.keys():
frequent_zones = np.where(self.field_zones[track_id] > np.percentile(self.field_zones[track_id], 80))
stats.append({
'Player ID': track_id,
'Distance Traveled (px)': round(self.player_distances[track_id], 2),
'Frequent Zones': list(zip(frequent_zones[0], frequent_zones[1]))
})
return pd.DataFrame(stats)