c6308dc822
* Add OCR test data and implement tests for various document formats - Created HTML file with multiple images for testing OCR extraction. - Added several PDF files with different layouts and image placements to validate OCR functionality. - Introduced PPTX files with complex layouts and images at various positions for comprehensive testing. - Included XLSX files with multiple images and complex layouts to ensure accurate OCR extraction. - Implemented a new test suite in `test_ocr.py` to validate OCR functionality across all document types, ensuring context preservation and accuracy. * Enhance OCR functionality and validation in document converters - Refactor image extraction and processing in PDF, PPTX, and XLSX converters for improved readability and consistency. - Implement detailed validation for OCR text positioning relative to surrounding text in test cases. - Introduce comprehensive tests for expected OCR results across various document types, ensuring no base64 images are present. - Improve error handling and logging for better debugging during OCR extraction. * Add support for scanned PDFs with full-page OCR fallback and implement tests * Bump version to 0.1.6b1 in __about__.py * Refactor OCR services to support LLM Vision, update README and tests accordingly * Add OCR-enabled converters and ensure consistent OCR format across document types * Refactor converters to improve import organization and enhance OCR functionality across DOCX, PDF, PPTX, and XLSX converters * Refactor exception imports for consistency across converters and tests * Fix OCR tests to match MockOCRService output and fix cross-platform file URI handling * Bump version to 0.1.6b1 in __about__.py * Skip DOCX/XLSX/PPTX OCR tests when optional dependencies are missing * Add comprehensive OCR test suite for various document formats - Introduced multiple test documents for PDF, DOCX, XLSX, and PPTX formats, covering scenarios with images at the start, middle, and end. - Implemented tests for complex layouts, multi-page documents, and documents with multiple images. - Created a new test script `test_ocr.py` to validate OCR functionality, ensuring context preservation and accurate text extraction. - Added expected OCR results for validation against ground truth. - Included tests for scanned documents to verify OCR fallback mechanisms. * Remove obsolete HTML test files and refactor test cases for file URIs and OCR format consistency - Deleted `html_image_start.html` and `html_multiple_images.html` as they are no longer needed. - Updated `test_file_uris` in `test_module_misc.py` to simplify assertions by removing unnecessary `url2pathname` usage. - Removed `test_ocr_format_consistency.py` as it is no longer relevant to the current testing framework. * Refactor OCR processing in PdfConverterWithOCR and enhance unit tests for multipage PDFs * Revert * Revert * Update REDMEs * Refactor import statements for consistency and improve formatting in converter and test files
149 lines
4.8 KiB
Python
149 lines
4.8 KiB
Python
"""
|
|
Unit tests for PptxConverterWithOCR.
|
|
|
|
For each PPTX test file: convert with a mock OCR service then compare the
|
|
full output string against the expected snapshot.
|
|
|
|
OCR block format used by the converter:
|
|
*[Image OCR]
|
|
MOCK_OCR_TEXT_12345
|
|
[End OCR]*
|
|
|
|
Note: PPTX slide text uses literal backslash-n (\\n) sequences from the
|
|
underlying PPTX converter template; OCR blocks use real newlines.
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
|
|
|
|
from markitdown_ocr._ocr_service import OCRResult # noqa: E402
|
|
from markitdown_ocr._pptx_converter_with_ocr import ( # noqa: E402
|
|
PptxConverterWithOCR,
|
|
)
|
|
from markitdown import StreamInfo # noqa: E402
|
|
|
|
TEST_DATA_DIR = Path(__file__).parent / "ocr_test_data"
|
|
|
|
_MOCK_TEXT = "MOCK_OCR_TEXT_12345"
|
|
_OCR_BLOCK = f"*[Image OCR]\n{_MOCK_TEXT}\n[End OCR]*"
|
|
|
|
|
|
class MockOCRService:
|
|
def extract_text(
|
|
self, # noqa: ANN101
|
|
image_stream: Any,
|
|
**kwargs: Any,
|
|
) -> OCRResult:
|
|
return OCRResult(text=_MOCK_TEXT, backend_used="mock")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def svc() -> MockOCRService:
|
|
return MockOCRService()
|
|
|
|
|
|
def _convert(filename: str, ocr_service: MockOCRService) -> str:
|
|
path = TEST_DATA_DIR / filename
|
|
if not path.exists():
|
|
pytest.skip(f"Test file not found: {path}")
|
|
converter = PptxConverterWithOCR()
|
|
with open(path, "rb") as f:
|
|
return converter.convert(
|
|
f, StreamInfo(extension=".pptx"), ocr_service=ocr_service
|
|
).text_content
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# pptx_image_start.pptx
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_pptx_image_start(svc: MockOCRService) -> None:
|
|
# Slide 1: title "Welcome" followed by an image
|
|
expected = (
|
|
"\\n\\n<!-- Slide number: 1 -->\\n# Welcome\\n\\n"
|
|
"\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
|
|
)
|
|
assert _convert("pptx_image_start.pptx", svc) == expected
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# pptx_image_middle.pptx
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_pptx_image_middle(svc: MockOCRService) -> None:
|
|
# Slide 1: Introduction | Slide 2: Architecture + image | Slide 3: Conclusion # noqa: E501
|
|
expected = (
|
|
"\\n\\n<!-- Slide number: 1 -->\\n# Introduction"
|
|
"\\n\\n\\n\\n<!-- Slide number: 2 -->\\n# Architecture\\n\\n"
|
|
"\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
|
|
"\\n\\n<!-- Slide number: 3 -->\\n# Conclusion\\n\\n"
|
|
)
|
|
assert _convert("pptx_image_middle.pptx", svc) == expected
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# pptx_image_end.pptx
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_pptx_image_end(svc: MockOCRService) -> None:
|
|
# Slide 1: Presentation | Slide 2: Thank You + image
|
|
expected = (
|
|
"\\n\\n<!-- Slide number: 1 -->\\n# Presentation"
|
|
"\\n\\n\\n\\n<!-- Slide number: 2 -->\\n# Thank You\\n\\n"
|
|
"\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
|
|
)
|
|
assert _convert("pptx_image_end.pptx", svc) == expected
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# pptx_multiple_images.pptx
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_pptx_multiple_images(svc: MockOCRService) -> None:
|
|
# Slide 1: two images, no title text
|
|
expected = (
|
|
"\\n\\n<!-- Slide number: 1 -->\\n# \\n"
|
|
"\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
|
|
"\n\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
|
|
)
|
|
assert _convert("pptx_multiple_images.pptx", svc) == expected
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# pptx_complex_layout.pptx
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_pptx_complex_layout(svc: MockOCRService) -> None:
|
|
expected = (
|
|
"\\n\\n<!-- Slide number: 1 -->\\n# Product Comparison"
|
|
"\\n\\nOur products lead the market\\n"
|
|
"\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*"
|
|
)
|
|
assert _convert("pptx_complex_layout.pptx", svc) == expected
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# No OCR service — no OCR tags emitted
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_pptx_no_ocr_service_no_tags() -> None:
|
|
path = TEST_DATA_DIR / "pptx_image_middle.pptx"
|
|
if not path.exists():
|
|
pytest.skip(f"Test file not found: {path}")
|
|
converter = PptxConverterWithOCR()
|
|
with open(path, "rb") as f:
|
|
md = converter.convert(f, StreamInfo(extension=".pptx")).text_content
|
|
assert "*[Image OCR]" not in md
|
|
assert "[End OCR]*" not in md
|