lbw_review_system / utils.py
dschandra's picture
Update utils.py
6776f03 verified
# utils.py
import cv2
import numpy as np
def analyze_frame_sequence(frames):
return {
"pitch": "in line",
"impact": "in line",
"trajectory": "hitting",
"shot_offered": True,
"pitch_point": (200, 300), # example pixel position
"impact_point": (220, 320),
"trajectory_curve": [(230, 330), (250, 310), (270, 290), (290, 270)],
"stump_zone": [(300, 200), (340, 400)] # top-left and bottom-right of projected stumps
}
def make_decision(analysis):
if analysis['pitch'] == 'outside leg':
return "NOT OUT", "Pitched outside leg stump."
if analysis['impact'] == 'outside off' and analysis['shot_offered']:
return "NOT OUT", "Impact outside off with shot offered."
if analysis['trajectory'] == 'missing':
return "NOT OUT", "Ball missing stumps."
if analysis['trajectory'] == 'umpires call':
return "UMPIRE’S CALL", "Marginal trajectory impact."
return "OUT", "Impact in line and ball hitting stumps."
def overlay_text(frame, text, pos, size=1.0, color=(255,255,255)):
cv2.putText(frame, text, pos, cv2.FONT_HERSHEY_SIMPLEX, size, color, 2, cv2.LINE_AA)
def overlay_annotations(frame, analysis):
# pitch point
if 'pitch_point' in analysis:
cv2.circle(frame, analysis['pitch_point'], 10, (0, 255, 0) if analysis['pitch'] == 'in line' else (0, 0, 255), -1)
overlay_text(frame, f"Pitch: {analysis['pitch'].capitalize()}", (analysis['pitch_point'][0]+10, analysis['pitch_point'][1]), 0.7)
# impact point
if 'impact_point' in analysis:
cv2.circle(frame, analysis['impact_point'], 10, (0, 255, 0) if analysis['impact'] == 'in line' else (0, 0, 255), -1)
overlay_text(frame, f"Impact: {analysis['impact'].capitalize()}", (analysis['impact_point'][0]+10, analysis['impact_point'][1]), 0.7)
# shot icon
shot_icon = "✓" if analysis['shot_offered'] else "✗"
overlay_text(frame, f"Shot Offered: {shot_icon}", (30, 170), 0.8, (0,255,0) if analysis['shot_offered'] else (0,0,255))
# trajectory arc
if 'trajectory_curve' in analysis:
for i in range(len(analysis['trajectory_curve'])-1):
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)
# transparent stump zone
if 'stump_zone' in analysis:
x1, y1 = analysis['stump_zone'][0]
x2, y2 = analysis['stump_zone'][1]
overlay = frame.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), (255, 255, 255), -1)
alpha = 0.2
frame[:] = cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0)
cv2.rectangle(frame, (x1, y1), (x2, y2), (180, 180, 180), 2)
overlay_text(frame, f"Trajectory: {analysis['trajectory'].capitalize()}", (30, 130), 0.8, (0,255,0) if analysis['trajectory']=="hitting" else (0,0,255))
overlay_text(frame, "ICC LBW Review • Third-Umpire Analysis", (30, frame.shape[0] - 30), 0.6, (180,180,180))
def render_annotated_clip(frames, analysis, decision, reason, output_path):
h, w = frames[0].shape[:2]
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (w, h))
# First 1s: Verdict card
verdict_card = np.zeros_like(frames[0])
overlay_text(verdict_card, f"FINAL DECISION: {decision}", (50, 200), 2.0, (255,255,255))
overlay_text(verdict_card, reason, (50, 250), 0.9, (200,200,200))
for _ in range(20):
out.write(verdict_card)
# Real-time with annotations
for frame in frames:
annotated = frame.copy()
overlay_annotations(annotated, analysis)
out.write(annotated)
# Slow-motion replay
for i in range(0, len(frames), 2):
annotated = frames[i].copy()
overlay_annotations(annotated, analysis)
out.write(annotated)
out.write(annotated)
# End frame 0.5s static
for _ in range(10):
out.write(verdict_card)
out.release()