Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,13 +18,13 @@ STUMPS_WIDTH = 0.2286 # meters (width of stumps)
|
|
18 |
BALL_DIAMETER = 0.073 # meters (approx. cricket ball diameter)
|
19 |
FRAME_RATE = 20 # Default frame rate, updated dynamically
|
20 |
SLOW_MOTION_FACTOR = 1.5 # Faster replay (e.g., 30 / 1.5 = 20 FPS)
|
21 |
-
CONF_THRESHOLD = 0.
|
22 |
IMPACT_ZONE_Y = 0.9 # Adjusted to 90% of frame height for impact zone
|
23 |
PITCH_LENGTH = 20.12 # meters (standard cricket pitch length)
|
24 |
STUMPS_HEIGHT = 0.71 # meters (stumps height)
|
25 |
CAMERA_HEIGHT = 2.0 # meters (assumed camera height)
|
26 |
CAMERA_DISTANCE = 10.0 # meters (assumed camera distance from pitch)
|
27 |
-
MAX_POSITION_JUMP =
|
28 |
|
29 |
def process_video(video_path):
|
30 |
if not os.path.exists(video_path):
|
@@ -50,27 +50,34 @@ def process_video(video_path):
|
|
50 |
break
|
51 |
frame_count += 1
|
52 |
frames.append(frame.copy())
|
53 |
-
# Enhance frame contrast and
|
54 |
-
frame = cv2.convertScaleAbs(frame, alpha=1.
|
55 |
-
|
56 |
-
frame = cv2.
|
57 |
-
|
58 |
-
results = model.predict(frame, conf=CONF_THRESHOLD, imgsz=(img_height, img_width), iou=0.5, max_det=3)
|
59 |
detections = sum(1 for detection in results[0].boxes if detection.cls == 0)
|
60 |
-
if detections
|
|
|
|
|
|
|
61 |
for detection in results[0].boxes:
|
62 |
if detection.cls == 0: # Class 0 is the ball
|
63 |
conf = detection.conf.cpu().numpy()[0]
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
else:
|
75 |
debug_log.append(f"Frame {frame_count}: {detections} ball detections")
|
76 |
frames[-1] = frame
|
@@ -79,9 +86,9 @@ def process_video(video_path):
|
|
79 |
cap.release()
|
80 |
|
81 |
if not ball_positions:
|
82 |
-
debug_log.append("No frames with
|
83 |
else:
|
84 |
-
debug_log.append(f"Total frames with
|
85 |
debug_log.append(f"Video resolution: {frame_width}x{frame_height}")
|
86 |
debug_log.append(f"Video frame rate: {FRAME_RATE}")
|
87 |
|
@@ -152,7 +159,7 @@ def estimate_trajectory(ball_positions, frames, detection_frames):
|
|
152 |
|
153 |
# Generate dense points for all frames between first and last detection
|
154 |
total_frames = max(detection_frames) - min(detection_frames) + 1
|
155 |
-
t_full = np.linspace(
|
156 |
x_full = fx(t_full)
|
157 |
y_full = fy(t_full)
|
158 |
trajectory_2d = list(zip(x_full, y_full))
|
|
|
18 |
BALL_DIAMETER = 0.073 # meters (approx. cricket ball diameter)
|
19 |
FRAME_RATE = 20 # Default frame rate, updated dynamically
|
20 |
SLOW_MOTION_FACTOR = 1.5 # Faster replay (e.g., 30 / 1.5 = 20 FPS)
|
21 |
+
CONF_THRESHOLD = 0.15 # Lowered for better detection
|
22 |
IMPACT_ZONE_Y = 0.9 # Adjusted to 90% of frame height for impact zone
|
23 |
PITCH_LENGTH = 20.12 # meters (standard cricket pitch length)
|
24 |
STUMPS_HEIGHT = 0.71 # meters (stumps height)
|
25 |
CAMERA_HEIGHT = 2.0 # meters (assumed camera height)
|
26 |
CAMERA_DISTANCE = 10.0 # meters (assumed camera distance from pitch)
|
27 |
+
MAX_POSITION_JUMP = 250 # Increased to include more detections
|
28 |
|
29 |
def process_video(video_path):
|
30 |
if not os.path.exists(video_path):
|
|
|
50 |
break
|
51 |
frame_count += 1
|
52 |
frames.append(frame.copy())
|
53 |
+
# Enhance frame contrast and sharpness
|
54 |
+
frame = cv2.convertScaleAbs(frame, alpha=1.5, beta=20)
|
55 |
+
kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
|
56 |
+
frame = cv2.filter2D(frame, -1, kernel)
|
57 |
+
results = model.predict(frame, conf=CONF_THRESHOLD, imgsz=(img_height, img_width), iou=0.5, max_det=5)
|
|
|
58 |
detections = sum(1 for detection in results[0].boxes if detection.cls == 0)
|
59 |
+
if detections >= 1: # Process frames with at least one ball detection
|
60 |
+
max_conf = 0
|
61 |
+
best_detection = None
|
62 |
+
conf_scores = []
|
63 |
for detection in results[0].boxes:
|
64 |
if detection.cls == 0: # Class 0 is the ball
|
65 |
conf = detection.conf.cpu().numpy()[0]
|
66 |
+
conf_scores.append(conf)
|
67 |
+
if conf > max_conf:
|
68 |
+
max_conf = conf
|
69 |
+
best_detection = detection
|
70 |
+
if best_detection:
|
71 |
+
x1, y1, x2, y2 = best_detection.xyxy[0].cpu().numpy()
|
72 |
+
# Scale coordinates back to original frame size
|
73 |
+
x1 = x1 * frame_width / img_width
|
74 |
+
x2 = x2 * frame_width / img_width
|
75 |
+
y1 = y1 * frame_height / img_height
|
76 |
+
y2 = y2 * frame_height / img_height
|
77 |
+
ball_positions.append([(x1 + x2) / 2, (y1 + y2) / 2])
|
78 |
+
detection_frames.append(frame_count - 1)
|
79 |
+
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
|
80 |
+
debug_log.append(f"Frame {frame_count}: {detections} ball detections, selected confidence={max_conf:.3f}, all confidences={conf_scores}")
|
81 |
else:
|
82 |
debug_log.append(f"Frame {frame_count}: {detections} ball detections")
|
83 |
frames[-1] = frame
|
|
|
86 |
cap.release()
|
87 |
|
88 |
if not ball_positions:
|
89 |
+
debug_log.append("No frames with ball detection")
|
90 |
else:
|
91 |
+
debug_log.append(f"Total frames with ball detection: {len(ball_positions)}")
|
92 |
debug_log.append(f"Video resolution: {frame_width}x{frame_height}")
|
93 |
debug_log.append(f"Video frame rate: {FRAME_RATE}")
|
94 |
|
|
|
159 |
|
160 |
# Generate dense points for all frames between first and last detection
|
161 |
total_frames = max(detection_frames) - min(detection_frames) + 1
|
162 |
+
t_full = np.linspace(min(detection_frames) / FRAME_RATE, max(detection_frames) / FRAME_RATE, int(total_frames * SLOW_MOTION_FACTOR))
|
163 |
x_full = fx(t_full)
|
164 |
y_full = fy(t_full)
|
165 |
trajectory_2d = list(zip(x_full, y_full))
|