Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image, ImageDraw
|
| 6 |
+
|
| 7 |
+
# Load the object detection pipeline with YOLO
|
| 8 |
+
detector = pipeline("object-detection", model="hustvl/yolos-tiny")
|
| 9 |
+
|
| 10 |
+
# Define the detection function
|
| 11 |
+
def detect_objects(img):
|
| 12 |
+
results = detector(img)
|
| 13 |
+
|
| 14 |
+
# Draw bounding boxes
|
| 15 |
+
draw = ImageDraw.Draw(img)
|
| 16 |
+
for obj in results:
|
| 17 |
+
box = obj["box"]
|
| 18 |
+
label = f"{obj['label']} ({obj['score']:.2f})"
|
| 19 |
+
draw.rectangle([box["xmin"], box["ymin"], box["xmax"], box["ymax"]], outline="red", width=3)
|
| 20 |
+
draw.text((box["xmin"], box["ymin"] - 10), label, fill="red")
|
| 21 |
+
|
| 22 |
+
return img
|
| 23 |
+
|
| 24 |
+
# Create the Gradio interface
|
| 25 |
+
interface = gr.Interface(
|
| 26 |
+
fn=detect_objects,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs=gr.Image(type="pil"),
|
| 29 |
+
title="YOLO Object Detection",
|
| 30 |
+
description="Upload an image to detect objects using a YOLOS-Tiny model."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the app
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
interface.launch()
|