|
a |
|
b/app.py |
|
|
1 |
|
|
|
2 |
|
|
|
3 |
from __future__ import division, print_function |
|
|
4 |
# coding=utf-8 |
|
|
5 |
import sys |
|
|
6 |
import os |
|
|
7 |
import glob |
|
|
8 |
import re |
|
|
9 |
import numpy as np |
|
|
10 |
|
|
|
11 |
# Keras |
|
|
12 |
from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions |
|
|
13 |
from tensorflow.keras.models import load_model |
|
|
14 |
from tensorflow.keras.preprocessing import image |
|
|
15 |
|
|
|
16 |
# Flask utils |
|
|
17 |
from flask import Flask, redirect, url_for, request, render_template |
|
|
18 |
from werkzeug.utils import secure_filename |
|
|
19 |
#from gevent.pywsgi import WSGIServer |
|
|
20 |
|
|
|
21 |
# Define a flask app |
|
|
22 |
app = Flask(__name__) |
|
|
23 |
|
|
|
24 |
# Model saved with Keras model.save() |
|
|
25 |
MODEL_PATH ='model_resnet50.h5' |
|
|
26 |
|
|
|
27 |
# Load your trained model |
|
|
28 |
model = load_model(MODEL_PATH) |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
def model_predict(img_path, model): |
|
|
34 |
img = image.load_img(img_path, target_size=(224, 224)) |
|
|
35 |
|
|
|
36 |
# Preprocessing the image |
|
|
37 |
x = image.img_to_array(img) |
|
|
38 |
# x = np.true_divide(x, 255) |
|
|
39 |
## Scaling |
|
|
40 |
x=x/255 |
|
|
41 |
x = np.expand_dims(x, axis=0) |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
preds = model.predict(x) |
|
|
47 |
preds=np.argmax(preds, axis=1) |
|
|
48 |
if preds==0: |
|
|
49 |
preds="The Patient has Lymphotic Cancer" |
|
|
50 |
elif preds==1: |
|
|
51 |
preds="The Patient has Promyelocytic Cancer" |
|
|
52 |
else: |
|
|
53 |
preds="The Patient has Segmented Neutrophils Cancer" |
|
|
54 |
|
|
|
55 |
|
|
|
56 |
return preds |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
@app.route('/', methods=['GET']) |
|
|
60 |
def index(): |
|
|
61 |
# Main page |
|
|
62 |
return render_template('index.html') |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
@app.route('/predict', methods=['GET', 'POST']) |
|
|
67 |
def upload(): |
|
|
68 |
if request.method == 'POST': |
|
|
69 |
# Get the file from post request |
|
|
70 |
f = request.files['file'] |
|
|
71 |
|
|
|
72 |
# Save the file to ./uploads |
|
|
73 |
basepath = os.path.dirname(__file__) |
|
|
74 |
file_path = os.path.join( |
|
|
75 |
basepath, 'uploads', secure_filename(f.filename)) |
|
|
76 |
f.save(file_path) |
|
|
77 |
|
|
|
78 |
# Make prediction |
|
|
79 |
preds = model_predict(file_path, model) |
|
|
80 |
result=preds |
|
|
81 |
return result |
|
|
82 |
return None |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
if __name__ == '__main__': |
|
|
86 |
app.run(debug=True) |