-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·102 lines (87 loc) · 3.26 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
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
import os
from flask import Flask, request, render_template, redirect, url_for
from flask import send_from_directory
from werkzeug.utils import secure_filename
from keras.models import load_model
from moleimages import MoleImages
import tensorflow as tf
import random
UPLOAD_FOLDER = 'tmp'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 3 * 2048 * 2048
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
from werkzeug import SharedDataMiddleware
app.add_url_rule('/uploads/<filename>', 'uploaded_file',
build_only=True)
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/uploads': app.config['UPLOAD_FOLDER']
})
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#os.system('rm tmp/test.png')
filename = 'test.' + filename.split('.')[-1]
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('predict', filename = filename))
# return redirect(url_for('uploaded_file',
# filename=filename))
return render_template('upload.html') #upload
# @app.route('/uploads/<filename>')
# def uploaded_file(filename):
# return send_from_directory(app.config['UPLOAD_FOLDER'],
# filename)
@app.route('/submit')
def submit():
return render_template('submit.html')
@app.route('/predict/<filename>')
def predict(filename):
#with open('data/model.pkl', 'rb') as f:
# model = pickle.load(f)
#text = [str(request.form['user_input'])]
#result = model.predict(text)
mimg = MoleImages()
path_to_file = app.config['UPLOAD_FOLDER'] + '/' + filename
X = mimg.load_image(path_to_file)
global graph
with graph.as_default():
y_pred = model.predict(X)[0,0]
print(y_pred,type(y_pred))
if y_pred > 0.9:
result = 'High Risk'
print(result)
elif (y_pred <= 0.9 and y_pred > 0.5):
result = 'Medium Risk'
print(result)
else:
result = 'Low Risk'
page = '{0}'
print (path_to_file)
print (y_pred)
print(y_pred.shape)
#result = 'Low Risk'
path_to_file = '/uploads/' + filename + '?' + str(random.randint(1000000,9999999))
return render_template('index.html', image = path_to_file, scroll = 'features', data = page.format(result))
if __name__ == '__main__':
global model
model = load_model('models/mymodel-2.h5')
graph = tf.get_default_graph()
app.run(host='127.0.0.1', port=7000, debug=True)