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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| """ 远程光电容积脉搏波(rPPG)心率提取
基于 Green/CHROM/POS 算法 从DMS面部视频中提取心率,无需接触式传感器
依赖: pip install numpy scipy opencv-python 参考: Poh et al. (2010), Verkruysse et al. (2008) """
import numpy as np from scipy.signal import butter, filtfilt, find_peaks from scipy.fft import fft, fftfreq from typing import Tuple, Dict
class rPPGHeartRate: """ 基于面部视频的rPPG心率提取 算法: CHROM (Chrominance-based rPPG) 参考: De Haan & Jeanne (2013) 核心原理: - 皮肤血液容积随心跳变化 - 绿色通道对血液容积变化最敏感 - 通过色度变换消除运动伪影 """ def __init__(self, fs: int = 30): """ Args: fs: 视频帧率 (Hz) """ self.fs = fs def extract_face_roi(self, frame: np.ndarray, landmarks=None) -> np.ndarray: """ 提取面部ROI区域 Args: frame: 视频帧 (H, W, 3) BGR格式 landmarks: 面部关键点 (可选,否则用肤色检测) Returns: roi: 面部ROI图像 """ if landmarks is not None: x_coords = [p[0] for p in landmarks] y_coords = [p[1] for p in landmarks] x1, x2 = int(min(x_coords)), int(max(x_coords)) y1, y2 = int(min(y_coords) * 0.3), int(max(y_coords) * 0.7) return frame[y1:y2, x1:x2] else: ycbcr = np.array(frame) ycbcr = cv2.cvtColor(ycbcr, cv2.COLOR_BGR2YCrCb) if frame.dtype == np.uint8 else frame if frame.dtype == np.uint8: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower = np.array([0, 30, 60]) upper = np.array([20, 150, 255]) mask = cv2.inRange(hsv, lower, upper) contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if contours: largest = max(contours, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(largest) return frame[y:y+h, x:x+w] h, w = frame.shape[:2] return frame[int(h*0.2):int(h*0.6), int(w*0.3):int(w*0.7)] def chrom_method(self, rgb_sequence: np.ndarray) -> np.ndarray: """ CHROM算法提取rPPG信号 色度信号 X = 3*R - 2*G 色度信号 Y = 1.5*R + G - 1.5*B rPPG信号 = X - α*Y Args: rgb_sequence: RGB均值序列, shape=(n_frames, 3) Returns: rppg_signal: 提取的rPPG信号 """ R = rgb_sequence[:, 0].astype(np.float64) G = rgb_sequence[:, 1].astype(np.float64) B = rgb_sequence[:, 2].astype(np.float64) R_norm = R / (R + G + B + 1e-10) G_norm = G / (R + G + B + 1e-10) B_norm = B / (R + G + B + 1e-10) X = 3 * R_norm - 2 * G_norm Y = 1.5 * R_norm + G_norm - 1.5 * B_norm alpha = np.std(X) / (np.std(Y) + 1e-10) rppg = X - alpha * Y nyq = self.fs / 2 b, a = butter(4, [0.7/nyq, 3.5/nyq], btype='band') rppg_filtered = filtfilt(b, a, rppg) return rppg_filtered def estimate_heart_rate(self, rppg_signal: np.ndarray, window_sec: int = 10) -> Tuple[float, Dict]: """ 从rPPG信号估计心率 Args: rppg_signal: 滤波后rPPG信号 window_sec: 分析窗口秒数 Returns: heart_rate: 心率 (bpm) details: 详细信息 """ window = window_sec * self.fs n = len(rppg_signal) if n < window: window = n signal = rppg_signal[-window:] freqs = fftfreq(len(signal), 1/self.fs) spectrum = np.abs(fft(signal)) mask = (freqs >= 0.7) & (freqs <= 3.0) pos_freqs = freqs[mask] pos_spectrum = spectrum[mask] if len(pos_spectrum) > 0: peak_idx = np.argmax(pos_spectrum) hr_freq = pos_freqs[peak_idx] heart_rate = hr_freq * 60 peak_power = pos_spectrum[peak_idx] mean_power = np.mean(pos_spectrum) snr = peak_power / (mean_power + 1e-10) else: heart_rate = 0 snr = 0 peaks, _ = find_peaks(signal, distance=self.fs*0.4) if len(peaks) > 2: intervals = np.diff(peaks) / self.fs hr_time = 60 / np.mean(intervals) if np.mean(intervals) > 0 else 0 else: hr_time = 0 if hr_freq > 0 and hr_time > 0: heart_rate = 0.6 * heart_rate + 0.4 * hr_time if len(peaks) > 3: intervals = np.diff(peaks) / self.fs rmssd = np.sqrt(np.mean(intervals**2)) sdnn = np.std(intervals) else: rmssd = 0 sdnn = 0 return heart_rate, { 'hr_freq': heart_rate, 'hr_time': hr_time, 'snr': snr, 'rmssd': rmssd, 'sdnn': sdnn, 'peak_count': len(peaks), 'signal_quality': min(snr / 3.0, 1.0), } def process_video(self, frames: np.ndarray) -> Dict: """ 处理视频帧序列 Args: frames: shape=(n_frames, H, W, 3) BGR Returns: { 'heart_rate': 心率bpm, 'hrv_rmssd': HRV, 'signal_quality': 信号质量0-1, 'stress_index': 压力指数 } """ n_frames = len(frames) rgb_means = np.zeros((n_frames, 3)) for i, frame in enumerate(frames): roi = self.extract_face_roi(frame) rgb_means[i] = roi.mean(axis=(0, 1)) rgb_means = rgb_means[:, ::-1] rppg = self.chrom_method(rgb_means) hr, details = self.estimate_heart_rate(rppg) if details['rmssd'] > 0: stress_index = 1.0 / (1.0 + details['rmssd'] * 10) else: stress_index = 0.5 return { 'heart_rate': round(hr, 1), 'hrv_rmssd': round(details['rmssd'], 3), 'hrv_sdnn': round(details['sdnn'], 3), 'signal_quality': round(details['signal_quality'], 2), 'stress_index': round(stress_index, 2), 'n_peaks': details['peak_count'], }
import cv2
if __name__ == "__main__": np.random.seed(42) estimator = rPPGHeartRate(fs=30) n_frames = 300 frames = np.zeros((n_frames, 200, 200, 3), dtype=np.uint8) t = np.arange(n_frames) / 30.0 hr = 72 pulse = 5 * np.sin(2 * np.pi * hr/60 * t) for i in range(n_frames): base_color = np.array([120, 100, 90], dtype=np.float64) modulated = base_color + pulse[i] frames[i] = np.clip(modulated, 0, 255).astype(np.uint8) frames[i] = np.random.randint(0, 10, (200, 200, 3), dtype=np.uint8) + frames[i] print("=== rPPG心率提取测试 ===") result = estimator.process_video(frames) for k, v in result.items(): print(f" {k}: {v}") print(f"\n真实心率: {hr} bpm") print(f"估计心率: {result['heart_rate']} bpm") print(f"误差: {abs(result['heart_rate'] - hr):.1f} bpm") print(f"\n信号质量: {result['signal_quality']:.0%}") print(f"压力指数: {result['stress_index']:.2f} (0=放松, 1=高压)")
|