2024-11-20 20:51:19 +09:00
|
|
|
import sys
|
2024-10-08 19:07:08 +02:00
|
|
|
from pathlib import Path
|
2025-05-21 18:12:33 +02:00
|
|
|
from typing import List, Tuple
|
2024-10-08 19:07:08 +02:00
|
|
|
|
|
|
|
from docling.backend.docling_parse_backend import DoclingParseDocumentBackend
|
2025-06-02 17:01:06 +02:00
|
|
|
from docling.datamodel.accelerator_options import AcceleratorDevice
|
2024-10-16 21:02:03 +02:00
|
|
|
from docling.datamodel.base_models import InputFormat
|
2024-10-08 19:07:08 +02:00
|
|
|
from docling.datamodel.document import ConversionResult
|
|
|
|
from docling.datamodel.pipeline_options import (
|
|
|
|
EasyOcrOptions,
|
2024-11-20 20:51:19 +09:00
|
|
|
OcrMacOptions,
|
2024-10-08 19:07:08 +02:00
|
|
|
OcrOptions,
|
2024-10-16 21:02:03 +02:00
|
|
|
PdfPipelineOptions,
|
2024-11-27 18:27:41 +05:30
|
|
|
RapidOcrOptions,
|
2024-10-08 19:07:08 +02:00
|
|
|
TesseractCliOcrOptions,
|
|
|
|
TesseractOcrOptions,
|
|
|
|
)
|
2024-10-16 21:02:03 +02:00
|
|
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
2024-10-08 19:07:08 +02:00
|
|
|
|
2025-03-18 10:38:19 +01:00
|
|
|
from .test_data_gen_flag import GEN_TEST_DATA
|
2024-10-16 21:02:03 +02:00
|
|
|
from .verify_utils import verify_conversion_result_v1, verify_conversion_result_v2
|
2024-10-08 19:07:08 +02:00
|
|
|
|
2025-03-18 10:38:19 +01:00
|
|
|
GENERATE_V1 = GEN_TEST_DATA
|
|
|
|
GENERATE_V2 = GEN_TEST_DATA
|
2024-10-08 19:07:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_pdf_paths():
|
|
|
|
# Define the directory you want to search
|
|
|
|
directory = Path("./tests/data_scanned")
|
|
|
|
|
|
|
|
# List all PDF files in the directory and its subdirectories
|
|
|
|
pdf_files = sorted(directory.rglob("*.pdf"))
|
|
|
|
return pdf_files
|
|
|
|
|
|
|
|
|
|
|
|
def get_converter(ocr_options: OcrOptions):
|
2024-10-16 21:02:03 +02:00
|
|
|
pipeline_options = PdfPipelineOptions()
|
2024-10-08 19:07:08 +02:00
|
|
|
pipeline_options.do_ocr = True
|
|
|
|
pipeline_options.do_table_structure = True
|
|
|
|
pipeline_options.table_structure_options.do_cell_matching = True
|
|
|
|
pipeline_options.ocr_options = ocr_options
|
2025-01-29 10:39:00 +01:00
|
|
|
pipeline_options.accelerator_options.device = AcceleratorDevice.CPU
|
2024-10-08 19:07:08 +02:00
|
|
|
|
|
|
|
converter = DocumentConverter(
|
2024-10-16 21:02:03 +02:00
|
|
|
format_options={
|
|
|
|
InputFormat.PDF: PdfFormatOption(
|
|
|
|
pipeline_options=pipeline_options,
|
|
|
|
backend=DoclingParseDocumentBackend,
|
|
|
|
)
|
|
|
|
}
|
2024-10-08 19:07:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return converter
|
|
|
|
|
|
|
|
|
|
|
|
def test_e2e_conversions():
|
|
|
|
pdf_paths = get_pdf_paths()
|
|
|
|
|
2025-05-21 18:12:33 +02:00
|
|
|
engines: List[Tuple[OcrOptions, bool]] = [
|
|
|
|
(TesseractOcrOptions(), True),
|
|
|
|
(TesseractCliOcrOptions(), True),
|
2025-06-13 19:01:55 +02:00
|
|
|
(EasyOcrOptions(), False),
|
2025-05-21 18:12:33 +02:00
|
|
|
(TesseractOcrOptions(force_full_page_ocr=True), True),
|
|
|
|
(TesseractOcrOptions(force_full_page_ocr=True, lang=["auto"]), True),
|
|
|
|
(TesseractCliOcrOptions(force_full_page_ocr=True), True),
|
|
|
|
(TesseractCliOcrOptions(force_full_page_ocr=True, lang=["auto"]), True),
|
2025-06-13 19:01:55 +02:00
|
|
|
(EasyOcrOptions(force_full_page_ocr=True), False),
|
2024-10-08 19:07:08 +02:00
|
|
|
]
|
|
|
|
|
2025-01-30 17:26:42 +01:00
|
|
|
# rapidocr is only available for Python >=3.6,<3.13
|
|
|
|
if sys.version_info < (3, 13):
|
2025-05-21 18:12:33 +02:00
|
|
|
engines.append((RapidOcrOptions(), False))
|
|
|
|
engines.append((RapidOcrOptions(force_full_page_ocr=True), False))
|
2025-01-30 17:26:42 +01:00
|
|
|
|
2024-11-20 20:51:19 +09:00
|
|
|
# only works on mac
|
|
|
|
if "darwin" == sys.platform:
|
2025-05-21 18:12:33 +02:00
|
|
|
engines.append((OcrMacOptions(), True))
|
|
|
|
engines.append((OcrMacOptions(force_full_page_ocr=True), True))
|
2024-11-20 20:51:19 +09:00
|
|
|
|
2025-05-21 18:12:33 +02:00
|
|
|
for ocr_options, supports_rotation in engines:
|
2025-01-23 12:40:50 +01:00
|
|
|
print(
|
|
|
|
f"Converting with ocr_engine: {ocr_options.kind}, language: {ocr_options.lang}"
|
|
|
|
)
|
2024-10-08 19:07:08 +02:00
|
|
|
converter = get_converter(ocr_options=ocr_options)
|
|
|
|
for pdf_path in pdf_paths:
|
2025-05-21 18:12:33 +02:00
|
|
|
if not supports_rotation and "rotated" in pdf_path.name:
|
|
|
|
continue
|
2024-10-08 19:07:08 +02:00
|
|
|
print(f"converting {pdf_path}")
|
|
|
|
|
2024-10-16 21:02:03 +02:00
|
|
|
doc_result: ConversionResult = converter.convert(pdf_path)
|
2024-10-08 19:07:08 +02:00
|
|
|
|
2024-10-16 21:02:03 +02:00
|
|
|
verify_conversion_result_v1(
|
|
|
|
input_path=pdf_path,
|
|
|
|
doc_result=doc_result,
|
2024-11-12 09:46:14 +01:00
|
|
|
generate=GENERATE_V1,
|
2024-10-16 21:02:03 +02:00
|
|
|
fuzzy=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
verify_conversion_result_v2(
|
2024-10-08 19:07:08 +02:00
|
|
|
input_path=pdf_path,
|
|
|
|
doc_result=doc_result,
|
2024-11-12 09:46:14 +01:00
|
|
|
generate=GENERATE_V2,
|
2024-10-11 10:21:19 +02:00
|
|
|
fuzzy=True,
|
2024-10-08 19:07:08 +02:00
|
|
|
)
|