Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions lib/sycamore/sycamore/transforms/detr_partitioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
from pypdf import PdfReader

from aryn_sdk.partition import partition_file
from sycamore.data import Element, BoundingBox, ImageElement, TableElement
from sycamore.data import Element, BoundingBox, TableElement
from sycamore.data.document import DocumentPropertyTypes
from sycamore.data.element import create_element
from sycamore.transforms.table_structure.extract import DEFAULT_TABLE_STRUCTURE_EXTRACTOR
from sycamore.utils import choose_device
from sycamore.utils.element_sort import sort_page
from sycamore.utils.cache import Cache
from sycamore.utils.image_utils import crop_to_bbox, image_to_bytes
from sycamore.utils.image_utils import crop_to_bbox, extract_images_from_elements
from sycamore.utils.import_utils import requires_modules
from sycamore.utils.markdown import elements_to_markdown
from sycamore.utils.memory_debugging import display_top, gc_tensor_dump
Expand Down Expand Up @@ -538,15 +538,7 @@ def process_batch_extraction(
with LogTime("extract_images_batch"):
for i, page_elements in enumerate(deformable_layout):
image = batch[i]
for element in page_elements:
if isinstance(element, ImageElement) and element.bbox is not None:
cropped_image = crop_to_bbox(image, element.bbox).convert("RGB")
resolved_format = None if extract_image_format == "PPM" else extract_image_format
element.binary_representation = image_to_bytes(cropped_image, format=resolved_format)
element.image_mode = cropped_image.mode
element.image_size = cropped_image.size
element.image_format = resolved_format

deformable_layout[i] = extract_images_from_elements(page_elements, image, extract_image_format)
return deformable_layout

def process_batch(
Expand Down
32 changes: 32 additions & 0 deletions lib/sycamore/sycamore/utils/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sycamore.data import Document
from sycamore.data.bbox import BoundingBox
from sycamore.data.document import DocumentPropertyTypes
from sycamore.data.element import ImageElement, Element

DEFAULT_PADDING = 10

Expand Down Expand Up @@ -271,3 +272,34 @@ def show_images(images: Union[Image.Image, list[Image.Image], list[ImageFile.Ima
else:
for image in images:
image.show()


def extract_image_from_element(
element: ImageElement, page_image: Image.Image, extract_image_format: str
) -> ImageElement:
"""Extracts the image from an element."""
assert element.bbox is not None, "Element must have a bounding box"
cropped_image = crop_to_bbox(page_image, element.bbox).convert("RGB")
resolved_format = None if extract_image_format == "PPM" else extract_image_format
element.binary_representation = image_to_bytes(cropped_image, format=resolved_format)
element.image_mode = cropped_image.mode
element.image_size = cropped_image.size
element.image_format = resolved_format
return element


def extract_images_from_elements(
elements: list[Element], image: Image.Image, extract_image_format: str
) -> list[Element]:
"""Extracts the images from a list of elements.

This function is used to extract the images from a list of elements.
Args:
elements: A list of elements.
image: The image to extract the images from.
extract_image_format: The format to extract the images in.
"""
for i, element in enumerate(elements):
if isinstance(element, ImageElement) and element.bbox is not None:
elements[i] = extract_image_from_element(element, image, extract_image_format)
return elements
Loading