dschandra commited on
Commit
49a3a3a
·
verified ·
1 Parent(s): f03d665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -18
app.py CHANGED
@@ -4,23 +4,21 @@ from sklearn.linear_model import LogisticRegression
4
  import cv2
5
  import os
6
  from werkzeug.utils import secure_filename
7
- from scipy.interpolate import splprep, splev # For smooth trajectory
8
 
9
  app = Flask(__name__)
10
 
11
- # Configure upload folder
12
  UPLOAD_FOLDER = '/tmp/uploads'
13
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
14
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
15
  ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov'}
16
 
17
- # Dummy ML model for LBW decision (to be replaced with a real model)
18
  def train_dummy_model():
19
  X = np.array([
20
- [0.5, 0.0, 0.4, 0.5, 30, 0], # Not Out
21
- [0.5, 0.5, 0.5, 0.5, 35, 2], # Out
22
- [0.6, 0.2, 0.5, 0.6, 32, 1], # Not Out
23
- [0.5, 0.4, 0.5, 0.4, 34, 0], # Out
24
  ])
25
  y = np.array([0, 1, 0, 1])
26
  model = LogisticRegression()
@@ -38,7 +36,7 @@ def smooth_trajectory(points):
38
  x = [p["x"] for p in points]
39
  y = [p["y"] for p in points]
40
  tck, u = splprep([x, y], s=0)
41
- u_new = np.linspace(0, 1, 50) # Smooth with 50 points
42
  x_new, y_new = splev(u_new, tck)
43
  return [{"x": x, "y": y} for x, y in zip(x_new, y_new)]
44
 
@@ -61,7 +59,7 @@ def process_video(video_path):
61
  break
62
 
63
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
64
- mask = cv2.inRange(hsv, (0, 120, 70), (10, 255, 255)) # Adjust for your ball color
65
  contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
66
 
67
  if contours:
@@ -78,24 +76,22 @@ def process_video(video_path):
78
  y_positions.append(norm_y)
79
  last_point = current_point
80
 
81
- # Detect pitching (first significant downward movement)
82
  if len(y_positions) > 2 and not pitching_detected:
83
  if y_positions[-1] < y_positions[-2] and y_positions[-2] < y_positions[-3]:
84
  pitching_detected = True
85
  pitching_x = actual_path[-2]["x"]
86
  pitching_y = actual_path[-2]["y"]
87
 
88
- # Detect impact (sudden slowdown or stop)
89
  if len(actual_path) > 2 and not impact_detected:
90
  speed_current = abs(y_positions[-1] - y_positions[-2])
91
  speed_prev = abs(y_positions[-2] - y_positions[-3])
92
- if speed_current < speed_prev * 0.3: # Significant slowdown
93
  impact_detected = True
94
  impact_x = actual_path[-1]["x"]
95
  impact_y = actual_path[-1]["y"]
96
 
97
  frame_count += 1
98
- if frame_count > 50: # Process more frames for accuracy
99
  break
100
 
101
  cap.release()
@@ -113,17 +109,12 @@ def process_video(video_path):
113
 
114
  fps = cap.get(cv2.CAP_PROP_FPS) or 30
115
  speed = (len(actual_path) / (frame_count / fps)) * 0.5
116
-
117
- # Smooth the actual path
118
  actual_path = smooth_trajectory(actual_path)
119
-
120
- # Projected path with basic physics (linear for now, add swing/spin later)
121
  projected_path = [
122
  {"x": impact_x, "y": impact_y},
123
  {"x": impact_x + spin * 0.1, "y": 1.0}
124
  ]
125
 
126
- # Determine pitching and impact status
127
  pitching_status = "Inline" if 0.4 <= pitching_x <= 0.6 else "Outside Leg" if pitching_x < 0.4 else "Outside Off"
128
  impact_status = "Inline" if 0.4 <= impact_x <= 0.6 else "Outside"
129
  wicket_status = "Hitting" if 0.4 <= projected_path[-1]["x"] <= 0.6 else "Missing"
@@ -171,5 +162,42 @@ def analyze():
171
  'wicket': wicket_status
172
  })
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  if __name__ == '__main__':
175
  app.run(host='0.0.0.0', port=7860, debug=True)
 
4
  import cv2
5
  import os
6
  from werkzeug.utils import secure_filename
7
+ from scipy.interpolate import splprep, splev
8
 
9
  app = Flask(__name__)
10
 
 
11
  UPLOAD_FOLDER = '/tmp/uploads'
12
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
13
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
14
  ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov'}
15
 
 
16
  def train_dummy_model():
