Skip to content

Instantly share code, notes, and snippets.

@skoegl
Created September 4, 2017 21:41
Show Gist options
  • Save skoegl/a322a3b66f997ef0591c5a48f5da872c to your computer and use it in GitHub Desktop.
Save skoegl/a322a3b66f997ef0591c5a48f5da872c to your computer and use it in GitHub Desktop.
Using google cloud vision api on google app engine with python - bare metal
# -*- coding: utf-8 -*-
__author__ = "Stefan Kögl"
from json import dumps, loads
from google.appengine.api.urlfetch import fetch, POST
def vision_api_web_detection(uri):
"""This is the minimal code to accomplish a web detect request to the google vision api
You don't need 56 MiB of python client code installing 'google-cloud-vision' to accomplish
that task on google app engine, which does not even work.
.. TODO:: you should have secured your api key before you deploy this code snippet.
Please take a look at https://support.google.com/cloud/answer/6310037?hl=en
:param uri: the complete uri to compare against the web
:type uri: str
:return: the result dictionary
:rtype: dict
"""
api_key = "YourSecretApiKey"
payload = {
"requests": [
{
"image": {
"source": {
"image_uri": uri
}
},
"features": [
{
"type": "WEB_DETECTION"
}
]
}
]
}
response = fetch(
"https://vision.googleapis.com/v1/images:annotate?key=" + api_key,
method=POST,
payload=dumps(payload),
headers={"Content-Type": "application/json"}
)
result = loads(response.content)
return result["responses"][0]["webDetection"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment