-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
197 lines (171 loc) · 6.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
import json
from skimage import io, data
from PIL import Image
from see import GeneticSearch
import dash_canvas
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
from dash.exceptions import PreventUpdate
from utils import (parse_jsonstring, segmentation_generic,
image_with_contour, image_string_to_PILImage)
from dash_canvas.components import image_upload_zone
import subprocess
import shutil
# Image to segment and shape parameters
# filename = 'https://upload.wikimedia.org/wikipedia/commons/e/e4/Mitochondria%2C_mammalian_lung_-_TEM_%282%29.jpg'
filename = "assets/Snail_resize.jpg"
try:
img = io.imread(filename, as_gray=True)
except:
img = data.coins()
height, width = img.shape
canvas_width = 500
canvas_height = round(height * canvas_width / width)
scale = canvas_width / width
app = dash.Dash(__name__)
server = app.server
app.config.suppress_callback_exceptions = True
app.layout = html.Div([
html.Div([
dcc.Tabs(
id='segmentation-tabs',
value='segmentation-canvas-tab',
children=[
dcc.Tab(
label='Annotation tool',
value='segmentation-canvas-tab',
children=[
dash_canvas.DashCanvas(
id='canvas',
width=canvas_width,
height=canvas_height,
scale=scale,
filename=filename,
goButtonTitle='Segmentation'
),
image_upload_zone('upload-image'),
]),
dcc.Tab(
label='Ground Truth Image',
value='ground-truth-tab',
children=[
html.Img(id='ground-truth',
src='assets/Snailcpy_GT.jpg',
width='100%'),
]),
dcc.Tab(
label='Segmentation result',
value='segmentation-result-tab',
children=[
dcc.Graph(
id='segmentation',
figure=image_with_contour(np.ones_like(img),
img > 0, shape=(height, width))
)
]),
dcc.Tab(
label='How to use this app',
value='segmentation-help-tab',
children=[
html.Img(id='segmentation-help',
src='assets/segmentation.gif',
width='100%'),
]
)
]
),
], className="seven columns"),
html.Div([
html.Img(src='https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png', width='30px'),
html.A(
id='gh-link',
children=[
'View on GitHub'],
href="http://github.com/plotly/canvas-portal/" "blob/master/apps/segmentation/app.py",
style={'color': 'black',
'border':'solid 1px black',
'float':'left'}
),
html.H2(children='SEE-Segment Annotation tool'),
dcc.Markdown('''
Draw on the picture to annotate each object
you want to segment, then press the "Segmentation"
button to trigger the segmentation.
'''),
html.Br(),
html.Label('Segmentation algorithm'),
dcc.Dropdown(
id='algorithm',
options=[
{'label': 'Watershed', 'value': 'watershed'},
{'label': 'Random Walker', 'value': 'random_walker'},
{'label': 'Random Forest', 'value': 'random_forest'}
],
value='watershed'
),
], className="five columns")],# Div
className="row")
@app.callback(Output('segmentation', 'figure'),
[Input('canvas', 'json_data')],
[State('canvas', 'image_content'),
State('algorithm', 'value')])
def update_figure_upload(string, image, algorithm):
print("update figure")
if string:
if image is None:
im = img
image = img
else:
#print("Before PILImage")
im = image_string_to_PILImage(image)
im = np.asarray(im)
#print("After PILImage")
shape = im.shape[:2]
#print(f"Before jsonstring - Dirk was here {len(string)}")
mask = parse_jsonstring(string, shape=shape)
#print("DIRK WAS HERE")
#skimage.io.imsave("medial.png", img_as_uint(imgSk))
io.imsave("Snail_GT.jpg", mask)
file_copy("Snail_GT.jpg", "assets/Snailcpy_GT.jpg")
#file_copy function can be removed, the code below can be called----
#shutil.copyfile("Snail_GT.jpg", "assets/Snailcopy1_GT.jpg")
#import subprocess
if mask.sum() > 0:
#ERRORS-
#CommandNotFound Error Message: Your shell has not been properly configured to use 'conda activate'
#FileNotFound: python: can't open file 'GeneticSearch.py': [Errno 2] No such file or directory
#command1 = subprocess.Popen(['python GeneticSearch.py', './assets/Snail.jpg', './assets/Snail_GT.jpg'])
#command1 = subprocess.Popen(['bash -c "conda activate root; python GeneticSearch.py"', './assets/Snail_resize.jpg', './assets/Snail_GT.jpg'], shell=True)
#subprocess.Popen(['conda run -n env; python GeneticSearch.py', './assets/Snail_resize.jpg', './assets/Snail_GT.jpg'], shell=True)
seg = segmentation_generic(im, mask, mode=algorithm)
else:
seg = np.zeros(shape)
return image_with_contour(im, seg, shape=shape)
else:
raise PreventUpdate
#This function can be removed and shutil.copfile() can be used in the update_figure_upload function
def file_copy(src, dest):
#Copy the source to destination
shutil.copyfile(src, dest) #Update dest filename after each segmentation
return
@app.callback(Output('canvas', 'image_content'),
[Input('upload-image', 'contents')])
def update_canvas_upload(image_string):
# The line below causes NoneType Error
if image_string is None:
raise PreventUpdate
else:
print("uploading", image_string[:100])
return image_string
@app.callback(Output('segmentation-tabs', 'value'),
[Input('canvas', 'json_data')])
def change_focus(string):
if string:
return 'segmentation-result-tab'
return 'segmentation-canvas-tab'
if __name__ == '__main__':
app.run_server(debug=True)