image.vision\ocr\accuracy_testing.mbt
88% · 169 / 192 points · 599 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # ocr/accuracy_testing - 3
/// - 4
/// 图像处理库子模块 - 5
/// Image processing library submodule - 6
// OCR 准确率测试框架(v12.21.0 P0 基础加固) - 7
// 提供准确率计算、混淆矩阵、错误分析、基准测试等工具 - 8
- 9
///| - 10
/// 字符识别结果 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct CharRecognitionResult { - 13
expected : String - 14
actual : String - 15
is_correct : Bool - 16
confidence : Double - 17
x : Int32 - 18
y : Int32 - 19
} - 20
- 21
///| - 22
pub extend CharRecognitionResult with Eq::{not_equal, equal} - 23
- 24
///| - 25
pub extend CharRecognitionResult with @debug.Debug::{to_repr} - 26
- 27
///| - 28
/// 准确率统计 - 29
#derive(Eq, @debug.Debug) - 30
pub(all) struct AccuracyStats { - 31
total_chars : Int32 - 32
correct_chars : Int32 - 33
char_accuracy : Double - 34
total_words : Int32 - 35
correct_words : Int32 - 36
word_accuracy : Double - 37
total_lines : Int32 - 38
correct_lines : Int32 - 39
line_accuracy : Double - 40
false_positives : Int32 - 41
false_negatives : Int32 - 42
precision : Double - 43
recall : Double - 44
f1_score : Double - 45
} - 46
- 47
///| - 48
pub extend AccuracyStats with Eq::{not_equal, equal} - 49
- 50
///| - 51
pub extend AccuracyStats with @debug.Debug::{to_repr} - 52
- 53
///| - 54
/// 混淆矩阵条目 - 55
#derive(Eq, @debug.Debug) - 56
pub(all) struct ConfusionEntry { - 57
expected : String - 58
actual : String - 59
count : Int32 - 60
} - 61
- 62
///| - 63
pub extend ConfusionEntry with Eq::{not_equal, equal} - 64
- 65
///| - 66
pub extend ConfusionEntry with @debug.Debug::{to_repr} - 67
- 68
///| - 69
/// 混淆矩阵 - 70
#derive(Eq, @debug.Debug) - 71
pub(all) struct ConfusionMatrix { - 72
entries : Array[ConfusionEntry] - 73
total_errors : Int32 - 74
} - 75
- 76
///| - 77
pub extend ConfusionMatrix with Eq::{not_equal, equal} - 78
- 79
///| - 80
pub extend ConfusionMatrix with @debug.Debug::{to_repr} - 81
- 82
///| - 83
/// 错误类型 - 84
/// `#non_exhaustive` 表示未来可能添加新构造器 - 85
#derive(Eq, @debug.Debug) - 86
#non_exhaustive - 87
pub(all) enum OcrErrorType { - 88
Substitution // 替换错误(A→B) - 89
Insertion // 插入错误(多识别了字符) - 90
Deletion // 删除错误(漏识别了字符) - 91
Reorder // 顺序错误 - 92
Unknown - 93
} - 94
- 95
///| - 96
pub extend OcrErrorType with @debug.Debug::{to_repr} - 97
- 98
///| - 99
pub extend OcrErrorType with Eq::{not_equal, equal} - 100
- 101
///| - 102
/// 错误分析条目 - 103
#derive(Eq, @debug.Debug) - 104
pub(all) struct ErrorAnalysisEntry { - 105
error_type : OcrErrorType - 106
expected : String - 107
actual : String - 108
context_before : String - 109
context_after : String - 110
confidence : Double - 111
count : Int32 - 112
} - 113
- 114
///| - 115
pub extend ErrorAnalysisEntry with Eq::{not_equal, equal} - 116
- 117
///| - 118
pub extend ErrorAnalysisEntry with @debug.Debug::{to_repr} - 119
- 120
///| - 121
/// 错误分析报告 - 122
#derive(Eq, @debug.Debug) - 123
pub(all) struct ErrorAnalysisReport { - 124
total_errors : Int32 - 125
substitution_count : Int32 - 126
insertion_count : Int32 - 127
deletion_count : Int32 - 128
reorder_count : Int32 - 129
top_errors : Array[ErrorAnalysisEntry] - 130
suggestions : Array[String] - 131
} - 132
- 133
///| - 134
pub extend ErrorAnalysisReport with Eq::{not_equal, equal} - 135
- 136
///| - 137
pub extend ErrorAnalysisReport with @debug.Debug::{to_repr} - 138
- 139
///| - 140
/// 性能基准结果 - 141
#derive(Eq, @debug.Debug) - 142
pub(all) struct PerformanceBenchmark { - 143
total_time_ms : Double - 144
preprocess_time_ms : Double - 145
layout_time_ms : Double - 146
recognition_time_ms : Double - 147
postprocess_time_ms : Double - 148
pages_per_second : Double - 149
chars_per_second : Double - 150
memory_usage_mb : Double - 151
cpu_usage_percent : Double - 152
} - 153
- 154
///| - 155
pub extend PerformanceBenchmark with Eq::{not_equal, equal} - 156
- 157
///| - 158
pub extend PerformanceBenchmark with @debug.Debug::{to_repr} - 159
- 160
///| - 161
/// 计算字符级准确率 - 162
pub fn calculate_char_accuracy(expected : String, actual : String) -> Double { - 163●
if expected.length() == 0 { - 164●
return if actual.length() == 0 { 1.0 } else { 0.0 } - 165
} - 166
let mut correct = 0 - 167●
let min_len = if expected.length() < actual.length() { - 168●
expected.length() - 169
} else { - 170●
actual.length() - 171
} - 172
for i in 0..<min_len { - 173●
if expected[i] == actual[i] { - 174●
correct += 1 - 175
} - 176
} - 177●
correct.to_double() / expected.length().to_double() - 178
} - 179
- 180
///| - 181
/// 计算词级准确率 - 182
pub fn calculate_word_accuracy(expected : String, actual : String) -> Double { - 183●
let expected_words = split_words(expected) - 184●
let actual_words = split_words(actual) - 185●
if expected_words.length() == 0 { - 186◐
return if actual_words.length() == 0 { 1.0 } else { 0.0 } - 187
} - 188
let mut correct = 0 - 189●
let min_len = if expected_words.length() < actual_words.length() { - 190○
expected_words.length() - 191
} else { - 192●
actual_words.length() - 193
} - 194
for i in 0..<min_len { - 195●
if expected_words[i] == actual_words[i] { - 196●
correct += 1 - 197
} - 198
} - 199●
correct.to_double() / expected_words.length().to_double() - 200
} - 201
- 202
///| - 203
/// 简单分词(按空格和标点分割) - 204
fn split_words(text : String) -> Array[String] { - 205●
let words : Array[String] = [] - 206
let mut current = "" - 207●
for i in 0..<text.length() { - 208●
let c = text[i] - 209
if c == ' ' || - 210
c == '\n' || - 211
c == '\t' || - 212
c == ',' || - 213
c == '.' || - 214
c == ',' || - 215
c == '。' || - 216
c == ';' || - 217
c == ':' { - 218●
if current.length() > 0 { - 219●
words.push(current) - 220
current = "" - 221
} - 222
} else { - 223●
current = current + c.to_string() - 224
} - 225
} - 226●
if current.length() > 0 { - 227●
words.push(current) - 228
} - 229
words - 230
} - 231
- 232
///| - 233
/// 计算编辑距离(Levenshtein) - 234
pub fn calculate_edit_distance(s1 : String, s2 : String) -> Int32 { - 235●
let m = s1.length() - 236●
let n = s2.length() - 237
if m == 0 { - 238●
return n - 239
} - 240
if n == 0 { - 241●
return m - 242
} - 243
// 使用滚动数组优化空间 - 244
let mut prev : Array[Int32] = [] - 245
let mut curr : Array[Int32] = [] - 246
for j in 0..<(n + 1) { - 247●
prev.push(j) - 248●
curr.push(0) - 249
} - 250
for i in 1..<(m + 1) { - 251●
curr[0] = i - 252
for j in 1..<(n + 1) { - 253●
let cost = if s1[i - 1] == s2[j - 1] { 0 } else { 1 } - 254
let del = prev[j] + 1 - 255
let ins = curr[j - 1] + 1 - 256
let sub = prev[j - 1] + cost - 257
curr[j] = if del < ins { - 258●
if del < sub { - 259●
del - 260
} else { - 261●
sub - 262
} - 263●
} else if ins < sub { - 264●
ins - 265
} else { - 266●
sub - 267
} - 268
} - 269
let temp = prev - 270
prev = curr - 271
curr = temp - 272
} - 273
prev[n] - 274
} - 275
- 276
///| - 277
/// 计算完整准确率统计 - 278
pub fn calculate_accuracy_stats( - 279
expected_lines : Array[String], - 280
actual_lines : Array[String], - 281
) -> AccuracyStats raise @types.LoadError { - 282
// 输入验证 - 283●
if expected_lines.length() == 0 { - 284●
raise @types.LoadError::DecodeFailed( - 285
"calculate_accuracy_stats: 期望文本行列表不能为空", - 286
) - 287
} - 288●
if actual_lines.length() == 0 { - 289●
raise @types.LoadError::DecodeFailed( - 290
"calculate_accuracy_stats: 实际文本行列表不能为空", - 291
) - 292
} - 293
let mut total_chars = 0 - 294
let mut correct_chars = 0 - 295
let mut total_words = 0 - 296
let mut correct_words = 0 - 297●
let total_lines = expected_lines.length() - 298
let mut correct_lines = 0 - 299
let mut false_positives = 0 - 300
let mut false_negatives = 0 - 301
- 302●
let min_lines = if expected_lines.length() < actual_lines.length() { - 303○
expected_lines.length() - 304
} else { - 305●
actual_lines.length() - 306
} - 307
- 308
for i in 0..<min_lines { - 309●
let expected = expected_lines[i] - 310
let actual = actual_lines[i] - 311●
total_chars += expected.length() - 312
let mut line_correct = 0 - 313●
let min_chars = if expected.length() < actual.length() { - 314○
expected.length() - 315
} else { - 316●
actual.length() - 317
} - 318
for j in 0..<min_chars { - 319●
if expected[j] == actual[j] { - 320●
correct_chars += 1 - 321
line_correct += 1 - 322
} - 323
} - 324●
if actual.length() > expected.length() { - 325○
false_positives += actual.length() - expected.length() - 326
} else { - 327●
false_negatives += expected.length() - actual.length() - 328
} - 329●
if line_correct == expected.length() && expected.length() == actual.length() { - 330●
correct_lines += 1 - 331
} - 332
// 词级准确率 - 333●
let exp_words = split_words(expected) - 334●
let act_words = split_words(actual) - 335●
total_words += exp_words.length() - 336●
let min_words = if exp_words.length() < act_words.length() { - 337○
exp_words.length() - 338
} else { - 339●
act_words.length() - 340
} - 341
for j in 0..<min_words { - 342●
if exp_words[j] == act_words[j] { - 343●
correct_words += 1 - 344
} - 345
} - 346
} - 347
- 348
// 多出的行 - 349●
if actual_lines.length() > expected_lines.length() { - 350○
for i in expected_lines.length()..<actual_lines.length() { - 351○
false_positives += actual_lines[i].length() - 352
} - 353
} else { - 354●
for i in actual_lines.length()..<expected_lines.length() { - 355●
false_negatives += expected_lines[i].length() - 356
} - 357
} - 358
- 359
let char_accuracy = if total_chars > 0 { - 360●
correct_chars.to_double() / total_chars.to_double() - 361
} else { - 362○
1.0 - 363
} - 364
let word_accuracy = if total_words > 0 { - 365●
correct_words.to_double() / total_words.to_double() - 366
} else { - 367○
1.0 - 368
} - 369
let line_accuracy = if total_lines > 0 { - 370●
correct_lines.to_double() / total_lines.to_double() - 371
} else { - 372○
1.0 - 373
} - 374
- 375
let precision = if correct_chars + false_positives > 0 { - 376●
correct_chars.to_double() / (correct_chars + false_positives).to_double() - 377
} else { - 378○
1.0 - 379
} - 380
let recall = if correct_chars + false_negatives > 0 { - 381●
correct_chars.to_double() / (correct_chars + false_negatives).to_double() - 382
} else { - 383○
1.0 - 384
} - 385
let f1_score = if precision + recall > 0 { - 386●
2.0 * precision * recall / (precision + recall) - 387
} else { - 388○
0.0 - 389
} - 390
- 391
AccuracyStats::{ - 392
total_chars, - 393
correct_chars, - 394
char_accuracy, - 395
total_words, - 396
correct_words, - 397
word_accuracy, - 398
total_lines, - 399
correct_lines, - 400
line_accuracy, - 401
false_positives, - 402
false_negatives, - 403
precision, - 404
recall, - 405
f1_score, - 406
} - 407
} - 408
- 409
///| - 410
/// 生成准确率报告 - 411
pub fn generate_accuracy_report( - 412
stats : AccuracyStats, - 413
dataset_name : String, - 414
) -> String { - 415●
let mut report = "## OCR 准确率测试报告\n\n" - 416
report = report + "**测试集**: " + dataset_name + "\n\n" - 417
report = report + "### 整体指标\n\n" - 418
report = report + "| 指标 | 数值 |\n" - 419
report = report + "|------|------|\n" - 420
report = report + - 421
"| 字符准确率 | " + - 422●
(stats.char_accuracy * 100.0).to_string() + - 423
"% |\n" - 424
report = report + - 425
"| 词准确率 | " + - 426●
(stats.word_accuracy * 100.0).to_string() + - 427
"% |\n" - 428
report = report + - 429
"| 行准确率 | " + - 430●
(stats.line_accuracy * 100.0).to_string() + - 431
"% |\n" - 432
report = report + - 433
"| 精确率 (Precision) | " + - 434●
(stats.precision * 100.0).to_string() + - 435
"% |\n" - 436
report = report + - 437
"| 召回率 (Recall) | " + - 438●
(stats.recall * 100.0).to_string() + - 439
"% |\n" - 440
report = report + - 441
"| F1 分数 | " + - 442●
(stats.f1_score * 100.0).to_string() + - 443
"% |\n\n" - 444
report = report + "### 详细统计\n\n" - 445
report = report + "| 指标 | 数值 |\n" - 446
report = report + "|------|------|\n" - 447●
report = report + "| 总字符数 | " + stats.total_chars.to_string() + " |\n" - 448
report = report + - 449
"| 正确字符数 | " + - 450●
stats.correct_chars.to_string() + - 451
" |\n" - 452●
report = report + "| 总词数 | " + stats.total_words.to_string() + " |\n" - 453
report = report + - 454
"| 正确词数 | " + - 455●
stats.correct_words.to_string() + - 456
" |\n" - 457●
report = report + "| 总行数 | " + stats.total_lines.to_string() + " |\n" - 458
report = report + - 459
"| 正确行数 | " + - 460●
stats.correct_lines.to_string() + - 461
" |\n" - 462
report = report + - 463
"| 误报 (False Positives) | " + - 464●
stats.false_positives.to_string() + - 465
" |\n" - 466
report = report + - 467
"| 漏报 (False Negatives) | " + - 468●
stats.false_negatives.to_string() + - 469
" |\n" - 470
report - 471
} - 472
- 473
///| - 474
/// 生成性能基准报告 - 475
pub fn generate_performance_report( - 476
benchmark : PerformanceBenchmark, - 477
dataset_name : String, - 478
) -> String { - 479●
let mut report = "## OCR 性能基准测试报告\n\n" - 480
report = report + "**测试集**: " + dataset_name + "\n\n" - 481
report = report + "### 处理时间\n\n" - 482
report = report + "| 阶段 | 耗时 (ms) | 占比 |\n" - 483
report = report + "|------|-----------|------|\n" - 484
let total = if benchmark.total_time_ms > 0 { - 485●
benchmark.total_time_ms - 486
} else { - 487●
1.0 - 488
} - 489
report = report + - 490
"| 预处理 | " + - 491●
benchmark.preprocess_time_ms.to_string() + - 492
" | " + - 493●
(benchmark.preprocess_time_ms / total * 100.0).to_string() + - 494
"% |\n" - 495
report = report + - 496
"| 版面分析 | " + - 497●
benchmark.layout_time_ms.to_string() + - 498
" | " + - 499●
(benchmark.layout_time_ms / total * 100.0).to_string() + - 500
"% |\n" - 501
report = report + - 502
"| 字符识别 | " + - 503●
benchmark.recognition_time_ms.to_string() + - 504
" | " + - 505●
(benchmark.recognition_time_ms / total * 100.0).to_string() + - 506
"% |\n" - 507
report = report + - 508
"| 后处理 | " + - 509●
benchmark.postprocess_time_ms.to_string() + - 510
" | " + - 511●
(benchmark.postprocess_time_ms / total * 100.0).to_string() + - 512
"% |\n" - 513
report = report + - 514
"| **总计** | **" + - 515●
benchmark.total_time_ms.to_string() + - 516
"** | **100%** |\n\n" - 517
report = report + "### 吞吐量\n\n" - 518
report = report + "| 指标 | 数值 |\n" - 519
report = report + "|------|------|\n" - 520
report = report + - 521
"| 处理速度 (页/秒) | " + - 522●
benchmark.pages_per_second.to_string() + - 523
" |\n" - 524
report = report + - 525
"| 识别速度 (字符/秒) | " + - 526●
benchmark.chars_per_second.to_string() + - 527
" |\n" - 528
report = report + - 529
"| 内存占用 (MB) | " + - 530●
benchmark.memory_usage_mb.to_string() + - 531
" |\n" - 532
report = report + - 533
"| CPU 使用率 (%) | " + - 534●
benchmark.cpu_usage_percent.to_string() + - 535
" |\n" - 536
report - 537
} - 538
- 539
///| - 540
/// 验证准确率是否达标 - 541
pub fn check_accuracy_threshold( - 542
stats : AccuracyStats, - 543
min_char_accuracy~ : Double = 0.95, - 544
min_word_accuracy~ : Double = 0.90, - 545
min_f1_score~ : Double = 0.92, - 546
) -> (Bool, Array[String]) raise @types.LoadError { - 547
// 输入验证 - 548●
if min_char_accuracy < 0.0 || min_char_accuracy > 1.0 { - 549●
raise @types.LoadError::DecodeFailed( - 550
"check_accuracy_threshold: 字符准确率阈值必须在0-1之间", - 551
) - 552
} - 553
if min_word_accuracy < 0.0 || min_word_accuracy > 1.0 { - 554●
raise @types.LoadError::DecodeFailed( - 555
"check_accuracy_threshold: 词准确率阈值必须在0-1之间", - 556
) - 557
} - 558
if min_f1_score < 0.0 || min_f1_score > 1.0 { - 559●
raise @types.LoadError::DecodeFailed( - 560
"check_accuracy_threshold: F1分数阈值必须在0-1之间", - 561
) - 562
} - 563
let min_char = min_char_accuracy - 564
let min_word = min_word_accuracy - 565
let min_f1 = min_f1_score - 566
let mut passed = true - 567
let issues : Array[String] = [] - 568
if stats.char_accuracy < min_char { - 569●
passed = false - 570●
issues.push( - 571
"字符准确率 " + - 572●
(stats.char_accuracy * 100.0).to_string() + - 573
"% 低于阈值 " + - 574●
(min_char * 100.0).to_string() + - 575
"%", - 576
) - 577
} - 578
if stats.word_accuracy < min_word { - 579●
passed = false - 580●
issues.push( - 581
"词准确率 " + - 582●
(stats.word_accuracy * 100.0).to_string() + - 583
"% 低于阈值 " + - 584●
(min_word * 100.0).to_string() + - 585
"%", - 586
) - 587
} - 588
if stats.f1_score < min_f1 { - 589●
passed = false - 590●
issues.push( - 591
"F1 分数 " + - 592●
(stats.f1_score * 100.0).to_string() + - 593
"% 低于阈值 " + - 594●
(min_f1 * 100.0).to_string() + - 595
"%", - 596
) - 597
} - 598
(passed, issues) - 599
}