17
  X = np.array([
18
+ [0.5, 0.0, 0.4, 0.5, 30, 0],
19
+ [0.5, 0.5, 0.5, 0.5, 35, 2],
20
+ [0.6, 0.2, 0.5, 0.6, 32, 1],
21
+ [0.5, 0.4, 0.5, 0.4, 34, 0],
22
  ])
23
  y = np.array([0, 1, 0, 1])
24
  model = LogisticRegression()
 
36
  x = [p["x"] for p in points]
37
  y = [p["y"] for p in points]
38
  tck, u = splprep([x, y], s=0)
39
+ u_new = np.linspace(0, 1, 50)
40
  x_new, y_new = splev(u_new, tck)
41
  return [{"x": x, "y": y} for x, y in zip(x_new, y_new)]
42
 
 
59
  break
60
 
61
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
62
+ mask = cv2.inRange(hsv, (0, 120, 70), (10, 255, 255))
63
  contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
64
 
65
  if contours:
 
76
  y_positions.append(norm_y)
77
  last_point = current_point
78
 
 
79
  if len(y_positions) > 2 and not pitching_detected:
80
  if y_positions[-1] < y_positions[-2] and y_positions[-2] < y_positions[-3]:
81
  pitching_detected = True
82
  pitching_x = actual_path[-2]["x"]
83
  pitching_y = actual_path[-2]["y"]
84
 
 
85
  if len(actual_path) > 2 and not impact_detected:
86
  speed_current = abs(y_positions[-1] - y_positions[-2])
87
  speed_prev = abs(y_positions[-2] - y_positions[-3])
88
+ if speed_current < speed_prev * 0.3:
89
  impact_detected = True
90
  impact_x = actual_path[-1]["x"]
91
  impact_y = actual_path[-1]["y"]
92
 
93
  frame_count += 1
94
+ if frame_count > 50:
95
  break
96
 
97
  cap.release()
 
109
 
110
  fps = cap.get(cv2.CAP_PROP_FPS) or 30
111
  speed = (len(actual_path) / (frame_count / fps)) * 0.5
 
 
112
  actual_path = smooth_trajectory(actual_path)
 
 
113
  projected_path = [
114
  {"x": impact_x, "y": impact_y},
115
  {"x": impact_x + spin * 0.1, "y": 1.0}
116
  ]
117
 
 
118
  pitching_status = "Inline" if 0.4 <= pitching_x <= 0.6 else "Outside Leg" if pitching_x < 0.4 else "Outside Off"
119
  impact_status = "Inline" if 0.4 <= impact_x <= 0.6 else "Outside"
120
  wicket_status = "Hitting" if 0.4 <= projected_path[-1]["x"] <= 0.6 else "Missing"
 
162
  'wicket': wicket_status
163
  })
164
 
165
+ @app.route('/analyze_data', methods=['POST'])
166
+ def analyze_data():
167
+ data = request.get_json()
168
+ actual_path = data['actual_path']
169
+ projected_path = data['projected_path']
170
+ pitching = data['pitching']
171
+ impact = data['impact']
172
+ speed = data['speed']
173
+ spin = data['spin']
174
+
175
+ pitching_x = pitching['x']
176
+ pitching_y = pitching['y']
177
+ impact_x = impact['x']
178
+ impact_y = impact['y']
179
+
180
+ pitching_status = "Inline" if 0.4 <= pitching_x <= 0.6 else "Outside Leg" if pitching_x < 0.4 else "Outside Off"
181
+ impact_status = "Inline" if 0.4 <= impact_x <= 0.6 else "Outside"
182
+ wicket_status = "Hitting" if 0.4 <= projected_path[-1]["x"] <= 0.6 else "Missing"
183
+
184
+ features = np.array([[pitching_x, pitching_y, impact_x, impact_y, speed, spin]])
185
+ prediction = model.predict(features)[0]
186
+ confidence = min(model.predict_proba(features)[0][prediction], 0.99)
187
+ decision = "Out" if prediction == 1 else "Not Out"
188
+
189
+ return jsonify({
190
+ 'actual_path': actual_path,
191
+ 'projected_path': projected_path,
192
+ 'decision': decision,
193
+ 'confidence': round(confidence, 2),
194
+ 'pitching': {'x': pitching_x, 'y': pitching_y, 'status': pitching_status},
195
+ 'impact': {'x': impact_x, 'y': impact_y, 'status': impact_status},
196
+ 'wicket': wicket_status
197
+ })
198
+
199
+ if __name__ == '__main__':
200
+ app.run(debug=True, port=5001)
201
+
202
  if __name__ == '__main__':
203
  app.run(host='0.0.0.0', port=7860, debug=True)