Team Ai
Apppublic

jarondon82/ComputerVisionProject

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
direct_detection.py94 linesDownload Raw Back to root
1import cv22import time3 4def main():5    """6    Script simple para probar la captura de cámara y dibujo de rectángulos en tiempo real.7    Este script no realiza detección facial, solo dibuja rectángulos predefinidos8    para verificar que el hardware y la visualización funcionan correctamente.9    """10    print("Iniciando prueba de cámara directa...")11    12    # Iniciar la cámara13    cap = cv2.VideoCapture(0)14    15    if not cap.isOpened():16        print("Error: No se pudo abrir la cámara. Verifica la conexión y permisos.")17        return18    19    print("Cámara iniciada. Presiona 'q' para salir.")20    21    # Contador de frames22    frame_count = 023    start_time = time.time()24    25    # Rectángulo fijo en el centro (simulando detección)26    rect_size = 20027    28    while True:29        # Capturar frame30        ret, frame = cap.read()31        32        if not ret:33            print("Error: No se pudo leer el frame. La cámara podría estar desconectada.")34            break35        36        # Conteo de FPS37        frame_count += 138        elapsed_time = time.time() - start_time39        fps = frame_count / elapsed_time if elapsed_time > 0 else 040        41        # Obtener dimensiones del frame42        height, width, _ = frame.shape43        44        # Crear una copia para dibujar45        display = frame.copy()46        47        # Dibujar rectángulo central fijo (simulando detección facial)48        center_x, center_y = width // 2, height // 249        x1 = center_x - rect_size // 250        y1 = center_y - rect_size // 251        x2 = center_x + rect_size // 252        y2 = center_y + rect_size // 253        54        # Dibujar con color verde y grosor 355        cv2.rectangle(display, (x1, y1), (x2, y2), (0, 255, 0), 3)56        cv2.putText(display, "ROSTRO SIMULADO", (x1, y1 - 10), 57                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)58        59        # Dibujar un segundo rectángulo en la esquina (para verificar que se dibujan correctamente)60        cv2.rectangle(display, (50, 50), (200, 200), (0, 0, 255), 3)61        cv2.putText(display, "PRUEBA", (50, 40), 62                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)63        64        # Mostrar información de FPS65        cv2.putText(display, f"FPS: {fps:.1f}", (10, height - 30), 66                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)67        68        # Mostrar número de frame69        cv2.putText(display, f"Frame: {frame_count}", (10, height - 60), 70                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)71        72        # Mostrar timestamp73        timestamp = time.strftime("%H:%M:%S")74        cv2.putText(display, f"Hora: {timestamp}", (width - 200, height - 30), 75                   cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)76        77        # Mostrar resultado78        cv2.imshow("Prueba de Visualización", display)79        80        # Salir si se presiona 'q'81        if cv2.waitKey(1) & 0xFF == ord('q'):82            break83    84    # Mostrar estadísticas85    print(f"Frames capturados: {frame_count}")86    print(f"Tiempo de ejecución: {elapsed_time:.2f} segundos")87    print(f"FPS promedio: {fps:.2f}")88    89    # Liberar recursos90    cap.release()91    cv2.destroyAllWindows()92 93if __name__ == "__main__":94    main()