File size: 3,001 Bytes
7d1ac51
 
8ac08dc
a2562c0
de9232e
 
8ac08dc
 
7d1ac51
 
 
8ac08dc
 
7d1ac51
 
 
de9232e
 
8ac08dc
de9232e
8ac08dc
 
 
de9232e
 
8ac08dc
de9232e
8ac08dc
 
de9232e
8ac08dc
 
 
de9232e
7d1ac51
 
 
 
8ac08dc
 
7d1ac51
8ac08dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de9232e
8ac08dc
7d1ac51
de9232e
 
7d1ac51
8ac08dc
7d1ac51
 
 
de9232e
8ac08dc
 
 
7d1ac51
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import gradio as gr
from ultralytics import YOLO
from PIL import Image
import tempfile

# Cargamos el modelo YOLOv8 (puedes usar yolov8n.pt, yolov8s.pt, etc.)
model = YOLO("yolov8n.pt")

def process_video(video_path):
    """
    Procesa un video, detecta personas, bicicletas y motos con YOLOv8,
    y dibuja los recuadros y etiquetas en cada frame. Devuelve un .mp4 anotado.
    """
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        return None

    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Creamos un archivo temporal para guardar el resultado
    tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
    output_path = tmp_file.name
    tmp_file.close()

    # Usamos un c贸dec compatible con navegadores (H.264 / avc1)
    fourcc = cv2.VideoWriter_fourcc(*'avc1')
    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

    # Clases que nos interesan
    valid_classes = ["person", "bicycle", "motorcycle"]

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        # Convertir BGR -> RGB para predecir con YOLO
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Hacemos la inferencia con un umbral de confianza del 0.5
        results = model.predict(frame_rgb, conf=0.5)
        # results es una lista; tomamos la primera predicci贸n
        boxes = results[0].boxes

        # Dibujamos cada bounding box
        for box in boxes:
            # box.cls, box.conf y box.xyxy son tensores, as铆 que convertimos a Python float/int
            cls_id = int(box.cls[0].item())     # 脥ndice de la clase
            conf   = float(box.conf[0].item())  # Confianza
            x1, y1, x2, y2 = box.xyxy[0]        # Coordenadas [xmin, ymin, xmax, ymax]

            class_name = model.names[cls_id]
            if class_name in valid_classes:
                # Dibujamos el rect谩ngulo
                cv2.rectangle(frame,
                              (int(x1), int(y1)),
                              (int(x2), int(y2)),
                              (0, 255, 0), 2)

                text = f"{class_name} {conf:.2f}"
                cv2.putText(frame, text,
                            (int(x1), int(y1) - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (0, 255, 0), 2)

        # Guardamos el frame anotado en el video de salida
        out.write(frame)

    cap.release()
    out.release()
    return output_path

# Interfaz de Gradio
iface = gr.Interface(
    fn=process_video,
    inputs=gr.Video(label="Sube tu video"),
    outputs=gr.Video(label="Video procesado"),
    title="Detecci贸n de Objetos con YOLOv8",
    description="Sube un video y se detectan personas, bicicletas y motos con YOLOv8. "
                "Los objetos se enmarcan y etiquetan en el video resultante."
)

if __name__ == "__main__":
    iface.launch()