forked from ankur219/ECG-Arrhythmia-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
179 lines (139 loc) · 5.22 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from __future__ import division, print_function
import json
# coding=utf-8
import sys
import os
import glob
import re
import numpy as np
import cv2
import pandas as pd
import numpy as np
import biosppy
import matplotlib.pyplot as plt
# Keras
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.models import load_model
from keras.preprocessing import image
# Flask utils
from flask import Flask, redirect, url_for, request, render_template
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer
# Define a flask app
app = Flask(__name__)
# Model saved with Keras model.save()
# Load your trained model
model = load_model('path to the model')
model._make_predict_function() # Necessary
print('Model loaded. Start serving...')
output = []
# You can also use pretrained model from Keras
# Check https://keras.io/applications/
#from keras.applications.resnet50 import ResNet50
#model = ResNet50(weights='imagenet')
#print('Model loaded. Check http://127.0.0.1:5000/')
def model_predict(uploaded_files, model):
flag = 1
for path in uploaded_files:
#index1 = str(path).find('sig-2') + 6
#index2 = -4
#ts = int(str(path)[index1:index2])
APC, NORMAL, LBB, PVC, PAB, RBB, VEB = [], [], [], [], [], [], []
output.append(str(path))
result = {"APC": APC, "Normal": NORMAL, "LBB": LBB, "PAB": PAB, "PVC": PVC, "RBB": RBB, "VEB": VEB}
indices = []
kernel = np.ones((4,4),np.uint8)
csv = pd.read_csv(path)
csv_data = csv[' Sample Value']
data = np.array(csv_data)
signals = []
count = 1
peaks = biosppy.signals.ecg.christov_segmenter(signal=data, sampling_rate = 200)[0]
for i in (peaks[1:-1]):
diff1 = abs(peaks[count - 1] - i)
diff2 = abs(peaks[count + 1]- i)
x = peaks[count - 1] + diff1//2
y = peaks[count + 1] - diff2//2
signal = data[x:y]
signals.append(signal)
count += 1
indices.append((x,y))
for count, i in enumerate(signals):
fig = plt.figure(frameon=False)
plt.plot(i)
plt.xticks([]), plt.yticks([])
for spine in plt.gca().spines.values():
spine.set_visible(False)
filename = 'fig' + '.png'
fig.savefig(filename)
im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
im_gray = cv2.erode(im_gray,kernel,iterations = 1)
im_gray = cv2.resize(im_gray, (128, 128), interpolation = cv2.INTER_LANCZOS4)
cv2.imwrite(filename, im_gray)
im_gray = cv2.imread(filename)
pred = model.predict(im_gray.reshape((1, 128, 128, 3)))
pred_class = pred.argmax(axis=-1)
if pred_class == 0:
APC.append(indices[count])
elif pred_class == 1:
NORMAL.append(indices[count])
elif pred_class == 2:
LBB.append(indices[count])
elif pred_class == 3:
PAB.append(indices[count])
elif pred_class == 4:
PVC.append(indices[count])
elif pred_class == 5:
RBB.append(indices[count])
elif pred_class == 6:
VEB.append(indices[count])
result = sorted(result.items(), key = lambda y: len(y[1]))[::-1]
output.append(result)
data = {}
data['filename'+ str(flag)] = str(path)
data['result'+str(flag)] = str(result)
json_filename = 'data.txt'
with open(json_filename, 'a+') as outfile:
json.dump(data, outfile)
flag+=1
with open(json_filename, 'r') as file:
filedata = file.read()
filedata = filedata.replace('}{', ',')
with open(json_filename, 'w') as file:
file.write(filedata)
os.remove('fig.png')
return output
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
# Get the file from post request
uploaded_files = []
# Save the file to ./uploads
print(uploaded_files)
for f in request.files.getlist('file'):
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads', secure_filename(f.filename))
print(file_path)
if file_path[-4:] == '.csv':
uploaded_files.append(file_path)
f.save(file_path)
print(uploaded_files)
# Make prediction
pred = model_predict(uploaded_files, model)
# Process your result for human
# Simple argmax
#pred_class = decode_predictions(pred, top=1) # ImageNet Decode
#result = str(pred_class[0][0][1]) # Convert to string
result = str(pred)
return result
return None
if __name__ == '__main__':
# app.run(port=5002, debug=True)
# Serve the app with gevent
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()