Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,67 @@
|
|
| 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
|
| 8 |
detector = pipeline("object-detection", model="hustvl/yolos-tiny")
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return img
|
| 23 |
|
| 24 |
-
#
|
| 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
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# Launch the app
|
| 34 |
if __name__ == "__main__":
|
| 35 |
interface.launch()
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
+
# app.py
|
| 4 |
+
|
| 5 |
from transformers import pipeline
|
| 6 |
import gradio as gr
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
+
import random
|
| 9 |
|
| 10 |
+
# Load the YOLO-based object detection pipeline
|
| 11 |
detector = pipeline("object-detection", model="hustvl/yolos-tiny")
|
| 12 |
|
| 13 |
+
# Generate a random color for each label
|
| 14 |
+
label_colors = {}
|
| 15 |
+
|
| 16 |
+
def get_color(label):
|
| 17 |
+
if label not in label_colors:
|
| 18 |
+
label_colors[label] = (
|
| 19 |
+
random.randint(0, 255),
|
| 20 |
+
random.randint(0, 255),
|
| 21 |
+
random.randint(0, 255)
|
| 22 |
+
)
|
| 23 |
+
return label_colors[label]
|
| 24 |
+
|
| 25 |
+
# Detection function
|
| 26 |
def detect_objects(img):
|
| 27 |
results = detector(img)
|
| 28 |
|
|
|
|
| 29 |
draw = ImageDraw.Draw(img)
|
| 30 |
+
font = ImageFont.load_default()
|
| 31 |
+
|
| 32 |
for obj in results:
|
| 33 |
+
label = obj["label"]
|
| 34 |
+
score = obj["score"]
|
| 35 |
box = obj["box"]
|
| 36 |
+
color = get_color(label)
|
| 37 |
+
|
| 38 |
+
# Draw bounding box
|
| 39 |
+
draw.rectangle(
|
| 40 |
+
[box["xmin"], box["ymin"], box["xmax"], box["ymax"]],
|
| 41 |
+
outline=color,
|
| 42 |
+
width=3
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Prepare label with confidence
|
| 46 |
+
label_text = f"{label} ({score:.2f})"
|
| 47 |
+
text_size = draw.textsize(label_text, font=font)
|
| 48 |
+
text_background = [box["xmin"], box["ymin"] - text_size[1], box["xmin"] + text_size[0], box["ymin"]]
|
| 49 |
+
|
| 50 |
+
# Draw background for text
|
| 51 |
+
draw.rectangle(text_background, fill=color)
|
| 52 |
+
draw.text((box["xmin"], box["ymin"] - text_size[1]), label_text, fill="black", font=font)
|
| 53 |
+
|
| 54 |
return img
|
| 55 |
|
| 56 |
+
# Gradio interface
|
| 57 |
interface = gr.Interface(
|
| 58 |
fn=detect_objects,
|
| 59 |
inputs=gr.Image(type="pil"),
|
| 60 |
outputs=gr.Image(type="pil"),
|
| 61 |
+
title="YOLO Object Detection with Color-coded Labels",
|
| 62 |
+
description="Upload an image. Detected objects are shown with bounding boxes and color-coded labels using YOLOS-Tiny."
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
interface.launch()
|
| 67 |
+
|