Spaces:
Build error
Build error
File size: 3,277 Bytes
7b0cfb4 |
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 89 90 91 92 93 94 |
import cv2
import time
def main():
"""
Script simple para probar la captura de cámara y dibujo de rectángulos en tiempo real.
Este script no realiza detección facial, solo dibuja rectángulos predefinidos
para verificar que el hardware y la visualización funcionan correctamente.
"""
print("Iniciando prueba de cámara directa...")
# Iniciar la cámara
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: No se pudo abrir la cámara. Verifica la conexión y permisos.")
return
print("Cámara iniciada. Presiona 'q' para salir.")
# Contador de frames
frame_count = 0
start_time = time.time()
# Rectángulo fijo en el centro (simulando detección)
rect_size = 200
while True:
# Capturar frame
ret, frame = cap.read()
if not ret:
print("Error: No se pudo leer el frame. La cámara podría estar desconectada.")
break
# Conteo de FPS
frame_count += 1
elapsed_time = time.time() - start_time
fps = frame_count / elapsed_time if elapsed_time > 0 else 0
# Obtener dimensiones del frame
height, width, _ = frame.shape
# Crear una copia para dibujar
display = frame.copy()
# Dibujar rectángulo central fijo (simulando detección facial)
center_x, center_y = width // 2, height // 2
x1 = center_x - rect_size // 2
y1 = center_y - rect_size // 2
x2 = center_x + rect_size // 2
y2 = center_y + rect_size // 2
# Dibujar con color verde y grosor 3
cv2.rectangle(display, (x1, y1), (x2, y2), (0, 255, 0), 3)
cv2.putText(display, "ROSTRO SIMULADO", (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# Dibujar un segundo rectángulo en la esquina (para verificar que se dibujan correctamente)
cv2.rectangle(display, (50, 50), (200, 200), (0, 0, 255), 3)
cv2.putText(display, "PRUEBA", (50, 40),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# Mostrar información de FPS
cv2.putText(display, f"FPS: {fps:.1f}", (10, height - 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Mostrar número de frame
cv2.putText(display, f"Frame: {frame_count}", (10, height - 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Mostrar timestamp
timestamp = time.strftime("%H:%M:%S")
cv2.putText(display, f"Hora: {timestamp}", (width - 200, height - 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
# Mostrar resultado
cv2.imshow("Prueba de Visualización", display)
# Salir si se presiona 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Mostrar estadísticas
print(f"Frames capturados: {frame_count}")
print(f"Tiempo de ejecución: {elapsed_time:.2f} segundos")
print(f"FPS promedio: {fps:.2f}")
# Liberar recursos
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main() |