djl234 commited on
Commit
100ae21
·
verified ·
1 Parent(s): ee6eede

Update app.py

Browse files

[feat]interactive mode

Files changed (1) hide show
  1. app.py +47 -3
app.py CHANGED
@@ -62,7 +62,34 @@ def crf_refine(img, annos):
62
  res = res * 255
63
  res = res.reshape(img.shape[:2])
64
  return res.astype('uint8')
65
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  #import argparse
67
  device='cpu'
68
  net = build_model(device).to(device)
@@ -119,7 +146,7 @@ img_lst=[(torch.rand(352,352,3)*255).numpy().astype(np.uint8) for i in range(5)]
119
  res=test('cpu',net,img_lst,5,224)
120
  '''for i in range(5):
121
  assert res[i].shape[0]==352 and res[i].shape[1]==352 and res[i].shape[2]==3'''
122
- def sepia(img1,img2,img3,img4,img5):
123
  print('sepia')
124
  '''ans=[]
125
  print(len(input_imgs))
@@ -138,6 +165,8 @@ def sepia(img1,img2,img3,img4,img5):
138
  #result_list=[result_list[i].resize((w_list[i], h_list[i]), Image.BILINEAR) for i in range(5)]
139
  img1,img2,img3,img4,img5=result_list#test('cpu',net,img_list,5,224)
140
  white=(torch.ones(img1.shape[0],2,3)*255).numpy().astype(np.uint8)
 
 
141
  return np.concatenate([img1,white,img2,white,img3,white,img4,white,img5],axis=1)
142
 
143
  #gr.Image(shape=(224, 2))
@@ -145,7 +174,8 @@ def sepia(img1,img2,img3,img4,img5):
145
  #demo = gr.Interface(sepia, inputs=["image","image","image","image","image"], outputs=["image"])
146
  #demo.launch(debug=True)
147
  #replace Interface with Blocks
148
- with gr.Blocks() as demo:
 
149
  with gr.Row():
150
  # 创建5列网格布局
151
  with gr.Column(scale=1, min_width=150):
@@ -171,4 +201,18 @@ with gr.Blocks() as demo:
171
  outputs=output
172
  )
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  demo.launch(debug=True)
 
62
  res = res * 255
63
  res = res.reshape(img.shape[:2])
64
  return res.astype('uint8')
65
+ def show_coord(evt: gr.SelectData):
66
+ return f"{evt.index[0]},{evt.index[1]}"
67
+
68
+ def generate_mask(img, coord):
69
+ x, y = map(int, coord.split(','))
70
+ #
71
+ mask = sepia(img,img,img,img,img stack_image=False)
72
+ return overlay_mask(img, mask)
73
+
74
+ def overlay_mask(img, mask):
75
+ # 实现带透明度的掩码叠加
76
+ return cv2.addWeighted(img, 0.7, mask, 0.3, 0)
77
+
78
+ def create_mode2_interface():
79
+ with gr.Blocks() as mode2:
80
+ with gr.Column():
81
+ img_input = gr.Image(tool="select", type="numpy")
82
+ coord_text = gr.Textbox(visible=False)
83
+ img_input.select(show_coord, None, coord_text)
84
+ btn = gr.Button("output corresponding mask")
85
+ mask_output = gr.Image()
86
+ btn.click(
87
+ generate_mask,
88
+ inputs=[img_input, coord_text],
89
+ outputs=mask_output
90
+ )
91
+ return mode2
92
+
93
  #import argparse
94
  device='cpu'
95
  net = build_model(device).to(device)
 
146
  res=test('cpu',net,img_lst,5,224)
147
  '''for i in range(5):
148
  assert res[i].shape[0]==352 and res[i].shape[1]==352 and res[i].shape[2]==3'''
149
+ def sepia(img1,img2,img3,img4,img5,stack_image=True):
150
  print('sepia')
151
  '''ans=[]
152
  print(len(input_imgs))
 
165
  #result_list=[result_list[i].resize((w_list[i], h_list[i]), Image.BILINEAR) for i in range(5)]
166
  img1,img2,img3,img4,img5=result_list#test('cpu',net,img_list,5,224)
167
  white=(torch.ones(img1.shape[0],2,3)*255).numpy().astype(np.uint8)
168
+ if not stack_image:
169
+ return img1
170
  return np.concatenate([img1,white,img2,white,img3,white,img4,white,img5],axis=1)
171
 
172
  #gr.Image(shape=(224, 2))
 
174
  #demo = gr.Interface(sepia, inputs=["image","image","image","image","image"], outputs=["image"])
175
  #demo.launch(debug=True)
176
  #replace Interface with Blocks
177
+ def create_mode1_interface():
178
+ with gr.Blocks() as demo:
179
  with gr.Row():
180
  # 创建5列网格布局
181
  with gr.Column(scale=1, min_width=150):
 
201
  outputs=output
202
  )
203
 
204
+ with gr.Blocks(title="交互式图像组分割系统") as demo:
205
+ #gr.Markdown("## mode choice")
206
+ mode = gr.Radio(["多图协同分割", "点提示交互分割"], value="多图协同分割", label="运行模式")
207
+
208
+ mode1 = create_mode1_interface()
209
+ mode2 = create_mode2_interface()
210
+
211
+ mode.change(
212
+ lambda x: (gr.update(visible=x=="多图协同分割"), gr.update(visible=x=="点提示交互分割")),
213
+ inputs=mode,
214
+ outputs=[mode1, mode2]
215
+ )
216
+
217
+ demo.launch()
218
  demo.launch(debug=True)