niulx commited on
Commit
877fe46
1 Parent(s): 884c413

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -49,18 +49,14 @@ def load_mask_ui(input_folder="example_tmp",load_edit = False):
49
  return mask_np_list, mask_label_list
50
 
51
  def load_image_ui(load_edit, input_folder="example_tmp"):
52
- #try:
53
- if 1:
54
- image, mask_np_list, mask_label_list = load_mask_ui(input_folder, load_edit = load_edit)
55
- #image = image.convert('RGB')
56
- segmentation = create_segmentation(mask_np_list)
57
- print("!!", len(mask_np_list))
58
- max_val = len(mask_np_list)-1
59
- sliderup = gr.Slider(value = 0, minimum=0, maximum=max_val, step=1, interactive=True)
60
- return image, segmentation, mask_np_list, mask_label_list, image, sliderup, sliderup
61
- #except:
62
- # print("Image folder invalid: The folder should contain image.png")
63
- # return None, None, None, None, None, None
64
 
65
 
66
  def run_segmentation_wrapper(image):
@@ -157,15 +153,6 @@ def save_as_edit_mask(mask_np_list_updated, mask_label_list, input_folder="examp
157
  visualize_mask_list_clean(mask_np_list_updated, savepath)
158
 
159
 
160
- def image_change():
161
- directory_path = "./example_tmp/"
162
- for filename in os.listdir(directory_path):
163
- file_path = os.path.join(directory_path, filename)
164
- if os.path.isfile(file_path) or os.path.islink(file_path):
165
- os.unlink(file_path)
166
- elif os.path.isdir(file_path):
167
- shutil.rmtree(file_path)
168
- return gr.Button("1.2 Load original masks",visible = False)
169
 
170
  def button_clickable(is_clickable):
171
  return gr.Button(interactive=is_clickable)
@@ -181,9 +168,16 @@ if os.path.isdir("./example_tmp"):
181
  shutil.rmtree("./example_tmp")
182
 
183
 
 
 
 
 
 
 
 
184
  from segment import run_segmentation
185
 
186
- with gr.Blocks() as demo:
187
  image = gr.State() # store mask
188
  image_loaded = gr.State()
189
  segmentation = gr.State()
@@ -202,8 +196,8 @@ with gr.Blocks() as demo:
202
  with gr.Row():
203
  with gr.Column():
204
  canvas = gr.Image(value = "./img.png", type="numpy", label="Draw Mask", show_label=True, height=LENGTH, width=LENGTH, interactive=True)
205
-
206
- segment_button = gr.Button("1.1 Run segmentation")
207
 
208
 
209
 
@@ -212,7 +206,6 @@ with gr.Blocks() as demo:
212
  # mask_np_list_updated.value = copy.deepcopy(mask_np_list.value) #!!
213
  mask_np_list_updated = mask_np_list
214
  with gr.Column():
215
- result_info0 = gr.Text(label="Response")
216
  gr.Markdown("""<p style="text-align: center; font-size: 20px">Edit Mask (Optional)</p>""")
217
  slider = gr.Slider(0, 20, step=1, label = 'mask id', interactive=False)
218
  label = gr.Text(label='label')
@@ -229,7 +222,7 @@ with gr.Blocks() as demo:
229
  with gr.Tab(label="2 Optimization"):
230
  with gr.Row():
231
  with gr.Column():
232
- result_info = gr.Text(label="Response")
233
 
234
  opt_flag = gr.State(0)
235
  gr.Markdown("""<p style="text-align: center; font-size: 20px">Optimization settings (SD)</p>""")
@@ -274,13 +267,13 @@ with gr.Blocks() as demo:
274
  )
275
  run_optimization()
276
  print('finish')
277
- return "Optimization finished!"
278
  except:
279
  return "CUDA out of memory, use a smaller batch size or try another picture."
280
 
281
 
282
  def immediate_update():
283
- return gr.Button("Run Optimization (Check Log for Completion).", interactive=True)
284
 
