Segizu commited on
Commit
de9232e
1 Parent(s): c286429
Files changed (1) hide show
  1. app.py +48 -24
app.py CHANGED
@@ -2,58 +2,82 @@ import cv2
2
  import gradio as gr
3
  from transformers import pipeline
4
  from PIL import Image
5
- #kfsaf
6
- # Cargar el modelo de detecci贸n de objetos (usando CPU)
 
7
  detector = pipeline("object-detection", model="facebook/detr-resnet-50", device=-1)
8
 
9
  def process_video(video_path):
10
  """
11
- Procesa un video y devuelve el m谩ximo n煤mero detectado de personas, bicicletas y motos en un fotograma.
 
 
12
  """
13
  cap = cv2.VideoCapture(video_path)
14
  if not cap.isOpened():
15
- return {"person": 0, "bicycle": 0, "motorcycle": 0}
 
 
 
 
 
16
 
17
- max_counts = {"person": 0, "bicycle": 0, "motorcycle": 0}
 
 
 
 
 
 
 
18
 
 
 
 
 
19
  while True:
20
  ret, frame = cap.read()
21
  if not ret:
22
  break
23
 
24
- # Convertir el frame de BGR a RGB
25
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
26
-
27
- # Convertir el array de NumPy a una imagen PIL
28
  pil_image = Image.fromarray(frame_rgb)
29
 
30
- # Realizar la detecci贸n de objetos
31
  results = detector(pil_image)
32
 
33
- # Contar objetos detectados en el frame actual (con umbral de confianza)
34
- frame_counts = {"person": 0, "bicycle": 0, "motorcycle": 0}
35
  for detection in results:
36
- if detection["score"] < 0.7:
37
- continue
38
  label = detection["label"].lower()
39
- if label in frame_counts:
40
- frame_counts[label] += 1
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # Actualizar el conteo m谩ximo si en este frame se detecta un mayor n煤mero
43
- for key in frame_counts:
44
- if frame_counts[key] > max_counts[key]:
45
- max_counts[key] = frame_counts[key]
46
 
47
  cap.release()
48
- return max_counts
 
49
 
50
- # Crear la interfaz de Gradio para el Space
51
  iface = gr.Interface(
52
  fn=process_video,
53
  inputs=gr.Video(label="Sube tu video"),
54
- outputs="json",
55
- title="Detecci贸n de Objetos en Video",
56
- description="Carga un video y detecta cu谩ntas personas, bicicletas y motos aparecen usando modelos de Hugging Face."
57
  )
58
 
59
  if __name__ == "__main__":
 
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 (utilizamos 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 a las que queremos aplicar detecci贸n
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 luego 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
+ # Obtener la caja del objeto en formato [xmin, ymin, width, height]
58
+ box = detection["box"]
59
+ xmin, ymin, w, h = box
60
+ xmax = xmin + w
61
+ ymax = ymin + h
62
+
63
+ # Dibujar el rect谩ngulo y la etiqueta en el frame
64
+ cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), color=(0, 255, 0), thickness=2)
65
+ text = f"{label}: {score:.2f}"
66
+ cv2.putText(frame, text, (int(xmin), int(ymin)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
67
 
68
+ # Escribir el frame anotado en el video de salida
69
+ out.write(frame)
 
 
70
 
71
  cap.release()
72
+ out.release()
73
+ return output_path
74
 
 
75
  iface = gr.Interface(
76
  fn=process_video,
77
  inputs=gr.Video(label="Sube tu video"),
78
+ outputs=gr.Video(label="Video procesado"),
79
+ title="Detecci贸n y Visualizaci贸n de Objetos en Video",
80
+ description="Carga un video y se detectan personas, bicicletas y motos. Los objetos se enmarcan y etiquetan, mostrando la detecci贸n en tiempo real."
81
  )
82
 
83
  if __name__ == "__main__":