Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -332,13 +332,14 @@ def predict(image, model_path, class_names_path, confidence_threshold):
|
|
| 332 |
return None, f"Error during inference: {e}", None
|
| 333 |
|
| 334 |
|
| 335 |
-
def build_interface(model_path, class_names_path):
|
| 336 |
"""
|
| 337 |
Build the Gradio interface components.
|
| 338 |
|
| 339 |
Args:
|
| 340 |
model_path: Path to the ONNX model
|
| 341 |
class_names_path: Path to the class names file
|
|
|
|
| 342 |
|
| 343 |
Returns:
|
| 344 |
gr.Blocks: The Gradio demo interface
|
|
@@ -374,6 +375,13 @@ def build_interface(model_path, class_names_path):
|
|
| 374 |
orientation="h",
|
| 375 |
label_title="Object Counts",
|
| 376 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
# Set up the click event inside the Blocks context
|
| 379 |
submit_btn.click(
|
|
@@ -394,9 +402,25 @@ def launch_demo():
|
|
| 394 |
"""
|
| 395 |
Launch the Gradio demo with hardcoded model and class names paths.
|
| 396 |
"""
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
demo.launch(share=False) # Set share=True if you want to create a shareable link
|
| 401 |
|
| 402 |
|
|
|
|
| 332 |
return None, f"Error during inference: {e}", None
|
| 333 |
|
| 334 |
|
| 335 |
+
def build_interface(model_path, class_names_path, example_images=None):
|
| 336 |
"""
|
| 337 |
Build the Gradio interface components.
|
| 338 |
|
| 339 |
Args:
|
| 340 |
model_path: Path to the ONNX model
|
| 341 |
class_names_path: Path to the class names file
|
| 342 |
+
example_images: List of example image paths
|
| 343 |
|
| 344 |
Returns:
|
| 345 |
gr.Blocks: The Gradio demo interface
|
|
|
|
| 375 |
orientation="h",
|
| 376 |
label_title="Object Counts",
|
| 377 |
)
|
| 378 |
+
|
| 379 |
+
# Add examples component if example images are provided
|
| 380 |
+
if example_images:
|
| 381 |
+
gr.Examples(
|
| 382 |
+
examples=example_images,
|
| 383 |
+
inputs=input_image,
|
| 384 |
+
)
|
| 385 |
|
| 386 |
# Set up the click event inside the Blocks context
|
| 387 |
submit_btn.click(
|
|
|
|
| 402 |
"""
|
| 403 |
Launch the Gradio demo with hardcoded model and class names paths.
|
| 404 |
"""
|
| 405 |
+
# Create examples directory if it doesn't exist
|
| 406 |
+
examples_dir = os.path.join(BASE_DIR, "examples")
|
| 407 |
+
if not os.path.exists(examples_dir):
|
| 408 |
+
os.makedirs(examples_dir)
|
| 409 |
+
print(f"Created examples directory at {examples_dir}")
|
| 410 |
+
|
| 411 |
+
# Get list of example images
|
| 412 |
+
example_images = []
|
| 413 |
+
if os.path.exists(examples_dir):
|
| 414 |
+
example_images = [
|
| 415 |
+
os.path.join(examples_dir, f)
|
| 416 |
+
for f in os.listdir(examples_dir)
|
| 417 |
+
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
|
| 418 |
+
]
|
| 419 |
+
print(f"Found {len(example_images)} example images")
|
| 420 |
+
|
| 421 |
+
demo = build_interface(MODEL_PATH, CLASS_NAMES_PATH, example_images)
|
| 422 |
+
|
| 423 |
+
# Launch the demo without the examples parameter
|
| 424 |
demo.launch(share=False) # Set share=True if you want to create a shareable link
|
| 425 |
|
| 426 |
|