-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
161 lines (115 loc) · 4.83 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
import matplotlib
import numpy as np
import streamlit as st
import matplotlib.pyplot as plt
from keras.models import load_model
# config streamlit
st.set_page_config(
page_title="irm cerveau",
layout="wide",
page_icon="🧠"
)
st.set_option('deprecation.showPyplotGlobalUse', False)
def plotImage(img_vol, slice_i, channel):
selected_slice = img_vol[:, :, slice_i, channel]
ax.imshow(selected_slice, 'gray', interpolation='none')
return fig
def plotImageSag(img_vol, slice_i, channel):
selected_slice3 = img_vol[:, slice_i, :, channel]
rotateIm = list(reversed(list(zip(*selected_slice3))))
ax2.imshow(rotateIm, 'gray', interpolation='none')
return fig2
def plotImageCor(img_vol, slice_i, channel):
selected_slice2 = img_vol[slice_i, :, :, channel]
rotateIm = list(reversed(list(zip(*selected_slice2))))
ax1.imshow(rotateIm, 'gray', interpolation='none')
return fig1
def plotMask(img_vol, slice_i):
selected_slice = img_vol[:, :, slice_i]
ax.imshow(selected_slice, interpolation='none')
return fig
def plotMaskSag(img_vol, slice_i):
selected_slice = img_vol[:, slice_i, :]
rotateIm = list(reversed(list(zip(*selected_slice))))
ax2.imshow(rotateIm, interpolation='none')
return fig2
def plotMaskCor(img_vol, slice_i):
selected_slice = img_vol[slice_i, :, :]
rotateIm = list(reversed(list(zip(*selected_slice))))
ax1.imshow(rotateIm, interpolation='none')
return fig1
def plotRedMask(fig, ax, img, slice_i, view):
if view == 'AX':
tm90 = test_prediction_argmax[:, :, slice_i]
tm90[tm90 >= 1] = 1
masked = np.ma.masked_where(tm90 == 0, tm90)
cmapm = matplotlib.colors.ListedColormap(["red", "red", "red"], name='from_list', N=None)
ax.imshow(masked, cmap=cmapm, interpolation='none', alpha=0.3)
ax.contour(tm90, colors='red', linewidths=1.0)
if view == 'CR':
tm90 = test_prediction_argmax[slice_i, :, :]
tm90[tm90 >= 1] = 1
masked = np.ma.masked_where(tm90 == 0, tm90)
cmapm = matplotlib.colors.ListedColormap(["red", "red", "red"], name='from_list', N=None)
rotMasked = list(reversed(list(zip(*masked))))
ax.imshow(rotMasked, cmap=cmapm, interpolation='none', alpha=0.3)
rot_tm90 = list(reversed(list(zip(*tm90))))
ax.contour(rot_tm90, colors='red', linewidths=1.0)
if view == 'SG':
tm90 = test_prediction_argmax[:, slice_i, :]
tm90[tm90 >= 1] = 1
masked = np.ma.masked_where(tm90 == 0, tm90)
cmapm = matplotlib.colors.ListedColormap(["red", "red", "red"], name='from_list', N=None)
rotMasked = list(reversed(list(zip(*masked))))
ax.imshow(rotMasked, cmap=cmapm, interpolation='none', alpha=0.3)
rot_tm90 = list(reversed(list(zip(*tm90))))
ax.contour(rot_tm90, colors='red', linewidths=1.0)
return fig, ax
# application // visualisation
st.title("Visualisation IRM cérébrale")
st.sidebar.title("📌 Menu")
npy_file = st.sidebar.file_uploader("Sélectionnez un fichier .npy", type=['npy', 'img'], accept_multiple_files=False)
if npy_file is not None:
npy_file = np.load(npy_file)
img_type = st.selectbox('''Type d'image''', ('FLAIR', 'T1CE', 'T2'))
if img_type == 'FLAIR':
channel = 0
elif img_type == 'T1CE':
channel = 1
else:
channel = 2
col1, col2, col3 = st.columns(3)
# slider
n_slices1 = npy_file.shape[2]
slice_i1 = col1.slider('Coupe Axiale', 0, n_slices1, int(n_slices1 / 2))
n_slices2 = npy_file.shape[0]
slice_i2 = col2.slider('Coupe Coronale', 0, n_slices2, int(n_slices2 / 2))
n_slices3 = npy_file.shape[1]
slice_i3 = col3.slider('Coupe Sagittale', 0, n_slices3, int(n_slices3 / 2))
# plot
fig, ax = plt.subplots()
plt.axis('off')
fig = plotImage(npy_file, slice_i1, channel)
fig1, ax1 = plt.subplots()
plt.axis('off')
fig1 = plotImageCor(npy_file, slice_i2, channel)
fig2, ax2 = plt.subplots()
plt.axis('off')
fig2 = plotImageSag(npy_file, slice_i3, channel)
plot = col1.pyplot(fig)
plot = col2.pyplot(fig1)
plot = col3.pyplot(fig2)
# application // creation masque avec modele unet
seg = st.sidebar.checkbox('Segmentation UNET 3D')
if seg:
col4, col5, col6 = st.columns(3)
model = load_model('drive/MyDrive/model/brats.hdf5', compile = False)
npy_file_input = np.expand_dims(npy_file, axis=0)
test_prediction = model.predict(npy_file_input)
test_prediction_argmax = np.argmax(test_prediction, axis=4)[0,:,:,:]
fig, ax = plotRedMask(fig, ax, npy_file, slice_i1, 'AX')
fig1, ax1 = plotRedMask(fig1, ax1, npy_file, slice_i2, 'CR')
fig2, ax2 = plotRedMask(fig2, ax2, npy_file, slice_i3, 'SG')
plot = col1.pyplot(fig)
plot = col2.pyplot(fig1)
plot = col3.pyplot(fig2)