|
a |
|
b/app.py |
|
|
1 |
import os |
|
|
2 |
from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory |
|
|
3 |
from werkzeug.utils import secure_filename |
|
|
4 |
from tensorflow.keras.models import load_model |
|
|
5 |
import numpy as np |
|
|
6 |
#from keras.preprocessing import image |
|
|
7 |
from tensorflow.keras.preprocessing import image |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
model=load_model("Esophageal_model.h5") |
|
|
12 |
|
|
|
13 |
UPLOAD_FOLDER = 'static/img' |
|
|
14 |
if not os.path.exists(UPLOAD_FOLDER): |
|
|
15 |
os.makedirs(UPLOAD_FOLDER) |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} |
|
|
19 |
|
|
|
20 |
app = Flask(__name__) |
|
|
21 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
|
22 |
def allowed_file(filename): |
|
|
23 |
return '.' in filename and \ |
|
|
24 |
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
# Home Page |
|
|
29 |
@app.route('/') |
|
|
30 |
def index(): |
|
|
31 |
return render_template('home.html') |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
@app.route('/prediction', methods=['GET', 'POST']) |
|
|
35 |
def upload_file(): |
|
|
36 |
if request.method == 'POST': |
|
|
37 |
import uuid |
|
|
38 |
u = uuid.uuid4() |
|
|
39 |
# check if the post request has the file part |
|
|
40 |
if 'file' not in request.files: |
|
|
41 |
flash('No file part') |
|
|
42 |
return redirect(request.url) |
|
|
43 |
file = request.files['file'] |
|
|
44 |
# if user does not select file, browser also |
|
|
45 |
# submit an empty part without filename |
|
|
46 |
if file.filename == '': |
|
|
47 |
flash('No selected file') |
|
|
48 |
return redirect(request.url) |
|
|
49 |
if file and allowed_file(file.filename): |
|
|
50 |
filename = secure_filename(file.filename) |
|
|
51 |
filename="temp"+u.hex+".jpg" |
|
|
52 |
fullname=os.path.join(UPLOAD_FOLDER, "temp"+u.hex+".jpg") |
|
|
53 |
file.save(fullname) |
|
|
54 |
test_image = image.load_img('static/img/'+filename, target_size = (224,224)) |
|
|
55 |
test_image = image.img_to_array(test_image) |
|
|
56 |
test_image = np.expand_dims(test_image, axis = 0) |
|
|
57 |
test_image = test_image.astype('float') / 255 |
|
|
58 |
result = model.predict(test_image) |
|
|
59 |
pred_prob = result.item() |
|
|
60 |
print(result) |
|
|
61 |
if result[0]>0.5: |
|
|
62 |
label = 'NON-Esophageal' |
|
|
63 |
accuracy = round(pred_prob * 100, 2) |
|
|
64 |
else: |
|
|
65 |
pred_1 = round((1 - pred_prob) * 100, 2) |
|
|
66 |
if pred_1 < 75: |
|
|
67 |
label = 'Early Detection of Esophageal' |
|
|
68 |
accuracy = round((1 - pred_prob) * 100, 2) |
|
|
69 |
else: |
|
|
70 |
label = 'Esophageal' |
|
|
71 |
accuracy = round((1 - pred_prob) * 100, 2) |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
return render_template('index.html', label=label, image_file_name=filename, accuracy=accuracy) |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
@app.route('/upload/<filename>') |
|
|
78 |
def send_file(filename): |
|
|
79 |
return send_from_directory(UPLOAD_FOLDER, filename) |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
if __name__ == '__main__': |
|
|
83 |
app.run(debug=False) |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|