1414
1515# [START app]
1616from datetime import datetime
17+ import logging
18+
19+ from flask import Flask , redirect , request
1720
18- from google .cloud import vision
19- from google .cloud import storage
2021from google .cloud import datastore
22+ from google .cloud import storage
23+ from google .cloud import vision
2124
22- from flask import Flask , request , redirect
23- import logging
2425
25- CLOUD_STORAGE_BUCKET = 'ryans-bucket-2017 '
26+ CLOUD_STORAGE_BUCKET = '<your-storage-bucket> '
2627
2728HEADER_MESSAGE = (
2829 '<h1>Google Cloud Platform - Face Detection Sample</h1>'
2930 '<p>This Python Flask application demonstrates App Engine Flexible, Google'
30- ' Cloud Storage, and the Cloud Vision API.</p><br>' )
31+ ' Cloud Storage, Datastore, and the Cloud Vision API.</p><br>' )
3132
3233app = Flask (__name__ )
3334
@@ -44,38 +45,28 @@ def homepage():
4445 <input type="submit" name="submit" value="Submit">
4546</form> """
4647
47- # Create a Cloud Storage client.
48- storage_client = storage .Client ()
49-
50- # Get your Cloud Storage bucket.
51- bucket = storage_client .get_bucket (CLOUD_STORAGE_BUCKET )
52-
5348 # Create a Cloud Datastore client.
5449 datastore_client = datastore .Client ()
5550
56- # Loop through all items in your Cloud Storage bucket.
57- for blob in bucket .list_blobs ():
58-
59- # Add HTML to display each image.
60- blob_public_url = blob .public_url
61- html_string += """<img src="{}" width=200 height=200>""" .format (blob_public_url )
62-
63- # Use the Cloud Datastore client to fetch the timestamp of when this
64- # image was uploaded and the face joy likelihood. Output the photo
65- # name, the timestamp, and the joy likelihood to HTML.
66- query = datastore_client .query (kind = 'PhotoTimestamps' )
67- query .add_filter ('blob_name' , '=' , blob .name )
68- image_entities = list (query .fetch ())
69- if len (image_entities ) > 0 :
70- timestamp = image_entities [0 ]['timestamp' ]
71- html_string += '<p>{} was uploaded {}.</p>' .format (
72- blob .name , timestamp )
73- face_joy = image_entities [0 ]['joy' ]
74- html_string += """<p>Joy Likelihood for Face: {}</p>""" .format (face_joy )
75-
76- html_string += """</body></html>"""
51+ # Use the Cloud Datastore client to fetch information from Datastore about
52+ # each photo.
53+ query = datastore_client .query (kind = 'PhotoTimestamps' )
54+ image_entities = list (query .fetch ())
55+
56+ for image_entity in image_entities :
57+ # Add HTML to display each image, its upload name, its timestamp,
58+ # its timestamp, and its joy likelihood.
59+ html_string += '<img src="{}" width=200 height=200>' .format (
60+ image_entity ['image_public_url' ])
61+ html_string += '<p>{} was uploaded {}.</p>' .format (
62+ image_entity ['blob_name' ], image_entity ['timestamp' ])
63+ html_string += """<p>Joy Likelihood for Face: {}</p>""" .format (
64+ image_entity ['joy' ])
65+
66+ html_string += '</body></html>'
7767 return html_string
7868
69+
7970@app .route ('/upload_photo' , methods = ['GET' , 'POST' ])
8071def upload_photo ():
8172 photo = request .files ['file' ]
@@ -122,17 +113,18 @@ def upload_photo():
122113
123114 # The kind for the new entity.
124115 kind = 'PhotoTimestamps'
125-
116+
126117 # The name/ID for the new entity.
127118 name = blob .name
128119
129120 # Create the Cloud Datastore key for the new entity.
130121 key = datastore_client .key (kind , name )
131122
132123 # Construct the new entity using the key. Set dictionary values for entity
133- # keys blob_name, timestamp, and joy.
124+ # keys blob_name, storage_public_url, timestamp, and joy.
134125 entity = datastore .Entity (key )
135126 entity ['blob_name' ] = blob .name
127+ entity ['image_public_url' ] = blob .public_url
136128 entity ['timestamp' ] = current_datetime
137129 entity ['joy' ] = face_joy
138130
@@ -142,6 +134,7 @@ def upload_photo():
142134 # Redirect to the home page.
143135 return redirect ('/' )
144136
137+
145138@app .errorhandler (500 )
146139def server_error (e ):
147140 logging .exception ('An error occurred during a request.' )
0 commit comments