dschandra commited on
Commit
088fd6f
·
verified ·
1 Parent(s): ac2cf6e

Update visualizer.py

Browse files
Files changed (1) hide show
  1. visualizer.py +30 -39
visualizer.py CHANGED
@@ -1,50 +1,41 @@
 
1
  import cv2
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
 
1
+ # visualizer.py
2
  import cv2
 
 
3
  import os
4
+ import uuid
5
+ from utils import save_video
6
 
7
+ OUTPUT_DIR = "output_videos"
8
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
 
 
9
 
10
+ def draw_visuals(frames, ball_positions, trajectory, impact_frame_idx, decision):
11
+ font = cv2.FONT_HERSHEY_SIMPLEX
12
+ output_frames = []
 
13
 
14
+ for i, frame in enumerate(frames):
15
+ frame_draw = frame.copy()
 
 
16
 
17
+ # Draw past ball positions
18
+ for _, x, y in ball_positions:
19
+ cv2.circle(frame_draw, (x, y), 5, (0, 255, 255), -1)
 
20
 
21
+ # Draw trajectory after impact
22
+ if i == impact_frame_idx:
23
+ for x, y in trajectory:
24
+ cv2.circle(frame_draw, (x, y), 4, (0, 0, 255), -1)
25
 
26
+ # Highlight impact frame
27
+ if i == impact_frame_idx:
28
+ cv2.putText(frame_draw, "IMPACT!", (50, 50), font, 1.0, (0, 0, 255), 2)
29
+ cv2.rectangle(frame_draw, (10, 10), (630, 470), (0, 0, 255), 4)
30
 
31
+ # Final decision on last frame
32
+ if i == len(frames) - 1:
33
+ cv2.putText(frame_draw, f"Decision: {decision}", (100, 420), font, 1.2, (255, 0, 0), 3)
34
 
35
+ output_frames.append(frame_draw)
 
 
 
36
 
37
+ # Save annotated video
38
+ video_id = str(uuid.uuid4())
39
+ out_path = os.path.join(OUTPUT_DIR, f"{video_id}_annotated.mp4")
40
+ save_video(output_frames, out_path)
41
+ return out_path