Spaces:
Sleeping
Sleeping
Agrego app.py
Browse files- README.md +11 -12
- app.py +104 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
-
|
| 2 |
-
title: PRY Transformers
|
| 3 |
-
emoji: 👁
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: green
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.44.1
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
short_description: Taller de Transformes Maestría Ciencia de Datos
|
| 11 |
-
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DETR Object Detection (Transformers + Gradio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
## Ejecutar localmente
|
| 4 |
+
```bash
|
| 5 |
+
python -m venv .venv
|
| 6 |
+
# Windows
|
| 7 |
+
.venv\Scripts\activate
|
| 8 |
+
# macOS/Linux
|
| 9 |
+
source .venv/bin/activate
|
| 10 |
+
|
| 11 |
+
pip install -r requirements.txt
|
| 12 |
+
python app.py
|
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 5 |
+
|
| 6 |
+
# --- Cargar modelo una sola vez ---
|
| 7 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 8 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50").eval()
|
| 9 |
+
|
| 10 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
model.to(DEVICE)
|
| 12 |
+
ID2LABEL = model.config.id2label
|
| 13 |
+
ALL_CLASSES = sorted(set(ID2LABEL.values()))
|
| 14 |
+
|
| 15 |
+
def _annotate(image: Image.Image, detections):
|
| 16 |
+
annotated = image.copy()
|
| 17 |
+
draw = ImageDraw.Draw(annotated)
|
| 18 |
+
try:
|
| 19 |
+
font = ImageFont.truetype("arial.ttf", 16)
|
| 20 |
+
except Exception:
|
| 21 |
+
font = ImageFont.load_default()
|
| 22 |
+
|
| 23 |
+
for d in detections:
|
| 24 |
+
x0, y0, x1, y1 = d["box_xyxy"]
|
| 25 |
+
label = d["label"]
|
| 26 |
+
score = d["score"]
|
| 27 |
+
txt = f"{label} {score:.2f}"
|
| 28 |
+
|
| 29 |
+
# Caja
|
| 30 |
+
draw.rectangle([x0, y0, x1, y1], outline="red", width=3)
|
| 31 |
+
|
| 32 |
+
# Texto con fondo
|
| 33 |
+
try:
|
| 34 |
+
tw = draw.textlength(txt, font=font) # Pillow 10+
|
| 35 |
+
th = 16
|
| 36 |
+
except Exception:
|
| 37 |
+
tw, th = font.getsize(txt) # fallback
|
| 38 |
+
draw.rectangle([x0, y0 - th - 4, x0 + tw + 6, y0], fill="red")
|
| 39 |
+
draw.text((x0 + 3, y0 - th - 2), txt, fill="white", font=font)
|
| 40 |
+
return annotated
|
| 41 |
+
|
| 42 |
+
def detect(image, threshold=0.9, classes=None, topk=0):
|
| 43 |
+
"""
|
| 44 |
+
Detecta objetos con DETR y retorna (imagen_anotada, lista_detecciones).
|
| 45 |
+
Cada detección: {'label', 'score', 'box_xyxy'} con valores redondeados.
|
| 46 |
+
"""
|
| 47 |
+
if image is None:
|
| 48 |
+
return None, []
|
| 49 |
+
|
| 50 |
+
inputs = processor(images=image, return_tensors="pt").to(DEVICE)
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
|
| 54 |
+
target_sizes = torch.tensor([image.size[::-1]], device=DEVICE) # (alto, ancho)
|
| 55 |
+
results = processor.post_process_object_detection(
|
| 56 |
+
outputs, target_sizes=target_sizes, threshold=float(threshold)
|
| 57 |
+
)[0]
|
| 58 |
+
|
| 59 |
+
dets = []
|
| 60 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 61 |
+
dets.append({
|
| 62 |
+
"label_id": int(label),
|
| 63 |
+
"label": ID2LABEL[int(label)],
|
| 64 |
+
"score": float(score),
|
| 65 |
+
"box_xyxy": [float(v) for v in box.tolist()]
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
# Filtro por clases (opcional)
|
| 69 |
+
if classes:
|
| 70 |
+
allow = set(classes)
|
| 71 |
+
dets = [d for d in dets if d["label"] in allow]
|
| 72 |
+
|
| 73 |
+
# Top-K por score (0 = sin límite)
|
| 74 |
+
if topk and int(topk) > 0:
|
| 75 |
+
dets = sorted(dets, key=lambda d: d["score"], reverse=True)[:int(topk)]
|
| 76 |
+
|
| 77 |
+
annotated = _annotate(image, dets)
|
| 78 |
+
nice_dets = [
|
| 79 |
+
{
|
| 80 |
+
"label": d["label"],
|
| 81 |
+
"score": round(d["score"], 4),
|
| 82 |
+
"box_xyxy": [round(v, 2) for v in d["box_xyxy"]],
|
| 83 |
+
}
|
| 84 |
+
for d in dets
|
| 85 |
+
]
|
| 86 |
+
return annotated, nice_dets
|
| 87 |
+
|
| 88 |
+
with gr.Blocks(title="DETR Object Detection") as demo:
|
| 89 |
+
gr.Markdown("## DETR Object Detection (Transformers + Gradio)\nSube una imagen, ajusta umbral y filtros.")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
img = gr.Image(type="pil", label="Imagen de entrada")
|
| 92 |
+
with gr.Column():
|
| 93 |
+
thr = gr.Slider(0.10, 0.99, value=0.90, step=0.01, label="Umbral (threshold)")
|
| 94 |
+
classes = gr.CheckboxGroup(choices=ALL_CLASSES, label="Filtrar por clases (opcional)")
|
| 95 |
+
topk = gr.Slider(0, 200, value=0, step=1, label="Top-K por score (0 = sin límite)")
|
| 96 |
+
btn = gr.Button("Detectar", variant="primary")
|
| 97 |
+
|
| 98 |
+
out_img = gr.Image(type="pil", label="Imagen anotada")
|
| 99 |
+
out_json = gr.JSON(label="Detecciones (JSON)")
|
| 100 |
+
|
| 101 |
+
btn.click(detect, inputs=[img, thr, classes, topk], outputs=[out_img, out_json])
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.42.0
|
| 2 |
+
torch>=2.2.0
|
| 3 |
+
torchvision>=0.17.0
|
| 4 |
+
gradio>=4.29.0
|
| 5 |
+
pillow>=10.3.0
|
| 6 |
+
safetensors>=0.4.3
|
| 7 |
+
timm>=0.9.16,<1.0.0
|