23
23
from google .cloud import vision
24
24
25
25
26
- CLOUD_STORAGE_BUCKET = os .environ .get (' CLOUD_STORAGE_BUCKET' )
26
+ CLOUD_STORAGE_BUCKET = os .environ .get (" CLOUD_STORAGE_BUCKET" )
27
27
28
28
29
29
app = Flask (__name__ )
30
30
31
31
32
- @app .route ('/' )
32
+ @app .route ("/" )
33
33
def homepage ():
34
34
# Create a Cloud Datastore client.
35
35
datastore_client = datastore .Client ()
36
36
37
37
# Use the Cloud Datastore client to fetch information from Datastore about
38
38
# each photo.
39
- query = datastore_client .query (kind = ' Faces' )
39
+ query = datastore_client .query (kind = " Faces" )
40
40
image_entities = list (query .fetch ())
41
41
42
42
# Return a Jinja2 HTML template and pass in image_entities as a parameter.
43
- return render_template (' homepage.html' , image_entities = image_entities )
43
+ return render_template (" homepage.html" , image_entities = image_entities )
44
44
45
45
46
- @app .route (' /upload_photo' , methods = [' GET' , ' POST' ])
46
+ @app .route (" /upload_photo" , methods = [" GET" , " POST" ])
47
47
def upload_photo ():
48
- photo = request .files [' file' ]
48
+ photo = request .files [" file" ]
49
49
50
50
# Create a Cloud Storage client.
51
51
storage_client = storage .Client ()
@@ -55,8 +55,7 @@ def upload_photo():
55
55
56
56
# Create a new blob and upload the file's content.
57
57
blob = bucket .blob (photo .filename )
58
- blob .upload_from_string (
59
- photo .read (), content_type = photo .content_type )
58
+ blob .upload_from_string (photo .read (), content_type = photo .content_type )
60
59
61
60
# Make the blob publicly viewable.
62
61
blob .make_public ()
@@ -65,10 +64,9 @@ def upload_photo():
65
64
vision_client = vision .ImageAnnotatorClient ()
66
65
67
66
# Use the Cloud Vision client to detect a face for our image.
68
- source_uri = 'gs://{}/{}' .format (CLOUD_STORAGE_BUCKET , blob .name )
69
- image = vision .types .Image (
70
- source = vision .types .ImageSource (gcs_image_uri = source_uri ))
71
- faces = vision_client .face_detection (image ).face_annotations
67
+ source_uri = "gs://{}/{}" .format (CLOUD_STORAGE_BUCKET , blob .name )
68
+ image = vision .Image (source = vision .ImageSource (gcs_image_uri = source_uri ))
69
+ faces = vision_client .face_detection (image = image ).face_annotations
72
70
73
71
# If a face is detected, save to Datastore the likelihood that the face
74
72
# displays 'joy,' as determined by Google's Machine Learning algorithm.
@@ -77,11 +75,16 @@ def upload_photo():
77
75
78
76
# Convert the likelihood string.
79
77
likelihoods = [
80
- 'Unknown' , 'Very Unlikely' , 'Unlikely' , 'Possible' , 'Likely' ,
81
- 'Very Likely' ]
78
+ "Unknown" ,
79
+ "Very Unlikely" ,
80
+ "Unlikely" ,
81
+ "Possible" ,
82
+ "Likely" ,
83
+ "Very Likely" ,
84
+ ]
82
85
face_joy = likelihoods [face .joy_likelihood ]
83
86
else :
84
- face_joy = ' Unknown'
87
+ face_joy = " Unknown"
85
88
86
89
# Create a Cloud Datastore client.
87
90
datastore_client = datastore .Client ()
@@ -90,7 +93,7 @@ def upload_photo():
90
93
current_datetime = datetime .now ()
91
94
92
95
# The kind for the new entity.
93
- kind = ' Faces'
96
+ kind = " Faces"
94
97
95
98
# The name/ID for the new entity.
96
99
name = blob .name
@@ -101,28 +104,33 @@ def upload_photo():
101
104
# Construct the new entity using the key. Set dictionary values for entity
102
105
# keys blob_name, storage_public_url, timestamp, and joy.
103
106
entity = datastore .Entity (key )
104
- entity [' blob_name' ] = blob .name
105
- entity [' image_public_url' ] = blob .public_url
106
- entity [' timestamp' ] = current_datetime
107
- entity [' joy' ] = face_joy
107
+ entity [" blob_name" ] = blob .name
108
+ entity [" image_public_url" ] = blob .public_url
109
+ entity [" timestamp" ] = current_datetime
110
+ entity [" joy" ] = face_joy
108
111
109
112
# Save the new entity to Datastore.
110
113
datastore_client .put (entity )
111
114
112
115
# Redirect to the home page.
113
- return redirect ('/' )
116
+ return redirect ("/" )
114
117
115
118
116
119
@app .errorhandler (500 )
117
120
def server_error (e ):
118
- logging .exception ('An error occurred during a request.' )
119
- return """
121
+ logging .exception ("An error occurred during a request." )
122
+ return (
123
+ """
120
124
An internal error occurred: {}
121
125
See logs for full stacktrace.
122
- """ .format (e ), 500
126
+ """ .format (
127
+ e
128
+ ),
129
+ 500 ,
130
+ )
123
131
124
132
125
- if __name__ == ' __main__' :
133
+ if __name__ == " __main__" :
126
134
# This is used when running locally. Gunicorn is used to run the
127
135
# application on Google App Engine. See entrypoint in app.yaml.
128
- app .run (host = ' 127.0.0.1' , port = 8080 , debug = True )
136
+ app .run (host = " 127.0.0.1" , port = 8080 , debug = True )
0 commit comments