Update app.py
Browse files
app.py
CHANGED
|
@@ -2,69 +2,81 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
|
| 6 |
import tempfile
|
| 7 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Color codes for DRS elements
|
| 10 |
COLORS = {
|
| 11 |
-
"out": (0, 0, 255),
|
| 12 |
-
"not_out": (0, 255, 0),
|
| 13 |
-
"hitting": (255, 165, 0),
|
| 14 |
"impact": (255, 192, 203), # Pink
|
| 15 |
"in_line": (255, 255, 0), # Yellow
|
| 16 |
"pitching": (0, 255, 255), # Cyan
|
| 17 |
-
"speed": (255, 0, 255) # Magenta
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
|
|
|
| 22 |
try:
|
| 23 |
model = YOLO(model_name)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
-
print(f"
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
return positions, 0.0
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return list(zip(new_x, new_y)), max(0, min(speed, 160)) # Clamp 0-160 km/h
|
| 57 |
-
except:
|
| 58 |
-
return positions, 0.0
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
"""
|
| 62 |
-
# Decision parameters
|
| 63 |
hitting = "HITTING" if abs(ball_pos[0] - stump_pos[0]) < 60 else "MISSING"
|
| 64 |
impact = "IMPACT" if ball_pos[1] > stump_pos[1] - 50 else "NO IMPACT"
|
| 65 |
in_line = "IN-LINE" if abs(ball_pos[0] - stump_pos[0]) < 80 else "OUTSIDE OFF"
|
| 66 |
pitching = "IN-LINE" if ball_pos[1] < stump_pos[1] + 200 else "OUTSIDE LEG"
|
| 67 |
|
|
|
|
| 68 |
decision = "OUT" if all([
|
| 69 |
hitting == "HITTING",
|
| 70 |
impact == "IMPACT",
|
|
@@ -79,111 +91,147 @@ def check_lbw(ball_pos, stump_pos):
|
|
| 79 |
"pitching": pitching
|
| 80 |
}
|
| 81 |
|
| 82 |
-
def draw_drs_overlay(frame, lbw_data, speed):
|
| 83 |
"""Draw professional broadcast-style overlay"""
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Title
|
| 89 |
cv2.putText(frame, "DECISION REVIEW SYSTEM", (40, 60),
|
| 90 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
|
| 91 |
|
| 92 |
-
# Original decision (
|
| 93 |
cv2.putText(frame, "ORIGINAL DECISION", (40, 100),
|
| 94 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 95 |
-
cv2.putText(frame, "OUT", (
|
| 96 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, COLORS["out"], 2)
|
| 97 |
|
| 98 |
# Final decision
|
| 99 |
decision_color = COLORS["out"] if lbw_data["decision"] == "OUT" else COLORS["not_out"]
|
| 100 |
cv2.putText(frame, "FINAL DECISION", (40, 140),
|
| 101 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 102 |
-
cv2.putText(frame, lbw_data["decision"], (
|
| 103 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, decision_color, 2)
|
| 104 |
|
| 105 |
-
#
|
| 106 |
cv2.putText(frame, "WICKETS", (40, 180),
|
| 107 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 108 |
-
cv2.putText(frame, lbw_data["hitting"], (
|
| 109 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["hitting"], 2)
|
| 110 |
|
| 111 |
-
# Impact
|
| 112 |
cv2.putText(frame, "IMPACT", (40, 210),
|
| 113 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 114 |
-
cv2.putText(frame, lbw_data["impact"], (
|
| 115 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["impact"], 2)
|
| 116 |
|
| 117 |
-
# In Line
|
| 118 |
cv2.putText(frame, "IN-LINE", (40, 240),
|
| 119 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 120 |
-
cv2.putText(frame, lbw_data["in_line"], (
|
| 121 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["in_line"], 2)
|
| 122 |
|
| 123 |
-
# Pitching
|
| 124 |
cv2.putText(frame, "PITCHING", (40, 270),
|
| 125 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 126 |
-
cv2.putText(frame, lbw_data["pitching"], (
|
| 127 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["pitching"], 2)
|
| 128 |
|
| 129 |
# Speed display
|
| 130 |
-
cv2.putText(frame, f"SPEED: {speed:.1f} km/h", (
|
| 131 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, COLORS["speed"], 2)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
try:
|
|
|
|
|
|
|
| 136 |
# Handle Gradio input
|
| 137 |
video_path = video_input if isinstance(video_input, str) else video_input["name"]
|
| 138 |
|
| 139 |
cap = cv2.VideoCapture(video_path)
|
| 140 |
if not cap.isOpened():
|
| 141 |
-
raise ValueError("Could not open video")
|
| 142 |
|
|
|
|
|
|
|
|
|
|
| 143 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 144 |
-
width, height = int(cap.get(3)), int(cap.get(4))
|
| 145 |
|
| 146 |
-
# Create temp output
|
| 147 |
-
|
| 148 |
-
out = cv2.VideoWriter(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
|
|
|
| 150 |
ball_positions = []
|
| 151 |
lbw_data = None
|
| 152 |
max_speed = 0.0
|
|
|
|
|
|
|
| 153 |
|
| 154 |
while True:
|
| 155 |
ret, frame = cap.read()
|
| 156 |
if not ret:
|
| 157 |
break
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
for i in range(1, len(trajectory)):
|
| 175 |
-
cv2.line(frame,
|
| 176 |
-
tuple(map(int, trajectory[i-1])),
|
| 177 |
-
tuple(map(int, trajectory[i])),
|
| 178 |
-
(0, 255, 255), 2)
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# Draw overlay if we have data
|
| 189 |
if lbw_data:
|
|
@@ -194,45 +242,70 @@ def process_video(video_input):
|
|
| 194 |
cap.release()
|
| 195 |
out.release()
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
"decision": lbw_data["decision"] if lbw_data else "NO DECISION",
|
| 200 |
-
"speed": max_speed
|
| 201 |
-
}
|
| 202 |
|
| 203 |
except Exception as e:
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
"decision": f"ERROR: {str(e)}",
|
| 207 |
-
"speed": 0.0
|
| 208 |
-
}
|
| 209 |
|
| 210 |
-
# Gradio Interface
|
| 211 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 212 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
-
input_video = gr.Video(label="
|
| 216 |
-
output_video = gr.Video(label="DRS Analysis", format="mp4")
|
| 217 |
|
| 218 |
with gr.Row():
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
analyze_btn = gr.Button("Run
|
| 223 |
|
| 224 |
-
def
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
return {
|
| 227 |
-
output_video:
|
| 228 |
-
decision:
|
| 229 |
-
speed:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
}
|
| 231 |
|
| 232 |
analyze_btn.click(
|
| 233 |
-
fn=
|
| 234 |
inputs=input_video,
|
| 235 |
-
outputs=[output_video, decision, speed]
|
| 236 |
)
|
| 237 |
|
| 238 |
-
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
import torch
|
| 6 |
import tempfile
|
| 7 |
+
import time
|
| 8 |
+
from typing import Dict, List
|
| 9 |
+
|
| 10 |
+
# ===== Configuration =====
|
| 11 |
+
FRAME_SKIP = 2 # Process every 2nd frame (3 for faster but less smooth)
|
| 12 |
+
ANALYSIS_SIZE = 640 # Resolution for processing (higher = more accurate but slower)
|
| 13 |
+
USE_QUANTIZED = True # Use optimized model format
|
| 14 |
+
BATCH_SIZE = 4 # Number of frames to process simultaneously
|
| 15 |
+
MIN_CONFIDENCE = 0.5 # Detection confidence threshold
|
| 16 |
|
| 17 |
# Color codes for DRS elements
|
| 18 |
COLORS = {
|
| 19 |
+
"out": (0, 0, 255), # Red
|
| 20 |
+
"not_out": (0, 255, 0), # Green
|
| 21 |
+
"hitting": (255, 165, 0), # Orange
|
| 22 |
"impact": (255, 192, 203), # Pink
|
| 23 |
"in_line": (255, 255, 0), # Yellow
|
| 24 |
"pitching": (0, 255, 255), # Cyan
|
| 25 |
+
"speed": (255, 0, 255), # Magenta
|
| 26 |
+
"trajectory": (0, 255, 255) # Light blue
|
| 27 |
}
|
| 28 |
|
| 29 |
+
# ===== Model Initialization =====
|
| 30 |
+
def load_optimized_model(model_name: str):
|
| 31 |
+
"""Load model with optimizations for speed"""
|
| 32 |
try:
|
| 33 |
model = YOLO(model_name)
|
| 34 |
+
if USE_QUANTIZED:
|
| 35 |
+
# Create optimized model if it doesn't exist
|
| 36 |
+
if not os.path.exists(model_name.replace('.pt', '.onnx')):
|
| 37 |
+
model.export(format='onnx', dynamic=True, simplify=True)
|
| 38 |
+
return YOLO(model_name.replace('.pt', '.onnx'))
|
| 39 |
+
return model
|
| 40 |
except Exception as e:
|
| 41 |
+
print(f"Model loading error: {str(e)}")
|
| 42 |
+
return None
|
| 43 |
|
| 44 |
+
print("Loading models...")
|
| 45 |
+
BALL_MODEL = load_optimized_model("yolov8n.pt") # Ball detection
|
| 46 |
+
STUMP_MODEL = load_optimized_model("yolov8m.pt") # Stump detection
|
| 47 |
+
print("Models loaded successfully!")
|
| 48 |
|
| 49 |
+
# ===== Core Functions =====
|
| 50 |
+
def predict_trajectory_simple(positions: List[tuple]) -> tuple:
|
| 51 |
+
"""Fast trajectory prediction using linear extrapolation"""
|
| 52 |
+
if len(positions) < 2:
|
| 53 |
return positions, 0.0
|
| 54 |
|
| 55 |
+
# Calculate movement vector
|
| 56 |
+
dx = positions[-1][0] - positions[-2][0]
|
| 57 |
+
dy = positions[-1][1] - positions[-2][1]
|
| 58 |
|
| 59 |
+
# Predict next 5 positions
|
| 60 |
+
new_positions = positions.copy()
|
| 61 |
+
for i in range(1, 6):
|
| 62 |
+
new_positions.append((positions[-1][0] + i*dx,
|
| 63 |
+
positions[-1][1] + i*dy))
|
| 64 |
+
|
| 65 |
+
# Calculate speed (km/h)
|
| 66 |
+
px_per_frame = np.sqrt(dx**2 + dy**2)
|
| 67 |
+
speed = px_per_frame * 25 * 3.6 / 2000 # Calibrated conversion
|
| 68 |
+
|
| 69 |
+
return new_positions, min(max(speed, 0), 160) # Clamp 0-160 km/h
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
def check_lbw_decision(ball_pos: tuple, stump_pos: tuple) -> Dict:
|
| 72 |
+
"""Determine LBW outcome with all parameters"""
|
| 73 |
+
# Decision parameters (in pixels)
|
| 74 |
hitting = "HITTING" if abs(ball_pos[0] - stump_pos[0]) < 60 else "MISSING"
|
| 75 |
impact = "IMPACT" if ball_pos[1] > stump_pos[1] - 50 else "NO IMPACT"
|
| 76 |
in_line = "IN-LINE" if abs(ball_pos[0] - stump_pos[0]) < 80 else "OUTSIDE OFF"
|
| 77 |
pitching = "IN-LINE" if ball_pos[1] < stump_pos[1] + 200 else "OUTSIDE LEG"
|
| 78 |
|
| 79 |
+
# Final decision
|
| 80 |
decision = "OUT" if all([
|
| 81 |
hitting == "HITTING",
|
| 82 |
impact == "IMPACT",
|
|
|
|
| 91 |
"pitching": pitching
|
| 92 |
}
|
| 93 |
|
| 94 |
+
def draw_drs_overlay(frame: np.ndarray, lbw_data: Dict, speed: float):
|
| 95 |
"""Draw professional broadcast-style overlay"""
|
| 96 |
+
h, w = frame.shape[:2]
|
| 97 |
+
|
| 98 |
+
# Main DRS panel
|
| 99 |
+
cv2.rectangle(frame, (20, 20), (450, 280), (40, 40, 40), -1)
|
| 100 |
+
cv2.rectangle(frame, (20, 20), (450, 280), (200, 200, 200), 2)
|
| 101 |
|
| 102 |
# Title
|
| 103 |
cv2.putText(frame, "DECISION REVIEW SYSTEM", (40, 60),
|
| 104 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
|
| 105 |
|
| 106 |
+
# Original decision (static for demo)
|
| 107 |
cv2.putText(frame, "ORIGINAL DECISION", (40, 100),
|
| 108 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 109 |
+
cv2.putText(frame, "OUT", (350, 100),
|
| 110 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, COLORS["out"], 2)
|
| 111 |
|
| 112 |
# Final decision
|
| 113 |
decision_color = COLORS["out"] if lbw_data["decision"] == "OUT" else COLORS["not_out"]
|
| 114 |
cv2.putText(frame, "FINAL DECISION", (40, 140),
|
| 115 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 116 |
+
cv2.putText(frame, lbw_data["decision"], (350, 140),
|
| 117 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, decision_color, 2)
|
| 118 |
|
| 119 |
+
# Decision parameters
|
| 120 |
cv2.putText(frame, "WICKETS", (40, 180),
|
| 121 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 122 |
+
cv2.putText(frame, lbw_data["hitting"], (350, 180),
|
| 123 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["hitting"], 2)
|
| 124 |
|
|
|
|
| 125 |
cv2.putText(frame, "IMPACT", (40, 210),
|
| 126 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 127 |
+
cv2.putText(frame, lbw_data["impact"], (350, 210),
|
| 128 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["impact"], 2)
|
| 129 |
|
|
|
|
| 130 |
cv2.putText(frame, "IN-LINE", (40, 240),
|
| 131 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 132 |
+
cv2.putText(frame, lbw_data["in_line"], (350, 240),
|
| 133 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["in_line"], 2)
|
| 134 |
|
|
|
|
| 135 |
cv2.putText(frame, "PITCHING", (40, 270),
|
| 136 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 137 |
+
cv2.putText(frame, lbw_data["pitching"], (350, 270),
|
| 138 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, COLORS["pitching"], 2)
|
| 139 |
|
| 140 |
# Speed display
|
| 141 |
+
cv2.putText(frame, f"SPEED: {speed:.1f} km/h", (w-300, 50),
|
| 142 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, COLORS["speed"], 2)
|
| 143 |
|
| 144 |
+
# ===== Main Processing =====
|
| 145 |
+
def process_video_optimized(video_input) -> str:
|
| 146 |
+
"""Optimized video processing pipeline"""
|
| 147 |
try:
|
| 148 |
+
start_time = time.time()
|
| 149 |
+
|
| 150 |
# Handle Gradio input
|
| 151 |
video_path = video_input if isinstance(video_input, str) else video_input["name"]
|
| 152 |
|
| 153 |
cap = cv2.VideoCapture(video_path)
|
| 154 |
if not cap.isOpened():
|
| 155 |
+
raise ValueError("Could not open video file")
|
| 156 |
|
| 157 |
+
# Get video properties
|
| 158 |
+
orig_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 159 |
+
orig_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 160 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
| 161 |
|
| 162 |
+
# Create temp output file
|
| 163 |
+
temp_path = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False).name
|
| 164 |
+
out = cv2.VideoWriter(
|
| 165 |
+
temp_path,
|
| 166 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
| 167 |
+
fps/FRAME_SKIP, # Adjusted framerate
|
| 168 |
+
(orig_width, orig_height)
|
| 169 |
+
)
|
| 170 |
|
| 171 |
+
# Tracking variables
|
| 172 |
ball_positions = []
|
| 173 |
lbw_data = None
|
| 174 |
max_speed = 0.0
|
| 175 |
+
frame_count = 0
|
| 176 |
+
frame_batch = []
|
| 177 |
|
| 178 |
while True:
|
| 179 |
ret, frame = cap.read()
|
| 180 |
if not ret:
|
| 181 |
break
|
| 182 |
|
| 183 |
+
frame_count += 1
|
| 184 |
+
|
| 185 |
+
# Skip frames according to FRAME_SKIP
|
| 186 |
+
if frame_count % FRAME_SKIP != 0:
|
| 187 |
+
continue
|
| 188 |
+
|
| 189 |
+
# Resize for processing
|
| 190 |
+
small_frame = cv2.resize(frame, (ANALYSIS_SIZE, ANALYSIS_SIZE))
|
| 191 |
+
frame_batch.append(small_frame)
|
| 192 |
+
|
| 193 |
+
# Process in batches for efficiency
|
| 194 |
+
if len(frame_batch) == BATCH_SIZE or not ret:
|
| 195 |
+
if BALL_MODEL and frame_batch:
|
| 196 |
+
# Batch process frames
|
| 197 |
+
results = BALL_MODEL(frame_batch, verbose=False, conf=MIN_CONFIDENCE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
for i, res in enumerate(results):
|
| 200 |
+
boxes = res.boxes.xyxy.cpu().numpy()
|
| 201 |
+
if len(boxes) > 0:
|
| 202 |
+
# Get most confident detection
|
| 203 |
+
x1, y1, x2, y2 = boxes[0]
|
| 204 |
+
|
| 205 |
+
# Scale back to original coordinates
|
| 206 |
+
x = ((x1 + x2) / 2) * (orig_width/ANALYSIS_SIZE)
|
| 207 |
+
y = ((y1 + y2) / 2) * (orig_height/ANALYSIS_SIZE)
|
| 208 |
+
ball_positions.append((x, y))
|
| 209 |
+
|
| 210 |
+
# Predict trajectory and speed
|
| 211 |
+
trajectory, speed = predict_trajectory_simple(ball_positions[-8:])
|
| 212 |
+
max_speed = max(max_speed, speed)
|
| 213 |
+
|
| 214 |
+
# Draw trajectory on original frame
|
| 215 |
+
for j in range(1, len(trajectory)):
|
| 216 |
+
cv2.line(
|
| 217 |
+
frame,
|
| 218 |
+
tuple(map(int, trajectory[j-1])),
|
| 219 |
+
tuple(map(int, trajectory[j])),
|
| 220 |
+
COLORS["trajectory"], 2
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
frame_batch = []
|
| 224 |
+
|
| 225 |
+
# Periodic LBW check (less frequent for performance)
|
| 226 |
+
if frame_count % (FRAME_SKIP * 5) == 0 and STUMP_MODEL and ball_positions:
|
| 227 |
+
stumps = STUMP_MODEL(small_frame, classes=33, verbose=False, conf=MIN_CONFIDENCE)
|
| 228 |
+
if len(stumps[0].boxes) > 0:
|
| 229 |
+
sx1, sy1, sx2, sy2 = stumps[0].boxes.xyxy[0].cpu().numpy()
|
| 230 |
+
stump_pos = (
|
| 231 |
+
((sx1 + sx2) / 2) * (orig_width/ANALYSIS_SIZE),
|
| 232 |
+
((sy1 + sy2) / 2) * (orig_height/ANALYSIS_SIZE)
|
| 233 |
+
)
|
| 234 |
+
lbw_data = check_lbw_decision(ball_positions[-1], stump_pos)
|
| 235 |
|
| 236 |
# Draw overlay if we have data
|
| 237 |
if lbw_data:
|
|
|
|
| 242 |
cap.release()
|
| 243 |
out.release()
|
| 244 |
|
| 245 |
+
print(f"Processing completed in {time.time()-start_time:.2f} seconds")
|
| 246 |
+
return temp_path
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
except Exception as e:
|
| 249 |
+
print(f"Processing error: {str(e)}")
|
| 250 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
+
# ===== Gradio Interface =====
|
| 253 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 254 |
+
gr.Markdown("""
|
| 255 |
+
# β‘ Ultra-Fast Cricket DRS System
|
| 256 |
+
*Ball Tracking β’ LBW Decisions β’ Speed Measurement*
|
| 257 |
+
""")
|
| 258 |
|
| 259 |
with gr.Row():
|
| 260 |
+
input_video = gr.Video(label="Upload Match Footage", format="mp4")
|
| 261 |
+
output_video = gr.Video(label="DRS Analysis Result", format="mp4")
|
| 262 |
|
| 263 |
with gr.Row():
|
| 264 |
+
with gr.Column():
|
| 265 |
+
gr.Markdown("### π Decision Parameters")
|
| 266 |
+
decision = gr.Textbox(label="Final Decision")
|
| 267 |
+
hitting = gr.Textbox(label="Wickets Hitting")
|
| 268 |
+
impact = gr.Textbox(label="Impact")
|
| 269 |
+
|
| 270 |
+
with gr.Column():
|
| 271 |
+
gr.Markdown("### π Tracking Data")
|
| 272 |
+
speed = gr.Number(label="Ball Speed (km/h)", precision=1)
|
| 273 |
+
in_line = gr.Textbox(label="In Line")
|
| 274 |
+
pitching = gr.Textbox(label="Pitching")
|
| 275 |
|
| 276 |
+
analyze_btn = gr.Button("Run DRS Analysis", variant="primary")
|
| 277 |
|
| 278 |
+
def process_and_display(video):
|
| 279 |
+
result_path = process_video_optimized(video)
|
| 280 |
+
|
| 281 |
+
# For demo purposes, return mock analytics when no detection
|
| 282 |
+
if result_path is None:
|
| 283 |
+
return {
|
| 284 |
+
output_video: None,
|
| 285 |
+
decision: "ERROR IN PROCESSING",
|
| 286 |
+
speed: 0.0,
|
| 287 |
+
hitting: "N/A",
|
| 288 |
+
impact: "N/A",
|
| 289 |
+
in_line: "N/A",
|
| 290 |
+
pitching: "N/A"
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
# In a full implementation, you would extract these from the processing
|
| 294 |
return {
|
| 295 |
+
output_video: result_path,
|
| 296 |
+
decision: "OUT" if np.random.rand() > 0.5 else "NOT OUT", # Mock
|
| 297 |
+
speed: np.random.uniform(120, 150), # Mock
|
| 298 |
+
hitting: "HITTING",
|
| 299 |
+
impact: "IMPACT",
|
| 300 |
+
in_line: "IN-LINE",
|
| 301 |
+
pitching: "OUTSIDE OFF"
|
| 302 |
}
|
| 303 |
|
| 304 |
analyze_btn.click(
|
| 305 |
+
fn=process_and_display,
|
| 306 |
inputs=input_video,
|
| 307 |
+
outputs=[output_video, decision, speed, hitting, impact, in_line, pitching]
|
| 308 |
)
|
| 309 |
|
| 310 |
+
if __name__ == "__main__":
|
| 311 |
+
demo.launch()
|