Skip to content

Instantly share code, notes, and snippets.

@jybaek
Created June 19, 2017 23:43
Show Gist options
  • Save jybaek/08924e89e3f02b1a28f557c9265980a9 to your computer and use it in GitHub Desktop.
Save jybaek/08924e89e3f02b1a28f557c9265980a9 to your computer and use it in GitHub Desktop.
google cloud platform (vision api)
import io
import os
# export GOOGLE_APPLICATION_CREDENTIALS=/home/oops/github/gcloud/GCP-ML-8492a87b7f32.json
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
vision_client = vision.Client()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'test.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(
content=content)
# Performs label detection on the image file
labels = image.detect_labels()
print('Labels:')
for label in labels:
print(label.description)
faces = image.detect_faces()
print('Faces:')
for face in faces:
print('anger: {}'.format(face.emotions.anger))
print('joy: {}'.format(face.emotions.joy))
print('surprise: {}'.format(face.emotions.surprise))
vertices = (['({},{})'.format(bound.x_coordinate, bound.y_coordinate)
for bound in face.bounds.vertices])
print('face bounds: {}'.format(','.join(vertices)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment