-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (118 loc) · 4.79 KB
/
main.py
File metadata and controls
152 lines (118 loc) · 4.79 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import cv2
import numpy as np
from ultralytics import YOLO
from pressure import PressureMap
import torch
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
VIDEO_PATH = "videomarocnig.mp4"
MODEL_PATH = "best.pt"
MATRIX_PATH = "h_matrix.npy"
OUTPUT_DETECTION = "output_detection.mp4"
OUTPUT_RADAR = "output_radar.mp4"
CONF_THRESHOLD = 0.25
IMG_SIZE = 1280
SCALE = 8
PITCH_W = 105 * SCALE
PITCH_H = 68 * SCALE
def load_homography():
try:
return np.load(MATRIX_PATH)
except:
print("Error: h_matrix.npy not found")
exit()
def project_point(H, x, y):
"""Project image coordinates to pitch coordinates"""
point_vec = np.array([[[x, y]]], dtype=np.float32)
dst = cv2.perspectiveTransform(point_vec, H)[0][0]
return dst[0], dst[1]
def draw_pitch_background():
"""Draw tactical pitch background"""
img = np.zeros((PITCH_H, PITCH_W, 3), dtype=np.uint8)
img[:] = (34, 139, 34)
mid_x = int(52.5 * SCALE)
cv2.line(img, (mid_x, 0), (mid_x, PITCH_H), (255, 255, 255), 2)
cv2.circle(img, (mid_x, int(34 * SCALE)), int(9.15 * SCALE), (255, 255, 255), 2)
return img
def main():
print(f"Device: {DEVICE}")
H = load_homography()
pm = PressureMap(pitch_width=105, pitch_height=68, resolution=1.0)
try:
model = YOLO(MODEL_PATH)
model.to(DEVICE)
except Exception as e:
print(f"Error loading {MODEL_PATH}")
print(e)
return
print(f"Classes: {model.names}")
id_maroc = None
id_nigeria = None
id_ball = None
id_ref = None
for id_class, name in model.names.items():
name_lower = name.lower()
if "maroc" in name_lower:
id_maroc = id_class
elif "nigeria" in name_lower:
id_nigeria = id_class
elif "ball" in name_lower:
id_ball = id_class
elif "ref" in name_lower:
id_ref = id_class
print(f"Mapping: Maroc={id_maroc}, Nigeria={id_nigeria}, Ball={id_ball}, Ref={id_ref}")
cap = cv2.VideoCapture(VIDEO_PATH)
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out_detection = cv2.VideoWriter(OUTPUT_DETECTION, fourcc, fps, (900, 500))
out_radar = cv2.VideoWriter(OUTPUT_RADAR, fourcc, fps, (PITCH_W, PITCH_H))
while True:
ret, frame = cap.read()
if not ret:
break
radar_img = draw_pitch_background()
results = model.track(frame, persist=True, conf=CONF_THRESHOLD, imgsz=IMG_SIZE, verbose=False)
current_frame_players = []
if results[0].boxes is not None and results[0].boxes.id is not None:
boxes = results[0].boxes.xyxy.cpu().numpy().astype(int)
cls_ids = results[0].boxes.cls.cpu().numpy().astype(int)
for box, cls in zip(boxes, cls_ids):
x1, y1, x2, y2 = box
feet_x, feet_y = (x1 + x2) / 2, y2
real_x, real_y = project_point(H, feet_x, feet_y)
rx, ry = int(real_x * SCALE), int(real_y * SCALE)
on_pitch = (0 <= rx < PITCH_W and 0 <= ry < PITCH_H)
if cls == id_maroc:
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
if on_pitch:
cv2.circle(radar_img, (rx, ry), 8, (0, 0, 255), -1)
current_frame_players.append((real_x, real_y, 0))
elif cls == id_nigeria:
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 255, 0), 2)
if on_pitch:
cv2.circle(radar_img, (rx, ry), 8, (255, 255, 0), -1)
elif cls == id_ball:
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 255), 2)
if on_pitch:
cv2.circle(radar_img, (rx, ry), 6, (0, 255, 255), -1)
elif cls == id_ref:
cv2.rectangle(frame, (x1, y1), (x2, y2), (100, 100, 100), 1)
if len(current_frame_players) > 0:
pressure_grid = pm.generate_heatmap(current_frame_players, team_of_interest_id=0)
heatmap_img = pm.visualize(pressure_grid, PITCH_W, PITCH_H)
radar_img = cv2.addWeighted(radar_img, 0.6, heatmap_img, 0.4, 0)
frame_resized = cv2.resize(frame, (900, 500))
cv2.imshow("Detection", frame_resized)
cv2.imshow("Radar", radar_img)
out_detection.write(frame_resized)
out_radar.write(radar_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out_detection.release()
out_radar.release()
cv2.destroyAllWindows()
print(f"Videos saved: {OUTPUT_DETECTION}, {OUTPUT_RADAR}")
if __name__ == "__main__":
main()