webcam
Browse files
app.py
CHANGED
|
@@ -257,6 +257,62 @@ def stop_video_inference():
|
|
| 257 |
return "βΉοΈ Video processing stopped.", gr.update(interactive=True), gr.update(interactive=False)
|
| 258 |
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
def enable_video_buttons(video):
|
| 261 |
"""Enable start button when video is uploaded."""
|
| 262 |
if video is not None:
|
|
@@ -488,6 +544,61 @@ def create_gradio_interface():
|
|
| 488 |
fn=run_video_inference,
|
| 489 |
cache_examples=True
|
| 490 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
return demo
|
| 493 |
|
|
|
|
| 257 |
return "βΉοΈ Video processing stopped.", gr.update(interactive=True), gr.update(interactive=False)
|
| 258 |
|
| 259 |
|
| 260 |
+
def run_webcam_inference(
|
| 261 |
+
frame: np.ndarray,
|
| 262 |
+
model_name: str,
|
| 263 |
+
confidence_threshold: float
|
| 264 |
+
) -> Tuple[Image.Image, str]:
|
| 265 |
+
"""
|
| 266 |
+
Process webcam stream - runs inference on captured camera frame.
|
| 267 |
+
|
| 268 |
+
Args:
|
| 269 |
+
frame: Input frame from webcam as numpy array
|
| 270 |
+
model_name: Name of the model to use
|
| 271 |
+
confidence_threshold: Confidence threshold for filtering predictions
|
| 272 |
+
|
| 273 |
+
Returns:
|
| 274 |
+
Tuple of (visualized_image, metrics_text)
|
| 275 |
+
"""
|
| 276 |
+
if frame is None:
|
| 277 |
+
return None, "β οΈ No frame received from webcam."
|
| 278 |
+
|
| 279 |
+
if model_name is None or model_name == "No models available":
|
| 280 |
+
return None, "β οΈ No model selected or available."
|
| 281 |
+
|
| 282 |
+
try:
|
| 283 |
+
# Load or use cached model
|
| 284 |
+
model = load_model(model_name, confidence_threshold=confidence_threshold)
|
| 285 |
+
|
| 286 |
+
# Run inference
|
| 287 |
+
result = model(frame)
|
| 288 |
+
|
| 289 |
+
# Visualize results
|
| 290 |
+
visualized_image = visualizer.render(frame, result)
|
| 291 |
+
|
| 292 |
+
# Get performance metrics
|
| 293 |
+
metrics = model.get_performance_metrics()
|
| 294 |
+
inference_time = metrics.get_inference_time()
|
| 295 |
+
preprocess_time = metrics.get_preprocess_time()
|
| 296 |
+
postprocess_time = metrics.get_postprocess_time()
|
| 297 |
+
fps = metrics.get_fps()
|
| 298 |
+
|
| 299 |
+
# Format metrics text
|
| 300 |
+
metrics_text = f"""π Preprocessing: {preprocess_time.mean()*1000:.2f} ms
|
| 301 |
+
βοΈ Inference: {inference_time.mean()*1000:.2f} ms
|
| 302 |
+
π Postprocessing: {postprocess_time.mean()*1000:.2f} ms
|
| 303 |
+
ββββββββββββββββββββββββββββββββββ
|
| 304 |
+
β±οΈ Total Time: {(preprocess_time.mean() + inference_time.mean() + postprocess_time.mean())*1000:.2f} ms
|
| 305 |
+
π― FPS: {fps:.2f}
|
| 306 |
+
π Total Frames: {inference_time.count}
|
| 307 |
+
"""
|
| 308 |
+
|
| 309 |
+
return visualized_image, metrics_text
|
| 310 |
+
|
| 311 |
+
except Exception as e:
|
| 312 |
+
error_msg = f"Error during webcam inference: {str(e)}"
|
| 313 |
+
return None, error_msg
|
| 314 |
+
|
| 315 |
+
|
| 316 |
def enable_video_buttons(video):
|
| 317 |
"""Enable start button when video is uploaded."""
|
| 318 |
if video is not None:
|
|
|
|
| 544 |
fn=run_video_inference,
|
| 545 |
cache_examples=True
|
| 546 |
)
|
| 547 |
+
|
| 548 |
+
with gr.TabItem("πΉ Live Inference"):
|
| 549 |
+
gr.Markdown("### Real-time inference using your webcam")
|
| 550 |
+
gr.Markdown("β οΈ **Note:** Allow browser access to your webcam when prompted.")
|
| 551 |
+
|
| 552 |
+
with gr.Row():
|
| 553 |
+
with gr.Column(scale=1):
|
| 554 |
+
webcam_input = gr.Image(
|
| 555 |
+
sources=["webcam"],
|
| 556 |
+
label="Webcam",
|
| 557 |
+
type="numpy",
|
| 558 |
+
streaming=True,
|
| 559 |
+
show_label=False,
|
| 560 |
+
height=400
|
| 561 |
+
)
|
| 562 |
+
|
| 563 |
+
webcam_model_dropdown = gr.Dropdown(
|
| 564 |
+
choices=available_models,
|
| 565 |
+
value=available_models[0] if available_models else None,
|
| 566 |
+
label="Select Model",
|
| 567 |
+
info="Choose a model from the models/ folder"
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
webcam_confidence_slider = gr.Slider(
|
| 571 |
+
minimum=0.0,
|
| 572 |
+
maximum=1.0,
|
| 573 |
+
value=0.3,
|
| 574 |
+
step=0.05,
|
| 575 |
+
label="Confidence Threshold",
|
| 576 |
+
info="Minimum confidence for displaying predictions"
|
| 577 |
+
)
|
| 578 |
+
|
| 579 |
+
with gr.Column(scale=1):
|
| 580 |
+
webcam_output = gr.Image(
|
| 581 |
+
label="Detection Result",
|
| 582 |
+
type="pil",
|
| 583 |
+
show_label=False,
|
| 584 |
+
height=400
|
| 585 |
+
)
|
| 586 |
+
|
| 587 |
+
webcam_metrics_output = gr.Textbox(
|
| 588 |
+
label="Performance Metrics",
|
| 589 |
+
lines=8,
|
| 590 |
+
max_lines=15
|
| 591 |
+
)
|
| 592 |
+
|
| 593 |
+
# Set up streaming from webcam
|
| 594 |
+
webcam_input.stream(
|
| 595 |
+
fn=run_webcam_inference,
|
| 596 |
+
inputs=[webcam_input, webcam_model_dropdown, webcam_confidence_slider],
|
| 597 |
+
outputs=[webcam_output, webcam_metrics_output],
|
| 598 |
+
time_limit=60,
|
| 599 |
+
stream_every=0.1,
|
| 600 |
+
concurrency_limit=16
|
| 601 |
+
)
|
| 602 |
|
| 603 |
return demo
|
| 604 |
|