-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
163 lines (139 loc) · 5.54 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except
# in compliance with the License. A copy of the License is located at
#
# https://aws.amazon.com/apache-2-0/
#
# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
"Demo Flask application"
import sys
import requests
import boto3
from flask import Flask, render_template_string
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
import config
import util
application = Flask(__name__)
application.secret_key = config.FLASK_SECRET
### FlaskForm set up
class PhotoForm(FlaskForm):
"""flask_wtf form class the file upload"""
photo = FileField('image', validators=[
FileRequired()
])
@application.route("/", methods=('GET', 'POST'))
def home():
"""Homepage route"""
all_labels = ["No labels yet"]
faces = ["No faces yet"]
#####
# s3 getting a list of photos in the bucket
#####
s3_client = boto3.client('s3')
prefix = "photos/"
response = s3_client.list_objects(
Bucket=config.PHOTOS_BUCKET,
Prefix=prefix
)
photos = []
if 'Contents' in response and response['Contents']:
photos = [s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': config.PHOTOS_BUCKET, 'Key': content['Key']}
) for content in response['Contents']]
form = PhotoForm()
url = None
if form.validate_on_submit():
image_bytes = util.resize_image(form.photo.data, (300, 300))
if image_bytes:
#######
# s3 excercise - save the file to a bucket
#######
key = prefix + util.random_hex_bytes(8) + '.png'
s3_client.put_object(
Bucket=config.PHOTOS_BUCKET,
Key=key,
Body=image_bytes,
ContentType='image/png'
)
# http://boto3.readthedocs.io/en/latest/guide/s3.html#generating-presigned-urls
url = s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': config.PHOTOS_BUCKET, 'Key': key})
#######
# rekcognition exercise
#######
rek = boto3.client('rekognition')
response = rek.detect_labels(
Image={
'S3Object': {
'Bucket': config.PHOTOS_BUCKET,
'Name': key
}
})
all_confidences = [label['Confidence'] for label in response['Labels']]
all_labels = [label['Name'] for label in response['Labels']]
for i in range(0, len(all_labels)):
all_labels[i]=all_labels[i]+": "+str(all_confidences[i])+"%"
return render_template_string("""
{% extends "main.html" %}
{% block content %}
<h4>Upload Photo</h4>
<form method="POST" enctype="multipart/form-data" action="{{ url_for('home') }}">
{{ form.csrf_token }}
<div class="control-group">
<label class="control-label">Photo</label>
{{ form.photo() }}
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" value="Upload">
</div>
</div>
</form>
{% if url %}
<hr/>
<h3>Uploaded!</h3>
<img src="{{url}}" /><br/>
{% for label in all_labels %}
<span class="label label-info">{{label}}</span>
{% endfor %}
{% endif %}
{% if photos %}
<hr/>
<h4>Photos</h4>
{% for photo in photos %}
<img width="150" src="{{photo}}" />
{% endfor %}
{% endif %}
{% endblock %}
""", form=form, url=url, photos=photos, all_labels=all_labels)
@application.route("/info")
def info():
"Webserver info route"
metadata = "http://169.254.169.254"
instance_id = requests.get(metadata +
"/latest/meta-data/instance-id").text
availability_zone = requests.get(metadata +
"/latest/meta-data/placement/availability-zone").text
return render_template_string("""
{% extends "main.html" %}
{% block content %}
<b>instance_id</b>: {{instance_id}} <br/>
<b>availability_zone</b>: {{availability_zone}} <br/>
<b>sys.version</b>: {{sys_version}} <br/>
{% endblock %}""",
instance_id=instance_id,
availability_zone=availability_zone,
sys_version=sys.version)
if __name__ == "__main__":
# http://flask.pocoo.org/docs/0.12/errorhandling/#working-with-debuggers
# https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html
use_c9_debugger = False
application.run(use_debugger=not use_c9_debugger, debug=True,
use_reloader=not use_c9_debugger, host='0.0.0.0', port=8080)