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