完整的文档OCR ONNX Pipeline
| 523 | |
| 524 | # ==================== OpenDoc ONNX Pipeline ==================== |
| 525 | class OpenDocONNX: |
| 526 | """完整的文档OCR ONNX Pipeline""" |
| 527 | |
| 528 | def __init__( |
| 529 | self, |
| 530 | layout_model_path: Optional[str] = None, |
| 531 | unirec_encoder_path: Optional[str] = None, |
| 532 | unirec_decoder_path: Optional[str] = None, |
| 533 | tokenizer_mapping_path: Optional[str] = None, |
| 534 | use_gpu: Optional[bool] = None, |
| 535 | layout_threshold: float = 0.5, |
| 536 | use_layout_detection: bool = True, |
| 537 | use_chart_recognition: bool = True, |
| 538 | auto_download: bool = True, |
| 539 | max_parallel_blocks: int = 4, |
| 540 | ): |
| 541 | """ |
| 542 | 初始化OpenDoc ONNX Pipeline |
| 543 | |
| 544 | Args: |
| 545 | layout_model_path: 版面检测ONNX模型路径. If None, use default cache directory. |
| 546 | unirec_encoder_path: UniRec编码器ONNX模型路径. If None, use default cache directory. |
| 547 | unirec_decoder_path: UniRec解码器ONNX模型路径. If None, use default cache directory. |
| 548 | tokenizer_mapping_path: Tokenizer映射文件路径. If None, use default cache directory. |
| 549 | use_gpu: Whether to use GPU. If None, auto-detect. If True, force GPU. If False, force CPU. |
| 550 | layout_threshold: 版面检测阈值 |
| 551 | use_layout_detection: 是否使用版面检测 |
| 552 | use_chart_recognition: 是否识别图表 |
| 553 | auto_download: If True, automatically download missing models |
| 554 | max_parallel_blocks: Maximum number of blocks to process in parallel for VLM recognition (default: 4) |
| 555 | """ |
| 556 | self.use_layout_detection = use_layout_detection |
| 557 | self.use_chart_recognition = use_chart_recognition |
| 558 | self.max_parallel_blocks = max(1, max_parallel_blocks) |
| 559 | |
| 560 | # Set default paths if not provided |
| 561 | if layout_model_path is None: |
| 562 | cache_dir = Path.home() / '.cache' / 'openocr' |
| 563 | layout_model_path = str(cache_dir / 'PP_DoclayoutV2_onnx' / 'PP-DoclayoutV2.onnx') |
| 564 | |
| 565 | # Markdown忽略的标签 |
| 566 | self.markdown_ignore_labels = [ |
| 567 | 'number', 'footnote', 'header', 'footer', 'aside_text', 'footer_image', 'header_image', 'chart' |
| 568 | ] |
| 569 | |
| 570 | # 为所有25种标签类型定义不同的颜色 (BGR格式) |
| 571 | self.colors = { |
| 572 | 'abstract': (255, 128, 0), # 橙色 |
| 573 | 'algorithm': (128, 0, 255), # 紫色 |
| 574 | 'aside_text': (128, 128, 128), # 灰色 |
| 575 | 'chart': (0, 255, 255), # 青色 |
| 576 | 'content': (0, 255, 0), # 绿色 |
| 577 | 'display_formula': (255, 0, 255), # 品红 |
| 578 | 'doc_title': (255, 0, 0), # 红色 |
| 579 | 'figure_title': (255, 128, 128), # 浅红 |
| 580 | 'footer': (64, 64, 64), # 深灰 |
| 581 | 'footer_image': (128, 64, 0), # 棕色 |
| 582 | 'footnote': (192, 192, 192), # 浅灰 |
no outgoing calls
no test coverage detected