Spaces:
Build error
Build error
File size: 2,169 Bytes
cf52cfc | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 | import torch
from diffusers import StableDiffusionPipeline
import gradio as gr
from transformers import DetrImageProcessor, DetrForObjectDetection
from PIL import ImageDraw, ImageFont, Image
import requests
import os
# Load the DETR processor and model
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
# Define the inference function
def inference(image):
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
# Convert outputs (bounding boxes and class logits) to COCO API
# Only keep detections with a score > 0.9
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
# Draw bounding boxes and labels on the image
draw = ImageDraw.Draw(image)
font_path = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"
if not os.path.exists(font_path):
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" # Alternate font path
font = ImageFont.truetype(font_path, 15)
detected_objects = []
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
box = [round(i, 2) for i in box.tolist()]
draw.rectangle(box, outline="red", width=2)
label_text = f"{model.config.id2label[label.item()]} ({round(score.item() * 100, 1)}%)"
draw.text((box[0], box[1]), label_text, fill="red", font=font)
detected_objects.append(f"{label_text} at {box}")
return image, "\n".join(detected_objects)
# Create the Gradio interface
demo = gr.Interface(
fn=inference,
inputs=gr.Image(type="pil"),
outputs=[gr.Image(type="pil"), gr.Textbox()],
title="DETR Object Detection",
description=(
"Upload an image, and this app will detect objects using the DETR model. "
"The detected objects are displayed with bounding boxes and confidence scores."
),
)
# Launch the app
demo.launch()
|