Skip to content

Conversation

@qued
Copy link
Contributor

@qued qued commented Apr 14, 2023

Added the capability to partition granular-scale elements that have been identified (words, characters) by proximity using the word/character height as a reference. In many cases this does a good job of grouping text blocks. Also moved logic for extracting text from a region into the region itself, so in the future different logic can be used for embedded text and images.

Testing:

In a jupyter notebook, try the following code:

from unstructured_inference.inference.layout import load_pdf
from unstructured_inference.inference.elements import partition_groups_from_regions

words, images = load_pdf('sample-docs/layout-parser-paper.pdf')

colors = ["red", "green", "blue", "cyan", "yellow", "magenta", "purple"] * 8

from PIL.ImageDraw import ImageDraw
def annotate_regions(rects, image, color):
    img = image.copy()
    draw = ImageDraw(img)
    for rect in rects:
        x1, y1, x2, y2 = rect.x1, rect.y1, rect.x2, rect.y2
        draw.rectangle(((x1, y1), (x2, y2)), outline=color)
    return img

def annotate_groups(groups, image):
    out_image = image.copy()
    for i, group in enumerate(groups):
        out_image = annotate_regions(group, out_image, colors[i])
    return out_image
 
groups = partition_groups_from_regions(words[0])
annotate_groups(groups, images[0])

The output should be the first page of the layout parser paper with color-coded groupings of the words in the page, representing the partition produced.

@qued qued requested a review from MthwRobinson April 14, 2023 05:20
Copy link
Contributor

@MthwRobinson MthwRobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a couple of inline question, but overall looks good. I also keep getting the following error message on the test code.

Also, I'm getting this error on the test code. I'm able to run the unstructured and unstructured-inference tests in the same environment though and the test code works in iPython so wondering if it's something related to my jupyter version.

ImportError: dlopen(/Users/mrobinson/.pyenv/versions/inference/lib/python3.8/site-packages/detectron2/_C.cpython-38-darwin.so, 0x0002): Symbol not found: __ZN2at4_ops10select_int4callERKNS_6TensorExx
  Referenced from: /Users/mrobinson/.pyenv/versions/3.8.13/envs/inference/lib/python3.8/site-packages/detectron2/_C.cpython-38-darwin.so
  Expected in: /Users/mrobinson/.pyenv/versions/3.8.13/envs/inference/lib/python3.8/site-packages/torch/lib/libtorch_cpu.dylib

Copy link
Contributor

@MthwRobinson MthwRobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending a comment on the coefficients!

@qued qued merged commit 4e7464f into main Apr 14, 2023
@qued qued deleted the refactor/partition-in-elements branch April 14, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants