Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,47 @@
|
|
1 |
-
from huggingface_hub import from_pretrained_fastai
|
2 |
import gradio as gr
|
3 |
-
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
|
8 |
repo_id = "JeanCGuerrero/Practica2"
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
#
|
14 |
def predict(img):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForObjectDetection, AutoImageProcessor
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
|
6 |
+
# Definir el repositorio en Hugging Face
|
|
|
|
|
7 |
repo_id = "JeanCGuerrero/Practica2"
|
8 |
|
9 |
+
# Cargar el modelo de Hugging Face
|
10 |
+
model = AutoModelForObjectDetection.from_pretrained(repo_id)
|
11 |
+
image_processor = AutoImageProcessor.from_pretrained(repo_id)
|
12 |
|
13 |
+
# Funci贸n que se encarga de llevar a cabo las predicciones
|
14 |
def predict(img):
|
15 |
+
img = img.convert("RGB") # Asegurar que la imagen est茅 en formato RGB
|
16 |
+
inputs = image_processor(images=img, return_tensors="pt")
|
17 |
+
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = model(**inputs)
|
20 |
+
|
21 |
+
# Procesar los resultados
|
22 |
+
target_sizes = torch.tensor([img.size[::-1]])
|
23 |
+
results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0]
|
24 |
+
|
25 |
+
# Dibujar las detecciones en la imagen
|
26 |
+
draw = ImageDraw.Draw(img)
|
27 |
+
detecciones = []
|
28 |
+
|
29 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
30 |
+
box = [round(i, 2) for i in box.tolist()]
|
31 |
+
x, y, x2, y2 = box
|
32 |
+
draw.rectangle([x, y, x2, y2], outline="red", width=3)
|
33 |
+
class_name = f"Clase {label.item()} - Confianza: {round(score.item(), 2)}"
|
34 |
+
draw.text((x, y), class_name, fill="red")
|
35 |
+
detecciones.append(class_name)
|
36 |
+
|
37 |
+
return img, "\n".join(detecciones)
|
38 |
+
|
39 |
+
# Crear la interfaz y lanzarla con Gradio
|
40 |
+
gr.Interface(
|
41 |
+
fn=predict,
|
42 |
+
inputs=gr.Image(type="pil"),
|
43 |
+
outputs=[gr.Image(), gr.Text()],
|
44 |
+
title="Detector de Mapaches 馃",
|
45 |
+
description="Este modelo detecta mapaches en im谩genes usando DETR con el dataset Raccoon.",
|
46 |
+
examples=['raccoon-108.jpg', 'raccoon-108.jpg']
|
47 |
+
).launch(share=False)
|