Spaces:
Sleeping
Sleeping
File size: 1,988 Bytes
e71d142 bbd5c96 a4e37d9 e71d142 a4e37d9 4574aff bbd5c96 4574aff e71d142 bbd5c96 a4e37d9 e71d142 a4e37d9 e71d142 bbd5c96 e71d142 bbd5c96 e71d142 bbd5c96 e71d142 a4e37d9 e71d142 4574aff bbd5c96 4574aff bbd5c96 4574aff bbd5c96 4574aff e71d142 4574aff e71d142 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# gully_drs_core/ball_detection.py
import cv2
import numpy as np
from .model_utils import load_model
def find_bounce_point(path):
"""
Finds the point where the ball bounces by detecting a Y-axis dip.
"""
for i in range(1, len(path)-1):
if path[i-1][1] > path[i][1] < path[i+1][1]: # y dips = bounce
return path[i]
return None
def analyze_video(file_path):
model = load_model()
cap = cv2.VideoCapture(file_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
ball_path = []
frames = []
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame)
for r in results:
for box in r.boxes:
cls = int(box.cls[0])
if cls == 32: # cricket ball class
x1, y1, x2, y2 = map(int, box.xyxy[0])
cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
ball_path.append((cx, cy))
cv2.circle(frame, (cx, cy), 6, (0, 255, 0), -1)
frames.append(frame)
cap.release()
# Analyze trajectory
bounce_point = find_bounce_point(ball_path)
impact_point = ball_path[-1] if ball_path else None
# Stump zone: middle area at bottom
stump_zone = (
width // 2 - 30, # x1
height - 100, # y1
width // 2 + 30, # x2
height # y2
)
# LBW Decision: if impact is in stump zone
decision = "OUT" if (
impact_point and
stump_zone[0] <= impact_point[0] <= stump_zone[2] and
stump_zone[1] <= impact_point[1] <= stump_zone[3]
) else "NOT OUT"
return {
"trajectory": ball_path,
"fps": fps,
"frames": frames,
"bounce_point": bounce_point,
"impact_point": impact_point,
"decision": decision,
"stump_zone": stump_zone
}
|