diff --git a/test_unstructured/partition/test_google_vision_bounds.py b/test_unstructured/partition/test_google_vision_bounds.py new file mode 100644 index 0000000000..2eb87e2964 --- /dev/null +++ b/test_unstructured/partition/test_google_vision_bounds.py @@ -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"] diff --git a/unstructured/partition/utils/ocr_models/google_vision_ocr.py b/unstructured/partition/utils/ocr_models/google_vision_ocr.py index 852f973ba4..f10a4ed585 100644 --- a/unstructured/partition/utils/ocr_models/google_vision_ocr.py +++ b/unstructured/partition/utils/ocr_models/google_vision_ocr.py @@ -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,