feat(ocr): Add parallel batch OCR processing via ThreadPoolExecutor#2197
Open
echojfree wants to merge 6 commits into
Open
feat(ocr): Add parallel batch OCR processing via ThreadPoolExecutor#2197echojfree wants to merge 6 commits into
echojfree wants to merge 6 commits into
Conversation
Replace serial image-by-image OCR with batched parallel processing across all four OCR-enhanced converters (PDF, DOCX, PPTX, XLSX). ## Changes ### OCR Service (_ocr_service.py) - Add max_workers parameter (default 5) to LLMVisionOCRService - Add extract_text_batch() method using ThreadPoolExecutor for parallel I/O-bound LLM API calls - Individual image failures captured in OCRResult.error field without interrupting the batch ### PDF Converter (_pdf_converter_with_ocr.py) - Single-pass PDF processing: collect all images from all pages first, then batch OCR, then interleave results with text lines by Y position for correct document flow - _ocr_full_pages() now collects page renders first, then batch OCRs them (both pdfplumber and PyMuPDF fallback paths) - Fix scanned-PDF fallback detection: regex-strip page-number headers before checking for real content - Remove _extract_page_images() in favor of in-pass collection - Extract _extract_text_lines_from_page() helper ### DOCX Converter (_docx_converter_with_ocr.py) - _extract_and_ocr_images(): collect all image rIds and streams first, then call extract_text_batch() once ### PPTX Converter (_pptx_converter_with_ocr.py) - Two-phase parallel strategy: Round 1: all images -> LLM caption in parallel Round 2: failed images -> OCR in parallel via extract_text_batch - Pre-scan phase collects all image shapes across all slides (recursive _collect_image_shapes handles groups) - Rendering phase consumes pre-computed results via cursor, maintaining identical output structure ### XLSX Converter (_xlsx_converter_with_ocr.py) - Cross-sheet collection: gather all images from all sheets before batch OCR, then distribute results by sheet - Rename _extract_and_ocr_sheet_images -> _extract_sheet_images (collection only; OCR moved to caller) ### Plugin Registration (_plugin.py) - Accept max_workers kwarg (default 5), forward to OCR service ### Tests (4 files) - Add extract_text_batch() to all MockOCRService classes - Update test_pdf_multipage expectation: batch-parallel path correctly extracts text+images in a single pass instead of artificially falling back to full-page OCR ## Performance Real-world test on 5-page scanned contract PDF (EMC): - Serial (max_workers=1): 74.4s - Parallel (max_workers=5): 24.1s - Speedup: 3.1x All 36 existing tests pass. Co-Authored-By: Claude <noreply@anthropic.com>
|
@echojfree please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
The relative import 'from ._llm_caption' resolves to markitdown_ocr._llm_caption which doesn't exist. The correct absolute import is from markitdown.converters._llm_caption. This was a pre-existing bug masked by try/except in the original code path. Co-Authored-By: Claude <noreply@anthropic.com>
- Default ocr_dpi reduced from 300 to 150 (4x fewer pixels = faster rendering + smaller base64 payloads + faster LLM processing) - Page rendering in _ocr_full_pages now uses ThreadPoolExecutor (both pdfplumber and PyMuPDF fallback paths) - Configurable via ocr_dpi= kwarg (e.g. MarkItDown(..., ocr_dpi=200)) Co-Authored-By: Claude <noreply@anthropic.com>
Full-page OCR DPI stays at 300 by default. Users who want faster processing can still pass ocr_dpi=150. Co-Authored-By: Claude <noreply@anthropic.com>
Replace batch-then-OCR with a streaming pipeline: each page is submitted for OCR the moment its render finishes, instead of waiting for ALL pages to render before starting ANY OCR. Rendering and OCR now run concurrently, so total time is max(render_all, ocr_all) rather than render_all + ocr_all. Single-page documents skip the thread pool entirely for zero overhead on small inputs. Co-Authored-By: Claude <noreply@anthropic.com>
## PPTX: fix literal backslash-n in output (HIGH) The PPTX converter emitted '\n' (literal backslash-n) instead of real newlines, producing malformed markdown. The upstream core _pptx_converter.py uses real '\n' — this aligns the OCR variant. Output is now clean markdown (leading newlines correctly stripped). Test expectations updated to match verified-correct output. ## XLSX: single-pass sheet read (perf) _convert_with_ocr re-parsed the entire workbook via pd.read_excel once per sheet (O(N) full parses). Now reads all sheets in one call with sheet_name=None, matching the standard-path approach. All 36 tests pass. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace serial image-by-image OCR with batched parallel processing across all four OCR-enhanced converters (PDF, DOCX, PPTX, XLSX).
Changes
OCR Service (_ocr_service.py)
PDF Converter (_pdf_converter_with_ocr.py)
DOCX Converter (_docx_converter_with_ocr.py)
PPTX Converter (_pptx_converter_with_ocr.py)
XLSX Converter (_xlsx_converter_with_ocr.py)
Plugin Registration (_plugin.py)
Tests (4 files)
Performance
Real-world test on 5-page scanned contract PDF (EMC):
All 36 existing tests pass.