Spaces:
Sleeping
Sleeping
Update visualizer.py
Browse files- visualizer.py +10 -29
visualizer.py
CHANGED
@@ -2,68 +2,49 @@ import cv2
|
|
2 |
import numpy as np
|
3 |
import tempfile
|
4 |
import os
|
5 |
-
from moviepy.editor import ImageSequenceClip
|
6 |
-
try:
|
7 |
-
from moviepy.editor import ImageSequenceClip
|
8 |
-
print("✅ moviepy is working.")
|
9 |
-
except ImportError as e:
|
10 |
-
print("❌ moviepy is broken:", e)
|
11 |
|
12 |
def draw_virtual_stumps(frame, stump_zone=(280, 360), pitch_y=570):
|
13 |
-
"""
|
14 |
-
Draw stumps and crease lines on the pitch.
|
15 |
-
"""
|
16 |
for x in range(stump_zone[0], stump_zone[1] + 1, 20):
|
17 |
cv2.line(frame, (x, pitch_y), (x, pitch_y - 60), (255, 255, 255), 2)
|
18 |
cv2.line(frame, (stump_zone[0] - 40, pitch_y), (stump_zone[1] + 40, pitch_y), (255, 255, 255), 2)
|
19 |
|
20 |
def draw_trajectory(frame, trajectory_points):
|
21 |
-
"""
|
22 |
-
Draws the predicted ball trajectory curve on frame.
|
23 |
-
"""
|
24 |
for pt in trajectory_points:
|
25 |
x, y = pt
|
26 |
cv2.circle(frame, (int(x), int(y)), 5, (0, 255, 0), -1)
|
27 |
|
28 |
def add_decision_text(frame, decision):
|
29 |
-
"""
|
30 |
-
Overlay OUT / NOT OUT decision on top of the frame.
|
31 |
-
"""
|
32 |
color = (0, 0, 255) if decision == "OUT" else (0, 255, 0)
|
33 |
cv2.putText(frame, f"Decision: {decision}", (30, 50),
|
34 |
cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 3)
|
35 |
|
36 |
def generate_output_video(frames, detection_data, prediction_data, fps=25):
|
37 |
-
"""
|
38 |
-
Generates replay video with overlays and returns its file path and decision.
|
39 |
-
"""
|
40 |
impact_frame = detection_data["impact_frame"]
|
41 |
decision = prediction_data["decision"]
|
42 |
trajectory = prediction_data["trajectory_points"]
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
for idx, frame in enumerate(frames):
|
47 |
frame_copy = frame.copy()
|
48 |
draw_virtual_stumps(frame_copy)
|
49 |
|
50 |
if idx <= impact_frame:
|
51 |
-
# Draw live ball trajectory up to impact
|
52 |
draw_trajectory(frame_copy, trajectory[:idx + 1])
|
53 |
else:
|
54 |
-
# Draw full trajectory post-impact
|
55 |
draw_trajectory(frame_copy, trajectory)
|
56 |
|
57 |
if idx == impact_frame:
|
58 |
-
cv2.rectangle(frame_copy, (0, 0), (
|
59 |
|
60 |
add_decision_text(frame_copy, decision)
|
61 |
-
|
62 |
-
|
63 |
-
# Save as video using moviepy
|
64 |
-
temp_dir = tempfile.mkdtemp()
|
65 |
-
output_path = os.path.join(temp_dir, "lbw_replay.mp4")
|
66 |
-
clip = ImageSequenceClip(annotated_frames, fps=fps)
|
67 |
-
clip.write_videofile(output_path, codec="libx264", audio=False, verbose=False, logger=None)
|
68 |
|
|
|
69 |
return output_path, decision
|
|
|
2 |
import numpy as np
|
3 |
import tempfile
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def draw_virtual_stumps(frame, stump_zone=(280, 360), pitch_y=570):
|
|
|
|
|
|
|
7 |
for x in range(stump_zone[0], stump_zone[1] + 1, 20):
|
8 |
cv2.line(frame, (x, pitch_y), (x, pitch_y - 60), (255, 255, 255), 2)
|
9 |
cv2.line(frame, (stump_zone[0] - 40, pitch_y), (stump_zone[1] + 40, pitch_y), (255, 255, 255), 2)
|
10 |
|
11 |
def draw_trajectory(frame, trajectory_points):
|
|
|
|
|
|
|
12 |
for pt in trajectory_points:
|
13 |
x, y = pt
|
14 |
cv2.circle(frame, (int(x), int(y)), 5, (0, 255, 0), -1)
|
15 |
|
16 |
def add_decision_text(frame, decision):
|
|
|
|
|
|
|
17 |
color = (0, 0, 255) if decision == "OUT" else (0, 255, 0)
|
18 |
cv2.putText(frame, f"Decision: {decision}", (30, 50),
|
19 |
cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 3)
|
20 |
|
21 |
def generate_output_video(frames, detection_data, prediction_data, fps=25):
|
|
|
|
|
|
|
22 |
impact_frame = detection_data["impact_frame"]
|
23 |
decision = prediction_data["decision"]
|
24 |
trajectory = prediction_data["trajectory_points"]
|
25 |
|
26 |
+
height, width, _ = frames[0].shape
|
27 |
+
temp_dir = tempfile.mkdtemp()
|
28 |
+
output_path = os.path.join(temp_dir, "lbw_replay.mp4")
|
29 |
+
|
30 |
+
# Define video writer
|
31 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
32 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
33 |
|
34 |
for idx, frame in enumerate(frames):
|
35 |
frame_copy = frame.copy()
|
36 |
draw_virtual_stumps(frame_copy)
|
37 |
|
38 |
if idx <= impact_frame:
|
|
|
39 |
draw_trajectory(frame_copy, trajectory[:idx + 1])
|
40 |
else:
|
|
|
41 |
draw_trajectory(frame_copy, trajectory)
|
42 |
|
43 |
if idx == impact_frame:
|
44 |
+
cv2.rectangle(frame_copy, (0, 0), (width, height), (255, 0, 0), 6)
|
45 |
|
46 |
add_decision_text(frame_copy, decision)
|
47 |
+
out.write(frame_copy)
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
out.release()
|
50 |
return output_path, decision
|