Segizu commited on
Commit
8ac08dc
1 Parent(s): 27e96af
Files changed (2) hide show
  1. app.py +50 -48
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,84 +1,86 @@
1
  import cv2
2
  import gradio as gr
3
- from transformers import pipeline
4
  from PIL import Image
5
  import tempfile
6
 
7
- # Cargar el modelo de detecci贸n de objetos usando CPU
8
- detector = pipeline("object-detection", model="facebook/detr-resnet-50", device=-1)
9
 
10
  def process_video(video_path):
11
  """
12
- Procesa un video, detecta objetos y dibuja cuadros y etiquetas sobre ellos.
13
- Solo se procesar谩n las detecciones de personas, bicicletas y motos.
14
- Devuelve el video anotado.
15
  """
16
  cap = cv2.VideoCapture(video_path)
17
  if not cap.isOpened():
18
  return None
19
 
20
- # Obtener propiedades del video
21
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
22
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
23
- fps = cap.get(cv2.CAP_PROP_FPS)
24
-
25
- # Crear un archivo temporal para guardar el video de salida
26
  tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
27
  output_path = tmp_file.name
28
- tmp_file.close() # Se cierra para que VideoWriter pueda escribir en 茅l
29
 
30
- # Configurar VideoWriter (usamos el c贸dec mp4v)
31
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
32
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
33
-
34
- # Definir las clases de inter茅s
35
- valid_labels = {"person", "bicycle", "motorcycle"}
36
- threshold = 0.7 # Umbral de confianza
37
 
38
  while True:
39
  ret, frame = cap.read()
40
  if not ret:
41
  break
42
-
43
- # Convertir el frame de BGR a RGB y a imagen PIL
44
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
45
- pil_image = Image.fromarray(frame_rgb)
46
-
47
- # Obtener detecciones con el pipeline
48
- results = detector(pil_image)
49
-
50
- # Dibujar cada detecci贸n v谩lida en el frame
51
- for detection in results:
52
- score = detection["score"]
53
- label = detection["label"].lower()
54
- if score < threshold or label not in valid_labels:
55
- continue
56
-
57
- # Extraer la caja del objeto (dado que es un diccionario)
58
- box = detection["box"]
59
- xmin = box["xmin"]
60
- ymin = box["ymin"]
61
- xmax = box["xmax"]
62
- ymax = box["ymax"]
63
-
64
- # Dibujar el rect谩ngulo y la etiqueta en el frame
65
- cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color=(0, 255, 0), thickness=2)
66
- text = f"{label}: {score:.2f}"
67
- cv2.putText(frame, text, (int(xmin), int(ymin)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
68
-
69
- # Escribir el frame anotado en el video de salida
 
 
 
70
  out.write(frame)
71
-
72
  cap.release()
73
  out.release()
74
  return output_path
75
 
 
76
  iface = gr.Interface(
77
  fn=process_video,
78
  inputs=gr.Video(label="Sube tu video"),
79
  outputs=gr.Video(label="Video procesado"),
80
- title="Detecci贸n y Visualizaci贸n de Objetos en Video",
81
- description="Carga un video y se detectan personas, bicicletas y motos. Los objetos se enmarcan y etiquetan en tiempo real."
 
82
  )
83
 
84
  if __name__ == "__main__":
 
1
  import cv2
2
  import gradio as gr
3
+ from ultralytics import YOLO
4
  from PIL import Image
5
  import tempfile
6
 
7
+ # Cargamos el modelo YOLOv8 (puedes usar yolov8n.pt, yolov8s.pt, etc.)
8
+ model = YOLO("yolov8n.pt")
9
 
10
  def process_video(video_path):
11
  """
12
+ Procesa un video, detecta personas, bicicletas y motos con YOLOv8,
13
+ y dibuja los recuadros y etiquetas en cada frame. Devuelve un .mp4 anotado.
 
14
  """
15
  cap = cv2.VideoCapture(video_path)
16
  if not cap.isOpened():
17
  return None
18
 
19
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
20
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
21
+ fps = cap.get(cv2.CAP_PROP_FPS)
22
+
23
+ # Creamos un archivo temporal para guardar el resultado
24
  tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
25
  output_path = tmp_file.name
26
+ tmp_file.close()
27
 
28
+ # Usamos un c贸dec compatible con navegadores (H.264 / avc1)
29
+ fourcc = cv2.VideoWriter_fourcc(*'avc1')
30
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
31
+
32
+ # Clases que nos interesan
33
+ valid_classes = ["person", "bicycle", "motorcycle"]
 
34
 
35
  while True:
36
  ret, frame = cap.read()
37
  if not ret:
38
  break
39
+
40
+ # Convertir BGR -> RGB para predecir con YOLO
41
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
42
+
43
+ # Hacemos la inferencia con un umbral de confianza del 0.5
44
+ results = model.predict(frame_rgb, conf=0.5)
45
+ # results es una lista; tomamos la primera predicci贸n
46
+ boxes = results[0].boxes
47
+
48
+ # Dibujamos cada bounding box
49
+ for box in boxes:
50
+ # box.cls, box.conf y box.xyxy son tensores, as铆 que convertimos a Python float/int
51
+ cls_id = int(box.cls[0].item()) # 脥ndice de la clase
52
+ conf = float(box.conf[0].item()) # Confianza
53
+ x1, y1, x2, y2 = box.xyxy[0] # Coordenadas [xmin, ymin, xmax, ymax]
54
+
55
+ class_name = model.names[cls_id]
56
+ if class_name in valid_classes:
57
+ # Dibujamos el rect谩ngulo
58
+ cv2.rectangle(frame,
59
+ (int(x1), int(y1)),
60
+ (int(x2), int(y2)),
61
+ (0, 255, 0), 2)
62
+
63
+ text = f"{class_name} {conf:.2f}"
64
+ cv2.putText(frame, text,
65
+ (int(x1), int(y1) - 10),
66
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5,
67
+ (0, 255, 0), 2)
68
+
69
+ # Guardamos el frame anotado en el video de salida
70
  out.write(frame)
71
+
72
  cap.release()
73
  out.release()
74
  return output_path
75
 
76
+ # Interfaz de Gradio
77
  iface = gr.Interface(
78
  fn=process_video,
79
  inputs=gr.Video(label="Sube tu video"),
80
  outputs=gr.Video(label="Video procesado"),
81
+ title="Detecci贸n de Objetos con YOLOv8",
82
+ description="Sube un video y se detectan personas, bicicletas y motos con YOLOv8. "
83
+ "Los objetos se enmarcan y etiquetan en el video resultante."
84
  )
85
 
86
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -4,4 +4,6 @@ transformers
4
  torch
5
  tensorflow
6
  torchvision
7
- timm
 
 
 
4
  torch
5
  tensorflow
6
  torchvision
7
+ timm
8
+ ultralytics
9
+ Pillow