Skip to content

Commit

Permalink
feat: add convert_document_to_annotate_file_json (#124)
Browse files Browse the repository at this point in the history
* feat: add convert_document_to_annotate_file_json

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
galz10 and gcf-owl-bot[bot] authored Jun 7, 2023
1 parent 70106c1 commit a6b75fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions google/cloud/documentai_toolbox/wrappers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,19 @@ def convert_document_to_annotate_file_response(self) -> AnnotateFileResponse:
"""
return _convert_to_vision_annotate_file_response(self.text, self.pages)

def convert_document_to_annotate_file_json_response(self) -> str:
r"""Convert OCR data from Document.proto to JSON str of AnnotateFileResponse for Vision API.
Args:
None.
Returns:
str:
JSON string of TextAnnotations.
"""
return AnnotateFileResponse.to_json(
_convert_to_vision_annotate_file_response(self.text, self.pages)
)

def export_images(
self, output_path: str, output_file_prefix: str, output_file_extension: str
) -> List[str]:
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,19 @@ def test_convert_document_to_annotate_file_response():
assert actual == expected


def test_convert_document_to_annotate_file_json_response():
doc = document.Document.from_document_path(
document_path="tests/unit/resources/0/toolbox_invoice_test-0.json"
)

actual = doc.convert_document_to_annotate_file_json_response()

with open("tests/unit/resources/toolbox_invoice_test-0-vision.json", "r") as f:
expected = f.read()

assert actual == expected


def test_export_images(get_bytes_images_mock):
doc = document.Document.from_gcs(
gcs_bucket_name="test-directory", gcs_prefix="documentai/output/123456789/0"
Expand Down

0 comments on commit a6b75fc

Please sign in to comment.