|
| 1 | +"""Define classes for layout visualization.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from copy import deepcopy |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from PIL import ImageDraw |
| 8 | +from PIL.Image import Image |
| 9 | +from pydantic import BaseModel |
| 10 | +from typing_extensions import override |
| 11 | + |
| 12 | +from docling_core.transforms.visualizer.base import BaseVisualizer |
| 13 | +from docling_core.types.doc.document import ContentLayer, DoclingDocument, TableItem |
| 14 | + |
| 15 | +_log = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class TableVisualizer(BaseVisualizer): |
| 19 | + """Table visualizer.""" |
| 20 | + |
| 21 | + class Params(BaseModel): |
| 22 | + """Table visualization parameters.""" |
| 23 | + |
| 24 | + # show_Label: bool = False |
| 25 | + show_cells: bool = True |
| 26 | + # show_rows: bool = False |
| 27 | + # show_cols: bool = False |
| 28 | + |
| 29 | + base_visualizer: Optional[BaseVisualizer] = None |
| 30 | + params: Params = Params() |
| 31 | + |
| 32 | + def _draw_table_cells( |
| 33 | + self, |
| 34 | + table: TableItem, |
| 35 | + page_image: Image, |
| 36 | + page_height: float, |
| 37 | + scale_x: float, |
| 38 | + scale_y: float, |
| 39 | + ): |
| 40 | + """Draw individual table cells.""" |
| 41 | + draw = ImageDraw.Draw(page_image, "RGBA") |
| 42 | + |
| 43 | + for cell in table.data.table_cells: |
| 44 | + if cell.bbox is not None: |
| 45 | + |
| 46 | + tl_bbox = cell.bbox.to_top_left_origin(page_height=page_height) |
| 47 | + |
| 48 | + cell_color = (256, 0, 0, 32) # Transparent black for cells |
| 49 | + |
| 50 | + cx0, cy0, cx1, cy1 = tl_bbox.as_tuple() |
| 51 | + cx0 *= scale_x |
| 52 | + cx1 *= scale_x |
| 53 | + cy0 *= scale_y |
| 54 | + cy1 *= scale_y |
| 55 | + |
| 56 | + draw.rectangle( |
| 57 | + [(cx0, cy0), (cx1, cy1)], |
| 58 | + outline=(256, 0, 0, 128), |
| 59 | + fill=cell_color, |
| 60 | + ) |
| 61 | + |
| 62 | + def _draw_doc_tables( |
| 63 | + self, |
| 64 | + doc: DoclingDocument, |
| 65 | + images: Optional[dict[Optional[int], Image]] = None, |
| 66 | + included_content_layers: Optional[set[ContentLayer]] = None, |
| 67 | + ): |
| 68 | + """Draw the document tables.""" |
| 69 | + my_images: dict[Optional[int], Image] = {} |
| 70 | + |
| 71 | + if images is not None: |
| 72 | + my_images = images |
| 73 | + |
| 74 | + if included_content_layers is None: |
| 75 | + included_content_layers = {c for c in ContentLayer} |
| 76 | + |
| 77 | + # Initialise `my_images` beforehand: sometimes, you have the |
| 78 | + # page-images but no DocItems! |
| 79 | + for page_nr, page in doc.pages.items(): |
| 80 | + page_image = doc.pages[page_nr].image |
| 81 | + if page_image is None or (pil_img := page_image.pil_image) is None: |
| 82 | + raise RuntimeError("Cannot visualize document without images") |
| 83 | + elif page_nr not in my_images: |
| 84 | + image = deepcopy(pil_img) |
| 85 | + my_images[page_nr] = image |
| 86 | + |
| 87 | + for idx, (elem, _) in enumerate( |
| 88 | + doc.iterate_items(included_content_layers=included_content_layers) |
| 89 | + ): |
| 90 | + if not isinstance(elem, TableItem): |
| 91 | + continue |
| 92 | + if len(elem.prov) == 0: |
| 93 | + continue # Skip elements without provenances |
| 94 | + |
| 95 | + if len(elem.prov) == 1: |
| 96 | + |
| 97 | + page_nr = elem.prov[0].page_no |
| 98 | + |
| 99 | + if page_nr in my_images: |
| 100 | + image = my_images[page_nr] |
| 101 | + |
| 102 | + if self.params.show_cells: |
| 103 | + self._draw_table_cells( |
| 104 | + table=elem, |
| 105 | + page_height=doc.pages[page_nr].size.height, |
| 106 | + page_image=image, |
| 107 | + scale_x=image.width / doc.pages[page_nr].size.width, |
| 108 | + scale_y=image.height / doc.pages[page_nr].size.height, |
| 109 | + ) |
| 110 | + |
| 111 | + else: |
| 112 | + raise RuntimeError(f"Cannot visualize page-image for {page_nr}") |
| 113 | + |
| 114 | + else: |
| 115 | + _log.error("Can not yet visualise tables with multiple provenances") |
| 116 | + |
| 117 | + return my_images |
| 118 | + |
| 119 | + @override |
| 120 | + def get_visualization( |
| 121 | + self, |
| 122 | + *, |
| 123 | + doc: DoclingDocument, |
| 124 | + **kwargs, |
| 125 | + ) -> dict[Optional[int], Image]: |
| 126 | + """Get visualization of the document as images by page.""" |
| 127 | + base_images = ( |
| 128 | + self.base_visualizer.get_visualization(doc=doc, **kwargs) |
| 129 | + if self.base_visualizer |
| 130 | + else None |
| 131 | + ) |
| 132 | + return self._draw_doc_tables( |
| 133 | + doc=doc, |
| 134 | + images=base_images, |
| 135 | + ) |
0 commit comments