mirror of
https://github.com/DS4SD/docling.git
synced 2025-07-30 14:04:27 +00:00
fix: make it work for multi-page
Signed-off-by: Yusik Kim <kmyusk@gmail.com>
This commit is contained in:
parent
50d2ef1ad6
commit
411a33fc69
@ -1,50 +1,27 @@
|
|||||||
import base64
|
from pathlib import Path
|
||||||
import io
|
from typing import cast
|
||||||
|
|
||||||
from PIL import Image as PILImage
|
|
||||||
from docling_core.experimental.serializer.doctags import (
|
from docling_core.experimental.serializer.doctags import (
|
||||||
DocTagsDocSerializer,
|
DocTagsDocSerializer,
|
||||||
DocTagsParams,
|
DocTagsParams,
|
||||||
)
|
)
|
||||||
from docling_core.types.doc import DoclingDocument, Size
|
from docling_core.types.doc import DoclingDocument, Size
|
||||||
from docling_core.types.doc.document import DocTagsDocument, ImageRef, PageItem
|
from docling_core.types.doc.document import DocTagsDocument
|
||||||
from pydantic import AnyUrl
|
from PIL import Image as PILImage
|
||||||
|
|
||||||
|
|
||||||
def remove_doctags_content(doctags: str, image: PILImage.Image) -> str:
|
def remove_doctags_content(doctags: list[str], images: list[PILImage.Image]) -> str:
|
||||||
def from_pil_to_base64(img: PILImage.Image) -> str:
|
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs(
|
||||||
# Convert the image to a base64 str
|
cast(list[str | Path], doctags), cast(list[PILImage.Image | Path], images)
|
||||||
buffered = io.BytesIO()
|
)
|
||||||
img.save(buffered, format="PNG") # Specify the format (e.g., JPEG, PNG, etc.)
|
|
||||||
image_bytes = buffered.getvalue()
|
|
||||||
|
|
||||||
# Encode the bytes to a Base64 string
|
|
||||||
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
|
||||||
return image_base64
|
|
||||||
|
|
||||||
def from_pil_to_base64uri(img: PILImage.Image) -> AnyUrl:
|
|
||||||
image_base64 = from_pil_to_base64(img)
|
|
||||||
uri = AnyUrl(f"data:image/png;base64,{image_base64}")
|
|
||||||
|
|
||||||
return uri
|
|
||||||
|
|
||||||
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([doctags], [image])
|
|
||||||
doc = DoclingDocument(name="dummy")
|
doc = DoclingDocument(name="dummy")
|
||||||
doc.load_from_doctags(doctags_doc)
|
doc.load_from_doctags(doctags_doc)
|
||||||
image_ref = ImageRef(
|
for idx, image in enumerate(images):
|
||||||
mimetype="image/png",
|
size = Size(width=float(image.width), height=float(image.height))
|
||||||
dpi=72,
|
doc.add_page(page_no=idx + 1, size=size)
|
||||||
size=Size(width=float(image.width), height=float(image.height)),
|
|
||||||
uri=from_pil_to_base64uri(image),
|
|
||||||
)
|
|
||||||
page_item = PageItem(
|
|
||||||
page_no=1,
|
|
||||||
size=Size(width=float(image.width), height=float(image.height)),
|
|
||||||
image=image_ref,
|
|
||||||
)
|
|
||||||
|
|
||||||
doc.pages[1] = page_item
|
|
||||||
dt_params = DocTagsParams(add_content=False)
|
dt_params = DocTagsParams(add_content=False)
|
||||||
ser = DocTagsDocSerializer(params=dt_params, doc=doc)
|
ser = DocTagsDocSerializer(params=dt_params, doc=doc)
|
||||||
pages = [ser.serialize(item=item) for item, _ in doc.iterate_items()]
|
items = [ser.serialize(item=item) for item, _ in doc.iterate_items()]
|
||||||
return ser.serialize_doc(pages=pages).text
|
dt_params = DocTagsParams(add_content=False)
|
||||||
|
ser = DocTagsDocSerializer(params=dt_params, doc=doc)
|
||||||
|
return ser.serialize_doc(pages=items).text
|
||||||
|
@ -2,10 +2,14 @@ from PIL import Image as PILImage
|
|||||||
|
|
||||||
from docling.utils.doctags_utils import remove_doctags_content
|
from docling.utils.doctags_utils import remove_doctags_content
|
||||||
|
|
||||||
|
|
||||||
def test_remove_doctags_content():
|
def test_remove_doctags_content():
|
||||||
img = PILImage.open("./tests/data_scanned/ocr_test.png")
|
img = PILImage.open("./tests/data_scanned/ocr_test.png")
|
||||||
with open("./tests/data_scanned/groundtruth/docling_v2/ocr_test.doctags.txt") as f:
|
with open("./tests/data_scanned/groundtruth/docling_v2/ocr_test.doctags.txt") as f:
|
||||||
doctags = f.read()
|
doctags = f.read()
|
||||||
actual = remove_doctags_content(doctags, img)
|
actual = remove_doctags_content([doctags] * 2, [img] * 2)
|
||||||
expected = "<doctag><text><loc_58><loc_44><loc_426><loc_91></text>\n</doctag>"
|
expected = """<doctag><text><loc_58><loc_44><loc_426><loc_91></text>
|
||||||
|
<page_break>
|
||||||
|
<text><loc_58><loc_44><loc_426><loc_91></text>
|
||||||
|
</doctag>"""
|
||||||
assert actual == expected
|
assert actual == expected
|
||||||
|
Loading…
Reference in New Issue
Block a user