image.vision\ocr\batch_processing.mbt
83.3% · 95 / 114 points · 566 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/batch_processing - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// OCR 批处理引擎(v12.25.0 P4 工程化) - 7
// 批量文档处理、进度跟踪、错误恢复、结果汇总、内存管理、性能优化 - 8
- 9
///| - 10
/// 批处理配置 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct BatchOcrConfig { - 13
max_concurrent : Int32 // 最大并发数 - 14
batch_size : Int32 // 每批处理数量 - 15
enable_progress_callback : Bool // 是否启用进度回调 - 16
enable_error_recovery : Bool // 是否启用错误恢复 - 17
output_format : String // 输出格式:"text" / "hocr" / "json" / "markdown" - 18
dpi : Int32 // 处理 DPI - 19
preprocess_config : String // 预处理配置:"auto" / "standard" / "enhanced" / "heavy" - 20
save_intermediate : Bool // 是否保存中间结果 - 21
output_directory : String // 输出目录 - 22
} - 23
- 24
///| - 25
pub extend BatchOcrConfig with Eq::{not_equal, equal} - 26
- 27
///| - 28
pub extend BatchOcrConfig with @debug.Debug::{to_repr} - 29
- 30
///| - 31
/// 批处理任务状态 - 32
#derive(Eq, @debug.Debug) - 33
pub(all) enum BatchTaskStatus { - 34
Pending - 35
Processing - 36
Completed - 37
Failed - 38
Skipped - 39
} - 40
- 41
///| - 42
pub extend BatchTaskStatus with @debug.Debug::{to_repr} - 43
- 44
///| - 45
pub extend BatchTaskStatus with Eq::{not_equal, equal} - 46
- 47
///| - 48
/// 单个批处理任务 - 49
#derive(Eq, @debug.Debug) - 50
pub(all) struct BatchTask { - 51
task_id : String - 52
input_path : String - 53
output_path : String - 54
status : BatchTaskStatus - 55
progress : Double - 56
error_message : String - 57
start_time : Double - 58
end_time : Double - 59
page_count : Int32 - 60
char_count : Int32 - 61
accuracy_estimate : Double - 62
} - 63
- 64
///| - 65
pub extend BatchTask with Eq::{not_equal, equal} - 66
- 67
///| - 68
pub extend BatchTask with @debug.Debug::{to_repr} - 69
- 70
///| - 71
/// 批处理进度 - 72
#derive(Eq, @debug.Debug) - 73
pub(all) struct BatchProgress { - 74
total_tasks : Int32 - 75
completed_tasks : Int32 - 76
failed_tasks : Int32 - 77
skipped_tasks : Int32 - 78
current_task_index : Int32 - 79
overall_progress : Double - 80
current_task_progress : Double - 81
elapsed_time_ms : Double - 82
estimated_remaining_ms : Double - 83
pages_per_second : Double - 84
chars_per_second : Double - 85
} - 86
- 87
///| - 88
pub extend BatchProgress with Eq::{not_equal, equal} - 89
- 90
///| - 91
pub extend BatchProgress with @debug.Debug::{to_repr} - 92
- 93
///| - 94
/// 批处理结果汇总 - 95
#derive(Eq, @debug.Debug) - 96
pub(all) struct BatchResultSummary { - 97
total_tasks : Int32 - 98
successful_tasks : Int32 - 99
failed_tasks : Int32 - 100
skipped_tasks : Int32 - 101
success_rate : Double - 102
total_pages : Int32 - 103
total_chars : Int32 - 104
total_time_ms : Double - 105
average_accuracy : Double - 106
average_pages_per_second : Double - 107
average_chars_per_second : Double - 108
failed_task_ids : Array[String] - 109
output_files : Array[String] - 110
} - 111
- 112
///| - 113
pub extend BatchResultSummary with Eq::{not_equal, equal} - 114
- 115
///| - 116
pub extend BatchResultSummary with @debug.Debug::{to_repr} - 117
- 118
///| - 119
/// 内存使用统计 - 120
#derive(Eq, @debug.Debug) - 121
pub(all) struct MemoryUsageStats { - 122
current_usage_mb : Double - 123
peak_usage_mb : Double - 124
average_usage_mb : Double - 125
total_allocated : Int32 - 126
total_freed : Int32 - 127
gc_count : Int32 - 128
} - 129
- 130
///| - 131
pub extend MemoryUsageStats with Eq::{not_equal, equal} - 132
- 133
///| - 134
pub extend MemoryUsageStats with @debug.Debug::{to_repr} - 135
- 136
///| - 137
/// 创建默认批处理配置 - 138
pub fn create_default_batch_config() -> BatchOcrConfig { - 139●
BatchOcrConfig::{ - 140
max_concurrent: 2, - 141
batch_size: 10, - 142
enable_progress_callback: true, - 143
enable_error_recovery: true, - 144
output_format: "markdown", - 145
dpi: 300, - 146
preprocess_config: "auto", - 147
save_intermediate: false, - 148
output_directory: "./ocr_output", - 149
} - 150
} - 151
- 152
///| - 153
/// 创建批处理任务列表 - 154
pub fn create_batch_tasks( - 155
input_files : Array[String], - 156
output_directory : String, - 157
output_format : String, - 158
) -> Array[BatchTask] raise @types.LoadError { - 159
// 输入验证 - 160●
if input_files.length() == 0 { - 161●
raise @types.LoadError::DecodeFailed( - 162
"create_batch_tasks: 输入文件列表不能为空", - 163
) - 164
} - 165●
if output_directory.length() == 0 { - 166●
raise @types.LoadError::DecodeFailed( - 167
"create_batch_tasks: 输出目录不能为空", - 168
) - 169
} - 170●
if output_format.length() == 0 { - 171●
raise @types.LoadError::DecodeFailed( - 172
"create_batch_tasks: 输出格式不能为空", - 173
) - 174
} - 175
let tasks : Array[BatchTask] = [] - 176●
for i in 0..<input_files.length() { - 177●
let input_path = input_files[i] - 178
// 生成输出路径 - 179●
let extension = match output_format { - 180●
"text" => ".txt" - 181○
"hocr" => ".hocr" - 182○
"json" => ".json" - 183●
"markdown" => ".md" - 184○
_ => ".txt" - 185
} - 186●
let output_path = output_directory + "/output_" + i.to_string() + extension - 187●
let task_id = "task_" + i.to_string() - 188●
tasks.push(BatchTask::{ - 189
task_id, - 190
input_path, - 191
output_path, - 192
status: BatchTaskStatus::Pending, - 193
progress: 0.0, - 194
error_message: "", - 195
start_time: 0.0, - 196
end_time: 0.0, - 197
page_count: 0, - 198
char_count: 0, - 199
accuracy_estimate: 0.0, - 200
}) - 201
} - 202
tasks - 203
} - 204
- 205
///| - 206
/// 更新任务进度 - 207
pub fn update_task_progress( - 208
task : BatchTask, - 209
progress : Double, - 210
current_page : Int32, - 211
total_pages : Int32, - 212
) -> BatchTask { - 213●
ignore(current_page) - 214
let new_progress = if progress > 1.0 { - 215○
1.0 - 216●
} else if progress < 0.0 { - 217○
0.0 - 218
} else { - 219●
progress - 220
} - 221
BatchTask::{ - 222
task_id: task.task_id, - 223
input_path: task.input_path, - 224
output_path: task.output_path, - 225
status: BatchTaskStatus::Processing, - 226
progress: new_progress, - 227
error_message: task.error_message, - 228
start_time: task.start_time, - 229
end_time: task.end_time, - 230
page_count: total_pages, - 231
char_count: task.char_count, - 232
accuracy_estimate: task.accuracy_estimate, - 233
} - 234
} - 235
- 236
///| - 237
/// 标记任务完成 - 238
pub fn mark_task_completed( - 239
task : BatchTask, - 240
char_count : Int32, - 241
accuracy : Double, - 242
end_time : Double, - 243
) -> BatchTask { - 244●
BatchTask::{ - 245
task_id: task.task_id, - 246
input_path: task.input_path, - 247
output_path: task.output_path, - 248
status: BatchTaskStatus::Completed, - 249
progress: 1.0, - 250
error_message: "", - 251
start_time: task.start_time, - 252
end_time, - 253
page_count: task.page_count, - 254
char_count, - 255
accuracy_estimate: accuracy, - 256
} - 257
} - 258
- 259
///| - 260
/// 标记任务失败 - 261
pub fn mark_task_failed( - 262
task : BatchTask, - 263
error_message : String, - 264
end_time : Double, - 265
) -> BatchTask { - 266●
BatchTask::{ - 267
task_id: task.task_id, - 268
input_path: task.input_path, - 269
output_path: task.output_path, - 270
status: BatchTaskStatus::Failed, - 271
progress: task.progress, - 272
error_message, - 273
start_time: task.start_time, - 274
end_time, - 275
page_count: task.page_count, - 276
char_count: task.char_count, - 277
accuracy_estimate: 0.0, - 278
} - 279
} - 280
- 281
///| - 282
/// 计算批处理进度 - 283
pub fn calculate_batch_progress( - 284
tasks : Array[BatchTask], - 285
elapsed_time_ms : Double, - 286
) -> BatchProgress raise @types.LoadError { - 287
// 输入验证 - 288●
if tasks.length() == 0 { - 289●
raise @types.LoadError::DecodeFailed( - 290
"calculate_batch_progress: 任务列表不能为空", - 291
) - 292
} - 293
if elapsed_time_ms < 0.0 { - 294○
raise @types.LoadError::DecodeFailed( - 295
"calculate_batch_progress: 已用时间不能为负数", - 296
) - 297
} - 298
let mut completed = 0 - 299
let mut failed = 0 - 300
let mut skipped = 0 - 301
let mut current_index = 0 - 302
let mut total_progress = 0.0 - 303
let mut current_progress = 0.0 - 304
let mut total_pages = 0 - 305
let mut total_chars = 0 - 306
- 307●
for i in 0..<tasks.length() { - 308●
let task = tasks[i] - 309●
match task.status { - 310●
BatchTaskStatus::Completed => { - 311
completed += 1 - 312
total_progress += 1.0 - 313
total_pages += task.page_count - 314
total_chars += task.char_count - 315
} - 316●
BatchTaskStatus::Failed => { - 317
failed += 1 - 318
total_progress += task.progress - 319
} - 320○
BatchTaskStatus::Skipped => skipped += 1 - 321●
BatchTaskStatus::Processing => { - 322
current_index = i - 323
current_progress = task.progress - 324
total_progress += task.progress - 325
} - 326○
BatchTaskStatus::Pending => () - 327
} - 328
} - 329
- 330●
let overall = if tasks.length() > 0 { - 331●
total_progress / tasks.length().to_double() - 332
} else { - 333○
0.0 - 334
} - 335
let elapsed_seconds = elapsed_time_ms / 1000.0 - 336
let pages_per_second = if elapsed_seconds > 0 { - 337●
total_pages.to_double() / elapsed_seconds - 338
} else { - 339○
0.0 - 340
} - 341
let chars_per_second = if elapsed_seconds > 0 { - 342●
total_chars.to_double() / elapsed_seconds - 343
} else { - 344○
0.0 - 345
} - 346
- 347
// 估算剩余时间 - 348
let estimated_remaining = if completed > 0 { - 349●
let avg_time_per_task = elapsed_time_ms / completed.to_double() - 350●
let remaining_tasks = tasks.length() - completed - failed - 351●
avg_time_per_task * remaining_tasks.to_double() - 352
} else { - 353○
0.0 - 354
} - 355
- 356
BatchProgress::{ - 357●
total_tasks: tasks.length(), - 358
completed_tasks: completed, - 359
failed_tasks: failed, - 360
skipped_tasks: skipped, - 361
current_task_index: current_index, - 362
overall_progress: overall, - 363
current_task_progress: current_progress, - 364
elapsed_time_ms, - 365
estimated_remaining_ms: estimated_remaining, - 366
pages_per_second, - 367
chars_per_second, - 368
} - 369
} - 370
- 371
///| - 372
/// 生成批处理结果汇总 - 373
pub fn generate_batch_summary( - 374
tasks : Array[BatchTask], - 375
total_time_ms : Double, - 376
) -> BatchResultSummary raise @types.LoadError { - 377
// 输入验证 - 378●
if tasks.length() == 0 { - 379●
raise @types.LoadError::DecodeFailed( - 380
"generate_batch_summary: 任务列表不能为空", - 381
) - 382
} - 383
if total_time_ms < 0.0 { - 384○
raise @types.LoadError::DecodeFailed( - 385
"generate_batch_summary: 总时间不能为负数", - 386
) - 387
} - 388
let mut successful = 0 - 389
let mut failed = 0 - 390
let mut skipped = 0 - 391
let mut total_pages = 0 - 392
let mut total_chars = 0 - 393
let mut total_accuracy = 0.0 - 394
let failed_ids : Array[String] = [] - 395
let output_files : Array[String] = [] - 396
- 397
for task in tasks { - 398●
match task.status { - 399●
BatchTaskStatus::Completed => { - 400
successful += 1 - 401
total_pages += task.page_count - 402
total_chars += task.char_count - 403
total_accuracy += task.accuracy_estimate - 404●
output_files.push(task.output_path) - 405
} - 406●
BatchTaskStatus::Failed => { - 407
failed += 1 - 408●
failed_ids.push(task.task_id) - 409
} - 410○
BatchTaskStatus::Skipped => skipped += 1 - 411○
_ => () - 412
} - 413
} - 414
- 415●
let success_rate = if tasks.length() > 0 { - 416●
successful.to_double() / tasks.length().to_double() - 417
} else { - 418○
0.0 - 419
} - 420
let avg_accuracy = if successful > 0 { - 421●
total_accuracy / successful.to_double() - 422
} else { - 423○
0.0 - 424
} - 425
let total_seconds = total_time_ms / 1000.0 - 426
let avg_pages_per_second = if total_seconds > 0 { - 427●
total_pages.to_double() / total_seconds - 428
} else { - 429○
0.0 - 430
} - 431
let avg_chars_per_second = if total_seconds > 0 { - 432●
total_chars.to_double() / total_seconds - 433
} else { - 434○
0.0 - 435
} - 436
- 437
BatchResultSummary::{ - 438●
total_tasks: tasks.length(), - 439
successful_tasks: successful, - 440
failed_tasks: failed, - 441
skipped_tasks: skipped, - 442
success_rate, - 443
total_pages, - 444
total_chars, - 445
total_time_ms, - 446
average_accuracy: avg_accuracy, - 447
average_pages_per_second: avg_pages_per_second, - 448
average_chars_per_second: avg_chars_per_second, - 449
failed_task_ids: failed_ids, - 450
output_files, - 451
} - 452
} - 453
- 454
///| - 455
/// 生成批处理报告 - 456
pub fn batch_summary_to_string(summary : BatchResultSummary) -> String { - 457●
let mut report = "## OCR 批处理结果报告\n\n" - 458
report = report + "### 整体统计\n\n" - 459
report = report + "| 指标 | 数值 |\n" - 460
report = report + "|------|------|\n" - 461
report = report + - 462
"| 总任务数 | " + - 463●
summary.total_tasks.to_string() + - 464
" |\n" - 465
report = report + - 466
"| 成功任务 | " + - 467●
summary.successful_tasks.to_string() + - 468
" |\n" - 469
report = report + - 470
"| 失败任务 | " + - 471●
summary.failed_tasks.to_string() + - 472
" |\n" - 473
report = report + - 474
"| 跳过任务 | " + - 475●
summary.skipped_tasks.to_string() + - 476
" |\n" - 477
report = report + - 478
"| **成功率** | **" + - 479●
(summary.success_rate * 100.0).to_string() + - 480
"%** |\n" - 481●
report = report + "| 总页数 | " + summary.total_pages.to_string() + " |\n" - 482
report = report + - 483
"| 总字符数 | " + - 484●
summary.total_chars.to_string() + - 485
" |\n" - 486
report = report + - 487
"| 总耗时 | " + - 488●
(summary.total_time_ms / 1000.0).to_string() + - 489
" 秒 |\n" - 490
report = report + - 491
"| 平均准确率 | " + - 492●
(summary.average_accuracy * 100.0).to_string() + - 493
"% |\n" - 494
report = report + - 495
"| 处理速度 | " + - 496●
summary.average_pages_per_second.to_string() + - 497
" 页/秒 |\n" - 498
report = report + - 499
"| 识别速度 | " + - 500●
summary.average_chars_per_second.to_string() + - 501
" 字符/秒 |\n\n" - 502
- 503
if summary.failed_tasks > 0 { - 504●
report = report + "### 失败任务列表\n\n" - 505●
for i in 0..<summary.failed_task_ids.length() { - 506●
report = report + - 507●
(i + 1).to_string() + - 508
". " + - 509
summary.failed_task_ids[i] + - 510
"\n" - 511
} - 512
report = report + "\n" - 513
} - 514
- 515
report = report + "### 输出文件\n\n" - 516●
for i in 0..<summary.output_files.length() { - 517●
report = report + - 518●
(i + 1).to_string() + - 519
". " + - 520
summary.output_files[i] + - 521
"\n" - 522
} - 523
- 524
report - 525
} - 526
- 527
///| - 528
/// 生成进度条字符串 - 529
pub fn progress_bar_to_string(progress : Double, width~ : Int32 = 30) -> String { - 530●
let bar_width = width - 531●
let filled = (progress * bar_width.to_double()).to_int() - 532
let mut bar = "[" - 533
for i in 0..<bar_width { - 534●
if i < filled { - 535●
bar = bar + "=" - 536●
} else if i == filled { - 537●
bar = bar + ">" - 538
} else { - 539●
bar = bar + " " - 540
} - 541
} - 542●
bar = bar + "] " + (progress * 100.0).to_string() + "%" - 543
bar - 544
} - 545
- 546
///| - 547
/// 估算内存使用(简化版) - 548
pub fn estimate_memory_usage( - 549
image_count : Int32, - 550
image_width : Int32, - 551
image_height : Int32, - 552
channels : Int32, - 553
) -> MemoryUsageStats { - 554●
let per_image_mb = (image_width * image_height * channels).to_double() / - 555
(1024.0 * 1024.0) - 556
let current_usage = per_image_mb * 3.0 // 原图+预处理+二值化 - 557●
let peak_usage = per_image_mb * image_count.to_double() * 1.5 - 558
MemoryUsageStats::{ - 559
current_usage_mb: current_usage, - 560
peak_usage_mb: peak_usage, - 561
average_usage_mb: (current_usage + peak_usage) / 2.0, - 562
total_allocated: image_count * 3, - 563
total_freed: image_count * 2, - 564
gc_count: image_count, - 565
} - 566
}