Created
May 30, 2016 23:29
-
-
Save tedder/cdbb57bf5bd08b7e6562f1614312f537 to your computer and use it in GitHub Desktop.
google cloud vision starter script.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # these should be sufficient to get you started. | |
| # https://cloud.google.com/vision/reference/rest/v1/images/annotate#request-body | |
| # https://cloud.google.com/vision/docs/pricing | |
| # https://cloud.google.com/vision/docs/auth-template/cloud-api-auth#setting_up_your_project | |
| from oauth2client.service_account import ServiceAccountCredentials | |
| from apiclient.discovery import build | |
| import sys | |
| import json | |
| from httplib2 import Http | |
| import base64 | |
| credentials = ServiceAccountCredentials.from_json_keyfile_name( | |
| '/Users/ted/.ssh/google-api-project.key.json', | |
| ['https://www.googleapis.com/auth/cloud-platform']) | |
| http_auth = credentials.authorize(Http()) | |
| service = build('vision', 'v1', http=http_auth) | |
| with open(sys.argv[1], "rb") as img: | |
| data = { | |
| "requests": [ | |
| { | |
| "image": { | |
| "content": base64.b64encode(img.read()).decode('UTF-8') | |
| }, | |
| "features": [ | |
| { | |
| "type": "LABEL_DETECTION", | |
| "maxResults": 10 | |
| }, | |
| { | |
| "type": "FACE_DETECTION", | |
| "maxResults": 10 | |
| }, | |
| { | |
| "type": "LANDMARK_DETECTION", | |
| "maxResults": 10 | |
| }, | |
| { | |
| "type": "LOGO_DETECTION", | |
| "maxResults": 10 | |
| }, | |
| { | |
| "type": "TEXT_DETECTION", | |
| "maxResults": 10 | |
| }, | |
| { | |
| "type": "SAFE_SEARCH_DETECTION", | |
| "maxResults": 10 | |
| }, | |
| { | |
| "type": "IMAGE_PROPERTIES", | |
| "maxResults": 10 | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| #ret = service.images().annotate(data) | |
| images = service.images() | |
| print(dir(images)) | |
| #imagerequest = images.annotate(body=data) | |
| #imageresult = imagerequest.execute() | |
| ret = images.annotate(body=data).execute() | |
| #ret = imageresult.getContent() | |
| print ret | |
| label = ret['responses'][0]['labelAnnotations'][0]['description'] | |
| print('Found label: %s' % (label)) | |
| for label in ret['responses'][0]['labelAnnotations']: | |
| print label | |
| print json.dumps(ret, indent=2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment