Create video_processing.py
Browse files- utils/video_processing.py +73 -0
utils/video_processing.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# backend/utils/video_processing.py
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from ultralytics import YOLO
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load YOLO model (assumed to be trained for cricket objects)
|
8 |
+
model = YOLO('models/yolov8_model.pt')
|
9 |
+
|
10 |
+
def track_ball(video_path: str) -> list:
|
11 |
+
"""
|
12 |
+
Track the ball in the video and return its trajectory as a list of (x, y) coordinates.
|
13 |
+
"""
|
14 |
+
cap = cv2.VideoCapture(video_path)
|
15 |
+
tracker = cv2.TrackerKCF_create()
|
16 |
+
trajectory = []
|
17 |
+
init = False
|
18 |
+
|
19 |
+
while cap.isOpened():
|
20 |
+
ret, frame = cap.read()
|
21 |
+
if not ret:
|
22 |
+
break
|
23 |
+
if not init:
|
24 |
+
# Detect ball using YOLO for initial bounding box
|
25 |
+
results = model(frame)
|
26 |
+
for detection in results[0].boxes:
|
27 |
+
if detection.cls == 0: # Assume class 0 is the ball
|
28 |
+
x, y, w, h = detection.xywh[0]
|
29 |
+
bbox = (int(x - w/2), int(y - h/2), int(w), int(h))
|
30 |
+
tracker.init(frame, bbox)
|
31 |
+
trajectory.append((x, y))
|
32 |
+
init = True
|
33 |
+
break
|
34 |
+
else:
|
35 |
+
# Update tracker
|
36 |
+
ok, bbox = tracker.update(frame)
|
37 |
+
if ok:
|
38 |
+
x, y, w, h = [int(v) for v in bbox]
|
39 |
+
trajectory.append((x + w/2, y + h/2))
|
40 |
+
|
41 |
+
cap.release()
|
42 |
+
return trajectory
|
43 |
+
|
44 |
+
def generate_replay(video_path: str, trajectory: list, decision: str) -> str:
|
45 |
+
"""
|
46 |
+
Generate a slow-motion replay video with ball trajectory and decision overlay.
|
47 |
+
"""
|
48 |
+
cap = cv2.VideoCapture(video_path)
|
49 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
50 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
51 |
+
fps = cap.get(cv2.CAP_PROP_FPS) / 2 # Slow motion (half speed)
|
52 |
+
|
53 |
+
replay_path = f"replays/replay_{os.path.basename(video_path)}"
|
54 |
+
out = cv2.VideoWriter(replay_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
55 |
+
frame_idx = 0
|
56 |
+
|
57 |
+
while cap.isOpened():
|
58 |
+
ret, frame = cap.read()
|
59 |
+
if not ret:
|
60 |
+
break
|
61 |
+
if frame_idx < len(trajectory):
|
62 |
+
x, y = trajectory[frame_idx]
|
63 |
+
cv2.circle(frame, (int(x), int(y)), 5, (0, 0, 255), -1)
|
64 |
+
for i in range(1, min(frame_idx + 1, len(trajectory))):
|
65 |
+
cv2.line(frame, (int(trajectory[i-1][0]), int(trajectory[i-1][1])),
|
66 |
+
(int(trajectory[i][0]), int(trajectory[i][1])), (255, 0, 0), 2)
|
67 |
+
cv2.putText(frame, f"Decision: {decision}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
68 |
+
out.write(frame)
|
69 |
+
frame_idx += 1
|
70 |
+
|
71 |
+
cap.release()
|
72 |
+
out.release()
|
73 |
+
return replay_path
|