-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (68 loc) · 2.62 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
import os
from flask import Flask, render_template, request, redirect, flash
from keras_preprocessing.image import load_img, img_to_array
from tensorflow import keras
from tensorflow.keras import layers
from werkzeug.utils import secure_filename
import numpy as np
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'jpg', 'jpeg'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'super secret key'
labels = {
0: "NORMAL",
1: "PNEUMONIA BACTERIA",
2: "PNEUMONIA VIRUS"
}
def load_model():
model = keras.Sequential()
model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(500, 700, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((3, 3)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((3, 3)))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((3, 3)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(3, activation='softmax'))
model.load_weights("models/model.39-0.54.h5")
return model
def getPrediction(filename):
model = load_model()
image = load_img('uploads/' + filename, target_size=(500, 700), color_mode="grayscale")
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
pred = model.predict(image)[0]
label = labels[np.argmax(pred)]
prob = np.max(pred)
return label, prob
@app.route('/')
def index():
model = load_model()
return render_template("index.html", model=model)
# Source: https://gist.github.com/mrron313/41f5691b4066876103bcfa77e6ccc065#file-index-py
@app.route('/', methods=['POST'])
def submit_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
label, prob = getPrediction(filename)
flash(f"{label}")
flash(f"{prob}")
return redirect('/')
if __name__ == "__main__":
app.run()