save
Browse files
app.py
CHANGED
|
@@ -193,37 +193,6 @@ def format_results(result, confidence_threshold: float) -> str:
|
|
| 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,79 +208,53 @@ def create_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
|
| 243 |
|
| 244 |
-
# Model selection (shared across tabs)
|
| 245 |
with gr.Row():
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 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 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
|
| 316 |
# Examples section
|
| 317 |
gr.Markdown("## 📸 Examples")
|
|
@@ -328,21 +271,12 @@ def create_gradio_interface():
|
|
| 328 |
cache_examples=False
|
| 329 |
)
|
| 330 |
|
| 331 |
-
# Connect the button to the inference function
|
| 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 |
|
|
|
|
| 193 |
return results_text
|
| 194 |
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
def create_gradio_interface():
|
| 197 |
"""
|
| 198 |
Create and configure the 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 |
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 |
|