mgumowsk commited on
Commit
dea64fb
·
1 Parent(s): d562442
Files changed (1) hide show
  1. app.py +111 -45
app.py CHANGED
@@ -193,6 +193,37 @@ def format_results(result, confidence_threshold: float) -> str:
193
  return results_text
194
 
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  def create_gradio_interface():
197
  """
198
  Create and configure the Gradio interface.
@@ -208,53 +239,79 @@ def create_gradio_interface():
208
 
209
  with gr.Blocks(title="Object Detection with model_api") as demo:
210
  gr.Markdown("# 🎯 Object Detection with model_api")
211
- gr.Markdown("Upload an image and select a model to perform object detection using OpenVINO and model_api")
212
 
 
213
  with gr.Row():
214
- with gr.Column(scale=1):
215
- input_image = gr.Image(
216
- label="Input Image",
217
- type="numpy",
218
- height=400
219
- )
220
-
221
- model_dropdown = gr.Dropdown(
222
- choices=available_models,
223
- value=available_models[0] if available_models else None,
224
- label="Select Model",
225
- info="Choose a model from the models/ folder"
226
- )
227
-
228
- confidence_slider = gr.Slider(
229
- minimum=0.0,
230
- maximum=1.0,
231
- value=0.3,
232
- step=0.05,
233
- label="Confidence Threshold",
234
- info="Minimum confidence for displaying predictions"
235
- )
236
-
237
- classify_btn = gr.Button("🚀 Run Inference", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
- with gr.Column(scale=1):
240
- output_image = gr.Image(
241
- label="Detection Result",
242
- type="pil",
243
- show_label=False,
244
- height=400
245
- )
246
-
247
- detections_output = gr.Textbox(
248
- label="Detected Objects",
249
- lines=8,
250
- max_lines=15
251
- )
252
-
253
- metrics_output = gr.Textbox(
254
- label="Performance Metrics",
255
- lines=8,
256
- max_lines=15
257
- )
258
 
259
  # Examples section
260
  gr.Markdown("## 📸 Examples")
@@ -271,12 +328,21 @@ def create_gradio_interface():
271
  cache_examples=False
272
  )
273
 
274
- # Connect the button to the inference function
275
  classify_btn.click(
276
  fn=run_inference,
277
  inputs=[input_image, model_dropdown, confidence_slider],
278
  outputs=[output_image, detections_output, metrics_output]
279
  )
 
 
 
 
 
 
 
 
 
280
 
281
  return demo
282
 
 
193
  return results_text
194
 
195
 
196
+ def process_webcam_stream(frame, model_name: str, confidence_threshold: float):
197
+ """
198
+ Process webcam stream frame by frame.
199
+
200
+ Args:
201
+ frame: Camera frame as numpy array
202
+ model_name: Name of the model to use
203
+ confidence_threshold: Confidence threshold for predictions
204
+
205
+ Returns:
206
+ Processed frame as PIL Image
207
+ """
208
+ if frame is None:
209
+ return None
210
+
211
+ try:
212
+ # Load model
213
+ model = load_model(model_name)
214
+
215
+ # Run inference
216
+ result = model(frame)
217
+
218
+ # Visualize results
219
+ result_image = visualizer.render(frame, result)
220
+
221
+ return result_image
222
+ except Exception as e:
223
+ print(f"[ERROR] Webcam stream error: {e}")
224
+ return frame
225
+
226
+
227
  def create_gradio_interface():
228
  """
229
  Create and configure the Gradio interface.
 
239
 
240
  with gr.Blocks(title="Object Detection with model_api") as demo:
241
  gr.Markdown("# 🎯 Object Detection with model_api")
242
+ gr.Markdown("Upload an image, use your webcam, or select a model to perform object detection using OpenVINO and model_api")
243
 
244
+ # Model selection (shared across tabs)
245
  with gr.Row():
246
+ model_dropdown = gr.Dropdown(
247
+ choices=available_models,
248
+ value=available_models[0] if available_models else None,
249
+ label="Select Model",
250
+ info="Choose a model from the models/ folder"
251
+ )
252
+
253
+ confidence_slider = gr.Slider(
254
+ minimum=0.0,
255
+ maximum=1.0,
256
+ value=0.3,
257
+ step=0.05,
258
+ label="Confidence Threshold",
259
+ info="Minimum confidence for displaying predictions"
260
+ )
261
+
262
+ # Create tabs for different input sources
263
+ with gr.Tabs():
264
+ # Image Tab
265
+ with gr.TabItem("📷 Image Upload"):
266
+ with gr.Row():
267
+ with gr.Column(scale=1):
268
+ input_image = gr.Image(
269
+ label="Input Image",
270
+ type="numpy",
271
+ height=400
272
+ )
273
+
274
+ classify_btn = gr.Button("🚀 Run Inference", variant="primary")
275
+
276
+ with gr.Column(scale=1):
277
+ output_image = gr.Image(
278
+ label="Detection Result",
279
+ type="pil",
280
+ show_label=False,
281
+ height=400
282
+ )
283
+
284
+ detections_output = gr.Textbox(
285
+ label="Detected Objects",
286
+ lines=8,
287
+ max_lines=15
288
+ )
289
+
290
+ metrics_output = gr.Textbox(
291
+ label="Performance Metrics",
292
+ lines=8,
293
+ max_lines=15
294
+ )
295
 
296
+ # Webcam Tab
297
+ with gr.TabItem("🎥 Webcam"):
298
+ with gr.Row():
299
+ with gr.Column(scale=1):
300
+ webcam_input = gr.Image(
301
+ sources=["webcam"],
302
+ label="Webcam Feed",
303
+ type="numpy",
304
+ streaming=True,
305
+ height=400
306
+ )
307
+
308
+ with gr.Column(scale=1):
309
+ webcam_output = gr.Image(
310
+ label="Live Detection",
311
+ type="numpy",
312
+ show_label=False,
313
+ height=400
314
+ )
315
 
316
  # Examples section
317
  gr.Markdown("## 📸 Examples")
 
328
  cache_examples=False
329
  )
330
 
331
+ # Connect the button to the inference function for image upload
332
  classify_btn.click(
333
  fn=run_inference,
334
  inputs=[input_image, model_dropdown, confidence_slider],
335
  outputs=[output_image, detections_output, metrics_output]
336
  )
337
+
338
+ # Connect webcam stream processing
339
+ webcam_input.stream(
340
+ fn=process_webcam_stream,
341
+ inputs=[webcam_input, model_dropdown, confidence_slider],
342
+ outputs=webcam_output,
343
+ stream_every=0.1,
344
+ time_limit=300
345
+ )
346
 
347
  return demo
348