|
a |
|
b/app.py |
|
|
1 |
from __future__ import division, print_function |
|
|
2 |
import json |
|
|
3 |
# coding=utf-8 |
|
|
4 |
import sys |
|
|
5 |
import os |
|
|
6 |
import glob |
|
|
7 |
import re |
|
|
8 |
import numpy as np |
|
|
9 |
import cv2 |
|
|
10 |
import pandas as pd |
|
|
11 |
import numpy as np |
|
|
12 |
import biosppy |
|
|
13 |
import matplotlib.pyplot as plt |
|
|
14 |
# Keras |
|
|
15 |
from keras.applications.imagenet_utils import preprocess_input, decode_predictions |
|
|
16 |
from keras.models import load_model |
|
|
17 |
from keras.preprocessing import image |
|
|
18 |
|
|
|
19 |
# Flask utils |
|
|
20 |
from flask import Flask, redirect, url_for, request, render_template |
|
|
21 |
from werkzeug.utils import secure_filename |
|
|
22 |
from gevent.pywsgi import WSGIServer |
|
|
23 |
|
|
|
24 |
# Define a flask app |
|
|
25 |
app = Flask(__name__) |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
# Model saved with Keras model.save() |
|
|
29 |
|
|
|
30 |
# Load your trained model |
|
|
31 |
model = load_model('path to the model') |
|
|
32 |
model._make_predict_function() # Necessary |
|
|
33 |
print('Model loaded. Start serving...') |
|
|
34 |
output = [] |
|
|
35 |
# You can also use pretrained model from Keras |
|
|
36 |
# Check https://keras.io/applications/ |
|
|
37 |
#from keras.applications.resnet50 import ResNet50 |
|
|
38 |
#model = ResNet50(weights='imagenet') |
|
|
39 |
#print('Model loaded. Check http://127.0.0.1:5000/') |
|
|
40 |
|
|
|
41 |
def model_predict(uploaded_files, model): |
|
|
42 |
flag = 1 |
|
|
43 |
|
|
|
44 |
for path in uploaded_files: |
|
|
45 |
#index1 = str(path).find('sig-2') + 6 |
|
|
46 |
#index2 = -4 |
|
|
47 |
#ts = int(str(path)[index1:index2]) |
|
|
48 |
APC, NORMAL, LBB, PVC, PAB, RBB, VEB = [], [], [], [], [], [], [] |
|
|
49 |
output.append(str(path)) |
|
|
50 |
result = {"APC": APC, "Normal": NORMAL, "LBB": LBB, "PAB": PAB, "PVC": PVC, "RBB": RBB, "VEB": VEB} |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
indices = [] |
|
|
54 |
|
|
|
55 |
kernel = np.ones((4,4),np.uint8) |
|
|
56 |
|
|
|
57 |
csv = pd.read_csv(path) |
|
|
58 |
csv_data = csv[' Sample Value'] |
|
|
59 |
data = np.array(csv_data) |
|
|
60 |
signals = [] |
|
|
61 |
count = 1 |
|
|
62 |
peaks = biosppy.signals.ecg.christov_segmenter(signal=data, sampling_rate = 200)[0] |
|
|
63 |
for i in (peaks[1:-1]): |
|
|
64 |
diff1 = abs(peaks[count - 1] - i) |
|
|
65 |
diff2 = abs(peaks[count + 1]- i) |
|
|
66 |
x = peaks[count - 1] + diff1//2 |
|
|
67 |
y = peaks[count + 1] - diff2//2 |
|
|
68 |
signal = data[x:y] |
|
|
69 |
signals.append(signal) |
|
|
70 |
count += 1 |
|
|
71 |
indices.append((x,y)) |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
for count, i in enumerate(signals): |
|
|
75 |
fig = plt.figure(frameon=False) |
|
|
76 |
plt.plot(i) |
|
|
77 |
plt.xticks([]), plt.yticks([]) |
|
|
78 |
for spine in plt.gca().spines.values(): |
|
|
79 |
spine.set_visible(False) |
|
|
80 |
|
|
|
81 |
filename = 'fig' + '.png' |
|
|
82 |
fig.savefig(filename) |
|
|
83 |
im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) |
|
|
84 |
im_gray = cv2.erode(im_gray,kernel,iterations = 1) |
|
|
85 |
im_gray = cv2.resize(im_gray, (128, 128), interpolation = cv2.INTER_LANCZOS4) |
|
|
86 |
cv2.imwrite(filename, im_gray) |
|
|
87 |
im_gray = cv2.imread(filename) |
|
|
88 |
pred = model.predict(im_gray.reshape((1, 128, 128, 3))) |
|
|
89 |
pred_class = pred.argmax(axis=-1) |
|
|
90 |
if pred_class == 0: |
|
|
91 |
APC.append(indices[count]) |
|
|
92 |
elif pred_class == 1: |
|
|
93 |
NORMAL.append(indices[count]) |
|
|
94 |
elif pred_class == 2: |
|
|
95 |
LBB.append(indices[count]) |
|
|
96 |
elif pred_class == 3: |
|
|
97 |
PAB.append(indices[count]) |
|
|
98 |
elif pred_class == 4: |
|
|
99 |
PVC.append(indices[count]) |
|
|
100 |
elif pred_class == 5: |
|
|
101 |
RBB.append(indices[count]) |
|
|
102 |
elif pred_class == 6: |
|
|
103 |
VEB.append(indices[count]) |
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
result = sorted(result.items(), key = lambda y: len(y[1]))[::-1] |
|
|
108 |
output.append(result) |
|
|
109 |
data = {} |
|
|
110 |
data['filename'+ str(flag)] = str(path) |
|
|
111 |
data['result'+str(flag)] = str(result) |
|
|
112 |
|
|
|
113 |
json_filename = 'data.txt' |
|
|
114 |
with open(json_filename, 'a+') as outfile: |
|
|
115 |
json.dump(data, outfile) |
|
|
116 |
flag+=1 |
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
with open(json_filename, 'r') as file: |
|
|
122 |
filedata = file.read() |
|
|
123 |
filedata = filedata.replace('}{', ',') |
|
|
124 |
with open(json_filename, 'w') as file: |
|
|
125 |
file.write(filedata) |
|
|
126 |
os.remove('fig.png') |
|
|
127 |
return output |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
@app.route('/', methods=['GET']) |
|
|
136 |
def index(): |
|
|
137 |
# Main page |
|
|
138 |
return render_template('index.html') |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
@app.route('/predict', methods=['GET', 'POST']) |
|
|
142 |
def upload(): |
|
|
143 |
if request.method == 'POST': |
|
|
144 |
# Get the file from post request |
|
|
145 |
uploaded_files = [] |
|
|
146 |
|
|
|
147 |
# Save the file to ./uploads |
|
|
148 |
print(uploaded_files) |
|
|
149 |
for f in request.files.getlist('file'): |
|
|
150 |
|
|
|
151 |
basepath = os.path.dirname(__file__) |
|
|
152 |
file_path = os.path.join( |
|
|
153 |
basepath, 'uploads', secure_filename(f.filename)) |
|
|
154 |
print(file_path) |
|
|
155 |
if file_path[-4:] == '.csv': |
|
|
156 |
uploaded_files.append(file_path) |
|
|
157 |
f.save(file_path) |
|
|
158 |
print(uploaded_files) |
|
|
159 |
# Make prediction |
|
|
160 |
pred = model_predict(uploaded_files, model) |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
# Process your result for human |
|
|
164 |
# Simple argmax |
|
|
165 |
#pred_class = decode_predictions(pred, top=1) # ImageNet Decode |
|
|
166 |
#result = str(pred_class[0][0][1]) # Convert to string |
|
|
167 |
result = str(pred) |
|
|
168 |
|
|
|
169 |
|
|
|
170 |
return result |
|
|
171 |
return None |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
if __name__ == '__main__': |
|
|
175 |
# app.run(port=5002, debug=True) |
|
|
176 |
|
|
|
177 |
# Serve the app with gevent |
|
|
178 |
http_server = WSGIServer(('', 5000), app) |
|
|
179 |
http_server.serve_forever() |