4
votes

I'm attempting to use the Cloud Vision API client to detect 'labels' and faces in an image.

I can detect labels like this with the key line of code being:

response = client.label_detection(image=image)
labels = response.label_annotations

and for detecting faces:

response = client.face_detection(image=image)
faces = response.face_annotations

So, currently I can make two API calls to get the information I need but I'd like to combine them into one API call if possible.

Update:

I found the annotate_image() method that can accept a list of features on this page:

response = client.annotate_image({
    'image': {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}},
    'features': [{'type': vision.enums.Feature.Type.FACE_DETECTION}],
})

But the image source only accepts a URL to the image or its path on Google Cloud. I want to run analysis on images that I have stored locally, is this possible?

3

3 Answers

3
votes

What you are looking for is called Batching Request, it allows you to send multiple files or multiple features in one single API request. Take a look at Batching Requests documentation.

To use multiple features in one API request, take a look at this Cloud Vision API Feature documentation. You will find all type of different features that you can request.

I did a little bit of coding myself and it worked for me. It reads an image from local storage and prints the LABEL and FACE detection results in one single API request. You can find my GitHub code here.

1
votes

It's possible to use local images. You have to put the image bytes ("content") in the request instead of "source". https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageRequest#Image

0
votes

Here is the code for v2 API:

import io
from google.cloud import vision
from google.protobuf.json_format import MessageToDict, MessageToJson


with io.open("a_pic.jpg", "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

request = {
    "image": image,
    "features": [
        {"type_": vision.Feature.Type.FACE_DETECTION},
        {"type_": vision.Feature.Type.LABEL_DETECTION},
        {"type_": vision.Feature.Type.IMAGE_PROPERTIES},
    ],
}

response = client.annotate_image(request)
print(MessageToDict(response._pb))