Spaces:
Sleeping
Sleeping
Update gully_drs_core/replay_utils.py
Browse files- gully_drs_core/replay_utils.py +17 -57
gully_drs_core/replay_utils.py
CHANGED
@@ -1,61 +1,21 @@
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
-
from scipy.interpolate import CubicSpline
|
4 |
import os
|
5 |
-
from gully_drs_core.video_utils import get_video_properties
|
6 |
|
7 |
-
def generate_replay(
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
break
|
24 |
-
|
25 |
-
# Draw stump zone
|
26 |
-
cv2.polylines(frame, [np.array(stump_zone)], isClosed=True, color=(0, 0, 255), thickness=2)
|
27 |
-
|
28 |
-
# Draw ball position
|
29 |
-
if frame_idx < len(ball_positions):
|
30 |
-
x, y = ball_positions[frame_idx]
|
31 |
-
cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
|
32 |
-
|
33 |
-
# Draw bounce point
|
34 |
-
if bounce_point and frame_idx >= len(ball_positions) // 2:
|
35 |
-
cv2.circle(frame, bounce_point, 8, (255, 255, 0), -1)
|
36 |
-
|
37 |
-
# Draw Bezier curve trajectory
|
38 |
-
if len(ball_positions) > 3 and frame_idx < len(ball_positions):
|
39 |
-
points = np.array(ball_positions[:frame_idx + 1])
|
40 |
-
if len(points) > 3:
|
41 |
-
t = np.linspace(0, 1, len(points))
|
42 |
-
cs_x = CubicSpline(t, points[:, 0])
|
43 |
-
cs_y = CubicSpline(t, points[:, 1])
|
44 |
-
t_fine = np.linspace(0, 1, min(100, len(points) * 10))
|
45 |
-
curve_x = cs_x(t_fine).astype(int)
|
46 |
-
curve_y = cs_y(t_fine).astype(int)
|
47 |
-
for i in range(1, len(curve_x)):
|
48 |
-
cv2.line(frame, (curve_x[i-1], curve_y[i-1]), (curve_x[i], curve_y[i]), (255, 0, 0), 2)
|
49 |
-
|
50 |
-
# Add text overlays
|
51 |
-
cv2.putText(frame, f"Decision: {decision}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
52 |
-
cv2.putText(frame, f"Speed: {speed_kmh:.2f} km/h", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
53 |
-
|
54 |
-
out.write(frame)
|
55 |
-
frame_idx += 1
|
56 |
-
|
57 |
-
cap.release()
|
58 |
-
out.release()
|
59 |
-
return output_path
|
60 |
-
except Exception as e:
|
61 |
-
raise Exception(f"Error generating replay: {str(e)}")
|
|
|
1 |
+
# gully_drs_core/replay_utils.py
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
4 |
import os
|
|
|
5 |
|
6 |
+
def generate_replay(frames, ball_path, output_path='output.mp4', fps=30):
|
7 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
8 |
+
height, width = frames[0].shape[:2]
|
9 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
10 |
+
|
11 |
+
for i, frame in enumerate(frames):
|
12 |
+
if i < len(ball_path):
|
13 |
+
for j in range(1, i):
|
14 |
+
if j < len(ball_path):
|
15 |
+
pt1 = ball_path[j-1]
|
16 |
+
pt2 = ball_path[j]
|
17 |
+
cv2.line(frame, pt1, pt2, (0, 0, 255), 2)
|
18 |
+
out.write(frame)
|
19 |
+
|
20 |
+
out.release()
|
21 |
+
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|