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
| import numpy as np from sklearn.ensemble import RandomForestRegressor from typing import Tuple, Dict import cv2
class ThermalComfortSystem: """ 非接触驾驶员热舒适感知系统 论文: Building and Environment, 2025 核心: 红外热成像 → YOLO面部检测 → RF TSV → 模糊HVAC TSV: -3(冷) -2(凉) -1(微凉) 0(适中) 1(微暖) 2(暖) 3(热) """ ROI_WEIGHTS = { 'forehead': 0.35, 'nose_tip': 0.25, 'cheek': 0.20, 'temple': 0.15, 'chin': 0.05 } TSV_SCALE = { -3: 'cold', -2: 'cool', -1: 'slightly_cool', 0: 'neutral', 1: 'slightly_warm', 2: 'warm', 3: 'hot' } def __init__(self): self.face_cascade = cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' ) self.tsv_model = RandomForestRegressor( n_estimators=100, max_depth=10, random_state=42 ) self.pmv_params = { 'met': 1.0, 'clo': 0.5, 'air_vel': 0.2, } def extract_facial_temperatures(self, thermal_image: np.ndarray) -> dict: """ 从红外热像提取面部各区域温度 Args: thermal_image: 红外图像(温度矩阵, °C) Returns: dict: 各区域平均温度 """ normalized = ((thermal_image - 20) / (40 - 20) * 255).astype(np.uint8) colored = cv2.applyColorMap(normalized, cv2.COLORMAP_JET) gray = cv2.cvtColor(colored, cv2.COLOR_BGR2GRAY) faces = self.face_cascade.detectMultiScale(gray, 1.3, 5) if len(faces) == 0: return {'error': 'no_face_detected'} x, y, w, h = faces[0] forehead = thermal_image[y:y+h//4, x+w//4:x+3*w//4] nose = thermal_image[y+h//4:y+3*h//8, x+3*w//8:x+5*w//8] cheek_l = thermal_image[y+h//4:y+h//2, x:x+w//4] cheek_r = thermal_image[y+h//4:y+h//2, x+3*w//4:x+w] cheek = np.concatenate([cheek_l.ravel(), cheek_r.ravel()]) temple = thermal_image[y:y+h//3, x+w-10:x+w] chin = thermal_image[y+3*h//4:y+h, x+w//4:x+3*w//4] return { 'forehead': np.mean(forehead), 'nose_tip': np.mean(nose), 'cheek': np.mean(cheek), 'temple': np.mean(temple), 'chin': np.mean(chin) } def predict_tsv(self, facial_temps: dict, ambient_temp: float = 24, humidity: float = 50) -> float: """ 预测热感觉投票(TSV) Args: facial_temps: 面部各区域温度 ambient_temp: 环境温度 humidity: 湿度 Returns: tsv: -3 to +3 """ if 'error' in facial_temps: return self._compute_pmv(ambient_temp, humidity) features = np.array([ facial_temps['forehead'], facial_temps['nose_tip'], facial_temps['cheek'], facial_temps['temple'], facial_temps['chin'], ambient_temp, humidity, facial_temps['forehead'] - facial_temps['nose_tip'], ]).reshape(1, -1) tsv = self.tsv_model.predict(features)[0] return np.clip(tsv, -3, 3) def _compute_pmv(self, air_temp: float, humidity: float) -> float: """ PMV (Predicted Mean Vote) 物理模型 用于面部检测失败时的降级方案 """ tr = air_temp pa = humidity / 100 * 2.338 L = 0.303 * np.exp(-0.036 * 58.2) - 0.028 pmv = (0.303 * L) * ( 58.2 - 0.35 * (air_temp - 30) - 0.42 * (58.2 - 58.15) - 0.0023 * (58.2) * (44 - pa) - 0.0014 * 58.2 * (34 - air_temp) - 1.7 * 0.1 * (1 - 0.01 * humidity) ) return np.clip(pmv, -3, 3) def compute_hvac_control(self, tsv: float, current_hvac: dict) -> dict: """ 模糊逻辑HVAC控制 Args: tsv: 当前TSV current_hvac: 当前HVAC状态 Returns: dict: 新HVAC设置 """ if tsv <= -2: return { 'target_temp': min(current_hvac.get('target_temp', 22) + 2, 28), 'fan_speed': min(current_hvac.get('fan_speed', 3) + 2, 8), 'mode': 'heating', 'air_direction': 'feet' } elif tsv <= -1: return { 'target_temp': current_hvac.get('target_temp', 22) + 1, 'fan_speed': current_hvac.get('fan_speed', 3) + 1, 'mode': 'heating', 'air_direction': 'feet' } elif tsv <= 1: return current_hvac elif tsv <= 2: return { 'target_temp': max(current_hvac.get('target_temp', 22) - 1, 18), 'fan_speed': current_hvac.get('fan_speed', 3) + 1, 'mode': 'cooling', 'air_direction': 'face' } else: return { 'target_temp': max(current_hvac.get('target_temp', 22) - 2, 16), 'fan_speed': min(current_hvac.get('fan_speed', 3) + 2, 8), 'mode': 'cooling', 'air_direction': 'face' }
if __name__ == "__main__": system = ThermalComfortSystem() np.random.seed(42) thermal = np.random.normal(33, 2, (120, 160)) temps = { 'forehead': 34.5, 'nose_tip': 31.2, 'cheek': 33.0, 'temple': 34.8, 'chin': 33.5 } tsv = system.predict_tsv(temps, ambient_temp=20, humidity=50) print("=== 驾驶员热舒适感知 ===") print(f"面部温度: {temps}") print(f"环境温度: 20°C, 湿度: 50%") print(f"预测TSV: {tsv:.1f} ({system.TSV_SCALE.get(int(tsv), 'unknown')})") current_hvac = {'target_temp': 22, 'fan_speed': 3, 'mode': 'auto'} new_hvac = system.compute_hvac_control(tsv, current_hvac) print(f"\n当前HVAC: {current_hvac}") print(f"建议HVAC: {new_hvac}") print(f"\n=== 论文性能 ===") print(f" RF TSV准确率: >85%") print(f" 能耗节省: 12-18%") print(f" 响应延迟: <2秒") print(f" 遮挡降级: 自动切换PMV模型")
|