File size: 1,551 Bytes
a2ddd3a
 
 
 
33754f1
 
a2ddd3a
 
 
 
33754f1
a2ddd3a
33754f1
a2ddd3a
33754f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2ddd3a
33754f1
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import gradio as gr
from transformers import AutoImageProcessor, AutoModelForObjectDetection, pipeline

# Carga el procesador de im谩genes y el modelo
image_processor = AutoImageProcessor.from_pretrained("seayala/practica_2")
model = AutoModelForObjectDetection.from_pretrained("seayala/practica_2")

# Crea el pipeline de detecci贸n de objetos
detector = pipeline("object-detection", model=model, image_processor=image_processor)

# Funci贸n para procesar la imagen y generar anotaciones
def predict(image):
    results = detector(image)

    # Extrae cajas en formato xmin, ymin, xmax, ymax
    boxes = []
    for obj in results:
        box = obj["box"]
        label = f'{obj["label"]} ({obj["score"]:.2f})'
        # Convierte el formato si es necesario
        if "x" in box and "y" in box and "width" in box and "height" in box:
            xmin = box["x"]
            ymin = box["y"]
            xmax = xmin + box["width"]
            ymax = ymin + box["height"]
        else:
            xmin = box.get("xmin", 0)
            ymin = box.get("ymin", 0)
            xmax = box.get("xmax", 0)
            ymax = box.get("ymax", 0)

        boxes.append({"label": label, "box": [xmin, ymin, xmax, ymax]})

    return image, boxes

# Interfaz Gradio
iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Sube una imagen"),
    outputs=gr.AnnotatedImage(label="Resultados de detecci贸n"),
    title="Detector de objetos",
    description="Sube una imagen para detectar objetos con tu modelo personalizado."
)

iface.launch()