Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vision - Cannot pass ImageContext parameter to document_text_detection #6387

Closed
sheshkovsky opened this issue Nov 5, 2018 · 1 comment
Closed
Assignees
Labels
api: vision Issues related to the Cloud Vision API. type: question Request for information or clarification. Not an issue.

Comments

@sheshkovsky
Copy link

I want to pass languageHints parameter to document_text_detection or (client.annotate_image) as described here but I get error with following Traceback:

  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)

@tseaver tseaver added api: vision Issues related to the Cloud Vision API. type: question Request for information or clarification. Not an issue. labels Nov 5, 2018
@tseaver
Copy link
Contributor

tseaver commented Nov 5, 2018

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: vision Issues related to the Cloud Vision API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants