Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -8,7 +8,11 @@ def analyze_frame_sequence(frames):
|
|
8 |
"pitch": "in line",
|
9 |
"impact": "in line",
|
10 |
"trajectory": "hitting",
|
11 |
-
"shot_offered": True
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
|
14 |
def make_decision(analysis):
|
@@ -25,6 +29,40 @@ def make_decision(analysis):
|
|
25 |
def overlay_text(frame, text, pos, size=1.0, color=(255,255,255)):
|
26 |
cv2.putText(frame, text, pos, cv2.FONT_HERSHEY_SIMPLEX, size, color, 2, cv2.LINE_AA)
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def render_annotated_clip(frames, analysis, decision, reason, output_path):
|
29 |
h, w = frames[0].shape[:2]
|
30 |
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (w, h))
|
@@ -36,25 +74,18 @@ def render_annotated_clip(frames, analysis, decision, reason, output_path):
|
|
36 |
for _ in range(20):
|
37 |
out.write(verdict_card)
|
38 |
|
39 |
-
#
|
40 |
-
for
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
overlay_text(f, f"Shot Offered: {'✓' if analysis['shot_offered'] else '✗'}", (30, 170), 0.8, (0,255,0) if analysis['shot_offered'] else (0,0,255))
|
45 |
-
overlay_text(f, "ICC LBW Review • Third-Umpire Analysis", (30, h - 30), 0.6, (180,180,180))
|
46 |
-
out.write(f)
|
47 |
|
48 |
-
# Slow-motion replay
|
49 |
for i in range(0, len(frames), 2):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
overlay_text(f, f"Shot Offered: {'✓' if analysis['shot_offered'] else '✗'}", (30, 170), 0.8, (0,255,0) if analysis['shot_offered'] else (0,0,255))
|
55 |
-
overlay_text(f, "ICC LBW Review • Third-Umpire Analysis", (30, h - 30), 0.6, (180,180,180))
|
56 |
-
out.write(f)
|
57 |
-
out.write(f)
|
58 |
|
59 |
# End frame 0.5s static
|
60 |
for _ in range(10):
|
|
|
8 |
"pitch": "in line",
|
9 |
"impact": "in line",
|
10 |
"trajectory": "hitting",
|
11 |
+
"shot_offered": True,
|
12 |
+
"pitch_point": (200, 300), # example pixel position
|
13 |
+
"impact_point": (220, 320),
|
14 |
+
"trajectory_curve": [(230, 330), (250, 310), (270, 290), (290, 270)],
|
15 |
+
"stump_zone": [(300, 200), (340, 400)] # top-left and bottom-right of projected stumps
|
16 |
}
|
17 |
|
18 |
def make_decision(analysis):
|
|
|
29 |
def overlay_text(frame, text, pos, size=1.0, color=(255,255,255)):
|
30 |
cv2.putText(frame, text, pos, cv2.FONT_HERSHEY_SIMPLEX, size, color, 2, cv2.LINE_AA)
|
31 |
|
32 |
+
def overlay_annotations(frame, analysis):
|
33 |
+
# pitch point
|
34 |
+
if 'pitch_point' in analysis:
|
35 |
+
cv2.circle(frame, analysis['pitch_point'], 10, (0, 255, 0) if analysis['pitch'] == 'in line' else (0, 0, 255), -1)
|
36 |
+
overlay_text(frame, f"Pitch: {analysis['pitch'].capitalize()}", (analysis['pitch_point'][0]+10, analysis['pitch_point'][1]), 0.7)
|
37 |
+
|
38 |
+
# impact point
|
39 |
+
if 'impact_point' in analysis:
|
40 |
+
cv2.circle(frame, analysis['impact_point'], 10, (0, 255, 0) if analysis['impact'] == 'in line' else (0, 0, 255), -1)
|
41 |
+
overlay_text(frame, f"Impact: {analysis['impact'].capitalize()}", (analysis['impact_point'][0]+10, analysis['impact_point'][1]), 0.7)
|
42 |
+
|
43 |
+
# shot icon
|
44 |
+
shot_icon = "✓" if analysis['shot_offered'] else "✗"
|
45 |
+
overlay_text(frame, f"Shot Offered: {shot_icon}", (30, 170), 0.8, (0,255,0) if analysis['shot_offered'] else (0,0,255))
|
46 |
+
|
47 |
+
# trajectory arc
|
48 |
+
if 'trajectory_curve' in analysis:
|
49 |
+
for i in range(len(analysis['trajectory_curve'])-1):
|
50 |
+
cv2.line(frame, analysis['trajectory_curve'][i], analysis['trajectory_curve'][i+1], (0,255,0) if analysis['trajectory']=="hitting" else (0,0,255), 2, cv2.LINE_AA)
|
51 |
+
|
52 |
+
# transparent stump zone
|
53 |
+
if 'stump_zone' in analysis:
|
54 |
+
x1, y1 = analysis['stump_zone'][0]
|
55 |
+
x2, y2 = analysis['stump_zone'][1]
|
56 |
+
overlay = frame.copy()
|
57 |
+
cv2.rectangle(overlay, (x1, y1), (x2, y2), (255, 255, 255), -1)
|
58 |
+
alpha = 0.2
|
59 |
+
frame[:] = cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0)
|
60 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (180, 180, 180), 2)
|
61 |
+
|
62 |
+
overlay_text(frame, f"Trajectory: {analysis['trajectory'].capitalize()}", (30, 130), 0.8, (0,255,0) if analysis['trajectory']=="hitting" else (0,0,255))
|
63 |
+
overlay_text(frame, "ICC LBW Review • Third-Umpire Analysis", (30, frame.shape[0] - 30), 0.6, (180,180,180))
|
64 |
+
|
65 |
+
|
66 |
def render_annotated_clip(frames, analysis, decision, reason, output_path):
|
67 |
h, w = frames[0].shape[:2]
|
68 |
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (w, h))
|
|
|
74 |
for _ in range(20):
|
75 |
out.write(verdict_card)
|
76 |
|
77 |
+
# Real-time with annotations
|
78 |
+
for frame in frames:
|
79 |
+
annotated = frame.copy()
|
80 |
+
overlay_annotations(annotated, analysis)
|
81 |
+
out.write(annotated)
|
|
|
|
|
|
|
82 |
|
83 |
+
# Slow-motion replay
|
84 |
for i in range(0, len(frames), 2):
|
85 |
+
annotated = frames[i].copy()
|
86 |
+
overlay_annotations(annotated, analysis)
|
87 |
+
out.write(annotated)
|
88 |
+
out.write(annotated)
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# End frame 0.5s static
|
91 |
for _ in range(10):
|