Spaces:
Sleeping
Sleeping
Create video_utils.py
Browse files- video_utils.py +42 -0
video_utils.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
from draw_utils import draw_trajectory
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
def process_video(video_path):
|
| 7 |
+
cap = cv2.VideoCapture(video_path)
|
| 8 |
+
frames = []
|
| 9 |
+
impact_frame = None
|
| 10 |
+
trajectory = []
|
| 11 |
+
|
| 12 |
+
frame_count = 0
|
| 13 |
+
while cap.isOpened():
|
| 14 |
+
ret, frame = cap.read()
|
| 15 |
+
if not ret:
|
| 16 |
+
break
|
| 17 |
+
|
| 18 |
+
# Simulated detection (replace with real ML model or logic)
|
| 19 |
+
if 20 < frame_count < 80: # Simulate ball in motion
|
| 20 |
+
trajectory.append((100 + frame_count, 200 + int(frame_count * 0.2))) # fake curve
|
| 21 |
+
if frame_count == 70:
|
| 22 |
+
impact_frame = frame.copy()
|
| 23 |
+
|
| 24 |
+
frames.append(frame)
|
| 25 |
+
frame_count += 1
|
| 26 |
+
|
| 27 |
+
cap.release()
|
| 28 |
+
|
| 29 |
+
# Draw trajectory
|
| 30 |
+
pred_path = f"pred_{uuid.uuid4().hex}.mp4"
|
| 31 |
+
draw_trajectory(frames, trajectory, pred_path)
|
| 32 |
+
|
| 33 |
+
# Save impact frame for analysis
|
| 34 |
+
analysis_data = {
|
| 35 |
+
"trajectory": trajectory,
|
| 36 |
+
"impact_frame": impact_frame
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
replay_path = f"replay_{uuid.uuid4().hex}.mp4"
|
| 40 |
+
draw_trajectory(frames, trajectory, replay_path, replay=True)
|
| 41 |
+
|
| 42 |
+
return pred_path, replay_path, analysis_data
|