Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
3 |
+
import torch
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
|
6 |
+
processor = DetrImageProcessor.from_pretrained("joortif/practica_2")
|
7 |
+
model = DetrForObjectDetection.from_pretrained("joortif/practica_2")
|
8 |
+
|
9 |
+
def detect_objects(img: Image.Image):
|
10 |
+
inputs = processor(images=img, return_tensors="pt")
|
11 |
+
outputs = model(**inputs)
|
12 |
+
|
13 |
+
target_sizes = torch.tensor([img.size[::-1]]) #
|
14 |
+
results = processor.post_process_object_detection(
|
15 |
+
outputs, target_sizes=target_sizes, threshold=0.9
|
16 |
+
)[0]
|
17 |
+
|
18 |
+
draw = ImageDraw.Draw(img)
|
19 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
20 |
+
box = [round(i, 2) for i in box.tolist()]
|
21 |
+
draw.rectangle(box, outline="red", width=3)
|
22 |
+
draw.text((box[0], box[1]), f"{model.config.id2label[label.item()]}: {score:.2f}", fill="red")
|
23 |
+
|
24 |
+
return img
|