image.vision\pose_estimation\human_pose_estimation.mbt

88.1% · 148 / 168 points · 837 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 人体姿态估计 pose_estimation/human_pose_estimation
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 人体姿态估计与动作识别(v32.0.0)
  7. 7// 关键点检测、骨架提取、姿态估计、动作识别、手势识别
  8. 8
  9. 9///|
  10. 10/// 姿态估计算法类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum PoseEstimationAlgorithm {
  15. 15 OpenPose
  16. 16 MediaPipe
  17. 17 AlphaPose
  18. 18 HRNet
  19. 19 SimpleBaseline
  20. 20 DeepPose
  21. 21 PoseNet
  22. 22 MoveNet
  23. 23 Unknown
  24. 24}
  25. 25
  26. 26///|
  27. 27pub extend PoseEstimationAlgorithm with @debug.Debug::{to_repr}
  28. 28
  29. 29///|
  30. 30pub extend PoseEstimationAlgorithm with Eq::{not_equal, equal}
  31. 31
  32. 32///|
  33. 33/// 动作识别算法类型
  34. 34/// `#non_exhaustive` 表示未来可能添加新构造器
  35. 35#derive(Eq, @debug.Debug)
  36. 36#non_exhaustive
  37. 37pub(all) enum ActionRecognitionAlgorithm {
  38. 38 C3D
  39. 39 I3D
  40. 40 TwoStream
  41. 41 TSN
  42. 42 TRN
  43. 43 SlowFast
  44. 44 TimeSformer
  45. 45 VideoMAE
  46. 46 Unknown
  47. 47}
  48. 48
  49. 49///|
  50. 50pub extend ActionRecognitionAlgorithm with @debug.Debug::{to_repr}
  51. 51
  52. 52///|
  53. 53pub extend ActionRecognitionAlgorithm with Eq::{not_equal, equal}
  54. 54
  55. 55///|
  56. 56/// 人体关键点
  57. 57#derive(Eq, @debug.Debug)
  58. 58pub(all) struct Keypoint {
  59. 59 x : Double
  60. 60 y : Double
  61. 61 confidence : Double
  62. 62 is_visible : Bool
  63. 63 part_name : String
  64. 64}
  65. 65
  66. 66///|
  67. 67pub extend Keypoint with Eq::{not_equal, equal}
  68. 68
  69. 69///|
  70. 70pub extend Keypoint with @debug.Debug::{to_repr}
  71. 71
  72. 72///|
  73. 73/// 人体姿态
  74. 74#derive(Eq, @debug.Debug)
  75. 75pub(all) struct HumanPose {
  76. 76 keypoints : Array[Keypoint]
  77. 77 num_keypoints : Int32
  78. 78 bounding_box : Array[Double]
  79. 79 confidence : Double
  80. 80 pose_id : Int32
  81. 81 is_tracked : Bool
  82. 82}
  83. 83
  84. 84///|
  85. 85pub extend HumanPose with Eq::{not_equal, equal}
  86. 86
  87. 87///|
  88. 88pub extend HumanPose with @debug.Debug::{to_repr}
  89. 89
  90. 90///|
  91. 91/// 骨架连接
  92. 92#derive(Eq, @debug.Debug)
  93. 93pub(all) struct SkeletonConnection {
  94. 94 joint1 : Int32
  95. 95 joint2 : Int32
  96. 96 confidence : Double
  97. 97 length : Double
  98. 98 angle : Double
  99. 99}
  100. 100
  101. 101///|
  102. 102pub extend SkeletonConnection with Eq::{not_equal, equal}
  103. 103
  104. 104///|
  105. 105pub extend SkeletonConnection with @debug.Debug::{to_repr}
  106. 106
  107. 107///|
  108. 108/// 动作识别结果
  109. 109#derive(Eq, @debug.Debug)
  110. 110pub(all) struct ActionRecognitionResult {
  111. 111 action_name : String
  112. 112 confidence : Double
  113. 113 action_id : Int32
  114. 114 duration_frames : Int32
  115. 115 start_frame : Int32
  116. 116 end_frame : Int32
  117. 117 is_completed : Bool
  118. 118}
  119. 119
  120. 120///|
  121. 121pub extend ActionRecognitionResult with Eq::{not_equal, equal}
  122. 122
  123. 123///|
  124. 124pub extend ActionRecognitionResult with @debug.Debug::{to_repr}
  125. 125
  126. 126///|
  127. 127/// 手势类型
  128. 128/// `#non_exhaustive` 表示未来可能添加新构造器
  129. 129#derive(Eq, @debug.Debug)
  130. 130#non_exhaustive
  131. 131pub(all) enum GestureType {
  132. 132 OpenPalm
  133. 133 Fist
  134. 134 Pointing
  135. 135 Victory
  136. 136 ThumbsUp
  137. 137 ThumbsDown
  138. 138 OK
  139. 139 Peace
  140. 140 Rock
  141. 141 Unknown
  142. 142}
  143. 143
  144. 144///|
  145. 145pub extend GestureType with @debug.Debug::{to_repr}
  146. 146
  147. 147///|
  148. 148pub extend GestureType with Eq::{not_equal, equal}
  149. 149
  150. 150///|
  151. 151/// 手势识别结果
  152. 152#derive(Eq, @debug.Debug)
  153. 153pub(all) struct GestureRecognitionResult {
  154. 154 gesture_type : GestureType
  155. 155 confidence : Double
  156. 156 hand_keypoints : Array[Keypoint]
  157. 157 is_left_hand : Bool
  158. 158 bounding_box : Array[Double]
  159. 159}
  160. 160
  161. 161///|
  162. 162pub extend GestureRecognitionResult with Eq::{not_equal, equal}
  163. 163
  164. 164///|
  165. 165pub extend GestureRecognitionResult with @debug.Debug::{to_repr}
  166. 166
  167. 167///|
  168. 168/// 姿态估计配置
  169. 169#derive(Eq, @debug.Debug)
  170. 170pub(all) struct PoseEstimationConfig {
  171. 171 algorithm : PoseEstimationAlgorithm
  172. 172 num_keypoints : Int32
  173. 173 confidence_threshold : Double
  174. 174 nms_threshold : Double
  175. 175 max_num_people : Int32
  176. 176 use_tracking : Bool
  177. 177 use_smoothing : Bool
  178. 178 smoothing_factor : Double
  179. 179}
  180. 180
  181. 181///|
  182. 182pub extend PoseEstimationConfig with Eq::{not_equal, equal}
  183. 183
  184. 184///|
  185. 185pub extend PoseEstimationConfig with @debug.Debug::{to_repr}
  186. 186
  187. 187///|
  188. 188/// 动作识别配置
  189. 189#derive(Eq, @debug.Debug)
  190. 190pub(all) struct ActionRecognitionConfig {
  191. 191 algorithm : ActionRecognitionAlgorithm
  192. 192 num_classes : Int32
  193. 193 window_size : Int32
  194. 194 stride : Int32
  195. 195 confidence_threshold : Double
  196. 196 use_temporal_smoothing : Bool
  197. 197 smoothing_window : Int32
  198. 198}
  199. 199
  200. 200///|
  201. 201pub extend ActionRecognitionConfig with Eq::{not_equal, equal}
  202. 202
  203. 203///|
  204. 204pub extend ActionRecognitionConfig with @debug.Debug::{to_repr}
  205. 205
  206. 206///|
  207. 207/// 创建默认姿态估计配置
  208. 208pub fn create_default_pose_config() -> PoseEstimationConfig {
  209. 209● PoseEstimationConfig::{
  210. 210 algorithm: PoseEstimationAlgorithm::OpenPose,
  211. 211 num_keypoints: 17,
  212. 212 confidence_threshold: 0.5,
  213. 213 nms_threshold: 0.6,
  214. 214 max_num_people: 10,
  215. 215 use_tracking: true,
  216. 216 use_smoothing: true,
  217. 217 smoothing_factor: 0.8,
  218. 218 }
  219. 219}
  220. 220
  221. 221///|
  222. 222/// 创建默认动作识别配置
  223. 223pub fn create_default_action_config() -> ActionRecognitionConfig {
  224. 224● ActionRecognitionConfig::{
  225. 225 algorithm: ActionRecognitionAlgorithm::I3D,
  226. 226 num_classes: 400,
  227. 227 window_size: 16,
  228. 228 stride: 8,
  229. 229 confidence_threshold: 0.5,
  230. 230 use_temporal_smoothing: true,
  231. 231 smoothing_window: 5,
  232. 232 }
  233. 233}
  234. 234
  235. 235///|
  236. 236/// COCO 17 关键点名称
  237. 237pub fn get_coco_keypoint_names() -> Array[String] {
  238. 238● [
  239. 239 "nose", "left_eye", "right_eye", "left_ear", "right_ear", "left_shoulder", "right_shoulder",
  240. 240 "left_elbow", "right_elbow", "left_wrist", "right_wrist", "left_hip", "right_hip",
  241. 241 "left_knee", "right_knee", "left_ankle", "right_ankle",
  242. 242 ]
  243. 243}
  244. 244
  245. 245///|
  246. 246/// COCO 骨架连接对
  247. 247pub fn get_coco_skeleton_connections() -> Array[Array[Int32]] {
  248. 248● [
  249. 249 [0, 1],
  250. 250 [0, 2],
  251. 251 [1, 3],
  252. 252 [2, 4], // 头部
  253. 253 [5, 6],
  254. 254 [5, 7],
  255. 255 [7, 9],
  256. 256 [6, 8],
  257. 257 [8, 10], // 手臂
  258. 258 [5, 11],
  259. 259 [6, 12],
  260. 260 [11, 12], // 躯干
  261. 261 [11, 13],
  262. 262 [13, 15],
  263. 263 [12, 14],
  264. 264 [14, 16], // 腿部
  265. 265 ]
  266. 266}
  267. 267
  268. 268///|
  269. 269/// 检测人体关键点(简化版:基于图像特征的启发式检测)
  270. 270pub fn detect_keypoints(
  271. 271 img : @types.Image,
  272. 272 config : PoseEstimationConfig,
  273. 273) -> Array[HumanPose] raise @types.LoadError {
  274. 274● ignore(config)
  275. 275 // 输入验证
  276. 276 if img.width <= 0 || img.height <= 0 {
  277. 277● raise @types.LoadError::DecodeFailed(
  278. 278 "detect_keypoints: 图像尺寸必须大于0",
  279. 279 )
  280. 280 }
  281. 281 if img.channels < 1 || img.channels > 4 {
  282. 282● raise @types.LoadError::DecodeFailed(
  283. 283 "detect_keypoints: 通道数必须在1-4之间",
  284. 284 )
  285. 285 }
  286. 286 let expected_size = img.width * img.height * img.channels
  287. 287● if img.data.length() != expected_size {
  288. 288● raise @types.LoadError::DecodeFailed(
  289. 289 "detect_keypoints: 数据大小不匹配",
  290. 290 )
  291. 291 }
  292. 292 let w = img.width
  293. 293 let h = img.height
  294. 294 let poses : Array[HumanPose] = []
  295. 295
  296. 296 // 简化版:检测图像中的高对比度区域作为人体
  297. 297 // 实际实现应该使用深度学习模型
  298. 298
  299. 299 // 计算图像梯度
  300. 300 let mut max_grad = 0.0
  301. 301● let mut center_x = w.to_double() / 2.0
  302. 302● let mut center_y = h.to_double() / 2.0
  303. 303
  304. 304 for y in 1..<(h - 1) {
  305. 305● for x in 1..<(w - 1) {
  306. 306● let idx = y * w + x
  307. 307● let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
  308. 308● let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
  309. 309● let grad = (gx * gx + gy * gy).sqrt()
  310. 310 if grad > max_grad {
  311. 311● max_grad = grad
  312. 312● center_x = x.to_double()
  313. 313● center_y = y.to_double()
  314. 314 }
  315. 315 }
  316. 316 }
  317. 317
  318. 318 // 生成简化的人体姿态(17个关键点)
  319. 319● let keypoint_names = get_coco_keypoint_names()
  320. 320 let keypoints : Array[Keypoint] = []
  321. 321
  322. 322 // 头部关键点
  323. 323● keypoints.push(Keypoint::{
  324. 324 x: center_x,
  325. 325● y: center_y - h.to_double() * 0.3,
  326. 326 confidence: 0.8,
  327. 327 is_visible: true,
  328. 328 part_name: keypoint_names[0],
  329. 329 })
  330. 330● keypoints.push(Keypoint::{
  331. 331 x: center_x - 10.0,
  332. 332● y: center_y - h.to_double() * 0.32,
  333. 333 confidence: 0.7,
  334. 334 is_visible: true,
  335. 335 part_name: keypoint_names[1],
  336. 336 })
  337. 337● keypoints.push(Keypoint::{
  338. 338 x: center_x + 10.0,
  339. 339● y: center_y - h.to_double() * 0.32,
  340. 340 confidence: 0.7,
  341. 341 is_visible: true,
  342. 342 part_name: keypoint_names[2],
  343. 343 })
  344. 344● keypoints.push(Keypoint::{
  345. 345 x: center_x - 15.0,
  346. 346● y: center_y - h.to_double() * 0.3,
  347. 347 confidence: 0.6,
  348. 348 is_visible: true,
  349. 349 part_name: keypoint_names[3],
  350. 350 })
  351. 351● keypoints.push(Keypoint::{
  352. 352 x: center_x + 15.0,
  353. 353● y: center_y - h.to_double() * 0.3,
  354. 354 confidence: 0.6,
  355. 355 is_visible: true,
  356. 356 part_name: keypoint_names[4],
  357. 357 })
  358. 358
  359. 359 // 躯干关键点
  360. 360● keypoints.push(Keypoint::{
  361. 361 x: center_x - 30.0,
  362. 362● y: center_y - h.to_double() * 0.15,
  363. 363 confidence: 0.8,
  364. 364 is_visible: true,
  365. 365 part_name: keypoint_names[5],
  366. 366 })
  367. 367● keypoints.push(Keypoint::{
  368. 368 x: center_x + 30.0,
  369. 369● y: center_y - h.to_double() * 0.15,
  370. 370 confidence: 0.8,
  371. 371 is_visible: true,
  372. 372 part_name: keypoint_names[6],
  373. 373 })
  374. 374
  375. 375 // 手臂关键点
  376. 376● keypoints.push(Keypoint::{
  377. 377 x: center_x - 50.0,
  378. 378 y: center_y,
  379. 379 confidence: 0.7,
  380. 380 is_visible: true,
  381. 381 part_name: keypoint_names[7],
  382. 382 })
  383. 383● keypoints.push(Keypoint::{
  384. 384 x: center_x + 50.0,
  385. 385 y: center_y,
  386. 386 confidence: 0.7,
  387. 387 is_visible: true,
  388. 388 part_name: keypoint_names[8],
  389. 389 })
  390. 390● keypoints.push(Keypoint::{
  391. 391 x: center_x - 60.0,
  392. 392● y: center_y + h.to_double() * 0.1,
  393. 393 confidence: 0.6,
  394. 394 is_visible: true,
  395. 395 part_name: keypoint_names[9],
  396. 396 })
  397. 397● keypoints.push(Keypoint::{
  398. 398 x: center_x + 60.0,
  399. 399● y: center_y + h.to_double() * 0.1,
  400. 400 confidence: 0.6,
  401. 401 is_visible: true,
  402. 402 part_name: keypoint_names[10],
  403. 403 })
  404. 404
  405. 405 // 髋部关键点
  406. 406● keypoints.push(Keypoint::{
  407. 407 x: center_x - 20.0,
  408. 408● y: center_y + h.to_double() * 0.15,
  409. 409 confidence: 0.8,
  410. 410 is_visible: true,
  411. 411 part_name: keypoint_names[11],
  412. 412 })
  413. 413● keypoints.push(Keypoint::{
  414. 414 x: center_x + 20.0,
  415. 415● y: center_y + h.to_double() * 0.15,
  416. 416 confidence: 0.8,
  417. 417 is_visible: true,
  418. 418 part_name: keypoint_names[12],
  419. 419 })
  420. 420
  421. 421 // 腿部关键点
  422. 422● keypoints.push(Keypoint::{
  423. 423 x: center_x - 25.0,
  424. 424● y: center_y + h.to_double() * 0.3,
  425. 425 confidence: 0.7,
  426. 426 is_visible: true,
  427. 427 part_name: keypoint_names[13],
  428. 428 })
  429. 429● keypoints.push(Keypoint::{
  430. 430 x: center_x + 25.0,
  431. 431● y: center_y + h.to_double() * 0.3,
  432. 432 confidence: 0.7,
  433. 433 is_visible: true,
  434. 434 part_name: keypoint_names[14],
  435. 435 })
  436. 436● keypoints.push(Keypoint::{
  437. 437 x: center_x - 30.0,
  438. 438● y: center_y + h.to_double() * 0.45,
  439. 439 confidence: 0.6,
  440. 440 is_visible: true,
  441. 441 part_name: keypoint_names[15],
  442. 442 })
  443. 443● keypoints.push(Keypoint::{
  444. 444 x: center_x + 30.0,
  445. 445● y: center_y + h.to_double() * 0.45,
  446. 446 confidence: 0.6,
  447. 447 is_visible: true,
  448. 448 part_name: keypoint_names[16],
  449. 449 })
  450. 450
  451. 451 let pose = HumanPose::{
  452. 452 keypoints,
  453. 453 num_keypoints: 17,
  454. 454 bounding_box: [
  455. 455 center_x - 80.0,
  456. 456● center_y - h.to_double() * 0.4,
  457. 457 160.0,
  458. 458● h.to_double() * 0.9,
  459. 459 ],
  460. 460 confidence: 0.75,
  461. 461 pose_id: 0,
  462. 462 is_tracked: true,
  463. 463 }
  464. 464
  465. 465● poses.push(pose)
  466. 466 poses
  467. 467}
  468. 468
  469. 469///|
  470. 470/// 提取骨架连接
  471. 471pub fn extract_skeleton(pose : HumanPose) -> Array[SkeletonConnection] {
  472. 472● let connections = get_coco_skeleton_connections()
  473. 473 let skeleton : Array[SkeletonConnection] = []
  474. 474
  475. 475 for conn in connections {
  476. 476● let j1 = conn[0]
  477. 477 let j2 = conn[1]
  478. 478● if j1 < pose.keypoints.length() && j2 < pose.keypoints.length() {
  479. 479● let k1 = pose.keypoints[j1]
  480. 480 let k2 = pose.keypoints[j2]
  481. 481 let dx = k2.x - k1.x
  482. 482 let dy = k2.y - k1.y
  483. 483● let length = (dx * dx + dy * dy).sqrt()
  484. 484 let angle = if length > 0.0 {
  485. 485● let sin_val = dy / length
  486. 486 // 简化的反正弦近似
  487. 487 sin_val * 57.3
  488. 488 } else {
  489. 489○ 0.0
  490. 490 }
  491. 491● skeleton.push(SkeletonConnection::{
  492. 492 joint1: j1,
  493. 493 joint2: j2,
  494. 494 confidence: (k1.confidence + k2.confidence) / 2.0,
  495. 495 length,
  496. 496 angle,
  497. 497 })
  498. 498 }
  499. 499 }
  500. 500
  501. 501 skeleton
  502. 502}
  503. 503
  504. 504///|
  505. 505/// 计算关节角度
  506. 506pub fn calculate_joint_angle(
  507. 507 pose : HumanPose,
  508. 508 joint1 : Int32,
  509. 509 joint_center : Int32,
  510. 510 joint2 : Int32,
  511. 511) -> Double {
  512. 512● if joint1 < pose.keypoints.length() &&
  513. 513● joint_center < pose.keypoints.length() &&
  514. 514● joint2 < pose.keypoints.length() {
  515. 515● let k1 = pose.keypoints[joint1]
  516. 516 let kc = pose.keypoints[joint_center]
  517. 517 let k2 = pose.keypoints[joint2]
  518. 518
  519. 519 let v1x = k1.x - kc.x
  520. 520 let v1y = k1.y - kc.y
  521. 521 let v2x = k2.x - kc.x
  522. 522 let v2y = k2.y - kc.y
  523. 523
  524. 524 let dot = v1x * v2x + v1y * v2y
  525. 525● let len1 = (v1x * v1x + v1y * v1y).sqrt()
  526. 526● let len2 = (v2x * v2x + v2y * v2y).sqrt()
  527. 527
  528. 528 if len1 > 0.0 && len2 > 0.0 {
  529. 529● let cos_angle = dot / (len1 * len2)
  530. 530 let clamped_cos = if cos_angle < -1.0 {
  531. 531○ -1.0
  532. 532● } else if cos_angle > 1.0 {
  533. 533○ 1.0
  534. 534 } else {
  535. 535● cos_angle
  536. 536 }
  537. 537 // 简化的反余弦
  538. 538 (1.0 - clamped_cos) * 90.0
  539. 539 } else {
  540. 540○ 0.0
  541. 541 }
  542. 542 } else {
  543. 543● 0.0
  544. 544 }
  545. 545}
  546. 546
  547. 547///|
  548. 548/// 识别动作(简化版:基于关节角度变化)
  549. 549pub fn recognize_action(
  550. 550 poses : Array[HumanPose],
  551. 551 config : ActionRecognitionConfig,
  552. 552) -> ActionRecognitionResult {
  553. 553● ignore(config)
  554. 554● if poses.length() == 0 {
  555. 555● return ActionRecognitionResult::{
  556. 556 action_name: "unknown",
  557. 557 confidence: 0.0,
  558. 558 action_id: -1,
  559. 559 duration_frames: 0,
  560. 560 start_frame: 0,
  561. 561 end_frame: 0,
  562. 562 is_completed: false,
  563. 563 }
  564. 564 }
  565. 565
  566. 566 let pose = poses[0]
  567. 567
  568. 568 // 计算关键关节角度
  569. 569● let left_elbow_angle = calculate_joint_angle(pose, 5, 7, 9) // 左肩-左肘-左腕
  570. 570● let right_elbow_angle = calculate_joint_angle(pose, 6, 8, 10) // 右肩-右肘-右腕
  571. 571● let left_knee_angle = calculate_joint_angle(pose, 11, 13, 15) // 左髋-左膝-左踝
  572. 572● let right_knee_angle = calculate_joint_angle(pose, 12, 14, 16) // 右髋-右膝-右踝
  573. 573
  574. 574 // 简化的动作分类
  575. 575 let mut action_name = "walking"
  576. 576 let mut confidence = 0.5
  577. 577
  578. 578 if left_elbow_angle < 60.0 && right_elbow_angle < 60.0 {
  579. 579○ action_name = "arms_crossed"
  580. 580 confidence = 0.7
  581. 581● } else if left_elbow_angle > 150.0 && right_elbow_angle > 150.0 {
  582. 582● action_name = "arms_raised"
  583. 583 confidence = 0.75
  584. 584○ } else if left_knee_angle < 90.0 || right_knee_angle < 90.0 {
  585. 585○ action_name = "squatting"
  586. 586 confidence = 0.65
  587. 587○ } else if left_knee_angle > 160.0 && right_knee_angle > 160.0 {
  588. 588○ action_name = "standing"
  589. 589 confidence = 0.8
  590. 590 } else {
  591. 591○ action_name = "walking"
  592. 592 confidence = 0.5
  593. 593 }
  594. 594
  595. 595 ActionRecognitionResult::{
  596. 596 action_name,
  597. 597 confidence,
  598. 598 action_id: 0,
  599. 599● duration_frames: poses.length(),
  600. 600 start_frame: 0,
  601. 601● end_frame: poses.length() - 1,
  602. 602 is_completed: true,
  603. 603 }
  604. 604}
  605. 605
  606. 606///|
  607. 607/// 识别手势(简化版:基于手部关键点)
  608. 608pub fn recognize_gesture(pose : HumanPose) -> GestureRecognitionResult {
  609. 609 // 简化版:基于手腕位置和手臂角度判断手势
  610. 610● let left_wrist = if 9 < pose.keypoints.length() {
  611. 611● pose.keypoints[9]
  612. 612 } else {
  613. 613○ Keypoint::{
  614. 614 x: 0.0,
  615. 615 y: 0.0,
  616. 616 confidence: 0.0,
  617. 617 is_visible: false,
  618. 618 part_name: "",
  619. 619 }
  620. 620 }
  621. 621● let right_wrist = if 10 < pose.keypoints.length() {
  622. 622● pose.keypoints[10]
  623. 623 } else {
  624. 624○ Keypoint::{
  625. 625 x: 0.0,
  626. 626 y: 0.0,
  627. 627 confidence: 0.0,
  628. 628 is_visible: false,
  629. 629 part_name: "",
  630. 630 }
  631. 631 }
  632. 632
  633. 633● let left_shoulder = if 5 < pose.keypoints.length() {
  634. 634● pose.keypoints[5]
  635. 635 } else {
  636. 636○ Keypoint::{
  637. 637 x: 0.0,
  638. 638 y: 0.0,
  639. 639 confidence: 0.0,
  640. 640 is_visible: false,
  641. 641 part_name: "",
  642. 642 }
  643. 643 }
  644. 644● let right_shoulder = if 6 < pose.keypoints.length() {
  645. 645● pose.keypoints[6]
  646. 646 } else {
  647. 647○ Keypoint::{
  648. 648 x: 0.0,
  649. 649 y: 0.0,
  650. 650 confidence: 0.0,
  651. 651 is_visible: false,
  652. 652 part_name: "",
  653. 653 }
  654. 654 }
  655. 655
  656. 656 // 简化的手势分类
  657. 657 let mut gesture_type = GestureType::Unknown
  658. 658 let mut confidence = 0.3
  659. 659 let mut is_left_hand = false
  660. 660
  661. 661 let left_arm_up = left_wrist.y < left_shoulder.y
  662. 662 let right_arm_up = right_wrist.y < right_shoulder.y
  663. 663
  664. 664 if right_arm_up && !left_arm_up {
  665. 665○ gesture_type = GestureType::Pointing
  666. 666 confidence = 0.6
  667. 667 is_left_hand = false
  668. 668● } else if left_arm_up && right_arm_up {
  669. 669○ gesture_type = GestureType::Victory
  670. 670 confidence = 0.55
  671. 671 is_left_hand = false
  672. 672● } else if !left_arm_up && !right_arm_up {
  673. 673● gesture_type = GestureType::OpenPalm
  674. 674 confidence = 0.5
  675. 675 is_left_hand = false
  676. 676 } else {
  677. 677○ gesture_type = GestureType::Unknown
  678. 678 confidence = 0.3
  679. 679 is_left_hand = false
  680. 680 }
  681. 681
  682. 682 GestureRecognitionResult::{
  683. 683 gesture_type,
  684. 684 confidence,
  685. 685 hand_keypoints: [],
  686. 686 is_left_hand,
  687. 687 bounding_box: [0.0, 0.0, 100.0, 100.0],
  688. 688 }
  689. 689}
  690. 690
  691. 691///|
  692. 692/// 可视化姿态(在图像上绘制骨架)
  693. 693pub fn visualize_pose(
  694. 694 img : @types.Image,
  695. 695 pose : HumanPose,
  696. 696) -> @types.Image raise @types.LoadError {
  697. 697 // 输入验证
  698. 698● if img.width <= 0 || img.height <= 0 {
  699. 699● raise @types.LoadError::DecodeFailed(
  700. 700 "visualize_pose: 图像尺寸必须大于0",
  701. 701 )
  702. 702 }
  703. 703 if img.channels < 1 || img.channels > 4 {
  704. 704● raise @types.LoadError::DecodeFailed(
  705. 705 "visualize_pose: 通道数必须在1-4之间",
  706. 706 )
  707. 707 }
  708. 708 let expected_size = img.width * img.height * img.channels
  709. 709● if img.data.length() != expected_size {
  710. 710● raise @types.LoadError::DecodeFailed(
  711. 711 "visualize_pose: 数据大小不匹配",
  712. 712 )
  713. 713 }
  714. 714 let w = img.width
  715. 715 let h = img.height
  716. 716 let total = w * h
  717. 717 let out : Array[Byte] = []
  718. 718
  719. 719 // 复制原图
  720. 720 for i in 0..<total {
  721. 721● out.push(img.data[i])
  722. 722 }
  723. 723
  724. 724 // 绘制关键点(红色圆点)
  725. 725 for kp in pose.keypoints {
  726. 726● if kp.confidence > 0.5 && kp.is_visible {
  727. 727● let cx = kp.x.to_int()
  728. 728● let cy = kp.y.to_int()
  729. 729 for dy in -3..<4 {
  730. 730● for dx in -3..<4 {
  731. 731● let nx = cx + dx
  732. 732 let ny = cy + dy
  733. 733 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  734. 734● out[ny * w + nx] = b'\xFF' // 红色
  735. 735 }
  736. 736 }
  737. 737 }
  738. 738 }
  739. 739 }
  740. 740
  741. 741 // 绘制骨架连接(绿色线条)
  742. 742● let skeleton = extract_skeleton(pose)
  743. 743 for conn in skeleton {
  744. 744● if conn.confidence > 0.5 {
  745. 745● let k1 = pose.keypoints[conn.joint1]
  746. 746 let k2 = pose.keypoints[conn.joint2]
  747. 747● let x1 = k1.x.to_int()
  748. 748● let y1 = k1.y.to_int()
  749. 749● let x2 = k2.x.to_int()
  750. 750● let y2 = k2.y.to_int()
  751. 751
  752. 752 // 简单的线条绘制
  753. 753 let steps = 20
  754. 754 for i in 0..<steps {
  755. 755● let t = i.to_double() / steps.to_double()
  756. 756● let x = (x1.to_double() + (x2 - x1).to_double() * t).to_int()
  757. 757● let y = (y1.to_double() + (y2 - y1).to_double() * t).to_int()
  758. 758 if x >= 0 && x < w && y >= 0 && y < h {
  759. 759● out[y * w + x] = b'\x00' // 绿色(简化版)
  760. 760 }
  761. 761 }
  762. 762 }
  763. 763 }
  764. 764
  765. 765 @types.Image::{
  766. 766 width: w,
  767. 767 height: h,
  768. 768 channels: img.channels,
  769. 769● data: Bytes::from_array(out),
  770. 770 }
  771. 771}
  772. 772
  773. 773///|
  774. 774/// 生成姿态估计报告
  775. 775pub fn pose_estimation_report_to_string(
  776. 776 poses : Array[HumanPose],
  777. 777 action_result : ActionRecognitionResult,
  778. 778) -> String {
  779. 779● let mut report = "## 人体姿态估计与动作识别报告\n\n"
  780. 780 report = report + "### 检测结果\n\n"
  781. 781 report = report + "| 指标 | 值 |\n|------|------|\n"
  782. 782● report = report + "| 检测人数 | " + poses.length().to_string() + " |\n"
  783. 783
  784. 784● if poses.length() > 0 {
  785. 785● let pose = poses[0]
  786. 786 report = report +
  787. 787 "| 关键点数量 | " +
  788. 788● pose.num_keypoints.to_string() +
  789. 789 " |\n"
  790. 790 report = report +
  791. 791 "| 姿态置信度 | " +
  792. 792● (pose.confidence * 100.0).to_string() +
  793. 793 "% |\n"
  794. 794 report = report +
  795. 795 "| 跟踪状态 | " +
  796. 796◐ (if pose.is_tracked { "跟踪中" } else { "未跟踪" }) +
  797. 797 " |\n\n"
  798. 798
  799. 799 // 关键点详情
  800. 800 report = report + "### 关键点详情\n\n"
  801. 801 report = report +
  802. 802 "| 部位 | X | Y | 置信度 | 可见 |\n|------|------|------|------|------|\n"
  803. 803 for kp in pose.keypoints {
  804. 804● report = report +
  805. 805 "| " +
  806. 806 kp.part_name +
  807. 807 " | " +
  808. 808● kp.x.to_string() +
  809. 809 " | " +
  810. 810● kp.y.to_string() +
  811. 811 " | " +
  812. 812● (kp.confidence * 100.0).to_string() +
  813. 813 "% | " +
  814. 814◐ (if kp.is_visible { "是" } else { "否" }) +
  815. 815 " |\n"
  816. 816 }
  817. 817
  818. 818 // 动作识别结果
  819. 819 report = report + "\n### 动作识别结果\n\n"
  820. 820 report = report + "| 指标 | 值 |\n|------|------|\n"
  821. 821 report = report + "| 动作名称 | " + action_result.action_name + " |\n"
  822. 822 report = report +
  823. 823 "| 置信度 | " +
  824. 824● (action_result.confidence * 100.0).to_string() +
  825. 825 "% |\n"
  826. 826 report = report +
  827. 827 "| 持续帧数 | " +
  828. 828● action_result.duration_frames.to_string() +
  829. 829 " |\n"
  830. 830 report = report +
  831. 831 "| 完成状态 | " +
  832. 832◐ (if action_result.is_completed { "已完成" } else { "进行中" }) +
  833. 833 " |\n"
  834. 834 }
  835. 835
  836. 836 report
  837. 837}