Spaces:
Build error
Build error
Danush Rajaram commited on
Upload 2 files
Browse files- app.py +53 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 5 |
+
from PIL import ImageDraw, ImageFont, Image
|
| 6 |
+
import requests
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Load the DETR processor and model
|
| 10 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 11 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
|
| 12 |
+
|
| 13 |
+
# Define the inference function
|
| 14 |
+
def inference(image):
|
| 15 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
|
| 18 |
+
# Convert outputs (bounding boxes and class logits) to COCO API
|
| 19 |
+
# Only keep detections with a score > 0.9
|
| 20 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 21 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 22 |
+
|
| 23 |
+
# Draw bounding boxes and labels on the image
|
| 24 |
+
draw = ImageDraw.Draw(image)
|
| 25 |
+
font_path = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"
|
| 26 |
+
if not os.path.exists(font_path):
|
| 27 |
+
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" # Alternate font path
|
| 28 |
+
font = ImageFont.truetype(font_path, 15)
|
| 29 |
+
|
| 30 |
+
detected_objects = []
|
| 31 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 32 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 33 |
+
draw.rectangle(box, outline="red", width=2)
|
| 34 |
+
label_text = f"{model.config.id2label[label.item()]} ({round(score.item() * 100, 1)}%)"
|
| 35 |
+
draw.text((box[0], box[1]), label_text, fill="red", font=font)
|
| 36 |
+
detected_objects.append(f"{label_text} at {box}")
|
| 37 |
+
|
| 38 |
+
return image, "\n".join(detected_objects)
|
| 39 |
+
|
| 40 |
+
# Create the Gradio interface
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=inference,
|
| 43 |
+
inputs=gr.Image(type="pil"),
|
| 44 |
+
outputs=[gr.Image(type="pil"), gr.Textbox()],
|
| 45 |
+
title="DETR Object Detection",
|
| 46 |
+
description=(
|
| 47 |
+
"Upload an image, and this app will detect objects using the DETR model. "
|
| 48 |
+
"The detected objects are displayed with bounding boxes and confidence scores."
|
| 49 |
+
),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Launch the app
|
| 53 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
diffusers
|
| 3 |
+
torch
|
| 4 |
+
gradio
|
| 5 |
+
Pillow
|