Skip to content

Commit b8b6446

Browse files
committed
Add language_hints support to detect_full_text.
1 parent 45c6a0a commit b8b6446

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

system_tests/vision.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ def test_detect_full_text_content(self):
102102
client = Config.CLIENT
103103
with open(FULL_TEXT_FILE, 'rb') as image_file:
104104
image = client.image(content=image_file.read())
105-
full_text = image.detect_full_text()
105+
full_text = image.detect_full_text(language_hints=['en'])
106106
self._assert_full_text(full_text)
107107

108108
def test_detect_full_text_filename(self):
109109
client = Config.CLIENT
110110
image = client.image(filename=FULL_TEXT_FILE)
111-
full_text = image.detect_full_text()
111+
full_text = image.detect_full_text(language_hints=['en'])
112112
self._assert_full_text(full_text)
113113

114114
def test_detect_full_text_gcs(self):
@@ -123,7 +123,7 @@ def test_detect_full_text_gcs(self):
123123

124124
client = Config.CLIENT
125125
image = client.image(source_uri=source_uri)
126-
full_text = image.detect_full_text()
126+
full_text = image.detect_full_text(language_hints=['en'])
127127
self._assert_full_text(full_text)
128128

129129

vision/google/cloud/vision/image.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,28 @@ def detect_faces(self, limit=10):
182182
annotations = self.detect(features)
183183
return annotations[0].faces
184184

185-
def detect_full_text(self, limit=10):
185+
def detect_full_text(self, language_hints=None, limit=10):
186186
"""Detect a full document's text.
187187
188+
:type language_hints: list
189+
:param language_hints: (Optional) A list of BCP-47 language codes. See:
190+
https://cloud.google.com/vision/docs/languages
191+
188192
:type limit: int
189-
:param limit: The number of documents to detect.
193+
:param limit: (Optional) The number of documents to detect.
190194
191195
:rtype: list
192196
:returns: List of :class:`~google.cloud.vision.text.TextAnnotation`.
193197
"""
194-
features = [Feature(FeatureTypes.DOCUMENT_TEXT_DETECTION, limit)]
195-
annotations = self.detect(features)
198+
feature_type = image_annotator_pb2.Feature.DOCUMENT_TEXT_DETECTION
199+
feature = image_annotator_pb2.Feature(type=feature_type,
200+
max_results=limit)
201+
image = _to_gapic_image(self)
202+
image_context = image_annotator_pb2.ImageContext(
203+
language_hints=language_hints)
204+
request = image_annotator_pb2.AnnotateImageRequest(
205+
image=image, features=[feature], image_context=image_context)
206+
annotations = self._detect_annotation_from_pb([request])
196207
return annotations[0].full_texts
197208

198209
def detect_labels(self, limit=10):

vision/unit_tests/test_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def test_detect_full_text_annotation(self):
309309
api = client._vision_api
310310
api._connection = _Connection(returned)
311311
image = client.image(source_uri=IMAGE_SOURCE)
312-
full_text = image.detect_full_text(limit=2)
312+
full_text = image.detect_full_text(language_hints=['en'], limit=2)
313313

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

325325
image_request = api._connection._requested[0]['data']['requests'][0]
326326
self.assertEqual(
327-
image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE)
327+
image_request['image']['source']['gcsImageUri'], IMAGE_SOURCE)
328+
self.assertEqual(
329+
len(image_request['imageContext']['languageHints']), 1)
330+
self.assertEqual(
331+
image_request['imageContext']['languageHints'][0], 'en')
328332
self.assertEqual(image_request['features'][0]['maxResults'], 2)
329333
self.assertEqual(
330334
image_request['features'][0]['type'], 'DOCUMENT_TEXT_DETECTION')

0 commit comments

Comments
 (0)