Skip to content
Draft
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
50 changes: 50 additions & 0 deletions test_unstructured/partition/test_google_vision_bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import numpy as np
import pytest
from google.cloud.vision import TextAnnotation

from unstructured.partition.utils.ocr_models.google_vision_ocr import OCRAgentGoogleVision


@pytest.mark.parametrize(
("vertices", "expected"),
[
([(20, 0), (100, 40), (80, 80), (0, 40)], [0, 0, 100, 80]),
([(100, 80), (0, 80), (0, 0), (100, 0)], [0, 0, 100, 80]),
([(0, 20), (40, 0), (80, 80), (40, 100)], [0, 0, 80, 100]),
([(10, 20), (90, 20), (90, 80), (10, 80)], [10, 20, 90, 80]),
],
)
def test_rotated_paragraph_bounds_include_all_vertices(vertices, expected):
document = TextAnnotation(
pages=[
{
"blocks": [
{
"paragraphs": [
{
"bounding_box": {
"vertices": [{"x": x, "y": y} for x, y in vertices]
},
"words": [
{
"symbols": [
{
"text": "x",
"property": {"detected_break": {"type_": 5}},
}
]
}
],
}
]
}
]
}
]
)
agent = object.__new__(OCRAgentGoogleVision)

regions = agent._parse_regions(document)

np.testing.assert_array_equal(regions.element_coords, [expected])
assert list(regions.texts) == ["x"]
6 changes: 4 additions & 2 deletions unstructured/partition/utils/ocr_models/google_vision_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ def _parse_regions(self, ocr_data: TextAnnotation) -> TextRegions:
for block in page.blocks:
for paragraph in block.paragraphs:
vertices = paragraph.bounding_box.vertices
x1, y1 = vertices[0].x, vertices[0].y
x2, y2 = vertices[2].x, vertices[2].y
# Vertex order follows the text's orientation, not the image axes. Taking
# only opposite corners crops rotated text and reverses 180-degree boxes.
x1, y1 = min(v.x for v in vertices), min(v.y for v in vertices)
x2, y2 = max(v.x for v in vertices), max(v.y for v in vertices)
text_region = build_text_region_from_coords(
x1,
y1,
Expand Down