Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection, pipeline
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
# Carga el procesador de imágenes y el modelo
|
| 6 |
-
image_processor = AutoImageProcessor.from_pretrained("seayala/practica_2")
|
| 7 |
-
model = AutoModelForObjectDetection.from_pretrained("seayala/practica_2")
|
| 8 |
|
| 9 |
# Crea el pipeline de detección de objetos
|
| 10 |
detector = pipeline("object-detection", model=model, image_processor=image_processor)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def predict(image):
|
| 14 |
-
|
| 15 |
-
return results
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
iface = gr.Interface(
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoImageProcessor, AutoModelForObjectDetection, pipeline
|
|
|
|
| 3 |
|
| 4 |
# Carga el procesador de imágenes y el modelo
|
| 5 |
+
image_processor = AutoImageProcessor.from_pretrained("seayala/practica_2")
|
| 6 |
+
model = AutoModelForObjectDetection.from_pretrained("seayala/practica_2")
|
| 7 |
|
| 8 |
# Crea el pipeline de detección de objetos
|
| 9 |
detector = pipeline("object-detection", model=model, image_processor=image_processor)
|
| 10 |
|
| 11 |
+
# Función para procesar la imagen y generar anotaciones
|
| 12 |
def predict(image):
|
| 13 |
+
results = detector(image)
|
|
|
|
| 14 |
|
| 15 |
+
# Extrae cajas en formato xmin, ymin, xmax, ymax
|
| 16 |
+
boxes = []
|
| 17 |
+
for obj in results:
|
| 18 |
+
box = obj["box"]
|
| 19 |
+
label = f'{obj["label"]} ({obj["score"]:.2f})'
|
| 20 |
+
# Convierte el formato si es necesario
|
| 21 |
+
if "x" in box and "y" in box and "width" in box and "height" in box:
|
| 22 |
+
xmin = box["x"]
|
| 23 |
+
ymin = box["y"]
|
| 24 |
+
xmax = xmin + box["width"]
|
| 25 |
+
ymax = ymin + box["height"]
|
| 26 |
+
else:
|
| 27 |
+
xmin = box.get("xmin", 0)
|
| 28 |
+
ymin = box.get("ymin", 0)
|
| 29 |
+
xmax = box.get("xmax", 0)
|
| 30 |
+
ymax = box.get("ymax", 0)
|
| 31 |
+
|
| 32 |
+
boxes.append({"label": label, "box": [xmin, ymin, xmax, ymax]})
|
| 33 |
+
|
| 34 |
+
return image, boxes
|
| 35 |
+
|
| 36 |
+
# Interfaz Gradio
|
| 37 |
iface = gr.Interface(
|
| 38 |
+
fn=predict,
|
| 39 |
+
inputs=gr.Image(type="pil", label="Sube una imagen"),
|
| 40 |
+
outputs=gr.AnnotatedImage(label="Resultados de detección"),
|
| 41 |
+
title="Detector de objetos",
|
| 42 |
+
description="Sube una imagen para detectar objetos con tu modelo personalizado."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
iface.launch()
|
|
|