|
| 1 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START app] |
| 16 | +from datetime import datetime |
| 17 | +import logging |
| 18 | +import os |
| 19 | + |
| 20 | +from flask import Flask, redirect, render_template, request |
| 21 | + |
| 22 | +from google.cloud import datastore |
| 23 | +from google.cloud import storage |
| 24 | +from google.cloud import vision |
| 25 | + |
| 26 | + |
| 27 | +CLOUD_STORAGE_BUCKET = os.environ.get('CLOUD_STORAGE_BUCKET') |
| 28 | + |
| 29 | + |
| 30 | +app = Flask(__name__) |
| 31 | + |
| 32 | + |
| 33 | +@app.route('/') |
| 34 | +def homepage(): |
| 35 | + # Create a Cloud Datastore client. |
| 36 | + datastore_client = datastore.Client() |
| 37 | + |
| 38 | + # Use the Cloud Datastore client to fetch information from Datastore about |
| 39 | + # each photo. |
| 40 | + query = datastore_client.query(kind='Faces') |
| 41 | + image_entities = list(query.fetch()) |
| 42 | + |
| 43 | + # Return a Jinja2 HTML template and pass in image_entities as a parameter. |
| 44 | + return render_template('homepage.html', image_entities=image_entities) |
| 45 | + |
| 46 | + |
| 47 | +@app.route('/upload_photo', methods=['GET', 'POST']) |
| 48 | +def upload_photo(): |
| 49 | + photo = request.files['file'] |
| 50 | + |
| 51 | + # Create a Cloud Storage client. |
| 52 | + storage_client = storage.Client() |
| 53 | + |
| 54 | + # Get the bucket that the file will be uploaded to. |
| 55 | + bucket = storage_client.get_bucket(CLOUD_STORAGE_BUCKET) |
| 56 | + |
| 57 | + # Create a new blob and upload the file's content. |
| 58 | + blob = bucket.blob(photo.filename) |
| 59 | + blob.upload_from_string( |
| 60 | + photo.read(), content_type=photo.content_type) |
| 61 | + |
| 62 | + # Make the blob publicly viewable. |
| 63 | + blob.make_public() |
| 64 | + |
| 65 | + # Create a Cloud Vision client. |
| 66 | + vision_client = vision.Client() |
| 67 | + |
| 68 | + # Use the Cloud Vision client to detect a face for our image. |
| 69 | + source_uri = 'gs://{}/{}'.format(CLOUD_STORAGE_BUCKET, blob.name) |
| 70 | + image = vision_client.image(source_uri=source_uri) |
| 71 | + faces = image.detect_faces(limit=1) |
| 72 | + |
| 73 | + # If a face is detected, save to Datastore the likelihood that the face |
| 74 | + # displays 'joy,' as determined by Google's Machine Learning algorithm. |
| 75 | + if len(faces) > 0: |
| 76 | + face = faces[0] |
| 77 | + |
| 78 | + # Convert the face.emotions.joy enum type to a string, which will be |
| 79 | + # something like 'Likelihood.VERY_LIKELY'. Parse that string by the |
| 80 | + # period to extract only the 'VERY_LIKELY' portion. |
| 81 | + face_joy = str(face.emotions.joy).split('.')[1] |
| 82 | + else: |
| 83 | + face_joy = 'Unknown' |
| 84 | + |
| 85 | + # Create a Cloud Datastore client. |
| 86 | + datastore_client = datastore.Client() |
| 87 | + |
| 88 | + # Fetch the current date / time. |
| 89 | + current_datetime = datetime.now() |
| 90 | + |
| 91 | + # The kind for the new entity. |
| 92 | + kind = 'Faces' |
| 93 | + |
| 94 | + # The name/ID for the new entity. |
| 95 | + name = blob.name |
| 96 | + |
| 97 | + # Create the Cloud Datastore key for the new entity. |
| 98 | + key = datastore_client.key(kind, name) |
| 99 | + |
| 100 | + # Construct the new entity using the key. Set dictionary values for entity |
| 101 | + # keys blob_name, storage_public_url, timestamp, and joy. |
| 102 | + entity = datastore.Entity(key) |
| 103 | + entity['blob_name'] = blob.name |
| 104 | + entity['image_public_url'] = blob.public_url |
| 105 | + entity['timestamp'] = current_datetime |
| 106 | + entity['joy'] = face_joy |
| 107 | + |
| 108 | + # Save the new entity to Datastore. |
| 109 | + datastore_client.put(entity) |
| 110 | + |
| 111 | + # Redirect to the home page. |
| 112 | + return redirect('/') |
| 113 | + |
| 114 | + |
| 115 | +@app.errorhandler(500) |
| 116 | +def server_error(e): |
| 117 | + logging.exception('An error occurred during a request.') |
| 118 | + return """ |
| 119 | + An internal error occurred: <pre>{}</pre> |
| 120 | + See logs for full stacktrace. |
| 121 | + """.format(e), 500 |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + # This is used when running locally. Gunicorn is used to run the |
| 126 | + # application on Google App Engine. See entrypoint in app.yaml. |
| 127 | + app.run(host='127.0.0.1', port=8080, debug=True) |
| 128 | +# [END app] |
0 commit comments