3

I am using Python Client for Google Cloud Vision API, basically same code as in documentation http://google-cloud-python.readthedocs.io/en/latest/vision/

>>> from google.cloud import vision
>>> client = vision.ImageAnnotatorClient()
>>> response = client.annotate_image({
...   'image': {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}},
...   'features': [{'type': vision.enums.Feature.Type.FACE_DETECTOIN}],
... })

problem is that response doesn't have field "annotations" (as it is documentation) but based on documentation has field for each "type". so when I try to get response.face_annotations I get and basically I don't know how to extract result from Vision API from response (AnnotateImageResponse) to get something like json/dictionary like data. version of google-cloud-vision is 0.25.1 and it was installed as full google-cloud library (pip install google-cloud). I think today is not my day I appreciate any clarification / help

zdenulo
  • 346
  • 3
  • 14

3 Answers3

1

Hm. It is a bit tricky, but the API is pretty great overall. You can actually directly call the face detection interface, and it'll spit back exactly what you want - a dictionary with all the info.

from google.cloud import vision
from google.cloud.vision import types

img = 'YOUR_IMAGE_URL'
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = img
faces = client.face_detection(image=image).face_annotations
print faces 
LeKhan9
  • 1,300
  • 1
  • 5
  • 15
0

Above answers wont help because delta in improvisation is happening which you can say reality vs theoretical.

The vision response is not json type, it is just the customized class type which is perfect for vision calls.

So after much research, I conjured this solution and it works

Here is the solution

Convert this output to ProtoBuff and then to json, it will be simple extraction.

def json_to_hash_dump(vision_response):
    """
    a function defined to take a convert the 
    response from vision api to json object  transformation via protoBuff 

    Args: 
            vision_response 
    Returns: 
            json_object
    """
    from google.protobuf.json_format import MessageToJson
    json_obj = MessageToJson((vision_response._pb))
    # to dict items 
    r = json.loads(json_obj)
    return r
TheExorcist
  • 1,966
  • 1
  • 19
  • 25
-1

well alternative is to use Python API Google client, example is here https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/vision/api/label/label.py

zdenulo
  • 346
  • 3
  • 14