Using the Cloud Vision API on App Engine
Here’s a quick snippet of how to use the Cloud Vision API on App Engine. It’s pretty straight forward, the only tricky thing is making sure you set the Content-Type
.
import json
from logging import info
from urllib import urlencode
from google.appengine.api import urlfetch
headers = {'Content-Type': 'application/json'}
url = 'https://vision.googleapis.com/v1/images:annotate?%s' % urlencode({
'key': '<google_api_key>'
})
payload = json.dumps({
'requests': [
{
'image': {
'source': {
'gcsImageUri': 'gs://bucket/path/to/file.jpg'
}
},
'features': [
{'type': 'LABEL_DETECTION', 'maxResults': 10}
]
}
]
})
response = urlfetch.fetch(url, deadline = 30, method = urlfetch.POST, headers = headers, payload = payload)
response_data =
...