285
  add_button.click(run_optimization_wrapper,
286
  inputs = [
@@ -300,7 +293,7 @@ with gr.Blocks() as demo:
300
  add_button.click(fn=immediate_update, inputs=[], outputs=[add_button])
301
  #add_button.update()
302
  def change_text():
303
- return gr.Textbox("Optimization Finished!", interactive = False)
304
 
305
 
306
  with gr.Tab(label="3 Editing"):
@@ -322,7 +315,7 @@ with gr.Blocks() as demo:
322
  edge_thickness = gr.Number(value="10", label="Editing: Edge thickness", interactive= True )
323
  strength = gr.Textbox(value="0.5", label="Editing: Mask strength", interactive= True )
324
 
325
- add_button = gr.Button("Run Editing (Check Log for Completion)")
326
  def run_edit_text_wrapper(
327
  mask_np_list,
328
  mask_label_list,
 
49
  return mask_np_list, mask_label_list
50
 
51
  def load_image_ui(load_edit, input_folder="example_tmp"):
52
+
53
+ image, mask_np_list, mask_label_list = load_mask_ui(input_folder, load_edit = load_edit)
54
+ #image = image.convert('RGB')
55
+ segmentation = create_segmentation(mask_np_list)
56
+ max_val = len(mask_np_list)-1
57
+ sliderup = gr.Slider(value = 0, minimum=0, maximum=max_val, step=1, interactive=True)
58
+ return image, segmentation, mask_np_list, mask_label_list, image, sliderup, sliderup
59
+
 
 
 
 
60
 
61
 
62
  def run_segmentation_wrapper(image):
 
153
  visualize_mask_list_clean(mask_np_list_updated, savepath)
154
 
155
 
 
 
 
 
 
 
 
 
 
156
 
157
  def button_clickable(is_clickable):
158
  return gr.Button(interactive=is_clickable)
 
168
  shutil.rmtree("./example_tmp")
169
 
170
 
171
+
172
+ css = """
173
+ #response {
174
+ font-size: 25px !important;
175
+ }
176
+ """
177
+
178
  from segment import run_segmentation
179
 
180
+ with gr.Blocks(css=css) as demo:
181
  image = gr.State() # store mask
182
  image_loaded = gr.State()
183
  segmentation = gr.State()
 
196
  with gr.Row():
197
  with gr.Column():
198
  canvas = gr.Image(value = "./img.png", type="numpy", label="Draw Mask", show_label=True, height=LENGTH, width=LENGTH, interactive=True)
199
+ result_info0 = gr.Text(label="Response", elem_id="response")
200
+ segment_button = gr.Button("Run segmentation")
201
 
202
 
203
 
 
206
  # mask_np_list_updated.value = copy.deepcopy(mask_np_list.value) #!!
207
  mask_np_list_updated = mask_np_list
208
  with gr.Column():
 
209
  gr.Markdown("""<p style="text-align: center; font-size: 20px">Edit Mask (Optional)</p>""")
210
  slider = gr.Slider(0, 20, step=1, label = 'mask id', interactive=False)
211
  label = gr.Text(label='label')
 
222
  with gr.Tab(label="2 Optimization"):
223
  with gr.Row():
224
  with gr.Column():
225
+ result_info = gr.Text(label="Response", elem_id="response")
226
 
227
  opt_flag = gr.State(0)
228
  gr.Markdown("""<p style="text-align: center; font-size: 20px">Optimization settings (SD)</p>""")
 
267
  )
268
  run_optimization()
269
  print('finish')
270
+ return "Optimization finished! Move to the next step."
271
  except:
272
  return "CUDA out of memory, use a smaller batch size or try another picture."
273
 
274
 
275
  def immediate_update():
276
+ return gr.Button("Run Optimization (Check log for progress.).", interactive=True)
277
 
278
  add_button.click(run_optimization_wrapper,
279
  inputs = [
 
293
  add_button.click(fn=immediate_update, inputs=[], outputs=[add_button])
294
  #add_button.update()
295
  def change_text():
296
+ return gr.Textbox("Optimization Finished! Move to the next step.", interactive = False)
297
 
298
 
299
  with gr.Tab(label="3 Editing"):
 
315
  edge_thickness = gr.Number(value="10", label="Editing: Edge thickness", interactive= True )
316
  strength = gr.Textbox(value="0.5", label="Editing: Mask strength", interactive= True )
317
 
318
+ add_button = gr.Button("Run Editing (Check log for progress.)")
319
  def run_edit_text_wrapper(
320
  mask_np_list,
321
  mask_label_list,