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
| """ Euro NCAP 2026 安全带路由分类 基于人体关键点+安全带分割的佩戴方式判定
分类类别: 1. Correct Routing: 正确佩戴(斜跨带绕过肩部+腰部) 2. Buckle Only: 仅扣扣子(带子未绕过身体) 3. Lap Belt Only: 仅腰带(斜跨带在背后) 4. Behind Back: 安全带在背后(整条带子绕到背后) """
import torch import torch.nn as nn import numpy as np from typing import Dict, Tuple from dataclasses import dataclass from enum import Enum
class BeltRoutingClass(Enum): """安全带路由分类""" CORRECT_ROUTING = 0 BUCKLE_ONLY = 1 LAP_BELT_ONLY = 2 BEHIND_BACK = 3
@dataclass class BeltRoutingResult: """安全带路由判定结果""" routing_class: BeltRoutingClass confidence: float shoulder_belt_detected: bool lap_belt_detected: bool shoulder_belt_crossing_body: bool lap_belt_on_waist: bool score: int
class BeltRoutingClassifier(nn.Module): """ 安全带路由分类网络 输入: - 人体关键点向量(18维:肩部/胸部/腰部/肘部坐标) - 安全带分割掩码特征(12维:斜跨带/腰带/扣子区域统计) 输出: - 4类路由分类(softmax概率) 网络架构: - 两分支特征提取(关键点+分割特征) - 全连接融合层 - 4类分类输出 """ def __init__(self, keypoints_dim: int = 18, segmentation_feat_dim: int = 12, hidden_dim: int = 64): super(BeltRoutingClassifier, self).__init__() self.keypoints_encoder = nn.Sequential( nn.Linear(keypoints_dim, hidden_dim), nn.ReLU(inplace=True), nn.Linear(hidden_dim, hidden_dim), nn.ReLU(inplace=True) ) self.segmentation_encoder = nn.Sequential( nn.Linear(segmentation_feat_dim, hidden_dim), nn.ReLU(inplace=True), nn.Linear(hidden_dim, hidden_dim), nn.ReLU(inplace=True) ) self.classifier = nn.Sequential( nn.Linear(hidden_dim * 2, hidden_dim), nn.ReLU(inplace=True), nn.Linear(hidden_dim, 4), nn.Softmax(dim=1) ) def forward(self, keypoints_feat: torch.Tensor, segmentation_feat: torch.Tensor) -> torch.Tensor: """ 前向传播 Args: keypoints_feat: 人体关键点特征(B×18) segmentation_feat: 安全带分割特征(B×12) Returns: output: 4类分类概率(B×4) """ kp_encoded = self.keypoints_encoder(keypoints_feat) seg_encoded = self.segmentation_encoder(segmentation_feat) fused = torch.cat([kp_encoded, seg_encoded], dim=1) output = self.classifier(fused) return output def classify_routing(self, keypoints: BodyKeypoints, segmentation: BeltSegmentationResult, device: str = 'cpu') -> BeltRoutingResult: """ 安全带路由分类推理 Args: keypoints: 人体关键点 segmentation: 安全带分割结果 device: 计算设备 Returns: BeltRoutingResult: 路由判定结果 """ self.eval() keypoints_feat = np.array([ keypoints.left_shoulder[0], keypoints.left_shoulder[1], keypoints.right_shoulder[0], keypoints.right_shoulder[1], keypoints.left_chest_estimate[0], keypoints.left_chest_estimate[1], keypoints.right_chest_estimate[0], keypoints.right_chest_estimate[1], keypoints.left_hip[0], keypoints.left_hip[1], keypoints.right_hip[0], keypoints.right_hip[1], keypoints.left_elbow[0], keypoints.left_elbow[1], keypoints.right_elbow[0], keypoints.right_elbow[1], keypoints.left_shoulder[2], keypoints.right_shoulder[2] ]) h, w = segmentation.shoulder_belt_mask.shape shoulder_area_ratio = np.sum(segmentation.shoulder_belt_mask > 0.5) / (h * w) lap_area_ratio = np.sum(segmentation.lap_belt_mask > 0.5) / (h * w) buckle_area_ratio = np.sum(segmentation.buckle_mask > 0.5) / (h * w) def get_mask_center(mask): if np.sum(mask) == 0: return 0.5, 0.5 y_coords, x_coords = np.where(mask > 0.5) return np.mean(x_coords) / w, np.mean(y_coords) / h shoulder_center_x, shoulder_center_y = get_mask_center(segmentation.shoulder_belt_mask) lap_center_x, lap_center_y = get_mask_center(segmentation.lap_belt_mask) buckle_center_x, buckle_center_y = get_mask_center(segmentation.buckle_mask) segmentation_feat = np.array([ shoulder_area_ratio, lap_area_ratio, buckle_area_ratio, shoulder_center_x, shoulder_center_y, lap_center_x, lap_center_y, buckle_center_x, buckle_center_y, segmentation.shoulder_belt_confidence, segmentation.lap_belt_confidence, segmentation.buckle_confidence ]) kp_tensor = torch.from_numpy(keypoints_feat.astype(np.float32)).unsqueeze(0) seg_tensor = torch.from_numpy(segmentation_feat.astype(np.float32)).unsqueeze(0) kp_tensor = kp_tensor.to(device) seg_tensor = seg_tensor.to(device) with torch.no_grad(): output = self.forward(kp_tensor, seg_tensor) probs = output.squeeze(0).cpu().numpy() pred_class_idx = np.argmax(probs) confidence = probs[pred_class_idx] routing_class = BeltRoutingClass(pred_class_idx) score_mapping = { BeltRoutingClass.CORRECT_ROUTING: 5, BeltRoutingClass.BUCKLE_ONLY: 0, BeltRoutingClass.LAP_BELT_ONLY: 0, BeltRoutingClass.BEHIND_BACK: 0 } score = score_mapping[routing_class] shoulder_detected = shoulder_area_ratio > 0.02 lap_detected = lap_area_ratio > 0.02 shoulder_belt_crossing = (shoulder_center_y > keypoints.left_shoulder[1]) and \ (shoulder_center_y < keypoints.left_hip[1]) lap_belt_on_waist = abs(lap_center_y - keypoints.left_hip[1]) < 0.1 return BeltRoutingResult( routing_class=routing_class, confidence=confidence, shoulder_belt_detected=shoulder_detected, lap_belt_detected=lap_detected, shoulder_belt_crossing_body=shoulder_belt_crossing, lap_belt_on_waist=lap_belt_on_waist, score=score )
if __name__ == "__main__": classifier = BeltRoutingClassifier() keypoints = BodyKeypoints( left_shoulder=(0.3, 0.4, 0.9), right_shoulder=(0.7, 0.4, 0.9), left_chest_estimate=(0.3, 0.55), right_chest_estimate=(0.7, 0.55), left_hip=(0.3, 0.8, 0.8), right_hip=(0.7, 0.8, 0.8), left_elbow=(0.2, 0.6, 0.7), right_elbow=(0.8, 0.6, 0.7), pose_confidence=0.85 ) segmentation = BeltSegmentationResult( shoulder_belt_mask=np.zeros((480, 640), dtype=np.uint8), lap_belt_mask=np.zeros((480, 640), dtype=np.uint8), buckle_mask=np.zeros((480, 640), dtype=np.uint8), shoulder_belt_confidence=0.0, lap_belt_confidence=0.0, buckle_confidence=0.0 ) result = classifier.classify_routing(keypoints, segmentation) print(f"=== 安全带路由分类结果 ===") print(f"分类:{result.routing_class.name}") print(f"置信度:{result.confidence:.3f}") print(f"斜跨带检测:{result.shoulder_belt_detected}") print(f"腰带检测:{result.lap_belt_detected}") print(f"Euro NCAP分值:{result.score}")
|