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