Adapt the previous message dumper sample by creating a new app.py:
from flask import Flask, request
...
@app.route('/', methods=['POST'])
def storage_event():
attr = json.loads(request.data)['Attributes']
if attr['eventType'] == 'OBJECT_FINALIZE':
analyze_image(attr['bucketId'], attr['objectId'])
return 'OK', 200Note: When the file is created/updated, an event of type OBJECT_FINALIZE is received. Bucket and file names are respectively given by bucketId and objectId attributes.
Once we have the file info, we can pass it to the Vision API:
from google.cloud import vision
...
def analyze_image(bucket_id, filename):
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = f'gs://{bucket_id}/{filename}'
response = client.label_detection(image=image)
annots = response.label_annotations
labels = ', '.join([a.description for a in annots if 0.5 <= a.score])
info(f'Picture labels: {labels}')Create a Dockerfile for the image:
FROM python:3.7-slim
RUN pip install Flask gunicorn google.cloud.vision
WORKDIR /app
COPY . .
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:appNote: google.cloud.vision client library is specified in addition to Flask & gunicorn.
Back to Integrate with Vision API