Delete app.py
Browse files
app.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
from PIL import Image
|
| 4 |
-
|
| 5 |
-
# Load a pre-trained object detection model from the transformers library
|
| 6 |
-
# Using 'detr-resnet50' as an example, a powerful object detection model
|
| 7 |
-
try:
|
| 8 |
-
object_detector = pipeline("object-detection", model="facebook/detr-resnet50")
|
| 9 |
-
except Exception as e:
|
| 10 |
-
print(f"Error loading model: {e}")
|
| 11 |
-
print("Please ensure you have an internet connection and sufficient disk space.")
|
| 12 |
-
object_detector = None # Set to None if model loading fails
|
| 13 |
-
|
| 14 |
-
def detect_objects_in_image(image):
|
| 15 |
-
"""
|
| 16 |
-
Performs object detection on the input image.
|
| 17 |
-
|
| 18 |
-
Args:
|
| 19 |
-
image: A PIL Image object.
|
| 20 |
-
|
| 21 |
-
Returns:
|
| 22 |
-
A list of dictionaries, where each dictionary represents a detected object
|
| 23 |
-
and contains 'box' (bounding box coordinates) and 'label' (object class).
|
| 24 |
-
Returns a string message if the model failed to load.
|
| 25 |
-
"""
|
| 26 |
-
if object_detector is None:
|
| 27 |
-
return "Object detection model failed to load. Cannot process image."
|
| 28 |
-
|
| 29 |
-
if image is None:
|
| 30 |
-
return [] # Return empty list if no image is provided
|
| 31 |
-
|
| 32 |
-
# Perform object detection
|
| 33 |
-
detections = object_detector(image)
|
| 34 |
-
|
| 35 |
-
# The pipeline returns a list of dictionaries with 'box' and 'label' keys
|
| 36 |
-
# Example: [{'box': {'xmin': 125, 'ymin': 138, 'xmax': 309, 'ymax': 403}, 'label': 'remote', 'score': 0.998}]
|
| 37 |
-
return detections
|
| 38 |
-
|
| 39 |
-
# Create the Gradio interface
|
| 40 |
-
if object_detector is not None:
|
| 41 |
-
interface = gr.Interface(
|
| 42 |
-
fn=detect_objects_in_image,
|
| 43 |
-
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 44 |
-
outputs=gr.Label(num_top_classes=5, label="Detected Objects"), # Using Label to display detections
|
| 45 |
-
title="Object Detection with Hugging Face and Gradio",
|
| 46 |
-
description="Upload an image to detect objects using a pre-trained model.",
|
| 47 |
-
allow_flagging="never"
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
# Launch the Gradio app
|
| 51 |
-
interface.launch()
|
| 52 |
-
else:
|
| 53 |
-
print("Gradio interface not launched because the object detection model failed to load.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|