AjaykumarPilla commited on
Commit
ce8f9f6
·
verified ·
1 Parent(s): 62d8ba1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -12
app.py CHANGED
@@ -76,7 +76,7 @@ def process_video(video_path):
76
 
77
  if not ball_detected:
78
  prediction = kalman.predict()
79
- x, y = prediction[0].item(), prediction[1].item() # Extract scalar values
80
  if 0 <= x < frame_width and 0 <= y < frame_height:
81
  ball_positions.append((frame_idx, x, y, 0.0))
82
  logger.debug(f"Frame {frame_idx}: Predicted ball at ({x:.2f}, {y:.2f})")
@@ -165,40 +165,87 @@ def process_video(video_path):
165
 
166
  output_video.release()
167
 
168
- # Generate Plotly trajectory plot
169
  if confident_positions:
170
  frames_range, x_coords, y_coords = zip(*confident_positions)
171
- fig = go.Figure()
172
- fig.add_trace(go.Scatter(
 
173
  x=frames_range, y=x_coords, mode='lines+markers', name='X Coordinate',
174
  line=dict(color='blue'), marker=dict(size=8)
175
  ))
176
- fig.add_trace(go.Scatter(
177
  x=frames_range, y=y_coords, mode='lines+markers', name='Y Coordinate',
178
  line=dict(color='red'), marker=dict(size=8)
179
  ))
180
  if release_frame:
181
- fig.add_trace(go.Scatter(
182
  x=[release_frame], y=[release_y], mode='markers', name='Release Point',
183
  marker=dict(size=12, color='blue', symbol='star')
184
  ))
185
- ifpitch_frame:
186
- fig.add_trace(go.Scatter(
187
  x=[pitch_frame], y=[pitch_y], mode='markers', name='Pitch Point',
188
  marker=dict(size=12, color='yellow', symbol='star')
189
  ))
190
  if impact_frame:
191
- fig.add_trace(go.Scatter(
192
  x=[impact_frame], y=[impact_y], mode='markers', name='Impact Point',
193
  marker=dict(size=12, color='green', symbol='star')
194
  ))
