dschandra commited on
Commit
0ff12de
·
verified ·
1 Parent(s): 5eaa37a

Create analyze_video

Browse files
Files changed (1) hide show
  1. analyze_video +50 -0
analyze_video ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def analyze_video(video_path):
2
+ frames = extract_frames(video_path)
3
+
4
+ ball_positions = []
5
+ impact_frame_idx = None
6
+ impact_zone = "unknown"
7
+
8
+ for i, frame in enumerate(frames):
9
+ mask = detect_objects_with_model(frame)
10
+
11
+ # Very simple segmentation logic
12
+ ball_mask = mask[0] > 0.5 # channel 0 for ball
13
+ pad_mask = mask[1] > 0.5 if mask.ndim > 2 else None # channel 1 for pad
14
+
15
+ # Detect ball center
16
+ contours, _ = cv2.findContours(ball_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
17
+ if contours:
18
+ largest = max(contours, key=cv2.contourArea)
19
+ M = cv2.moments(largest)
20
+ if M['m00'] != 0:
21
+ cx = int(M['m10']/M['m00'])
22
+ cy = int(M['m01']/M['m00'])
23
+ ball_positions.append((i, cx, cy))
24
+
25
+ # Detect pad hit (optional logic: ball near pad area)
26
+ if pad_mask is not None and contours:
27
+ overlap = np.logical_and(ball_mask, pad_mask).sum()
28
+ if overlap > 10: # simple overlap threshold
29
+ impact_frame_idx = i
30
+ impact_zone = "pad"
31
+ break
32
+
33
+ # Run trajectory prediction if ball was detected
34
+ trajectory = predict_trajectory(ball_positions)
35
+
36
+ # Predict outcome
37
+ decision = "OUT" if trajectory_hits_stumps(trajectory) and impact_zone == "pad" else "NOT OUT"
38
+
39
+ # Visualize
40
+ result_path = draw_visuals(frames, ball_positions, trajectory, impact_frame_idx, decision)
41
+
42
+ return result_path, decision
43
+
44
+
45
+ def trajectory_hits_stumps(trajectory):
46
+ # Simple rule-based check (assuming stumps are around x=300 to 340 px for now)
47
+ for (x, y) in trajectory:
48
+ if 300 < x < 340 and y < 480: # ball projected height intersects stump zone
49
+ return True
50
+ return False