image.vision\face_recognition\face_recognition_system.mbt
91.3% · 209 / 229 points · 948 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 人脸识别系统 face_recognition/face_recognition_system - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 人脸识别与表情识别(v33.0.0) - 7
// 人脸检测、人脸对齐、特征提取、人脸识别、表情识别、年龄性别估计 - 8
- 9
///| - 10
/// 人脸检测算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum FaceDetectionAlgorithm { - 15
HaarCascade - 16
HOG_SVM - 17
MTCNN - 18
RetinaFace - 19
YOLOFace - 20
BlazeFace - 21
FaceBoxes - 22
Unknown - 23
} - 24
- 25
///| - 26
pub extend FaceDetectionAlgorithm with @debug.Debug::{to_repr} - 27
- 28
///| - 29
pub extend FaceDetectionAlgorithm with Eq::{not_equal, equal} - 30
- 31
///| - 32
/// 人脸识别算法类型 - 33
/// `#non_exhaustive` 表示未来可能添加新构造器 - 34
#derive(Eq, @debug.Debug) - 35
#non_exhaustive - 36
pub(all) enum FaceRecognitionAlgorithm { - 37
FaceNet - 38
ArcFace - 39
CosFace - 40
SphereFace - 41
DeepFace - 42
VGGFace - 43
InsightFace - 44
Unknown - 45
} - 46
- 47
///| - 48
pub extend FaceRecognitionAlgorithm with @debug.Debug::{to_repr} - 49
- 50
///| - 51
pub extend FaceRecognitionAlgorithm with Eq::{not_equal, equal} - 52
- 53
///| - 54
/// 表情类型 - 55
/// `#non_exhaustive` 表示未来可能添加新构造器 - 56
#derive(Eq, @debug.Debug) - 57
#non_exhaustive - 58
pub(all) enum ExpressionType { - 59
Neutral - 60
Happy - 61
Sad - 62
Angry - 63
Surprise - 64
Fear - 65
Disgust - 66
Contempt - 67
Unknown - 68
} - 69
- 70
///| - 71
pub extend ExpressionType with @debug.Debug::{to_repr} - 72
- 73
///| - 74
pub extend ExpressionType with Eq::{not_equal, equal} - 75
- 76
///| - 77
/// 人脸检测结果 - 78
#derive(Eq, @debug.Debug) - 79
pub(all) struct FaceDetectionResult { - 80
bounding_box : Array[Double] - 81
confidence : Double - 82
landmarks : Array[Array[Double]] - 83
num_landmarks : Int32 - 84
face_id : Int32 - 85
is_frontal : Bool - 86
yaw : Double - 87
pitch : Double - 88
roll : Double - 89
} - 90
- 91
///| - 92
pub extend FaceDetectionResult with Eq::{not_equal, equal} - 93
- 94
///| - 95
pub extend FaceDetectionResult with @debug.Debug::{to_repr} - 96
- 97
///| - 98
/// 人脸特征向量 - 99
#derive(Eq, @debug.Debug) - 100
pub(all) struct FaceFeature { - 101
vector : Array[Double] - 102
dimension : Int32 - 103
norm : Double - 104
is_normalized : Bool - 105
} - 106
- 107
///| - 108
pub extend FaceFeature with Eq::{not_equal, equal} - 109
- 110
///| - 111
pub extend FaceFeature with @debug.Debug::{to_repr} - 112
- 113
///| - 114
/// 人脸识别结果 - 115
#derive(Eq, @debug.Debug) - 116
pub(all) struct FaceRecognitionResult { - 117
face_id : Int32 - 118
identity : String - 119
confidence : Double - 120
similarity : Double - 121
is_known : Bool - 122
matched_face_id : Int32 - 123
bounding_box : Array[Double] - 124
} - 125
- 126
///| - 127
pub extend FaceRecognitionResult with Eq::{not_equal, equal} - 128
- 129
///| - 130
pub extend FaceRecognitionResult with @debug.Debug::{to_repr} - 131
- 132
///| - 133
/// 表情识别结果 - 134
#derive(Eq, @debug.Debug) - 135
pub(all) struct ExpressionRecognitionResult { - 136
expression : ExpressionType - 137
confidence : Double - 138
probabilities : Array[Double] - 139
num_classes : Int32 - 140
dominant_expression : String - 141
} - 142
- 143
///| - 144
pub extend ExpressionRecognitionResult with Eq::{not_equal, equal} - 145
- 146
///| - 147
pub extend ExpressionRecognitionResult with @debug.Debug::{to_repr} - 148
- 149
///| - 150
/// 年龄性别估计结果 - 151
#derive(Eq, @debug.Debug) - 152
pub(all) struct AgeGenderEstimationResult { - 153
age : Double - 154
age_range : Array[Double] - 155
gender : String - 156
gender_confidence : Double - 157
is_male : Bool - 158
} - 159
- 160
///| - 161
pub extend AgeGenderEstimationResult with Eq::{not_equal, equal} - 162
- 163
///| - 164
pub extend AgeGenderEstimationResult with @debug.Debug::{to_repr} - 165
- 166
///| - 167
/// 人脸属性分析结果 - 168
#derive(Eq, @debug.Debug) - 169
pub(all) struct FaceAttributeAnalysis { - 170
detection : FaceDetectionResult - 171
recognition : FaceRecognitionResult - 172
expression : ExpressionRecognitionResult - 173
age_gender : AgeGenderEstimationResult - 174
face_quality : Double - 175
brightness : Double - 176
sharpness : Double - 177
occlusion_rate : Double - 178
} - 179
- 180
///| - 181
pub extend FaceAttributeAnalysis with Eq::{not_equal, equal} - 182
- 183
///| - 184
pub extend FaceAttributeAnalysis with @debug.Debug::{to_repr} - 185
- 186
///| - 187
/// 人脸库 - 188
#derive(Eq, @debug.Debug) - 189
pub(all) struct FaceDatabase { - 190
faces : Array[FaceFeature] - 191
identities : Array[String] - 192
num_faces : Int32 - 193
threshold : Double - 194
algorithm : FaceRecognitionAlgorithm - 195
} - 196
- 197
///| - 198
pub extend FaceDatabase with Eq::{not_equal, equal} - 199
- 200
///| - 201
pub extend FaceDatabase with @debug.Debug::{to_repr} - 202
- 203
///| - 204
/// 人脸识别配置 - 205
#derive(Eq, @debug.Debug) - 206
pub(all) struct FaceRecognitionConfig { - 207
detection_algorithm : FaceDetectionAlgorithm - 208
recognition_algorithm : FaceRecognitionAlgorithm - 209
detection_threshold : Double - 210
recognition_threshold : Double - 211
min_face_size : Int32 - 212
max_num_faces : Int32 - 213
use_landmarks : Bool - 214
use_alignment : Bool - 215
feature_dimension : Int32 - 216
} - 217
- 218
///| - 219
pub extend FaceRecognitionConfig with Eq::{not_equal, equal} - 220
- 221
///| - 222
pub extend FaceRecognitionConfig with @debug.Debug::{to_repr} - 223
- 224
///| - 225
/// 创建默认人脸识别配置 - 226
pub fn create_default_face_config() -> FaceRecognitionConfig { - 227●
FaceRecognitionConfig::{ - 228
detection_algorithm: FaceDetectionAlgorithm::MTCNN, - 229
recognition_algorithm: FaceRecognitionAlgorithm::ArcFace, - 230
detection_threshold: 0.7, - 231
recognition_threshold: 0.5, - 232
min_face_size: 40, - 233
max_num_faces: 10, - 234
use_landmarks: true, - 235
use_alignment: true, - 236
feature_dimension: 512, - 237
} - 238
} - 239
- 240
///| - 241
/// 创建空的人脸库 - 242
pub fn create_face_database( - 243
algorithm : FaceRecognitionAlgorithm, - 244
threshold : Double, - 245
) -> FaceDatabase { - 246●
FaceDatabase::{ - 247
faces: [], - 248
identities: [], - 249
num_faces: 0, - 250
threshold, - 251
algorithm, - 252
} - 253
} - 254
- 255
///| - 256
/// 检测人脸(简化版:基于肤色和图像特征的启发式检测) - 257
pub fn detect_faces( - 258
img : @types.Image, - 259
config : FaceRecognitionConfig, - 260
) -> Array[FaceDetectionResult] raise @types.LoadError { - 261●
ignore(config) - 262
// 输入验证 - 263
if img.width <= 0 || img.height <= 0 { - 264●
raise @types.LoadError::DecodeFailed( - 265
"detect_faces: 图像尺寸必须大于0", - 266
) - 267
} - 268
if img.channels < 1 || img.channels > 4 { - 269●
raise @types.LoadError::DecodeFailed( - 270
"detect_faces: 通道数必须在1-4之间", - 271
) - 272
} - 273
let expected_size = img.width * img.height * img.channels - 274●
if img.data.length() != expected_size { - 275●
raise @types.LoadError::DecodeFailed("detect_faces: 数据大小不匹配") - 276
} - 277
let w = img.width - 278
let h = img.height - 279
let faces : Array[FaceDetectionResult] = [] - 280
- 281
// 简化版:检测图像中的高对比度区域作为人脸 - 282
// 实际实现应该使用深度学习模型或 Haar 级联分类器 - 283
- 284
// 计算图像梯度,寻找类人脸区域 - 285
let mut max_response = 0.0 - 286●
let mut center_x = w.to_double() / 2.0 - 287●
let mut center_y = h.to_double() / 2.0 - 288
- 289
let block_size = 32 - 290
for by in 0..<(h / block_size) { - 291●
for bx in 0..<(w / block_size) { - 292●
let mut response = 0.0 - 293
for y in 0..<block_size { - 294●
for x in 0..<block_size { - 295●
let px = bx * block_size + x - 296
let py = by * block_size + y - 297
if px > 0 && px < w - 1 && py > 0 && py < h - 1 { - 298●
let idx = py * w + px - 299●
let gx = img.data[idx + 1].to_double() - - 300●
img.data[idx - 1].to_double() - 301●
let gy = img.data[idx + w].to_double() - - 302●
img.data[idx - w].to_double() - 303●
response += (gx * gx + gy * gy).sqrt() - 304
} - 305
} - 306
} - 307●
response /= (block_size * block_size).to_double() - 308
if response > max_response { - 309●
max_response = response - 310●
center_x = (bx * block_size + block_size / 2).to_double() - 311●
center_y = (by * block_size + block_size / 2).to_double() - 312
} - 313
} - 314
} - 315
- 316
// 生成简化的人脸检测结果 - 317
let face_width = 100.0 - 318
let face_height = 120.0 - 319
let bounding_box = [ - 320
center_x - face_width / 2.0, - 321
center_y - face_height / 2.0, - 322
face_width, - 323
face_height, - 324
] - 325
- 326
// 5 个关键点(双眼、鼻尖、嘴角) - 327
let landmarks : Array[Array[Double]] = [] - 328●
landmarks.push([center_x - 20.0, center_y - 20.0]) // 左眼 - 329●
landmarks.push([center_x + 20.0, center_y - 20.0]) // 右眼 - 330●
landmarks.push([center_x, center_y]) // 鼻尖 - 331●
landmarks.push([center_x - 15.0, center_y + 25.0]) // 左嘴角 - 332●
landmarks.push([center_x + 15.0, center_y + 25.0]) // 右嘴角 - 333
- 334
let face = FaceDetectionResult::{ - 335
bounding_box, - 336
confidence: 0.75, - 337
landmarks, - 338
num_landmarks: 5, - 339
face_id: 0, - 340
is_frontal: true, - 341
yaw: 0.0, - 342
pitch: 0.0, - 343
roll: 0.0, - 344
} - 345
- 346●
faces.push(face) - 347
faces - 348
} - 349
- 350
///| - 351
/// 提取人脸特征(简化版:基于图像统计特征) - 352
pub fn extract_face_feature( - 353
img : @types.Image, - 354
face : FaceDetectionResult, - 355
config : FaceRecognitionConfig, - 356
) -> FaceFeature raise @types.LoadError { - 357
// 输入验证 - 358●
if img.width <= 0 || img.height <= 0 { - 359●
raise @types.LoadError::DecodeFailed( - 360
"extract_face_feature: 图像尺寸必须大于0", - 361
) - 362
} - 363
if img.channels < 1 || img.channels > 4 { - 364●
raise @types.LoadError::DecodeFailed( - 365
"extract_face_feature: 通道数必须在1-4之间", - 366
) - 367
} - 368
let expected_size = img.width * img.height * img.channels - 369●
if img.data.length() != expected_size { - 370●
raise @types.LoadError::DecodeFailed( - 371
"extract_face_feature: 数据大小不匹配", - 372
) - 373
} - 374
let w = img.width - 375
let h = img.height - 376
let dimension = config.feature_dimension - 377
let vector : Array[Double] = [] - 378
- 379
// 简化版:使用人脸区域的图像统计特征作为特征向量 - 380
// 实际实现应该使用深度学习模型(FaceNet/ArcFace 等) - 381
- 382●
let bx = face.bounding_box[0].to_int() - 383●
let by = face.bounding_box[1].to_int() - 384●
let bw = face.bounding_box[2].to_int() - 385●
let bh = face.bounding_box[3].to_int() - 386
- 387
// 计算人脸区域的统计特征 - 388
let mut sum = 0.0 - 389
let mut sum_sq = 0.0 - 390
let mut count = 0 - 391
for y in 0..<bh { - 392●
for x in 0..<bw { - 393●
let px = bx + x - 394
let py = by + y - 395
if px >= 0 && px < w && py >= 0 && py < h { - 396●
let pixel = img.data[py * w + px].to_double() - 397
sum += pixel - 398
sum_sq += pixel * pixel - 399
count += 1 - 400
} - 401
} - 402
} - 403
- 404●
let mean = if count > 0 { sum / count.to_double() } else { 0.0 } - 405
let variance = if count > 0 { - 406●
sum_sq / count.to_double() - mean * mean - 407
} else { - 408●
0.0 - 409
} - 410●
let std_dev = if variance > 0.0 { variance.sqrt() } else { 0.0 } - 411
- 412
// 生成特征向量(简化版:用统计特征填充) - 413
for i in 0..<dimension { - 414●
let value = mean * 0.01 + std_dev * 0.001 + (i % 10).to_double() * 0.001 - 415●
vector.push(value) - 416
} - 417
- 418
// 计算 L2 范数 - 419
let mut norm_sq = 0.0 - 420
for v in vector { - 421●
norm_sq += v * v - 422
} - 423◐
let norm = if norm_sq > 0.0 { norm_sq.sqrt() } else { 1.0 } - 424
- 425
FaceFeature::{ vector, dimension, norm, is_normalized: false, } - 426
} - 427
- 428
///| - 429
/// 计算余弦相似度 - 430
pub fn calculate_cosine_similarity( - 431
feat1 : FaceFeature, - 432
feat2 : FaceFeature, - 433
) -> Double { - 434●
if feat1.dimension != feat2.dimension { - 435●
return 0.0 - 436
} - 437
- 438
let mut dot = 0.0 - 439
for i in 0..<feat1.dimension { - 440●
dot += feat1.vector[i] * feat2.vector[i] - 441
} - 442
- 443
if feat1.norm > 0.0 && feat2.norm > 0.0 { - 444●
dot / (feat1.norm * feat2.norm) - 445
} else { - 446●
0.0 - 447
} - 448
} - 449
- 450
///| - 451
/// 向人脸库添加人脸 - 452
pub fn add_face_to_database( - 453
database : FaceDatabase, - 454
feature : FaceFeature, - 455
identity : String, - 456
) -> FaceDatabase { - 457●
let new_faces = database.faces - 458●
new_faces.push(feature) - 459
let new_identities = database.identities - 460●
new_identities.push(identity) - 461
- 462
FaceDatabase::{ - 463
faces: new_faces, - 464
identities: new_identities, - 465
num_faces: database.num_faces + 1, - 466
threshold: database.threshold, - 467
algorithm: database.algorithm, - 468
} - 469
} - 470
- 471
///| - 472
/// 在人脸库中识别人脸 - 473
pub fn recognize_face_in_database( - 474
database : FaceDatabase, - 475
feature : FaceFeature, - 476
bounding_box : Array[Double], - 477
) -> FaceRecognitionResult { - 478●
let mut best_similarity = 0.0 - 479
let mut best_index = -1 - 480
- 481
for i in 0..<database.num_faces { - 482●
let similarity = calculate_cosine_similarity(feature, database.faces[i]) - 483
if similarity > best_similarity { - 484●
best_similarity = similarity - 485
best_index = i - 486
} - 487
} - 488
- 489
if best_index >= 0 && best_similarity >= database.threshold { - 490●
FaceRecognitionResult::{ - 491
face_id: 0, - 492
identity: database.identities[best_index], - 493
confidence: best_similarity, - 494
similarity: best_similarity, - 495
is_known: true, - 496
matched_face_id: best_index, - 497
bounding_box, - 498
} - 499
} else { - 500●
FaceRecognitionResult::{ - 501
face_id: 0, - 502
identity: "unknown", - 503
confidence: if best_similarity > 0.0 { - 504●
best_similarity - 505
} else { - 506●
0.0 - 507
}, - 508
similarity: best_similarity, - 509
is_known: false, - 510
matched_face_id: -1, - 511
bounding_box, - 512
} - 513
} - 514
} - 515
- 516
///| - 517
/// 识别表情(简化版:基于人脸区域的图像特征) - 518
pub fn recognize_expression( - 519
img : @types.Image, - 520
face : FaceDetectionResult, - 521
) -> ExpressionRecognitionResult raise @types.LoadError { - 522
// 输入验证 - 523●
if img.width <= 0 || img.height <= 0 { - 524●
raise @types.LoadError::DecodeFailed( - 525
"recognize_expression: 图像尺寸必须大于0", - 526
) - 527
} - 528
if img.channels < 1 || img.channels > 4 { - 529●
raise @types.LoadError::DecodeFailed( - 530
"recognize_expression: 通道数必须在1-4之间", - 531
) - 532
} - 533
let expected_size = img.width * img.height * img.channels - 534●
if img.data.length() != expected_size { - 535●
raise @types.LoadError::DecodeFailed( - 536
"recognize_expression: 数据大小不匹配", - 537
) - 538
} - 539
let w = img.width - 540
let h = img.height - 541
- 542
// 简化版:基于人脸区域的亮度和对比度变化判断表情 - 543
// 实际实现应该使用深度学习模型 - 544
- 545●
let bx = face.bounding_box[0].to_int() - 546●
let by = face.bounding_box[1].to_int() - 547●
let bw = face.bounding_box[2].to_int() - 548●
let bh = face.bounding_box[3].to_int() - 549
- 550
// 计算嘴部区域的亮度变化(简化的表情判断) - 551
let mouth_y = by + bh * 3 / 4 - 552
let mut mouth_brightness = 0.0 - 553
let mut count = 0 - 554
for y in mouth_y..<(mouth_y + bh / 4) { - 555●
for x in bx..<(bx + bw) { - 556●
if x >= 0 && x < w && y >= 0 && y < h { - 557●
mouth_brightness += img.data[y * w + x].to_double() - 558
count += 1 - 559
} - 560
} - 561
} - 562
mouth_brightness = if count > 0 { - 563●
mouth_brightness / count.to_double() - 564
} else { - 565○
0.0 - 566
} - 567
- 568
// 简化的表情分类 - 569
let mut expression = ExpressionType::Neutral - 570
let mut confidence = 0.55 - 571
let probabilities : Array[Double] = [] - 572
- 573
if mouth_brightness > 150.0 { - 574●
expression = ExpressionType::Happy - 575
confidence = 0.6 - 576●
probabilities.push(0.1) // Neutral - 577●
probabilities.push(0.6) // Happy - 578●
probabilities.push(0.1) // Sad - 579●
probabilities.push(0.05) // Angry - 580●
probabilities.push(0.05) // Surprise - 581●
probabilities.push(0.05) // Fear - 582●
probabilities.push(0.03) // Disgust - 583●
probabilities.push(0.02) // Contempt - 584●
} else if mouth_brightness < 80.0 { - 585●
expression = ExpressionType::Sad - 586
confidence = 0.5 - 587●
probabilities.push(0.15) - 588●
probabilities.push(0.1) - 589●
probabilities.push(0.5) - 590●
probabilities.push(0.1) - 591●
probabilities.push(0.05) - 592●
probabilities.push(0.05) - 593●
probabilities.push(0.03) - 594●
probabilities.push(0.02) - 595
} else { - 596●
expression = ExpressionType::Neutral - 597
confidence = 0.55 - 598●
probabilities.push(0.55) - 599●
probabilities.push(0.15) - 600●
probabilities.push(0.1) - 601●
probabilities.push(0.05) - 602●
probabilities.push(0.05) - 603●
probabilities.push(0.05) - 604●
probabilities.push(0.03) - 605●
probabilities.push(0.02) - 606
} - 607
- 608
let dominant_expression = if expression == ExpressionType::Neutral { - 609●
"neutral" - 610●
} else if expression == ExpressionType::Happy { - 611●
"happy" - 612●
} else if expression == ExpressionType::Sad { - 613●
"sad" - 614○
} else if expression == ExpressionType::Angry { - 615○
"angry" - 616○
} else if expression == ExpressionType::Surprise { - 617○
"surprise" - 618○
} else if expression == ExpressionType::Fear { - 619○
"fear" - 620○
} else if expression == ExpressionType::Disgust { - 621○
"disgust" - 622○
} else if expression == ExpressionType::Contempt { - 623○
"contempt" - 624
} else { - 625○
"unknown" - 626
} - 627
- 628
ExpressionRecognitionResult::{ - 629
expression, - 630
confidence, - 631
probabilities, - 632
num_classes: 8, - 633
dominant_expression, - 634
} - 635
} - 636
- 637
///| - 638
/// 估计年龄和性别(简化版) - 639
pub fn estimate_age_gender( - 640
img : @types.Image, - 641
face : FaceDetectionResult, - 642
) -> AgeGenderEstimationResult raise @types.LoadError { - 643
// 输入验证 - 644●
if img.width <= 0 || img.height <= 0 { - 645●
raise @types.LoadError::DecodeFailed( - 646
"estimate_age_gender: 图像尺寸必须大于0", - 647
) - 648
} - 649
if img.channels < 1 || img.channels > 4 { - 650●
raise @types.LoadError::DecodeFailed( - 651
"estimate_age_gender: 通道数必须在1-4之间", - 652
) - 653
} - 654
let expected_size = img.width * img.height * img.channels - 655●
if img.data.length() != expected_size { - 656●
raise @types.LoadError::DecodeFailed( - 657
"estimate_age_gender: 数据大小不匹配", - 658
) - 659
} - 660
let w = img.width - 661
let h = img.height - 662
- 663
// 简化版:基于人脸区域的纹理和皱纹估计年龄 - 664
// 实际实现应该使用深度学习模型 - 665
- 666●
let bx = face.bounding_box[0].to_int() - 667●
let by = face.bounding_box[1].to_int() - 668●
let bw = face.bounding_box[2].to_int() - 669●
let bh = face.bounding_box[3].to_int() - 670
- 671
// 计算人脸区域的纹理复杂度(简化的年龄估计) - 672
let mut texture_complexity = 0.0 - 673
let mut count = 0 - 674
for y in 1..<(bh - 1) { - 675●
for x in 1..<(bw - 1) { - 676●
let px = bx + x - 677
let py = by + y - 678
if px >= 1 && px < w - 1 && py >= 1 && py < h - 1 { - 679●
let idx = py * w + px - 680●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 681●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 682●
texture_complexity += (gx * gx + gy * gy).sqrt() - 683
count += 1 - 684
} - 685
} - 686
} - 687
texture_complexity = if count > 0 { - 688●
texture_complexity / count.to_double() - 689
} else { - 690○
0.0 - 691
} - 692
- 693
// 简化的年龄估计(纹理越复杂,年龄越大) - 694
let age = 20.0 + texture_complexity * 2.0 - 695
let age_range = [age - 5.0, age + 5.0] - 696
- 697
// 简化的性别判断(基于人脸区域的亮度和形状) - 698
let is_male = texture_complexity > 10.0 - 699●
let gender = if is_male { "male" } else { "female" } - 700
let gender_confidence = 0.6 - 701
- 702
AgeGenderEstimationResult::{ - 703
age, - 704
age_range, - 705
gender, - 706
gender_confidence, - 707
is_male, - 708
} - 709
} - 710
- 711
///| - 712
/// 评估人脸质量 - 713
pub fn evaluate_face_quality( - 714
img : @types.Image, - 715
face : FaceDetectionResult, - 716
) -> Double raise @types.LoadError { - 717
// 输入验证 - 718●
if img.width <= 0 || img.height <= 0 { - 719●
raise @types.LoadError::DecodeFailed( - 720
"evaluate_face_quality: 图像尺寸必须大于0", - 721
) - 722
} - 723
if img.channels < 1 || img.channels > 4 { - 724●
raise @types.LoadError::DecodeFailed( - 725
"evaluate_face_quality: 通道数必须在1-4之间", - 726
) - 727
} - 728
let expected_size = img.width * img.height * img.channels - 729●
if img.data.length() != expected_size { - 730●
raise @types.LoadError::DecodeFailed( - 731
"evaluate_face_quality: 数据大小不匹配", - 732
) - 733
} - 734
let w = img.width - 735
let h = img.height - 736
- 737●
let bx = face.bounding_box[0].to_int() - 738●
let by = face.bounding_box[1].to_int() - 739●
let bw = face.bounding_box[2].to_int() - 740●
let bh = face.bounding_box[3].to_int() - 741
- 742
// 计算亮度 - 743
let mut brightness = 0.0 - 744
let mut sharpness = 0.0 - 745
let mut count = 0 - 746
for y in 1..<(bh - 1) { - 747●
for x in 1..<(bw - 1) { - 748●
let px = bx + x - 749
let py = by + y - 750
if px >= 1 && px < w - 1 && py >= 1 && py < h - 1 { - 751●
let idx = py * w + px - 752●
brightness += img.data[idx].to_double() - 753●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 754●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 755●
sharpness += (gx * gx + gy * gy).sqrt() - 756
count += 1 - 757
} - 758
} - 759
} - 760
- 761◐
brightness = if count > 0 { brightness / count.to_double() } else { 0.0 } - 762◐
sharpness = if count > 0 { sharpness / count.to_double() } else { 0.0 } - 763
- 764
// 综合质量评分(亮度 40% + 锐度 60%) - 765
let brightness_score = if brightness > 80.0 && brightness < 180.0 { - 766●
1.0 - 767
} else { - 768○
0.5 - 769
} - 770
let sharpness_score = if sharpness > 20.0 { - 771●
1.0 - 772●
} else if sharpness > 10.0 { - 773○
0.7 - 774
} else { - 775●
0.4 - 776
} - 777
- 778
brightness_score * 0.4 + sharpness_score * 0.6 - 779
} - 780
- 781
///| - 782
/// 完整的人脸属性分析 - 783
pub fn analyze_face_attributes( - 784
img : @types.Image, - 785
face : FaceDetectionResult, - 786
database : FaceDatabase, - 787
config : FaceRecognitionConfig, - 788
) -> FaceAttributeAnalysis raise @types.LoadError { - 789
// 输入验证 - 790●
if img.width <= 0 || img.height <= 0 { - 791●
raise @types.LoadError::DecodeFailed( - 792
"analyze_face_attributes: 图像尺寸必须大于0", - 793
) - 794
} - 795
if img.channels < 1 || img.channels > 4 { - 796●
raise @types.LoadError::DecodeFailed( - 797
"analyze_face_attributes: 通道数必须在1-4之间", - 798
) - 799
} - 800
let expected_size = img.width * img.height * img.channels - 801●
if img.data.length() != expected_size { - 802●
raise @types.LoadError::DecodeFailed( - 803
"analyze_face_attributes: 数据大小不匹配", - 804
) - 805
} - 806●
let feature = extract_face_feature(img, face, config) - 807●
let recognition = recognize_face_in_database( - 808
database, - 809
feature, - 810
face.bounding_box, - 811
) - 812●
let expression = recognize_expression(img, face) - 813●
let age_gender = estimate_age_gender(img, face) - 814●
let quality = evaluate_face_quality(img, face) - 815
- 816
// 计算亮度和锐度 - 817
let w = img.width - 818
let h = img.height - 819●
let bx = face.bounding_box[0].to_int() - 820●
let by = face.bounding_box[1].to_int() - 821●
let bw = face.bounding_box[2].to_int() - 822●
let bh = face.bounding_box[3].to_int() - 823
- 824
let mut brightness = 0.0 - 825
let mut sharpness = 0.0 - 826
let mut count = 0 - 827
for y in 0..<bh { - 828●
for x in 0..<bw { - 829●
let px = bx + x - 830
let py = by + y - 831
if px >= 0 && px < w && py >= 0 && py < h { - 832●
brightness += img.data[py * w + px].to_double() - 833
count += 1 - 834
} - 835
} - 836
} - 837◐
brightness = if count > 0 { brightness / count.to_double() } else { 0.0 } - 838
sharpness = quality * 50.0 - 839
- 840
FaceAttributeAnalysis::{ - 841
detection: face, - 842
recognition, - 843
expression, - 844
age_gender, - 845
face_quality: quality, - 846
brightness, - 847
sharpness, - 848
occlusion_rate: 0.0, - 849
} - 850
} - 851
- 852
///| - 853
/// 生成人脸识别报告 - 854
pub fn face_recognition_report_to_string( - 855
analysis : FaceAttributeAnalysis, - 856
) -> String { - 857●
let mut report = "## 人脸识别与表情识别报告\n\n" - 858
report = report + "### 人脸检测\n\n" - 859
report = report + "| 指标 | 值 |\n|------|------|\n" - 860
report = report + - 861
"| 检测置信度 | " + - 862●
(analysis.detection.confidence * 100.0).to_string() + - 863
"% |\n" - 864
report = report + - 865
"| 关键点数量 | " + - 866●
analysis.detection.num_landmarks.to_string() + - 867
" |\n" - 868
report = report + - 869
"| 是否正面 | " + - 870◐
(if analysis.detection.is_frontal { "是" } else { "否" }) + - 871
" |\n" - 872
report = report + - 873
"| 偏航角 | " + - 874●
analysis.detection.yaw.to_string() + - 875
"° |\n" - 876
report = report + - 877
"| 俯仰角 | " + - 878●
analysis.detection.pitch.to_string() + - 879
"° |\n" - 880
report = report + - 881
"| 翻滚角 | " + - 882●
analysis.detection.roll.to_string() + - 883
"° |\n\n" - 884
- 885
report = report + "### 人脸识别\n\n" - 886
report = report + "| 指标 | 值 |\n|------|------|\n" - 887
report = report + "| 身份 | " + analysis.recognition.identity + " |\n" - 888
report = report + - 889
"| 是否已知 | " + - 890●
(if analysis.recognition.is_known { "是" } else { "否" }) + - 891
" |\n" - 892
report = report + - 893
"| 相似度 | " + - 894●
(analysis.recognition.similarity * 100.0).to_string() + - 895
"% |\n" - 896
report = report + - 897
"| 匹配置信度 | " + - 898●
(analysis.recognition.confidence * 100.0).to_string() + - 899
"% |\n\n" - 900
- 901
report = report + "### 表情识别\n\n" - 902
report = report + "| 指标 | 值 |\n|------|------|\n" - 903
report = report + - 904
"| 表情 | " + - 905
analysis.expression.dominant_expression + - 906
" |\n" - 907
report = report + - 908
"| 置信度 | " + - 909●
(analysis.expression.confidence * 100.0).to_string() + - 910
"% |\n" - 911
report = report + - 912
"| 类别数 | " + - 913●
analysis.expression.num_classes.to_string() + - 914
" |\n\n" - 915
- 916
report = report + "### 年龄性别估计\n\n" - 917
report = report + "| 指标 | 值 |\n|------|------|\n" - 918
report = report + - 919
"| 估计年龄 | " + - 920●
analysis.age_gender.age.to_string() + - 921
" 岁 |\n" - 922
report = report + - 923
"| 年龄范围 | " + - 924●
analysis.age_gender.age_range[0].to_string() + - 925
" - " + - 926●
analysis.age_gender.age_range[1].to_string() + - 927
" 岁 |\n" - 928
report = report + "| 性别 | " + analysis.age_gender.gender + " |\n" - 929
report = report + - 930
"| 性别置信度 | " + - 931●
(analysis.age_gender.gender_confidence * 100.0).to_string() + - 932
"% |\n\n" - 933
- 934
report = report + "### 人脸质量\n\n" - 935
report = report + "| 指标 | 值 |\n|------|------|\n" - 936
report = report + - 937
"| 综合质量 | " + - 938●
(analysis.face_quality * 100.0).to_string() + - 939
"% |\n" - 940●
report = report + "| 亮度 | " + analysis.brightness.to_string() + " |\n" - 941●
report = report + "| 锐度 | " + analysis.sharpness.to_string() + " |\n" - 942
report = report + - 943
"| 遮挡率 | " + - 944●
(analysis.occlusion_rate * 100.0).to_string() + - 945
"% |\n" - 946
- 947
report - 948
}