-
Notifications
You must be signed in to change notification settings - Fork 60
feat: Visualizer for table cells #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
"""Define classes for layout visualization.""" | ||
|
||
import logging | ||
from copy import deepcopy | ||
from typing import Optional | ||
|
||
from PIL import ImageDraw | ||
from PIL.Image import Image | ||
from pydantic import BaseModel | ||
from typing_extensions import override | ||
|
||
from docling_core.transforms.visualizer.base import BaseVisualizer | ||
from docling_core.types.doc.document import ContentLayer, DoclingDocument, TableItem | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
class TableVisualizer(BaseVisualizer): | ||
"""Table visualizer.""" | ||
|
||
class Params(BaseModel): | ||
"""Table visualization parameters.""" | ||
|
||
# show_Label: bool = False | ||
show_cells: bool = True | ||
# show_rows: bool = False | ||
# show_cols: bool = False | ||
|
||
base_visualizer: Optional[BaseVisualizer] = None | ||
params: Params = Params() | ||
|
||
def _draw_table_cells( | ||
self, | ||
table: TableItem, | ||
page_image: Image, | ||
page_height: float, | ||
scale_x: float, | ||
scale_y: float, | ||
): | ||
"""Draw individual table cells.""" | ||
draw = ImageDraw.Draw(page_image, "RGBA") | ||
|
||
for cell in table.data.table_cells: | ||
if cell.bbox is not None: | ||
|
||
tl_bbox = cell.bbox.to_top_left_origin(page_height=page_height) | ||
|
||
cell_color = (256, 0, 0, 32) # Transparent black for cells | ||
|
||
cx0, cy0, cx1, cy1 = tl_bbox.as_tuple() | ||
cx0 *= scale_x | ||
cx1 *= scale_x | ||
cy0 *= scale_y | ||
cy1 *= scale_y | ||
|
||
draw.rectangle( | ||
[(cx0, cy0), (cx1, cy1)], | ||
outline=(256, 0, 0, 128), | ||
fill=cell_color, | ||
) | ||
|
||
def _draw_doc_tables( | ||
self, | ||
doc: DoclingDocument, | ||
images: Optional[dict[Optional[int], Image]] = None, | ||
included_content_layers: Optional[set[ContentLayer]] = None, | ||
): | ||
"""Draw the document tables.""" | ||
my_images: dict[Optional[int], Image] = {} | ||
|
||
if images is not None: | ||
my_images = images | ||
|
||
if included_content_layers is None: | ||
included_content_layers = {c for c in ContentLayer} | ||
|
||
# Initialise `my_images` beforehand: sometimes, you have the | ||
# page-images but no DocItems! | ||
for page_nr, page in doc.pages.items(): | ||
page_image = doc.pages[page_nr].image | ||
if page_image is None or (pil_img := page_image.pil_image) is None: | ||
raise RuntimeError("Cannot visualize document without images") | ||
elif page_nr not in my_images: | ||
image = deepcopy(pil_img) | ||
my_images[page_nr] = image | ||
|
||
for idx, (elem, _) in enumerate( | ||
doc.iterate_items(included_content_layers=included_content_layers) | ||
): | ||
if not isinstance(elem, TableItem): | ||
continue | ||
if len(elem.prov) == 0: | ||
continue # Skip elements without provenances | ||
|
||
if len(elem.prov) == 1: | ||
|
||
page_nr = elem.prov[0].page_no | ||
|
||
if page_nr in my_images: | ||
image = my_images[page_nr] | ||
|
||
if self.params.show_cells: | ||
self._draw_table_cells( | ||
table=elem, | ||
page_height=doc.pages[page_nr].size.height, | ||
page_image=image, | ||
scale_x=image.width / doc.pages[page_nr].size.width, | ||
scale_y=image.height / doc.pages[page_nr].size.height, | ||
) | ||
|
||
else: | ||
raise RuntimeError(f"Cannot visualize page-image for {page_nr}") | ||
|
||
else: | ||
_log.error("Can not yet visualise tables with multiple provenances") | ||
|
||
return my_images | ||
|
||
@override | ||
def get_visualization( | ||
self, | ||
*, | ||
doc: DoclingDocument, | ||
**kwargs, | ||
) -> dict[Optional[int], Image]: | ||
"""Get visualization of the document as images by page.""" | ||
base_images = ( | ||
self.base_visualizer.get_visualization(doc=doc, **kwargs) | ||
if self.base_visualizer | ||
else None | ||
) | ||
return self._draw_doc_tables( | ||
doc=doc, | ||
images=base_images, | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this does not stop the execution, I would rather use a warning instead of error.