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
| import numpy as np from typing import Tuple, List from dataclasses import dataclass from enum import Enum
class BodySize(Enum): """体型分类""" UNKNOWN = "unknown" P5 = "5th_percentile" P50 = "50th_percentile" P95 = "95th_percentile" CHILD = "child"
@dataclass class OccupantMetrics: """乘员测量数据""" shoulder_height: float head_top_height: float shoulder_width: float torso_length: float thigh_length: float
class OccupantClassifier: """ 乘员体型分类器 基于 3D 关键点测量乘员体型 """ def __init__(self): self.size_reference = { 'female': { BodySize.P5: { 'height': 152.0, 'shoulder_height': 125.0, 'shoulder_width': 36.0, 'torso_length': 50.0 }, BodySize.P50: { 'height': 163.0, 'shoulder_height': 135.0, 'shoulder_width': 40.0, 'torso_length': 55.0 }, BodySize.P95: { 'height': 175.0, 'shoulder_height': 145.0, 'shoulder_width': 45.0, 'torso_length': 60.0 } }, 'male': { BodySize.P5: { 'height': 163.0, 'shoulder_height': 135.0, 'shoulder_width': 40.0, 'torso_length': 55.0 }, BodySize.P50: { 'height': 175.0, 'shoulder_height': 145.0, 'shoulder_width': 45.0, 'torso_length': 60.0 }, BodySize.P95: { 'height': 188.0, 'shoulder_height': 155.0, 'shoulder_width': 50.0, 'torso_length': 65.0 } } } self.classification_tolerance = 0.10 def classify(self, keypoints_3d: np.ndarray, seat_position: float = 0.5) -> Tuple[BodySize, float]: """ 分类乘员体型 Args: keypoints_3d: 3D 关键点,shape=(N, 3),单位 cm seat_position: 座椅位置 (0=最后, 1=最前) Returns: body_size: 体型分类 confidence: 置信度 """ metrics = self._extract_metrics(keypoints_3d) if metrics is None: return BodySize.UNKNOWN, 0.0 best_match = BodySize.UNKNOWN best_score = 0.0 for gender in ['male', 'female']: for size, reference in self.size_reference[gender].items(): score = self._compute_similarity(metrics, reference) if score > best_score: best_score = score best_match = size if metrics.head_top_height < 100: return BodySize.CHILD, 0.9 return best_match, best_score def _extract_metrics(self, keypoints_3d: np.ndarray) -> OccupantMetrics: """从关键点提取测量数据""" if len(keypoints_3d) < 13: return None shoulder_height = (keypoints_3d[5, 1] + keypoints_3d[6, 1]) / 2 eye_height = (keypoints_3d[1, 1] + keypoints_3d[2, 1]) / 2 head_top_height = eye_height + 10 shoulder_width = np.linalg.norm(keypoints_3d[5] - keypoints_3d[6]) shoulder_center = (keypoints_3d[5] + keypoints_3d[6]) / 2 hip_center = (keypoints_3d[11] + keypoints_3d[12]) / 2 torso_length = np.linalg.norm(shoulder_center - hip_center) thigh_length = np.linalg.norm( (keypoints_3d[11] + keypoints_3d[12]) / 2 - (keypoints_3d[13] + keypoints_3d[14]) / 2 ) return OccupantMetrics( shoulder_height=shoulder_height, head_top_height=head_top_height, shoulder_width=shoulder_width, torso_length=torso_length, thigh_length=thigh_length ) def _compute_similarity(self, metrics: OccupantMetrics, reference: dict) -> float: """计算与参考数据的相似度""" errors = [] if 'shoulder_height' in reference: error = abs(metrics.shoulder_height - reference['shoulder_height']) / reference['shoulder_height'] errors.append(1 - error) if 'shoulder_width' in reference: error = abs(metrics.shoulder_width - reference['shoulder_width']) / reference['shoulder_width'] errors.append(1 - error) if 'torso_length' in reference: error = abs(metrics.torso_length - reference['torso_length']) / reference['torso_length'] errors.append(1 - error) return np.mean(errors) if errors else 0.0
|