1313# limitations under the License.
1414
1515# [START app]
16- import logging
16+ from datetime import datetime
1717
1818from google .cloud import vision
1919from google .cloud import storage
20+ from google .cloud import datastore
2021
2122from flask import Flask , request , redirect
2223
@@ -48,8 +49,8 @@ def homepage():
4849 # Get your Cloud Storage bucket.
4950 bucket = storage_client .get_bucket (CLOUD_STORAGE_BUCKET )
5051
51- # Create a Cloud Vision client.
52- vision_client = vision .Client ()
52+ # Create a Cloud Datastore client.
53+ datastore_client = datastore .Client ()
5354
5455 # Loop through all items in your Cloud Storage bucket.
5556 for blob in bucket .list_blobs ():
@@ -58,17 +59,18 @@ def homepage():
5859 blob_public_url = blob .public_url
5960 html_string += """<img src="{}" width=200 height=200>""" .format (blob_public_url )
6061
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 )
62+ # Use the Cloud Datastore client to fetch the timestamp of when this
63+ # image was uploaded and the face joy likelihood. Output the photo
64+ # name, the timestamp, and the joy likelihood to HTML.
65+ query = datastore_client .query (kind = 'PhotoTimestamps' )
66+ query .add_filter ('blob_name' , '=' , blob .name )
67+ image_entities = list (query .fetch ())
68+ if len (image_entities ) > 0 :
69+ timestamp = image_entities [0 ]['timestamp' ]
70+ html_string += '<p>{} was uploaded {}.</p>' .format (
71+ blob .name , timestamp )
72+ face_joy = image_entities [0 ]['joy' ]
73+ html_string += """<p>Joy Likelihood for Face: {}</p>""" .format (face_joy )
7274
7375 html_string += """</body></html>"""
7476 return html_string
@@ -91,6 +93,51 @@ def upload_photo():
9193 # Make the blob publicly viewable.
9294 blob .make_public ()
9395
96+ # Create a Cloud Vision client.
97+ vision_client = vision .Client ()
98+
99+ # Use the Cloud Vision client to detect a face for our image.
100+ media_link = blob .media_link
101+ image = vision_client .image (source_uri = media_link )
102+ faces = image .detect_faces (limit = 1 )
103+
104+ # If a face is detected, save to Datastore the likelihood that the face
105+ # displays 'joy,' as determined by Google's Machine Learning algorithm.
106+ if len (faces ) > 0 :
107+ face = faces [0 ]
108+
109+ # Convert the face.emotions.joy enum type to a string, which will be
110+ # something like 'Likelihood.VERY_LIKELY'. Parse that string by the
111+ # period to extract only the 'VERY_LIKELY' portion.
112+ face_joy = str (face .emotions .joy ).split ('.' )[1 ]
113+ else :
114+ face_joy = 'Unknown'
115+
116+ # Create a Cloud Datastore client.
117+ datastore_client = datastore .Client ()
118+
119+ # Fetch the current date / time.
120+ current_datetime = datetime .now ()
121+
122+ # The kind for the new entity.
123+ kind = 'PhotoTimestamps'
124+
125+ # The name/ID for the new entity.
126+ name = blob .name
127+
128+ # Create the Cloud Datastore key for the new entity.
129+ key = datastore_client .key (kind , name )
130+
131+ # Construct the new entity using the key. Set dictionary values for entity
132+ # keys blob_name, timestamp, and joy.
133+ entity = datastore .Entity (key )
134+ entity ['blob_name' ] = blob .name
135+ entity ['timestamp' ] = current_datetime
136+ entity ['joy' ] = face_joy
137+
138+ # Save the new entity to Datastore.
139+ datastore_client .put (entity )
140+
94141 # Redirect to the home page.
95142 return redirect ('/' )
96143
0 commit comments