Skip to content
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

Fix/pdf miner source property #228

Merged
merged 9 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.6.5-dev0

* Fix `source` property for elements generated by pdfminer.
* Add 'OCR-tesseract' and 'OCR-paddle' as sources for elements generated by OCR.

## 0.6.4

* add a function to automatically scale table crop images based on text height so the text height is optimum for `tesseract` OCR task
Expand Down
15 changes: 15 additions & 0 deletions test_unstructured_inference/inference/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from unstructured_inference.constants import OCRMode
from unstructured_inference.inference import elements, layout, layoutelement
from unstructured_inference.models import chipper, detectron2, tesseract
from unstructured_inference.models.base import get_model
from unstructured_inference.models.unstructuredmodel import (
UnstructuredElementExtractionModel,
UnstructuredObjectDetectionModel,
Expand Down Expand Up @@ -117,6 +118,19 @@ def detect(self, *args):
assert elements.ocr(text_block, image=image) == ""


def test_ocr_source():
file = "sample-docs/loremipsum-flat.pdf"
model = get_model("yolox_tiny")
doc = layout.DocumentLayout.from_file(
file,
model,
ocr_mode=OCRMode.FULL_PAGE.value,
supplement_with_ocr_elements=True,
ocr_strategy="force",
)
assert "OCR-tesseract" in {e.source for e in doc.pages[0].elements}


class MockLayoutModel:
def __init__(self, layout):
self.layout_return = layout
Expand Down Expand Up @@ -678,6 +692,7 @@ def test_ocr_image(region, objects, ocr_strategy, expected):
@pytest.mark.parametrize("filename", ["loremipsum.pdf", "IRS-form-1987.pdf"])
def test_load_pdf(filename):
layouts, images = layout.load_pdf(f"sample-docs/{filename}")
assert "pdfminer" in {e.source for e in layouts[0]}
assert len(layouts)
for lo in layouts:
assert len(lo)
Expand Down
2 changes: 1 addition & 1 deletion unstructured_inference/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.4" # pragma: no cover
__version__ = "0.6.5-dev0" # pragma: no cover
13 changes: 10 additions & 3 deletions unstructured_inference/inference/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,14 @@ def load_pdf(
else:
continue

text_region = element_class(x1 * coef, y1 * coef, x2 * coef, y2 * coef, text=_text)
text_region = element_class(
x1 * coef,
y1 * coef,
x2 * coef,
y2 * coef,
text=_text,
source="pdfminer",
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we have those string constants defined as constants? this is so that user can import the names instead of need to read the code to find out how they are spelled and what options are there.

Copy link
Contributor Author

@benjats07 benjats07 Sep 26, 2023

Choose a reason for hiding this comment

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

Sure, but...will be a special case: the merged type in

https://github.com/Unstructured-IO/unstructured-inference/blob/2de61a4d5c60dfda4d789e7d7fdcdf49f1f04960/unstructured_inference/inference/layoutelement.py#L314C62-L314C62

If we want to transform this into constants, then the source for those elements needs to be just "merged" 🤔 (losing the source of origin elements), any ideas about how to solve this? (apart from enumerating all possible combinations)

Edit: I added other attribute to merged elements, but not sure if is the best approach (see

setattr(element, "merged_sources", sources)
)

)

if text_region.area > 0:
layout.append(text_region)
Expand Down Expand Up @@ -738,7 +745,7 @@ def parse_ocr_data_tesseract(ocr_data: dict) -> List[TextRegion]:
(x1, y1, x2, y2) = l, t, l + w, t + h
text = ocr_data["text"][i]
if text:
text_region = TextRegion(x1, y1, x2, y2, text=text, source="OCR")
text_region = TextRegion(x1, y1, x2, y2, text=text, source="OCR-tesseract")
text_regions.append(text_region)

return text_regions
Expand Down Expand Up @@ -774,7 +781,7 @@ def parse_ocr_data_paddle(ocr_data: list) -> List[TextRegion]:
y2 = max([i[1] for i in line[0]])
text = line[1][0]
if text:
text_region = TextRegion(x1, y1, x2, y2, text)
text_region = TextRegion(x1, y1, x2, y2, text, source="OCR-paddle")
text_regions.append(text_region)

return text_regions
2 changes: 1 addition & 1 deletion unstructured_inference/inference/layoutelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def get_elements_from_ocr_regions(ocr_regions: List[TextRegion]) -> List[LayoutE
r.x2,
r.y2,
text=r.text,
source=None,
source=r.source,
type="UncategorizedText",
)
for r in merged_regions
Expand Down