File size: 846 Bytes
1f30831
201eff5
 
1f30831
 
201eff5
1f30831
 
201eff5
1f30831
 
 
 
 
201eff5
1f30831
 
 
 
 
 
 
 
 
 
201eff5
1f30831
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import gradio as gr
from PIL import Image
from ultralytics import YOLO
from spaces import GPU  # Enables GPU-specific execution in Hugging Face Spaces

# Prevent config write errors by setting Ultralytics settings path to /tmp
os.environ["YOLO_CONFIG_DIR"] = "/tmp"

# Load YOLO11 model (you can also use yolo11s.pt, yolo11m.pt etc.)
model = YOLO("yolo11n.pt")

@GPU  # Ensures GPU is allocated for this function
def detect_objects(image: Image.Image) -> Image.Image:
    results = model(image)
    return results[0].plot()  # Returns image with bounding boxes drawn

# Create Gradio interface
demo = gr.Interface(
    fn=detect_objects,
    inputs=gr.Image(type="pil"),
    outputs="image",
    title="YOLO11 Object Detection",
    description="Upload an image to detect objects using Ultralytics YOLO11"
)

# Launch app
demo.launch()