Spaces:
Sleeping
Sleeping
import cv2 | |
import numpy as np | |
import tempfile | |
import os | |
def draw_virtual_stumps(frame, stump_zone=(280, 360), pitch_y=570): | |
for x in range(stump_zone[0], stump_zone[1] + 1, 20): | |
cv2.line(frame, (x, pitch_y), (x, pitch_y - 60), (255, 255, 255), 2) | |
cv2.line(frame, (stump_zone[0] - 40, pitch_y), (stump_zone[1] + 40, pitch_y), (255, 255, 255), 2) | |
def draw_trajectory(frame, trajectory_points): | |
for pt in trajectory_points: | |
x, y = pt | |
cv2.circle(frame, (int(x), int(y)), 5, (0, 255, 0), -1) | |
def add_decision_text(frame, decision): | |
color = (0, 0, 255) if decision == "OUT" else (0, 255, 0) | |
cv2.putText(frame, f"Decision: {decision}", (30, 50), | |
cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 3) | |
def generate_output_video(frames, detection_data, prediction_data, fps=25): | |
impact_frame = detection_data["impact_frame"] | |
decision = prediction_data["decision"] | |
trajectory = prediction_data["trajectory_points"] | |
height, width, _ = frames[0].shape | |
temp_dir = tempfile.mkdtemp() | |
output_path = os.path.join(temp_dir, "lbw_replay.mp4") | |
# Define video writer | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) | |
for idx, frame in enumerate(frames): | |
frame_copy = frame.copy() | |
draw_virtual_stumps(frame_copy) | |
if idx <= impact_frame: | |
draw_trajectory(frame_copy, trajectory[:idx + 1]) | |
else: | |
draw_trajectory(frame_copy, trajectory) | |
if idx == impact_frame: | |
cv2.rectangle(frame_copy, (0, 0), (width, height), (255, 0, 0), 6) | |
add_decision_text(frame_copy, decision) | |
out.write(frame_copy) | |
out.release() | |
return output_path, decision | |