Spaces:
Sleeping
Sleeping
File size: 1,165 Bytes
331cf91 |
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 30 31 32 33 34 35 36 37 38 39 40 41 |
import gradio as gr
from transformers import pipeline
from PIL import Image, ImageDraw
# Load object detection pipeline
detector = pipeline("object-detection", model="facebook/detr-resnet-50")
def detect_objects(image):
results = detector(image)
# Draw bounding boxes on the image
img = image.copy()
draw = ImageDraw.Draw(img)
for obj in results:
box = obj["box"]
label = obj["label"]
score = obj["score"]
# Rectangle around object
draw.rectangle(
[(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
outline="red", width=3
)
# Label text
draw.text((box["xmin"], box["ymin"] - 10), f"{label} ({score:.2f})", fill="red")
return img
with gr.Blocks() as demo:
gr.Markdown("## Object Detection App (with Bounding Boxes)")
with gr.Row():
input_img = gr.Image(type="pil", label="Upload an Image")
output_img = gr.Image(type="pil", label="Detected Objects")
btn = gr.Button("Detect Objects")
btn.click(fn=detect_objects, inputs=input_img, outputs=output_img)
demo.launch()
|