-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
154 lines (133 loc) · 4.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import Flask, request, jsonify
import cv2
import numpy as np
import tensorflow as tf
from flask_cors import CORS
import base64
app = Flask(__name__)
API_URL = '127.0.0.1'
API_PORT = 5000
LARAVEL_APP_URL = 'http://127.0.0.1:8000'
CORS(app, origins=LARAVEL_APP_URL, allow_methods=["GET", "POST"])
IMAGE_SHAPE = (224, 224)
demo_model = tf.keras.models.load_model("./models/mobilenet_model.h5")
# model_v2 = tf.keras.models.load_model("./models/NIH_Seresnet152_model.h5")
demo_model_classes = {
0: 'Atelectasis',
1: 'Cardiomegaly',
2: 'Effusion',
3: 'Infiltration',
4: 'Mass',
5: 'Nodule',
6: 'Pneumonia',
7: 'Pneumothorax',
8: 'Consolidation',
9: 'Edema',
10: 'Emphysema',
11: 'Fibrosis',
12: 'Pleural_Thickening',
13: 'Hernia'
}
model_v2_classes = [
'Cardiomegaly', 'Hernia', 'Infiltration', 'Nodule', 'Emphysema', 'Effusion',
'Atelectasis', 'Pleural_Thickening', 'Pneumothorax', 'Mass', 'Fibrosis',
'Consolidation', 'Edema', 'Pneumonia'
]
def decode_image64(image64):
try:
if image64 is None:
raise (Exception('Image64 is Null'))
prefix, image64 = image64.split(',', 1)
image_type = prefix.split(';')[0].split(':')[1]
image64 += '=' * ((4 - len(image64) % 4) % 4)
image_data = base64.b64decode(image64)
return image_data
except Exception as e:
return jsonify({'error': 'decoding base64 image failed'}), 400
@app.route('/', methods=['GET'])
def welcome():
response = "<h1 style='color:#04aa6d'>welcome to doctor ai collab web api</h1>"
return response
@app.route('/info', methods=['GET'])
def info():
return {
"demo_model": {
"accuracy": 20,
"classes": 'demo_model',
"predict": 'demo_model/predict',
},
# "model_v2": {
# "accuracy": 65,
# "classes": 'demo_model',
# "predict": 'demo_model/predict',
# },
}
@app.route('/demo_model', methods=['GET'])
def demo_model_info():
response = "<h1 style='color:#04aa6d'>Supported classes by model-v1:</h1>"
response += "<ul>"
for val in demo_model_classes.values():
response += f"<li>{val}</li>"
response += "</ul>"
return response
@app.route('/demo_model/predict', methods=['POST'])
def demo_model_predict():
try:
data = request.get_json()
image64 = data.get('image64')
if image64 is None:
return jsonify({'error': 'Base64 image not provided'}), 400
except Exception as e:
return jsonify({'error': 'Bad request format'}), 400
try:
image_data = decode_image64(image64)
image = cv2.imdecode(np.frombuffer(image_data, np.uint8), cv2.IMREAD_COLOR)
except Exception as e:
return jsonify({'error': 'Image Processing Failed!'}), 400
resized_image = cv2.resize(image, IMAGE_SHAPE)
processed_image = np.expand_dims(resized_image, axis=0)
predictions = demo_model.predict(processed_image)
results = {
demo_model_classes[class_index]: "{:.2f}".format(float(predictions[0][class_index]) * 100)
for class_index in range(len(demo_model_classes))
}
return jsonify(results)
# @app.route('/model_v2', methods=['GET'])
# def model_v2_info():
# response = "<h1 style='color:#04aa6d'>Supported classes by model-v2:</h1>"
# response += "<ul>"
# for val in model_v2_classes:
# response += f"<li>{val}</li>"
# response += "</ul>"
# return response
# @app.route('/model_v2/predict', methods=['POST'])
# def model_v2_predict():
# img_size = 600
# try:
# data = request.get_json()
# image64 = data.get('image64')
# if image64 is None:
# return jsonify({'error': 'Base64 image not provided'}), 400
# except Exception as e:
# return jsonify({'error': 'Bad request format'}), 400
#
# try:
# image_data = decode_image64(image64)
# image = cv2.imdecode(np.frombuffer(image_data, np.uint8), cv2.IMREAD_COLOR)
# except Exception as e:
# return jsonify({'error': 'Image Processing Failed!'}), 400
#
# resized_image = cv2.resize(image, (img_size, img_size))
# processed_image = np.expand_dims(resized_image, axis=0)
#
# # Use the loaded model for prediction
# predictions = model_v2.predict(processed_image)
#
# results = {
# model_v2_classes[class_index]: "{:.2f}".format(float(predictions[0][class_index]) * 100)
# for class_index in range(len(model_v2_classes))
# }
#
# return jsonify(results)
if __name__ == '__main__':
app.run(host=API_URL, port=API_PORT)