Now let’s extract vital signs using Eulerian Video Magnification:
import cv2import sysimport osfrom collections import dequeimport numpy as np# Add project root to pathcurrent_dir = os.path.dirname(os.path.abspath(__file__))parent_dir = os.path.dirname(current_dir)sys.path.insert(0, parent_dir)from src.face_detector.manager import FaceDetectorfrom src.evm.evm_manager import process_video_evm_vital_signsfrom src.config import TARGET_ROI_SIZEdef measure_vital_signs(video_path, buffer_size=200): """Extract heart rate and respiratory rate from video.""" cap = cv2.VideoCapture(video_path) if not cap.isOpened(): print(f"Error: Could not open {video_path}") return # Initialize face detector face_detector = FaceDetector(model_type='mediapipe') frame_buffer = [] hr_history = deque(maxlen=10) print("Processing video...") try: while True: ret, frame = cap.read() if not ret: break # Detect face and extract ROI roi = face_detector.detect_face(frame) if roi: x, y, w, h = roi roi_frame = frame[y:y+h, x:x+w] roi_frame = cv2.resize(roi_frame, TARGET_ROI_SIZE) frame_buffer.append(roi_frame) # Process when buffer is full if len(frame_buffer) >= buffer_size: result = process_video_evm_vital_signs(frame_buffer) hr = result['heart_rate'] rr = result['respiratory_rate'] if hr is not None: hr_history.append(hr) filtered_hr = np.median(list(hr_history)) print(f"Heart Rate: {filtered_hr:.1f} BPM") if rr is not None: print(f"Respiratory Rate: {rr:.1f} BPM") frame_buffer.clear() finally: cap.release() face_detector.close() print("Processing complete!")if __name__ == "__main__": # Replace with your video file path measure_vital_signs("path/to/your/video.mp4")
Each “chunk” represents 200 frames (approximately 6.7 seconds at 30 FPS). The system needs this duration to accurately estimate vital signs via frequency analysis.
================================================================================1. MÉTRICAS DE DETECCIÓN FACIAL (ROI)--------------------------------------------------------------------------------Frames procesados: 10800Tiempo promedio por frame: 25.1 msFPS de detección: 39.82. MÉTRICAS DE PROCESAMIENTO EVM--------------------------------------------------------------------------------Chunks procesados: 54Tiempo promedio por frame: 9.5 msFPS de procesamiento EVM: 105.33. MÉTRICAS END-TO-END (DETECCIÓN + EVM)--------------------------------------------------------------------------------FPS end-to-end: 28.7Tiempo por medición: 6.97s (200 frames)Desglose de tiempo promedio: Detección ROI: 73.2% Procesamiento EVM: 26.8%4. MÉTRICAS DE PRECISIÓN (HEART RATE)--------------------------------------------------------------------------------Número de mediciones: 54Métricas de Error: Mean Absolute Error (MAE): 3.42 BPM Root Mean Square Error (RMSE): 4.18 BPMDistribución de Errores: Dentro de ±5 BPM: 87.0% Dentro de ±10 BPM: 96.3%Correlación con Ground Truth: 0.924