-
Notifications
You must be signed in to change notification settings - Fork 3
/
flask-app.py
53 lines (45 loc) · 1.68 KB
/
flask-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
from flask import Flask, render_template, redirect, flash, request, url_for
from flask_ngrok import run_with_ngrok
import os
from matplotlib.pyplot import imread
from fastai.vision import load_learner, open_image
ALLOWED_EXTENSIONS = set(['bmp', 'png', 'jpg', 'jpeg', 'gif'])
app=Flask(__name__)
app.secret_key = "secret key"
run_with_ngrok(app)
path=os.path.abspath(os.curdir)
learn = load_learner(path,'model.pkl')
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html')
@app.route('/', methods=['POST'])
def submit():
print(request.url)
if 'file' not in request.files:
flash('No file received')
return redirect(request.url)
file=request.files['file']
if file.filename == '':
flash('No file selected.')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = file.filename
img_path=(os.path.join(path,'static',filename))
file.save(img_path)
print(img_path)
img=open_image(img_path)
_,pred_idx,pred_conf = learn.predict(img)
pred_class = learn.data.train_ds.y.classes[pred_idx.data.numpy()]
conf=pred_conf.max().numpy()
output_string=f'{pred_class.__str__().title()} [ {conf*100:.4} % ]'
return render_template('prediction.html',image=filename, prediction=output_string)
else:
flash('The file type is not allowed')
return redirect(request.url)
if __name__=='__main__':
print('Server starting')
# port = int(os.environ.get('PORT', 5000))
# app.run(host='127.0.0.1', port=port)
app.run()