-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
77 lines (62 loc) Β· 2.39 KB
/
app.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
from flask import Flask, render_template, request
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from PIL import Image
import io
import boto3
AWS_ACCESS_KEY = ""
AWS_SECRET_KEY = ""
BUCKET_NAME = ""
s3 = boto3.client('s3',
aws_access_key_id = AWS_ACCESS_KEY,
aws_secret_access_key = AWS_SECRET_KEY)
app = Flask(__name__)
# export model
model = load_model('data/h5/model.h5')
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html')
if request.method == 'POST':
try:
if request.form.get('gender'):
# female predict
img = request.files["file"]
pred_img = img.read()
upload_img(img)
pred_img = Image.open(io.BytesIO(pred_img)).convert("RGB")
pred_img = pred_img.resize((256, 256))
pred_img = img_to_array(pred_img)
pred_img = pred_img.reshape((1, 256, 256, 3))
pred = model.predict(pred_img)
label = pred.argmax()
label = 'f' + str(label)
print(label)
return render_template("index.html", label=label)
else:
# male predict
img = request.files["file"]
pred_img = img.read()
upload_img(img)
pred_img = Image.open(io.BytesIO(pred_img)).convert("RGB")
pred_img = pred_img.resize((256, 256))
pred_img = img_to_array(pred_img)
pred_img = pred_img.reshape((1, 256, 256, 3))
pred = model.predict(pred_img)
label = pred.argmax()
label = 'm' + str(label)
return render_template("index.html", label=label)
except:
return render_template('404.html'), 404
def upload_img(image):
image.seek(0) # s3 μ μ₯μ νμ
s3.put_object(
Bucket = BUCKET_NAME, # λ²ν· μ΄λ¦
Body = image, # μ
λ‘λ νμΌ
Key = 'image/{}'.format(image.filename), # μ μ₯ μμΉ λ° μ΄λ¦ μ§μ
ContentType = image.content_type) # μ΄λ―Έμ§ νμ
if __name__ == "__main__":
app.run(debug=True)