Google-cloud-python: Vision - Cannot pass ImageContext parameter to document_text_detection

Created on 5 Nov 2018  路  1Comment  路  Source: googleapis/google-cloud-python

I want to pass languageHints parameter to document_text_detection or (client.annotate_image) as described here but I get error with following Traceback:
```Traceback (most recent call last):
File ".../virtual/lib/python3.6/site-packages/google/protobuf/internal/python_message.py", line 545, in _GetFieldByName
return message_descriptor.fields_by_name[field_name]
KeyError: 'imageContext'

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../common/google_vision.py", line 55, in ocr_by_vision
resp = client.document_text_detection(image=image, imageContext={"languageHints": ["it"]})
File ".../virtual/lib/python3.6/site-packages/google/cloud/vision_helpers/decorators.py", line 111, in inner
response = self.annotate_image(request, retry=retry, timeout=timeout)
File ".../virtual/lib/python3.6/site-packages/google/cloud/vision_helpers/__init__.py", line 67, in annotate_image
r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
File ".../virtual/lib/python3.6/site-packages/google/cloud/vision_v1/gapic/image_annotator_client.py", line 218, in batch_annotate_images
requests=requests, )
File ".../virtual/lib/python3.6/site-packages/google/protobuf/internal/python_message.py", line 503, in init
copy.add(val)
File ".../virtual/lib/python3.6/site-packages/google/protobuf/internal/containers.py", line 372, in add
new_element = self._message_descriptor._concrete_class(
kwargs)
File ".../virtual/lib/python3.6/site-packages/google/protobuf/internal/python_message.py", line 484, in init
field = _GetFieldByName(message_descriptor, field_name)
File ".../virtual/lib/python3.6/site-packages/google/protobuf/internal/python_message.py", line 548, in _GetFieldByName
(message_descriptor.name, field_name))
ValueError: Protocol message AnnotateImageRequest has no "imageContext" field.

To reproduce I tried :

from google.cloud import vision
from google.cloud.vision import types as vision_types

path = '/tmp/0.jpg'
client = vision.ImageAnnotatorClient()


with open(path, 'rb') as image_file:
content = image_file.read()
image = vision_types.Image(content=content)
resp = client.document_text_detection(image=image, imageContext={"languageHints": ["it"]})

or

req = {
"image": {"source": {'filename': path}},
"features": [
{"type": 1},
],
"imageContext": {
"languageHints": ["it"]
}
}
resp = client.annotate_image(request=req)
```
Python Version: 3.6.4
google-cloud-vision Version: 0.34.0
OS: MacOS 10.14

Is there anyway to pass imageContext optional parameters? (I guess some fix like this is needed)

question vision

Most helpful comment

@goolila The link you reference above is for the REST version of the Vision API, but google-cloud-vision wraps only the RPC version, which uses names_with_underscores rather than namesWithCamelCase. Your examples should therefore read::



with open(path, 'rb') as image_file:
    content = image_file.read()
image = vision_types.Image(content=content)
resp = client.document_text_detection(
    image=image, image_context={"language_hints": ["it"]})

or:

req = {
    "image": {"source": {'filename': path}},
    "features": [
        {"type": 1},
    ],
    "image_context": {
        "language_hints": ["it"]
    }
}
resp = client.annotate_image(request=req)

>All comments

@goolila The link you reference above is for the REST version of the Vision API, but google-cloud-vision wraps only the RPC version, which uses names_with_underscores rather than namesWithCamelCase. Your examples should therefore read::



with open(path, 'rb') as image_file:
    content = image_file.read()
image = vision_types.Image(content=content)
resp = client.document_text_detection(
    image=image, image_context={"language_hints": ["it"]})

or:

req = {
    "image": {"source": {'filename': path}},
    "features": [
        {"type": 1},
    ],
    "image_context": {
        "language_hints": ["it"]
    }
}
resp = client.annotate_image(request=req)
Was this page helpful?
0 / 5 - 0 ratings