195
- fig.update_layout(
196
  title="Ball Trajectory (X, Y vs Frame Index)",
197
  xaxis_title="Frame Index",
198
  yaxis_title="Pixel Coordinate",
199
  template="plotly_dark",
200
  showlegend=True
201
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  else:
203
  fig = go.Figure()
204
  fig.add_annotation(text="No confident detections for trajectory plot", showarrow=False)
@@ -214,10 +261,10 @@ iface = gr.Interface(
214
  outputs=[
215
  gr.Video(label="Replay Video"),
216
  gr.Textbox(label="Decision"),
217
- gr.Plot(label="Trajectory Plot")
218
  ],
219
  title="Cricket DRS System",
220
- description="Upload a cricket video to get a DRS analysis with smooth ball trajectory, Out/Not Out decision, and trajectory plot."
221
  )
222
 
223
  if __name__ == "__main__":
 
76
 
77
  if not ball_detected:
78
  prediction = kalman.predict()
79
+ x, y = prediction[0].item(), prediction[1].item()
80
  if 0 <= x < frame_width and 0 <= y < frame_height:
81
  ball_positions.append((frame_idx, x, y, 0.0))
82
  logger.debug(f"Frame {frame_idx}: Predicted ball at ({x:.2f}, {y:.2f})")
 
165
 
166
  output_video.release()
167
 
168
+ # Generate Plotly trajectory plots
169
  if confident_positions:
170
  frames_range, x_coords, y_coords = zip(*confident_positions)
171
+ # Plot 1: X, Y vs Frame Index
172
+ fig1 = go.Figure()
173
+ fig1.add_trace(go.Scatter(
174
  x=frames_range, y=x_coords, mode='lines+markers', name='X Coordinate',
175
  line=dict(color='blue'), marker=dict(size=8)
176
  ))
177
+ fig1.add_trace(go.Scatter(
178
  x=frames_range, y=y_coords, mode='lines+markers', name='Y Coordinate',
179
  line=dict(color='red'), marker=dict(size=8)
180
  ))
181
  if release_frame:
182
+ fig1.add_trace(go.Scatter(
183
  x=[release_frame], y=[release_y], mode='markers', name='Release Point',
184
  marker=dict(size=12, color='blue', symbol='star')
185
  ))
186
+ if pitch_frame:
187
+ fig1.add_trace(go.Scatter(
188
  x=[pitch_frame], y=[pitch_y], mode='markers', name='Pitch Point',
189
  marker=dict(size=12, color='yellow', symbol='star')
190
  ))
191
  if impact_frame:
192
+ fig1.add_trace(go.Scatter(
193
  x=[impact_frame], y=[impact_y], mode='markers', name='Impact Point',
194
  marker=dict(size=12, color='green', symbol='star')
195
  ))
196
+ fig1.update_layout(
197
  title="Ball Trajectory (X, Y vs Frame Index)",
198
  xaxis_title="Frame Index",
199
  yaxis_title="Pixel Coordinate",
200
  template="plotly_dark",
201
  showlegend=True
202
  )
203
+
204
+ # Plot 2: X vs Y (Spatial Trajectory)
205
+ fig2 = go.Figure()
206
+ fig2.add_trace(go.Scatter(
207
+ x=x_coords, y=y_coords, mode='lines+markers', name='Ball Trajectory',
208
+ line=dict(color='red'), marker=dict(size=8)
209
+ ))
210
+ if release_frame:
211
+ fig2.add_trace(go.Scatter(
212
+ x=[release_x], y=[release_y], mode='markers', name='Release Point',
213
+ marker=dict(size=12, color='blue', symbol='star')
214
+ ))
215
+ if pitch_frame:
216
+ fig2.add_trace(go.Scatter(
217
+ x=[pitch_x], y=[pitch_y], mode='markers', name='Pitch Point',
218
+ marker=dict(size=12, color='yellow', symbol='star')
219
+ ))
220
+ if impact_frame:
221
+ fig2.add_trace(go.Scatter(
222
+ x=[impact_x], y=[impact_y], mode='markers', name='Impact Point',
223
+ marker=dict(size=12, color='green', symbol='star')
224
+ ))
225
+ fig2.update_layout(
226
+ title="Ball Trajectory (X vs Y, Spatial View)",
227
+ xaxis_title="X Coordinate (pixels)",
228
+ yaxis_title="Y Coordinate (pixels)",
229
+ template="plotly_dark",
230
+ showlegend=True,
231
+ xaxis=dict(range=[0, frame_width]),
232
+ yaxis=dict(range=[frame_height, 0]) # Invert y-axis to match video orientation
233
+ )
234
+
235
+ # Combine plots
236
+ fig = go.Figure()
237
+ fig.add_traces(fig1.data + fig2.data)
238
+ fig.update_layout(
239
+ title="Ball Trajectory Analysis",
240
+ grid=dict(rows=2, columns=1),
241
+ subplot_titles=["X, Y vs Frame Index", "X vs Y (Spatial View)"],
242
+ template="plotly_dark",
243
+ showlegend=True
244
+ )
245
+ fig.layout['xaxis'].update(title="Frame Index", range=[min(frames_range), max(frames_range)])
246
+ fig.layout['yaxis'].update(title="Pixel Coordinate")
247
+ fig.layout['xaxis2'].update(title="X Coordinate (pixels)", range=[0, frame_width])
248
+ fig.layout['yaxis2'].update(title="Y Coordinate (pixels)", range=[frame_height, 0])
249
  else:
250
  fig = go.Figure()
251
  fig.add_annotation(text="No confident detections for trajectory plot", showarrow=False)
 
261
  outputs=[
262
  gr.Video(label="Replay Video"),
263
  gr.Textbox(label="Decision"),
264
+ gr.Plot(label="Trajectory Plots (Temporal and Spatial)")
265
  ],
266
  title="Cricket DRS System",
267
+ description="Upload a cricket video to get a DRS analysis with smooth ball trajectory, Out/Not Out decision, and trajectory plots."
268
  )
269
 
270
  if __name__ == "__main__":