Spaces:
Sleeping
Sleeping
Update gully_drs_core/replay_utils.py
Browse files- gully_drs_core/replay_utils.py +28 -35
gully_drs_core/replay_utils.py
CHANGED
|
@@ -1,49 +1,49 @@
|
|
| 1 |
import cv2
|
| 2 |
-
import streamlit as st
|
| 3 |
import tempfile
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
def save_replay_clip(video_path,
|
| 7 |
cap = cv2.VideoCapture(video_path)
|
| 8 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 9 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 10 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 11 |
-
|
| 12 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 13 |
-
out = cv2.VideoWriter(
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
frame_count = 0
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
while True:
|
| 23 |
ret, frame = cap.read()
|
| 24 |
if not ret:
|
| 25 |
break
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
for i in range(1, len(
|
| 29 |
-
cv2.line(frame,
|
| 30 |
-
|
| 31 |
# Draw stump zone
|
| 32 |
x1, y1, x2, y2 = stump_zone
|
| 33 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 34 |
-
|
| 35 |
# Auto-zoom on impact frame
|
| 36 |
-
if frame_count == zoomed_frame_index:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
out.write(frame)
|
| 41 |
-
|
| 42 |
frame_count += 1
|
| 43 |
-
|
| 44 |
cap.release()
|
| 45 |
out.release()
|
| 46 |
-
|
| 47 |
return temp_output.name
|
| 48 |
|
| 49 |
def zoom_on_point(frame, point, zoom_factor=2.0):
|
|
@@ -54,16 +54,9 @@ def zoom_on_point(frame, point, zoom_factor=2.0):
|
|
| 54 |
y1 = max(0, cy - box_h // 2)
|
| 55 |
x2 = min(w, cx + box_w // 2)
|
| 56 |
y2 = min(h, cy + box_h // 2)
|
| 57 |
-
|
| 58 |
cropped = frame[y1:y2, x1:x2]
|
| 59 |
zoomed = cv2.resize(cropped, (w, h), interpolation=cv2.INTER_LINEAR)
|
| 60 |
return zoomed
|
| 61 |
|
| 62 |
-
|
| 63 |
-
with open(file_path, "rb") as file:
|
| 64 |
-
btn = st.download_button(
|
| 65 |
-
label="💾 Download Replay Clip",
|
| 66 |
-
data=file,
|
| 67 |
-
file_name=filename,
|
| 68 |
-
mime="video/mp4"
|
| 69 |
-
)
|
|
|
|
| 1 |
import cv2
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
def save_replay_clip(video_path, ball_path_3d, stump_zone, impact_frame=None):
|
| 6 |
cap = cv2.VideoCapture(video_path)
|
| 7 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 8 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 9 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 10 |
+
|
| 11 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 12 |
+
out = cv2.VideoWriter(
|
| 13 |
+
temp_output.name,
|
| 14 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
| 15 |
+
fps,
|
| 16 |
+
(frame_width, frame_height)
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
frame_count = 0
|
| 20 |
+
zoomed_frame_index = impact_frame - 1 if impact_frame else -1
|
| 21 |
+
|
| 22 |
+
# Extract 2D coordinates for video overlay
|
| 23 |
+
ball_path_2d = [(x, y) for x, y, z in ball_path_3d]
|
| 24 |
+
|
| 25 |
while True:
|
| 26 |
ret, frame = cap.read()
|
| 27 |
if not ret:
|
| 28 |
break
|
| 29 |
+
|
| 30 |
+
# Draw 2D trajectory on video
|
| 31 |
+
for i in range(1, len(ball_path_2d)):
|
| 32 |
+
cv2.line(frame, ball_path_2d[i-1], ball_path_2d[i], (0, 255, 0), 2)
|
| 33 |
+
|
| 34 |
# Draw stump zone
|
| 35 |
x1, y1, x2, y2 = stump_zone
|
| 36 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 37 |
+
|
| 38 |
# Auto-zoom on impact frame
|
| 39 |
+
if frame_count == zoomed_frame_index and ball_path_2d:
|
| 40 |
+
frame = zoom_on_point(frame, ball_path_2d[-1])
|
| 41 |
+
|
| 42 |
+
out.write(frame)
|
|
|
|
|
|
|
| 43 |
frame_count += 1
|
| 44 |
+
|
| 45 |
cap.release()
|
| 46 |
out.release()
|
|
|
|
| 47 |
return temp_output.name
|
| 48 |
|
| 49 |
def zoom_on_point(frame, point, zoom_factor=2.0):
|
|
|
|
| 54 |
y1 = max(0, cy - box_h // 2)
|
| 55 |
x2 = min(w, cx + box_w // 2)
|
| 56 |
y2 = min(h, cy + box_h // 2)
|
| 57 |
+
|
| 58 |
cropped = frame[y1:y2, x1:x2]
|
| 59 |
zoomed = cv2.resize(cropped, (w, h), interpolation=cv2.INTER_LINEAR)
|
| 60 |
return zoomed
|
| 61 |
|
| 62 |
+
# Removed download_button (handled in live_match.py with Gradio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|