2024-11-20 20:51:19 +09:00
|
|
|
import sys
|
2024-10-08 19:07:08 +02:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from docling.backend.docling_parse_backend import DoclingParseDocumentBackend
|
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
|
|
|
|
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
|
|
|
|
2024-11-12 09:46:14 +01:00
|
|
|
GENERATE_V1 = False
|
|
|
|
GENERATE_V2 = False
|
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
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
engines: List[OcrOptions] = [
|
|
|
|
EasyOcrOptions(),
|
|
|
|
TesseractOcrOptions(),
|
|
|
|
TesseractCliOcrOptions(),
|
2024-11-27 18:27:41 +05:30
|
|
|
RapidOcrOptions(),
|
2024-11-12 09:46:14 +01:00
|
|
|
EasyOcrOptions(force_full_page_ocr=True),
|
|
|
|
TesseractOcrOptions(force_full_page_ocr=True),
|
2025-01-23 12:40:50 +01:00
|
|
|
TesseractOcrOptions(force_full_page_ocr=True, lang=["auto"]),
|
2024-11-12 09:46:14 +01:00
|
|
|
TesseractCliOcrOptions(force_full_page_ocr=True),
|
2025-01-26 08:07:56 +01:00
|
|
|
TesseractCliOcrOptions(force_full_page_ocr=True, lang=["auto"]),
|
2024-11-27 18:27:41 +05:30
|
|
|
RapidOcrOptions(force_full_page_ocr=True),
|
2024-10-08 19:07:08 +02:00
|
|
|
]
|
|
|
|
|
2024-11-20 20:51:19 +09:00
|
|
|
# only works on mac
|
|
|
|
if "darwin" == sys.platform:
|
|
|
|
engines.append(OcrMacOptions())
|
|
|
|
engines.append(OcrMacOptions(force_full_page_ocr=True))
|
|
|
|
|
2024-10-08 19:07:08 +02:00
|
|
|
for ocr_options 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:
|
|
|
|
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
|
|
|
)
|