Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install --upgrade transformers gradio
|
2 |
+
import torch
|
3 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection, pipeline
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
print(f"Using device: {device}")
|
9 |
+
|
10 |
+
checkpoint = "PekingU/rtdetr_v2_r50vd" # Or any of the other checkpoints
|
11 |
+
|
12 |
+
image_processor = AutoImageProcessor.from_pretrained(checkpoint)
|
13 |
+
model = AutoModelForObjectDetection.from_pretrained(checkpoint).to(device)
|
14 |
+
|
15 |
+
# Colors for visualization (same as before)
|
16 |
+
COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
|
17 |
+
[0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]] * 100
|
18 |
+
|
19 |
+
def inference(image, threshold=0.3):
|
20 |
+
"""Performs object detection and returns an annotated image."""
|
21 |
+
pipe = pipeline("object-detection", model=model, image_processor=image_processor, device=device)
|
22 |
+
results = pipe(image, threshold=threshold)
|
23 |
+
|
24 |
+
annotated_image = image.copy()
|
25 |
+
draw = ImageDraw.Draw(annotated_image)
|
26 |
+
|
27 |
+
for i, result in enumerate(results):
|
28 |
+
box = result["box"]
|
29 |
+
color = tuple([int(x * 255) for x in COLORS[i]])
|
30 |
+
xmin, ymin, xmax, ymax = box["xmin"], box["ymin"], box["xmax"], box["ymax"]
|
31 |
+
draw.rectangle((xmin, ymin, xmax, ymax), fill=None, outline=color, width=2)
|
32 |
+
draw.text((xmin, ymin), text=f"{result['label']}: {result['score']:.2f}", fill=color)
|
33 |
+
|
34 |
+
return annotated_image
|
35 |
+
|
36 |
+
# Gradio interface
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=inference,
|
39 |
+
inputs=gr.Image(type="pil"),
|
40 |
+
outputs=gr.Image(type="pil"),
|
41 |
+
title="RT-DETR v2 Object Detection",
|
42 |
+
description="Upload an image to detect objects.",
|
43 |
+
examples=["/content/crowd7.jpg"],
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch()
|