Spaces:
Sleeping
Sleeping
| 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() | |