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
| import numpy as np
class ThermalCameraSimulation: """ 热成像相机仿真 Raytron技术: - 长波红外(LWIR)8-14μm - 微测辐射热计阵列 - 锗镜头聚焦红外辐射 - 无需主动照明 """ def __init__(self, resolution: tuple = (640, 512), temperature_range: tuple = (-20, 120)): self.resolution = resolution self.temp_range = temperature_range def simulate_thermal_image(self, scene_objects: list, ambient_temp: float = 20.0) -> np.ndarray: """ 模拟热成像 Args: scene_objects: 场景物体列表 [{type, temp, bbox}] ambient_temp: 环境温度 Returns: thermal_image: 热成像图, shape=(H, W) """ H, W = self.resolution thermal_image = np.ones((H, W)) * ambient_temp for obj in scene_objects: obj_type = obj["type"] obj_temp = obj["temp"] bbox = obj["bbox"] x1 = int(bbox[0] * W) y1 = int(bbox[1] * H) x2 = int(bbox[2] * W) y2 = int(bbox[3] * H) thermal_image[y1:y2, x1:x2] = obj_temp return thermal_image def detect_living_objects(self, thermal_image: np.ndarray, temp_threshold: float = 30.0) -> list: """ 检测生命体(温度高于阈值) Args: thermal_image: 热成像图 temp_threshold: 温度阈值 Returns: detections: 检测列表 [{bbox, temp}] """ H, W = thermal_image.shape detections = [] hot_mask = thermal_image > temp_threshold from scipy.ndimage import label labeled, num_features = label(hot_mask) for i in range(1, num_features + 1): region_mask = labeled == i rows = np.any(region_mask, axis=1) cols = np.any(region_mask, axis=0) y_min, y_max = np.where(rows)[0][[0, -1]] x_min, x_max = np.where(cols)[0][[0, -1]] region_temp = np.mean(thermal_image[region_mask]) detections.append({ "bbox": (x_min/W, y_min/H, x_max/W, y_max/H), "temp": region_temp, "area": np.sum(region_mask) }) return detections def classify_object_type(self, detection: dict, thermal_image: np.ndarray) -> str: """ 分类物体类型 Args: detection: 检测结果 thermal_image: 热成像图 Returns: obj_type: "human", "animal", "vehicle", "unknown" """ temp = detection["temp"] area = detection["area"] if temp > 36.0: if area > 5000: return "human" elif area > 1000: return "child" else: return "animal" elif temp > 50.0: return "vehicle" else: return "unknown"
if __name__ == "__main__": thermal = ThermalCameraSimulation(resolution=(640, 512)) scene_objects = [ {"type": "adult", "temp": 36.5, "bbox": (0.1, 0.3, 0.2, 0.7)}, {"type": "child", "temp": 36.8, "bbox": (0.5, 0.6, 0.6, 0.9)}, {"type": "vehicle", "temp": 60.0, "bbox": (0.3, 0.2, 0.5, 0.5)} ] thermal_image = thermal.simulate_thermal_image(scene_objects, ambient_temp=20.0) print(f"热成像形状: {thermal_image.shape}") print(f"最高温度: {np.max(thermal_image):.1f}°C") detections = thermal.detect_living_objects(thermal_image, temp_threshold=30.0) print(f"\n检测到 {len(detections)} 个生命体:") for i, det in enumerate(detections): obj_type = thermal.classify_object_type(det, thermal_image) print(f" {i+1}. 类型: {obj_type}, 温度: {det['temp']:.1f}°C")
|