mirror of
https://github.com/DS4SD/docling.git
synced 2025-12-08 12:48:28 +00:00
refactor: allow the usage of backends in the enrich models and generalize the interface (#742)
* fix get image with cropbox Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * allow the usage of backends in the enrich models and generalize the interface Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * move logic in BaseTextImageEnrichmentModel Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * renaming Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> --------- Signed-off-by: Michele Dolfi <dol@zurich.ibm.com>
This commit is contained in:
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Union
|
||||
from docling_core.types.doc import (
|
||||
BoundingBox,
|
||||
DocItemLabel,
|
||||
NodeItem,
|
||||
PictureDataType,
|
||||
Size,
|
||||
TableCell,
|
||||
@@ -201,6 +202,13 @@ class AssembledUnit(BaseModel):
|
||||
headers: List[PageElement] = []
|
||||
|
||||
|
||||
class ItemAndImageEnrichmentElement(BaseModel):
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
item: NodeItem
|
||||
image: Image
|
||||
|
||||
|
||||
class Page(BaseModel):
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
@@ -219,12 +227,28 @@ class Page(BaseModel):
|
||||
{}
|
||||
) # Cache of images in different scales. By default it is cleared during assembling.
|
||||
|
||||
def get_image(self, scale: float = 1.0) -> Optional[Image]:
|
||||
def get_image(
|
||||
self, scale: float = 1.0, cropbox: Optional[BoundingBox] = None
|
||||
) -> Optional[Image]:
|
||||
if self._backend is None:
|
||||
return self._image_cache.get(scale, None)
|
||||
|
||||
if not scale in self._image_cache:
|
||||
self._image_cache[scale] = self._backend.get_page_image(scale=scale)
|
||||
return self._image_cache[scale]
|
||||
if cropbox is None:
|
||||
self._image_cache[scale] = self._backend.get_page_image(scale=scale)
|
||||
else:
|
||||
return self._backend.get_page_image(scale=scale, cropbox=cropbox)
|
||||
|
||||
if cropbox is None:
|
||||
return self._image_cache[scale]
|
||||
else:
|
||||
page_im = self._image_cache[scale]
|
||||
assert self.size is not None
|
||||
return page_im.crop(
|
||||
cropbox.to_top_left_origin(page_height=self.size.height)
|
||||
.scaled(scale=scale)
|
||||
.as_tuple()
|
||||
)
|
||||
|
||||
@property
|
||||
def image(self) -> Optional[Image]:
|
||||
|
||||
Reference in New Issue
Block a user