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