Skip to content

Instantly share code, notes, and snippets.

@shabububu
Last active March 6, 2019 17:53
Show Gist options
  • Save shabububu/50b00d9fc03290c21ed753518c92b2bc to your computer and use it in GitHub Desktop.
Save shabububu/50b00d9fc03290c21ed753518c92b2bc to your computer and use it in GitHub Desktop.
# originally taken and modified from:
# https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/CPB100/lab4c/mlapis.ipynb
# sudo apt-get install python3-pip
# pip3 install --upgrade google-api-python-client
# google2625857_student@training-vm:~$ more vision_test.py
APIKEY="SOMEKEY-HERE"
from googleapiclient.discovery import build
import base64
# Taken from: https://ww2db.com/image.php?image_id=4332
IMAGE="gs://qwiklabs-gcp-719849632f728829-img/air_marauder5.jpg"
vservice = build('vision', 'v1', developerKey=APIKEY)
request = vservice.images().annotate(body={
'requests': [{
'image': {
'source': {
'gcs_image_uri': IMAGE
}
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': 3,
}]
}],
})
responses = request.execute(num_retries=3)
print(responses)
'''
google2625857_student@training-vm:~$ python3 vision_test.py
{'responses': [{'faceAnnotations': [{'landmarks': [{'position': {'x': 187.05318, 'z': 0.0008621045, 'y': 186.72792}, 'type': 'LEFT_EYE'}, {'position': {'x': 238.9
238, 'z': -6.1807375, 'y': 192.39471}, 'type': 'RIGHT_EYE'}, {'position': {'x': 174.34836, 'z': 6.713543, 'y': 173.2517}, 'type': 'LEFT_OF_LEFT_EYEBROW'}, {'posit
ion': {'x': 201.59763, 'z': -11.051298, 'y': 176.02231}, 'type': 'RIGHT_OF_LEFT_EYEBROW'}, {'position': {'x': 225.16624, 'z': -13.893339, 'y': 179.41582}, 'type':
'LEFT_OF_RIGHT_EYEBROW'}, {'position': {'x': 258.30804, 'z': -2.4386592, 'y': 183.2594}, 'type': 'RIGHT_OF_RIGHT_EYEBROW'}, {'position': {'x': 211.10382, 'z': -1
3.52981, 'y': 187.78723}, 'type': 'MIDPOINT_BETWEEN_EYES'}, {'position': {'x': 206.54327, 'z': -28.607937, 'y': 214.36743}, 'type': 'NOSE_TIP'}, {'position': {'x'
: 206.09314, 'z': -17.716269, 'y': 232.88522}, 'type': 'UPPER_LIP'}, {'position': {'x': 203.54158, 'z': -14.388103, 'y': 253.50777}, 'type': 'LOWER_LIP'}, {'posit
ion': {'x': 184.1876, 'z': -0.9868826, 'y': 236.35771}, 'type': 'MOUTH_LEFT'}, {'position': {'x': 230.42896, 'z': -5.8275695, 'y': 242.75406}, 'type': 'MOUTH_RIGH
T'}, {'position': {'x': 205.463, 'z': -14.229888, 'y': 241.20724}, 'type': 'MOUTH_CENTER'}, {'position': {'x': 222.46991, 'z': -11.343588, 'y': 220.7621}, 'type':
'NOSE_BOTTOM_RIGHT'}, {'position': {'x': 193.81697, 'z': -7.786273, 'y': 217.92354}, 'type': 'NOSE_BOTTOM_LEFT'}, {'position': {'x': 206.41647, 'z': -17.616518,
'y': 222.66272}, 'type': 'NOSE_BOTTOM_CENTER'}, {'position': {'x': 188.74162, 'z': -3.5028992, 'y': 183.09929}, 'type': 'LEFT_EYE_TOP_BOUNDARY'}, {'position': {'x
': 198.22017, 'z': -1.2288369, 'y': 188.76624}, 'type': 'LEFT_EYE_RIGHT_CORNER'}, {'position': {'x': 187.04967, 'z': -0.6793399, 'y': 190.02841}, 'type': 'LEFT_EY
E_BOTTOM_BOUNDARY'}, {'position': {'x': 179.44627, 'z': 5.5101757, 'y': 185.80647}, 'type': 'LEFT_EYE_LEFT_CORNER'}, {'position': {'x': 187.81927, 'z': -1.5181799
, 'y': 186.46217}, 'type': 'LEFT_EYE_PUPIL'}, {'position': {'x': 237.56377, 'z': -9.421677, 'y': 189.0849}, 'type': 'RIGHT_EYE_TOP_BOUNDARY'}, {'position': {'x':
248.79169, 'z': -2.6116328, 'y': 194.7652}, 'type': 'RIGHT_EYE_RIGHT_CORNER'}, {'position': {'x': 238.8376, 'z': -6.6553683, 'y': 195.88316}, 'type': 'RIGHT_EYE_B
OTTOM_BOUNDARY'}, {'position': {'x': 227.57541, 'z': -4.7901783, 'y': 192.63214}, 'type': 'RIGHT_EYE_LEFT_CORNER'}, {'position': {'x': 237.90363, 'z': -7.6364317,
'y': 192.6175}, 'type': 'RIGHT_EYE_PUPIL'}, {'position': {'x': 188.23714, 'z': -5.4232583, 'y': 168.41728}, 'type': 'LEFT_EYEBROW_UPPER_MIDPOINT'}, {'position':
{'x': 241.17542, 'z': -11.807294, 'y': 174.90384}, 'type': 'RIGHT_EYEBROW_UPPER_MIDPOINT'}, {'position': {'x': 161.75961, 'z': 63.697933, 'y': 209.36774}, 'type':
'LEFT_EAR_TRAGION'}, {'position': {'x': 272.6492, 'z': 50.23729, 'y': 222.76157}, 'type': 'RIGHT_EAR_TRAGION'}, {'position': {'x': 213.292, 'z': -14.428018, 'y':
176.70872}, 'type': 'FOREHEAD_GLABELLA'}, {'position': {'x': 202.28642, 'z': -8.405014, 'y': 272.89215}, 'type': 'CHIN_GNATHION'}, {'position': {'x': 160.90475,
'z': 42.219437, 'y': 240.44077}, 'type': 'CHIN_LEFT_GONION'}, {'position': {'x': 260.93127, 'z': 30.056393, 'y': 252.78091}, 'type': 'CHIN_RIGHT_GONION'}], 'sorro
wLikelihood': 'VERY_UNLIKELY', 'tiltAngle': 1.5910228, 'surpriseLikelihood': 'VERY_UNLIKELY', 'joyLikelihood': 'VERY_LIKELY', 'underExposedLikelihood': 'VERY_UNLI
KELY', 'angerLikelihood': 'VERY_UNLIKELY', 'rollAngle': 7.181523, 'boundingPoly': {'vertices': [{'x': 133, 'y': 104}, {'x': 298, 'y': 104}, {'x': 298, 'y': 296},
{'x': 133, 'y': 296}]}, 'landmarkingConfidence': 0.6504338, 'fdBoundingPoly': {'vertices': [{'x': 147, 'y': 145}, {'x': 276, 'y': 145}, {'x': 276, 'y': 274}, {'x'
: 147, 'y': 274}]}, 'detectionConfidence': 0.9266158, 'panAngle': -6.868952, 'headwearLikelihood': 'VERY_UNLIKELY', 'blurredLikelihood': 'VERY_UNLIKELY'}]}]}
'''
# More basic tests...
# vservice = build('vision', 'v1', developerKey=APIKEY)
imgs=["gs://cloud-training-demos/images/met/APS6880.jpg", "gs://cloud-training-demos/images/met/DP205018.jpg", "gs://cloud-training-demos/images/met/DP290402.jpg", "gs://cloud-training-demos/images/met/DP700302.jpg"]
for img in imgs:
request = vservice.images().annotate(body={
'requests': [{
'image': {
'source': {
'gcs_image_uri': img
}
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': 3,
}]
}],
})
responses = request.execute(num_retries=3)
print(img)
print("happy: " + responses['responses'][0]['faceAnnotations'][0]['joyLikelihood'])
print("unhappy: " + responses['responses'][0]['faceAnnotations'][0]['sorrowLikelihood'])
'''
gs://cloud-training-demos/images/met/APS6880.jpg
happy: LIKELY
unhappy: VERY_UNLIKELY
gs://cloud-training-demos/images/met/DP205018.jpg
happy: LIKELY
unhappy: VERY_UNLIKELY
gs://cloud-training-demos/images/met/DP290402.jpg
happy: VERY_UNLIKELY
unhappy: VERY_UNLIKELY
gs://cloud-training-demos/images/met/DP700302.jpg
happy: VERY_UNLIKELY
unhappy: VERY_UNLIKELY
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment