Skip to content
Merged
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
6 changes: 3 additions & 3 deletions system_tests/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ def test_detect_full_text_content(self):
client = Config.CLIENT
with open(FULL_TEXT_FILE, 'rb') as image_file:
image = client.image(content=image_file.read())
full_text = image.detect_full_text()
full_text = image.detect_full_text(language_hints=['en'])
self._assert_full_text(full_text)

def test_detect_full_text_filename(self):
client = Config.CLIENT
image = client.image(filename=FULL_TEXT_FILE)
full_text = image.detect_full_text()
full_text = image.detect_full_text(language_hints=['en'])
self._assert_full_text(full_text)

def test_detect_full_text_gcs(self):
Expand All @@ -123,7 +123,7 @@ def test_detect_full_text_gcs(self):

client = Config.CLIENT
image = client.image(source_uri=source_uri)
full_text = image.detect_full_text()
full_text = image.detect_full_text(language_hints=['en'])
self._assert_full_text(full_text)


Expand Down
19 changes: 15 additions & 4 deletions vision/google/cloud/vision/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,28 @@ def detect_faces(self, limit=10):
annotations = self.detect(features)
return annotations[0].faces

def detect_full_text(self, limit=10):
def detect_full_text(self, language_hints=None, limit=10):
"""Detect a full document's text.

:type language_hints: list
:param language_hints: (Optional) A list of BCP-47 language codes. See:
https://cloud.google.com/vision/docs/languages

:type limit: int
:param limit: The number of documents to detect.
:param limit: (Optional) The number of documents to detect.

:rtype: list
:returns: List of :class:`~google.cloud.vision.text.TextAnnotation`.
"""
features = [Feature(FeatureTypes.DOCUMENT_TEXT_DETECTION, limit)]
annotations = self.detect(features)
feature_type = image_annotator_pb2.Feature.DOCUMENT_TEXT_DETECTION

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

feature = image_annotator_pb2.Feature(type=feature_type,
max_results=limit)
image = _to_gapic_image(self)
image_context = image_annotator_pb2.ImageContext(
language_hints=language_hints)
request = image_annotator_pb2.AnnotateImageRequest(
image=image, features=[feature], image_context=image_context)
annotations = self._detect_annotation_from_pb([request])
return annotations[0].full_texts

def detect_labels(self, limit=10):
Expand Down
8 changes: 6 additions & 2 deletions vision/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def test_detect_full_text_annotation(self):
api = client._vision_api
api._connection = _Connection(returned)
image = client.image(source_uri=IMAGE_SOURCE)
full_text = image.detect_full_text(limit=2)
full_text = image.detect_full_text(language_hints=['en'], limit=2)

self.assertIsInstance(full_text, TextAnnotation)
self.assertEqual(full_text.text, 'The Republic\nBy Plato')
Expand All @@ -324,7 +324,11 @@ def test_detect_full_text_annotation(self):

image_request = api._connection._requested[0]['data']['requests'][0]
self.assertEqual(
image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE)
image_request['image']['source']['gcsImageUri'], IMAGE_SOURCE)
self.assertEqual(
len(image_request['imageContext']['languageHints']), 1)
self.assertEqual(
image_request['imageContext']['languageHints'][0], 'en')
self.assertEqual(image_request['features'][0]['maxResults'], 2)
self.assertEqual(
image_request['features'][0]['type'], 'DOCUMENT_TEXT_DETECTION')
Expand Down