dschandra commited on
Commit
c4fc8b6
·
verified ·
1 Parent(s): ab2a34e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -33
app.py CHANGED
@@ -1,20 +1,24 @@
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
 
4
  from sklearn.linear_model import LinearRegression
5
  import tempfile
6
 
7
  def process_video(video_path):
8
  cap = cv2.VideoCapture(video_path)
9
- frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
10
- frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
11
 
12
  out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
13
- out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*"mp4v"), 20.0, (frame_w, frame_h))
14
 
15
- ball_color_lower = np.array([5, 100, 100]) # HSV orange-red
 
16
  ball_color_upper = np.array([20, 255, 255])
 
17
  trajectory = []
 
18
 
19
  while True:
20
  ret, frame = cap.read()
@@ -37,51 +41,64 @@ def process_video(video_path):
37
  trajectory.append(center)
38
  cv2.circle(frame, center, int(radius), (0, 0, 255), 2)
39
 
40
- # Draw stumps area
41
- stump_box = (frame_w // 2 - 30, frame_h - 120, frame_w // 2 + 30, frame_h - 50)
42
- cv2.rectangle(frame, stump_box[:2], stump_box[2:], (0, 255, 255), 2)
43
-
44
- # Draw trajectory
45
  for i in range(1, len(trajectory)):
46
  cv2.line(frame, trajectory[i - 1], trajectory[i], (255, 0, 0), 2)
47
 
48
- out.write(frame)
 
 
49
 
50
- cap.release()
51
- out.release()
 
 
52
 
53
- decision = "NOT OUT"
54
- projected_hit = False
55
 
56
- # Predict trajectory
57
- if len(trajectory) >= 5:
58
- X = np.array([x for x, y in trajectory]).reshape(-1, 1)
59
- y = np.array([y for x, y in trajectory])
60
 
61
- model = LinearRegression()
62
- model.fit(X, y)
63
 
64
- # Predict at stump x-position
65
- stump_x = frame_w // 2
66
- pred_y = int(model.predict(np.array([[stump_x]]))[0])
67
 
68
- # Check if predicted Y is in stump area
69
- if stump_box[1] <= pred_y <= stump_box[3]:
70
- projected_hit = True
 
 
 
 
 
 
 
 
 
 
 
71
  decision = "OUT"
 
72
 
73
- # Write final frame with decision
74
- final_frame = cv2.imread(out_path)
75
- cv2.putText(final_frame, f"DECISION: {decision}", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0) if decision == "NOT OUT" else (0, 0, 255), 4)
 
 
 
 
 
76
 
77
  return out_path
78
 
79
  iface = gr.Interface(
80
  fn=process_video,
81
- inputs=gr.Video(label="Upload Bowling Video (Camera View)"),
82
- outputs=gr.Video(label="Replay with Trajectory and Decision"),
83
- title="Cricket DRS LBW Tracker",
84
- description="Track ball trajectory and check if it's LBW OUT based on impact prediction."
85
  )
86
 
87
  iface.launch()
 
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
4
+ from sklearn.preprocessing import PolynomialFeatures
5
  from sklearn.linear_model import LinearRegression
6
  import tempfile
7
 
8
  def process_video(video_path):
9
  cap = cv2.VideoCapture(video_path)
10
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
11
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
12
 
13
  out_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
14
+ out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*"mp4v"), 20.0, (width, height))
15
 
16
+ # Color range for ball (adjust if needed)
17
+ ball_color_lower = np.array([5, 100, 100])
18
  ball_color_upper = np.array([20, 255, 255])
19
+
20
  trajectory = []
21
+ predicted_points = []
22
 
23
  while True:
24
  ret, frame = cap.read()
 
41
  trajectory.append(center)
42
  cv2.circle(frame, center, int(radius), (0, 0, 255), 2)
43
 
44
+ # Draw trajectory line
 
 
 
 
45
  for i in range(1, len(trajectory)):
46
  cv2.line(frame, trajectory[i - 1], trajectory[i], (255, 0, 0), 2)
47
 
48
+ # Draw stumps box
49
+ stump_box = (width // 2 - 30, height - 120, width // 2 + 30, height - 50)
50
+ cv2.rectangle(frame, stump_box[:2], stump_box[2:], (0, 255, 255), 2)
51
 
52
+ # Draw projected path if enough points collected
53
+ if len(trajectory) >= 5 and not predicted_points:
54
+ X = np.array([x for x, y in trajectory]).reshape(-1, 1)
55
+ Y = np.array([y for x, y in trajectory])
56
 
57
+ poly = PolynomialFeatures(degree=2)
58
+ X_poly = poly.fit_transform(X)
59
 
60
+ model = LinearRegression()
61
+ model.fit(X_poly, Y)
 
 
62
 
63
+ x_future = np.linspace(min(X)[0], max(X)[0] + 150, num=20).reshape(-1, 1)
64
+ y_future = model.predict(poly.transform(x_future))
65
 
66
+ predicted_points = list(zip(x_future.flatten().astype(int), y_future.astype(int)))
 
 
67
 
68
+ # Draw projected path (dotted yellow)
69
+ for pt in predicted_points:
70
+ if 0 <= pt[0] < width and 0 <= pt[1] < height:
71
+ cv2.circle(frame, pt, 3, (0, 255, 255), -1)
72
+
73
+ out.write(frame)
74
+
75
+ cap.release()
76
+ out.release()
77
+
78
+ # Determine OUT/NOT OUT
79
+ decision = "NOT OUT"
80
+ for x, y in predicted_points:
81
+ if stump_box[0] <= x <= stump_box[2] and stump_box[1] <= y <= stump_box[3]:
82
  decision = "OUT"
83
+ break
84
 
85
+ # Final frame update
86
+ final_frame = cv2.VideoCapture(out_path)
87
+ ret, last_frame = final_frame.read()
88
+ if ret:
89
+ cv2.putText(last_frame, f"DECISION: {decision}", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255) if decision == "OUT" else (0, 255, 0), 4)
90
+ out_final = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*"mp4v"), 20.0, (width, height))
91
+ out_final.write(last_frame)
92
+ out_final.release()
93
 
94
  return out_path
95
 
96
  iface = gr.Interface(
97
  fn=process_video,
98
+ inputs=gr.Video(label="Upload Bowling Video"),
99
+ outputs=gr.Video(label="LBW Tracker Output"),
100
+ title="DRS LBW Review System",
101
+ description="Detect ball trajectory, project path, and decide OUT/NOT OUT using AI"
102
  )
103
 
104
  iface.launch()