forked from catlaughing/bangkit_project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
47 lines (36 loc) · 1.45 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
import os
from flask import Flask, request, render_template
from flask_gtts import gtts
from tensorflow.keras.models import load_model
from inference import createCaption
from preprocessing import preprocessing_image, get_encoding
app = Flask(__name__)
gtts(app)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = 'static/img/uploaded'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'jfif'])
model = load_model('models/model_rn50_glove.h5')
resnet = load_model('models/resnet50.h5')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET','POST'])
def main():
if request.method == 'GET':
return render_template('index.html')
if request.method == 'POST':
if 'file' not in request.files:
return render_template('main.html')
file = request.files['file']
if file and allowed_file(file.filename):
filename = file.filename.replace(' ','_')
dest = 'static/img/uploaded/'+filename
file.stream.seek(0)
file.save(dest)
file.stream.seek(0)
image = preprocessing_image(dest)
encoded_image = get_encoding(resnet, image)
caption = createCaption(encoded_image,model)
return render_template('result.html', caption=caption.capitalize() ,image_file=dest)
if __name__ == '__main__':
app.run(debug=False)