|
| 1 | +# Copyright 2015 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 | +import logging |
| 17 | + |
| 18 | +from google.cloud import vision |
| 19 | +from google.cloud import storage |
| 20 | + |
| 21 | +from flask import Flask, request, redirect |
| 22 | + |
| 23 | +CLOUD_STORAGE_BUCKET = 'ryans-bucket-2017' |
| 24 | + |
| 25 | +HEADER_MESSAGE = ( |
| 26 | + '<h1>Google Cloud Platform - Face Detection Sample</h1>' |
| 27 | + '<p>This Python Flask application demonstrates App Engine Flexible, Google' |
| 28 | + ' Cloud Storage, and the Cloud Vision API.</p><br>') |
| 29 | + |
| 30 | +app = Flask(__name__) |
| 31 | + |
| 32 | + |
| 33 | +@app.route('/') |
| 34 | +def homepage(): |
| 35 | + |
| 36 | + # Format the header message and a form to submit images. |
| 37 | + html_string = HEADER_MESSAGE |
| 38 | + html_string += """ |
| 39 | +<html><body> |
| 40 | +<form action="upload_photo" method="POST" enctype="multipart/form-data"> |
| 41 | + Upload File: <input type="file" name="file"><br> |
| 42 | + <input type="submit" name="submit" value="Submit"> |
| 43 | +</form> """ |
| 44 | + |
| 45 | + # Create a Cloud Storage client. |
| 46 | + storage_client = storage.Client() |
| 47 | + |
| 48 | + # Get your Cloud Storage bucket. |
| 49 | + bucket = storage_client.get_bucket(CLOUD_STORAGE_BUCKET) |
| 50 | + |
| 51 | + # Create a Cloud Vision client. |
| 52 | + vision_client = vision.Client() |
| 53 | + |
| 54 | + # Loop through all items in your Cloud Storage bucket. |
| 55 | + for blob in bucket.list_blobs(): |
| 56 | + |
| 57 | + # Add HTML to display each image. |
| 58 | + blob_public_url = blob.public_url |
| 59 | + html_string += """<img src="{}" width=200 height=200>""".format(blob_public_url) |
| 60 | + |
| 61 | + # Use the Cloud Vision client to detect a face for each image. |
| 62 | + media_link = blob.media_link |
| 63 | + image = vision_client.image(source_uri=media_link) |
| 64 | + faces = image.detect_faces(limit=1) |
| 65 | + |
| 66 | + # If a face is detected, output HTML with the likelihood that the face |
| 67 | + # displays 'joy,' as determined by Google's Machine Learning algorithm. |
| 68 | + if len(faces) > 0: |
| 69 | + first_face = faces[0] |
| 70 | + first_face_happiness = first_face.emotions.joy |
| 71 | + html_string += """<p>Joy Likelihood for Face: {}</p>""".format(first_face_happiness) |
| 72 | + |
| 73 | + html_string += """</body></html>""" |
| 74 | + return html_string |
| 75 | + |
| 76 | +@app.route('/upload_photo', methods=['GET', 'POST']) |
| 77 | +def upload_photo(): |
| 78 | + photo = request.files['file'] |
| 79 | + |
| 80 | + # Create a Cloud Storage client. |
| 81 | + storage_client = storage.Client() |
| 82 | + |
| 83 | + # Get the bucket that the file will be uploaded to. |
| 84 | + bucket = storage_client.get_bucket(CLOUD_STORAGE_BUCKET) |
| 85 | + |
| 86 | + # Create a new blob and upload the file's content. |
| 87 | + blob = bucket.blob(photo.filename) |
| 88 | + blob.upload_from_string( |
| 89 | + photo.read(), content_type=photo.content_type) |
| 90 | + |
| 91 | + # Make the blob publicly viewable. |
| 92 | + blob.make_public() |
| 93 | + |
| 94 | + # Redirect to the home page. |
| 95 | + return redirect('/') |
| 96 | + |
| 97 | +@app.errorhandler(500) |
| 98 | +def server_error(e): |
| 99 | + logging.exception('An error occurred during a request.') |
| 100 | + return """ |
| 101 | + An internal error occurred: <pre>{}</pre> |
| 102 | + See logs for full stacktrace. |
| 103 | + """.format(e), 500 |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + # This is used when running locally. Gunicorn is used to run the |
| 108 | + # application on Google App Engine. See entrypoint in app.yaml. |
| 109 | + app.run(host='127.0.0.1', port=8080, debug=True) |
| 110 | +# [END app] |
0 commit comments