Skip to content

Instantly share code, notes, and snippets.

@hongseok
Last active April 2, 2016 01:08
Show Gist options
  • Save hongseok/7bc4a27ed6de9afefb144af57ab74324 to your computer and use it in GitHub Desktop.
Save hongseok/7bc4a27ed6de9afefb144af57ab74324 to your computer and use it in GitHub Desktop.
Google Vision API Python Sample
#!/usr/bin/python
import json
import urllib2
import base64
API_KEY = "API_KEY"
API_URL = "https://vision.googleapis.com/v1/images:annotate?key=" + API_KEY
with open("faulkner.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
features = [{"type": "LABEL_DETECTION", "maxResults": 10}]
image = {"content": encoded_string}
request = {"image" : image, "features" : features}
body = { "requests": [request] }
req = urllib2.Request(API_URL)
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(body))
print response.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment