Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
import torchvision.transforms as T
|
| 7 |
+
|
| 8 |
+
# Cargar modelo
|
| 9 |
+
checkpoint = "PablitoGil14/Practica2"
|
| 10 |
+
processor = AutoImageProcessor.from_pretrained(checkpoint)
|
| 11 |
+
model = AutoModelForObjectDetection.from_pretrained(checkpoint)
|
| 12 |
+
|
| 13 |
+
# Funci贸n de predicci贸n
|
| 14 |
+
def detectar_canguros(imagen):
|
| 15 |
+
inputs = processor(images=imagen, return_tensors="pt")
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
|
| 19 |
+
# Filtrar predicciones con una confianza > 0.7
|
| 20 |
+
target_sizes = torch.tensor([imagen.size[::-1]])
|
| 21 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.05)[0]
|
| 22 |
+
|
| 23 |
+
draw = Image.fromarray(imagen).convert("RGB")
|
| 24 |
+
draw_ctx = ImageDraw.Draw(draw)
|
| 25 |
+
|
| 26 |
+
for box, score, label in zip(results["boxes"], results["scores"], results["labels"]):
|
| 27 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 28 |
+
draw_ctx.rectangle(box, outline="red", width=3)
|
| 29 |
+
draw_ctx.text((box[0], box[1]), f"{model.config.id2label[label.item()]}: {round(score.item(), 2)}", fill="red")
|
| 30 |
+
|
| 31 |
+
return draw
|
| 32 |
+
|
| 33 |
+
# Interfaz Gradio
|
| 34 |
+
gr.Interface(
|
| 35 |
+
fn=detectar_canguros,
|
| 36 |
+
inputs=gr.Image(type="pil"),
|
| 37 |
+
outputs=gr.Image(type="pil"),
|
| 38 |
+
title="Detector de Canguros",
|
| 39 |
+
description="Sube una imagen y detecta canguros usando el modelo YOLOS entrenado"
|
| 40 |
+
).launch()
|