Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,20 @@
|
|
1 |
-
import gradio as gr
|
2 |
import cv2
|
|
|
3 |
import numpy as np
|
|
|
4 |
import tempfile
|
5 |
-
import os
|
6 |
-
|
7 |
-
def detect_and_predict(video):
|
8 |
-
cap = cv2.VideoCapture(video)
|
9 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
10 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
ball_color_lower = np.array([5,
|
19 |
-
ball_color_upper = np.array([
|
20 |
-
|
21 |
|
22 |
while True:
|
23 |
ret, frame = cap.read()
|
@@ -35,29 +32,56 @@ def detect_and_predict(video):
|
|
35 |
if contours:
|
36 |
c = max(contours, key=cv2.contourArea)
|
37 |
((x, y), radius) = cv2.minEnclosingCircle(c)
|
38 |
-
|
39 |
if radius > 3:
|
40 |
-
|
41 |
-
|
|
|
42 |
|
43 |
-
# Draw
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
# Draw
|
48 |
-
|
|
|
49 |
|
50 |
out.write(frame)
|
51 |
|
52 |
cap.release()
|
53 |
out.release()
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return out_path
|
56 |
|
57 |
-
iface = gr.Interface(
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
|
63 |
iface.launch()
|
|
|
|
|
1 |
import cv2
|
2 |
+
import gradio as gr
|
3 |
import numpy as np
|
4 |
+
from sklearn.linear_model import LinearRegression
|
5 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def process_video(video_path):
|
8 |
+
cap = cv2.VideoCapture(video_path)
|
9 |
+
frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
10 |
+
frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
11 |
|
12 |
+
out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
13 |
+
out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*"mp4v"), 20.0, (frame_w, frame_h))
|
14 |
|
15 |
+
ball_color_lower = np.array([5, 100, 100]) # HSV orange-red
|
16 |
+
ball_color_upper = np.array([20, 255, 255])
|
17 |
+
trajectory = []
|
18 |
|
19 |
while True:
|
20 |
ret, frame = cap.read()
|
|
|
32 |
if contours:
|
33 |
c = max(contours, key=cv2.contourArea)
|
34 |
((x, y), radius) = cv2.minEnclosingCircle(c)
|
|
|
35 |
if radius > 3:
|
36 |
+
center = (int(x), int(y))
|
37 |
+
trajectory.append(center)
|
38 |
+
cv2.circle(frame, center, int(radius), (0, 0, 255), 2)
|
39 |
|
40 |
+
# Draw stumps area
|
41 |
+
stump_box = (frame_w // 2 - 30, frame_h - 120, frame_w // 2 + 30, frame_h - 50)
|
42 |
+
cv2.rectangle(frame, stump_box[:2], stump_box[2:], (0, 255, 255), 2)
|
43 |
|
44 |
+
# Draw trajectory
|
45 |
+
for i in range(1, len(trajectory)):
|
46 |
+
cv2.line(frame, trajectory[i - 1], trajectory[i], (255, 0, 0), 2)
|
47 |
|
48 |
out.write(frame)
|
49 |
|
50 |
cap.release()
|
51 |
out.release()
|
52 |
|
53 |
+
decision = "NOT OUT"
|
54 |
+
projected_hit = False
|
55 |
+
|
56 |
+
# Predict trajectory
|
57 |
+
if len(trajectory) >= 5:
|
58 |
+
X = np.array([x for x, y in trajectory]).reshape(-1, 1)
|
59 |
+
y = np.array([y for x, y in trajectory])
|
60 |
+
|
61 |
+
model = LinearRegression()
|
62 |
+
model.fit(X, y)
|
63 |
+
|
64 |
+
# Predict at stump x-position
|
65 |
+
stump_x = frame_w // 2
|
66 |
+
pred_y = int(model.predict(np.array([[stump_x]]))[0])
|
67 |
+
|
68 |
+
# Check if predicted Y is in stump area
|
69 |
+
if stump_box[1] <= pred_y <= stump_box[3]:
|
70 |
+
projected_hit = True
|
71 |
+
decision = "OUT"
|
72 |
+
|
73 |
+
# Write final frame with decision
|
74 |
+
final_frame = cv2.imread(out_path)
|
75 |
+
cv2.putText(final_frame, f"DECISION: {decision}", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0) if decision == "NOT OUT" else (0, 0, 255), 4)
|
76 |
+
|
77 |
return out_path
|
78 |
|
79 |
+
iface = gr.Interface(
|
80 |
+
fn=process_video,
|
81 |
+
inputs=gr.Video(label="Upload Bowling Video (Camera View)"),
|
82 |
+
outputs=gr.Video(label="Replay with Trajectory and Decision"),
|
83 |
+
title="Cricket DRS LBW Tracker",
|
84 |
+
description="Track ball trajectory and check if it's LBW OUT based on impact prediction."
|
85 |
+
)
|
86 |
|
87 |
iface.launch()
|