Repository navigation
Expand file tree
/
Copy pathdoc_parser.rs
More file actions
339 lines (301 loc) · 11.5 KB
/
Copy pathdoc_parser.rs
File metadata and controls
339 lines (301 loc) · 11.5 KB
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
//! Unified Document Parser Example
//!
//! This example demonstrates the PageParser API for external
//! layout-first document parsing (layout detection + region recognition).
//!
//! HunyuanOCR and the MinerU models are intentionally not exposed here: their
//! reference-quality usage is full-page prompt-driven parsing or a model-native
//! two-step pipeline, not forced external layout crops.
//!
//! # Usage
//!
//! ```bash
//! # Using PaddleOCR-VL model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name paddleocr-vl \
//! --model-dir PaddlePaddle/PaddleOCR-VL \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//!
//! # Using PaddleOCR-VL-1.5 model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name paddleocr-vl-1.5 \
//! --model-dir PaddlePaddle/PaddleOCR-VL-1.5 \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//!
//! # Using PaddleOCR-VL-1.6 model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name paddleocr-vl-1.6 \
//! --model-dir PaddlePaddle/PaddleOCR-VL-1.6 \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//!
//! # Using GLM-OCR model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name glmocr \
//! --model-dir zai-org/GLM-OCR \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//!
//! # Using TeleOCR model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name teleocr \
//! --model-dir XingChen-AGI/TeleOCR \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//!
//! # Using WeVisDoc model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name wevisdoc \
//! --model-dir Tencent/WeVisDoc-2B \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//!
//! # Using jina-ocr-v1 model
//! cargo run -p oar-ocr-vl --example doc_parser -- \
//! --model-name jina-ocr \
//! --model-dir jinaai/jina-ocr-v1 \
//! --layout-dir PaddlePaddle/PP-DocLayoutV3_safetensors \
//! document.jpg
//! ```
mod utils;
use clap::{Parser, ValueEnum};
use std::path::PathBuf;
use std::time::Instant;
use tracing::{error, info};
use oar_ocr_vl::utils::image::load_image;
use oar_ocr_vl::utils::parse_device;
use oar_ocr_vl::{DocParserOptions, LayoutPageParser, PageParser, PpDocLayout};
/// Recognition model type
#[derive(Debug, Clone, Copy, ValueEnum)]
enum ModelName {
/// PaddleOCR-VL 0.9B: VLM with task prompts
#[value(name = "paddleocr-vl")]
PaddleOcrVl,
/// PaddleOCR-VL 1.5 (0.9B): spotting and seal recognition
#[value(name = "paddleocr-vl-1.5")]
PaddleOcrVl15,
/// PaddleOCR-VL 1.6 (0.9B): region-aware enhanced recognition
#[value(name = "paddleocr-vl-1.6")]
PaddleOcrVl16,
/// GLM-OCR: OCR expert VLM (GLM-V)
#[value(name = "glmocr")]
GlmOcr,
/// TeleOCR: document parsing VLM (Qwen2.5-VL backbone)
#[value(name = "teleocr")]
TeleOcr,
/// jina-ocr-v1: end-to-end page-to-Markdown parser (SAM+CLIP over a
/// DeepSeek-V2 MoE decoder)
#[value(name = "jina-ocr")]
JinaOcr,
/// WeVisDoc: document parsing VLM (Qwen3-VL backbone)
#[value(name = "wevisdoc")]
WeVisDoc,
}
/// Command-line arguments
#[derive(Parser)]
#[command(name = "doc_parser")]
#[command(
about = "Unified external-layout DocParser - supports PaddleOCR-VL, PaddleOCR-VL-1.5/1.6, GLM-OCR, TeleOCR, jina-ocr-v1, and WeVisDoc"
)]
struct Args {
/// Recognition model to use
#[arg(short = 'n', long, value_enum, default_value = "paddleocr-vl-1.5")]
model_name: ModelName,
/// Path to the model directory
#[arg(short, long)]
model_dir: PathBuf,
/// Path to a PP-DocLayoutV2/V3 checkpoint directory
#[arg(short, long)]
layout_dir: PathBuf,
/// Paths to input document images
#[arg(required = true)]
images: Vec<PathBuf>,
/// Device to run on: auto, cpu, cuda, cuda:N, or metal
#[arg(short, long, default_value = "auto")]
device: String,
/// Directory to save markdown output
#[arg(short, long)]
output_dir: Option<PathBuf>,
/// Generation limit per region; omitted uses the backend default.
#[arg(long)]
max_tokens: Option<usize>,
/// Maximum same-task crops per region batch. Increase cautiously;
/// VLM batches trade more memory for throughput; equal-token batches also
/// enable Metal's fused decode path.
#[arg(long, default_value = "1")]
region_batch_size: usize,
/// Enable verbose output
#[arg(short, long)]
verbose: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
use oar_ocr_vl::{GlmOcr, JinaOcr, PaddleOcrVl, TeleOcr, WeVisDoc};
utils::init_tracing();
let args = Args::parse();
info!("Unified Document Parser Example");
info!("Model: {:?}", args.model_name);
// Verify files exist
if !args.model_dir.exists() {
error!("Model directory not found: {}", args.model_dir.display());
return Err("Model directory not found".into());
}
if !args.layout_dir.exists() {
error!(
"Layout checkpoint not found: {}. Use the hunyuanocr/mineru examples for \
model-native full-page parsing.",
args.layout_dir.display()
);
return Err("Layout checkpoint not found".into());
}
// Filter valid images
let existing_images: Vec<PathBuf> =
args.images.iter().filter(|p| p.exists()).cloned().collect();
if existing_images.is_empty() {
return Err("No valid image files found".into());
}
// Create output directory if needed
if let Some(ref dir) = args.output_dir {
std::fs::create_dir_all(dir)?;
}
let device = parse_device(&args.device)?;
info!("Device: {:?}", device);
info!("Loading PP-DocLayout...");
let layout = PpDocLayout::from_dir(&args.layout_dir, device.clone())?;
info!("Layout model: {:?}", layout.version());
let options = match args.max_tokens {
Some(limit) => DocParserOptions::default().with_max_tokens(limit),
None => DocParserOptions::default(),
};
// Process images with the selected model
match args.model_name {
ModelName::PaddleOcrVl | ModelName::PaddleOcrVl15 | ModelName::PaddleOcrVl16 => {
info!("Loading PaddleOCR-VL model...");
let load_start = Instant::now();
let vl = PaddleOcrVl::from_dir(&args.model_dir, device)?;
info!(
"PaddleOCR-VL loaded in {:.2}ms",
load_start.elapsed().as_secs_f64() * 1000.0
);
let parser = LayoutPageParser::new(layout, vl)
.with_options(options)
.with_region_batch_size(args.region_batch_size);
process_images(&parser, &existing_images, &args)?;
}
ModelName::GlmOcr => {
info!("Loading GLM-OCR model...");
let load_start = Instant::now();
let model = GlmOcr::from_dir(&args.model_dir, device)?;
info!(
"GLM-OCR loaded in {:.2}ms",
load_start.elapsed().as_secs_f64() * 1000.0
);
let parser = LayoutPageParser::new(layout, model)
.with_options(options)
.with_region_batch_size(args.region_batch_size);
process_images(&parser, &existing_images, &args)?;
}
ModelName::TeleOcr => {
info!("Loading TeleOCR model...");
let load_start = Instant::now();
let model = TeleOcr::from_dir(&args.model_dir, device)?;
info!(
"TeleOCR loaded in {:.2}ms",
load_start.elapsed().as_secs_f64() * 1000.0
);
let parser = LayoutPageParser::new(layout, model)
.with_options(options)
.with_region_batch_size(args.region_batch_size);
process_images(&parser, &existing_images, &args)?;
}
ModelName::JinaOcr => {
info!("Loading jina-ocr-v1 model...");
let load_start = Instant::now();
let model = JinaOcr::from_dir(&args.model_dir, device)?;
info!(
"jina-ocr-v1 loaded in {:.2}ms",
load_start.elapsed().as_secs_f64() * 1000.0
);
let parser = LayoutPageParser::new(layout, model)
.with_options(options)
.with_region_batch_size(args.region_batch_size);
process_images(&parser, &existing_images, &args)?;
}
ModelName::WeVisDoc => {
info!("Loading WeVisDoc model...");
let load_start = Instant::now();
let model = WeVisDoc::from_dir(&args.model_dir, device)?;
info!(
"WeVisDoc loaded in {:.2}ms",
load_start.elapsed().as_secs_f64() * 1000.0
);
let parser = LayoutPageParser::new(layout, model)
.with_options(options)
.with_region_batch_size(args.region_batch_size);
process_images(&parser, &existing_images, &args)?;
}
}
Ok(())
}
fn process_images<B: oar_ocr_vl::RecognitionBackend>(
parser: &LayoutPageParser<PpDocLayout, B>,
images: &[PathBuf],
args: &Args,
) -> Result<(), Box<dyn std::error::Error>> {
info!("\n=== Processing {} images ===", images.len());
for image_path in images {
info!("\nProcessing: {}", image_path.display());
let rgb_img = match load_image(image_path) {
Ok(img) => {
if args.verbose {
info!(" Image size: {}x{}", img.width(), img.height());
}
img
}
Err(e) => {
error!(" Failed to load: {}", e);
continue;
}
};
let start = Instant::now();
let result = parser.parse_page(&rgb_img, &Default::default());
match result {
Ok(result) => {
info!(" Parsed in {:.2}s", start.elapsed().as_secs_f64());
info!(" Elements: {}", result.blocks.len());
let markdown = result.markdown.unwrap_or_default();
// Save or print
if let Some(ref dir) = args.output_dir {
let name = image_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("out");
let path = dir.join(format!("{}.md", name));
std::fs::write(&path, &markdown)?;
info!(" Saved: {}", path.display());
} else {
println!("\n--- Markdown ---\n{}\n--- End ---", markdown);
}
if args.verbose {
for (i, el) in result.blocks.iter().enumerate() {
let preview = el
.content
.as_ref()
.map(|t| {
if t.chars().count() > 40 {
format!("{}...", t.chars().take(40).collect::<String>())
} else {
t.clone()
}
})
.unwrap_or_default();
info!(" [{}] {:?}: {}", i, el.block_type, preview);
}
}
}
Err(e) => error!(" Failed: {}", e),
}
}
Ok(())
}