in functions/image-analysis/python/main.py [0:0]
def vision_analysis(event, context):
print('Event: ' + json.dumps(event))
#event = json.load(event)
filename = event['name']
filebucket = event['bucket']
print('New picture uploaded ' + filename + ' in ' + filebucket)
# invoking the Vision API
client = vision.ImageAnnotatorClient()
response= json.loads(MessageToJson(client.annotate_image({
'image': {'source': {'image_uri': 'gs://'+filebucket+'/'+filename}},
'features': [
{'type_': vision.Feature.Type.LABEL_DETECTION},
{'type_': vision.Feature.Type.IMAGE_PROPERTIES},
{'type_': vision.Feature.Type.SAFE_SEARCH_DETECTION},
],
})))
print(response)
if 'error' not in response:
# listing the labels found in the picture
labels = [ desc['description'] for desc in sorted(response['labelAnnotations'], key=lambda x: x['score'], reverse=True) ]
print('Labels: ' + ", ".join(labels))
# retrieving the dominant color of the picture
color = sorted(response['imagePropertiesAnnotation']['dominantColors']['colors'], key=lambda x: x['score'], reverse=True)[0]['color']
colorHex = decColorToHex((0 if 'red' not in color else int(color['red']), 0 if 'green' not in color else int(color['green']), 0 if 'blue' not in color else int(color['blue'])))
print('Colors: ' + colorHex)
# determining if the picture is safe to show
safeSearch = response['safeSearchAnnotation']
isSafe = True if list(dict((k, safeSearch[k]) for k in ["adult", "spoof", "medical", "violence", "racy"] if k in safeSearch).values()) not in ['LIKELY', 'VERY_LIKELY'] else False
print('Safe? ' + str(isSafe))
# if the picture is safe to display, store it in Firestore
if isSafe:
pictureStore = store.collection(u'pictures')
doc = pictureStore.document(filename)
doc.set({
u'labels': labels,
u'color': colorHex,
u'created': datetime.datetime.now()
}, merge=True)
print("Stored metadata in Firestore")
else:
print('Vision API error: code '+response['error']['code']+', message: "'+ response['error']['message'] +'"')