Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,52 +1,22 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
def
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
distances = []
|
25 |
-
for i in range(1, len(ball_positions)):
|
26 |
-
_, x1, y1 = ball_positions[i - 1]
|
27 |
-
_, x2, y2 = ball_positions[i]
|
28 |
-
dist_px = euclidean_distance((x1, y1), (x2, y2))
|
29 |
-
distances.append(dist_px)
|
30 |
-
|
31 |
-
avg_px_per_frame = sum(distances) / len(distances)
|
32 |
-
speed_m_per_s = avg_px_per_frame * scale * fps
|
33 |
-
return round(speed_m_per_s, 2)
|
34 |
-
|
35 |
-
def frame_to_time(frame_idx, fps):
|
36 |
-
"""
|
37 |
-
Converts frame index to timestamp (seconds).
|
38 |
-
"""
|
39 |
-
return round(frame_idx / fps, 2)
|
40 |
-
|
41 |
-
def format_impact_info(impact_frame, impact_type, fps):
|
42 |
-
"""
|
43 |
-
Creates a descriptive impact summary.
|
44 |
-
"""
|
45 |
-
time_sec = frame_to_time(impact_frame, fps)
|
46 |
-
return f"Impact at {time_sec}s on {impact_type.upper()}"
|
47 |
-
|
48 |
-
def log(message):
|
49 |
-
"""
|
50 |
-
Simple print logger with prefix.
|
51 |
-
"""
|
52 |
-
print(f"[LBW-DRS] {message}")
|
|
|
1 |
+
import cv2
|
2 |
+
|
3 |
+
def save_video(frames, output_path, fps=30):
|
4 |
+
if not frames:
|
5 |
+
return
|
6 |
+
height, width = frames[0].shape[:2]
|
7 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
8 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
9 |
+
for frame in frames:
|
10 |
+
out.write(frame)
|
11 |
+
out.release()
|
12 |
+
|
13 |
+
def extract_frames(video_path):
|
14 |
+
cap = cv2.VideoCapture(video_path)
|
15 |
+
frames = []
|
16 |
+
while True:
|
17 |
+
ret, frame = cap.read()
|
18 |
+
if not ret:
|
19 |
+
break
|
20 |
+
frames.append(frame)
|
21 |
+
cap.release()
|
22 |
+
return frames
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|