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
40 changes: 40 additions & 0 deletions test_unstructured/partition/test_google_vision_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest.mock import Mock

import pytest
from google.cloud.vision import AnnotateImageResponse, TextAnnotation
from PIL import Image

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


def _agent(response):
agent = object.__new__(OCRAgentGoogleVision)
agent.language = "en"
agent.client = Mock()
agent.client.document_text_detection.return_value = response
return agent


def test_layout_elements_use_one_annotation_response_for_text_and_regions():
response = AnnotateImageResponse(full_text_annotation=TextAnnotation(text=""))
agent = _agent(response)

layout = agent.get_layout_elements_from_image(Image.new("RGB", (4, 4)))

assert len(layout) == 0
assert agent.client.document_text_detection.call_count == 1
request = agent.client.document_text_detection.call_args.kwargs
assert list(request["image_context"].language_hints) == ["en"]
assert request["image"].content.startswith(b"\x89PNG")


@pytest.mark.parametrize(
"method", ["get_text_from_image", "get_layout_from_image", "get_layout_elements_from_image"]
)
def test_service_errors_are_not_silently_treated_as_empty_documents(method):
agent = _agent(AnnotateImageResponse(error={"code": 7, "message": "Permission denied"}))

with pytest.raises(RuntimeError, match=r"Google Vision OCR failed \(7\): Permission denied"):
getattr(agent, method)(Image.new("RGB", (4, 4)))

assert agent.client.document_text_detection.call_count == 1
29 changes: 13 additions & 16 deletions unstructured/partition/utils/ocr_models/google_vision_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,27 @@ def is_text_sorted(self) -> bool:
return True

def get_text_from_image(self, image: PILImage.Image) -> str:
return self._detect_document(image).text

def _detect_document(self, image: PILImage.Image) -> TextAnnotation:
"""Request OCR once and reject service errors before interpreting the annotation."""
image_context = ImageContext(language_hints=[self.language]) if self.language else None
with BytesIO() as buffer:
image.save(buffer, format="PNG")
response = self.client.document_text_detection(
image=Image(content=buffer.getvalue()), image_context=image_context
)
if response.error.code:
raise RuntimeError(
f"Google Vision OCR failed ({response.error.code}): {response.error.message}"
)
document = response.full_text_annotation
assert isinstance(document, TextAnnotation)
return document.text
return document

def get_layout_from_image(self, image: PILImage.Image) -> TextRegions:
trace_logger.detail("Processing entire page OCR with Google Vision API...")
image_context = ImageContext(language_hints=[self.language]) if self.language else None
with BytesIO() as buffer:
image.save(buffer, format="PNG")
response = self.client.document_text_detection(
image=Image(content=buffer.getvalue()), image_context=image_context
)
document = response.full_text_annotation
assert isinstance(document, TextAnnotation)
document = self._detect_document(image)
regions = self._parse_regions(document)
return regions

Expand All @@ -62,15 +63,11 @@ def get_layout_elements_from_image(self, image: PILImage.Image) -> LayoutElement
build_layout_elements_from_ocr_regions,
)

ocr_regions = self.get_layout_from_image(
image,
)
ocr_text = self.get_text_from_image(
image,
)
document = self._detect_document(image)
ocr_regions = self._parse_regions(document)
return build_layout_elements_from_ocr_regions(
ocr_regions=ocr_regions,
ocr_text=ocr_text,
ocr_text=document.text,
group_by_ocr_text=False,
)

Expand Down