djl234 commited on
Commit
eb9155a
·
verified ·
1 Parent(s): 2d1e815

Update app.py

Browse files

[feat]box interactive seg

Files changed (1) hide show
  1. app.py +53 -43
app.py CHANGED
@@ -30,7 +30,6 @@ def show_coord(evt: gr.SelectData):
30
 
31
  def generate_mask(img, coord):
32
  #x, y = map(int, coord.split(','))
33
-
34
  #
35
  mask = sepia((img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8), stack_image=False)
36
  mask = F.interpolate(torch.from_numpy(mask).unsqueeze(0).unsqueeze(0),size=[img.shape[0],img.shape[1]],mode='bilinear').squeeze().numpy()
@@ -45,44 +44,6 @@ def generate_mask(img, coord):
45
  mix = (1-mask_torch)*img+mask_torch*img*0.5+mask_torch*col*0.5
46
  return mix.numpy().astype(np.uint8)#overlay_mask(img, mask)
47
 
48
- def overlay_mask(img, mask):
49
- # 前置校验(参考网页1数据验证方案)
50
- assert mask is not None, "掩码数据为空"
51
- assert img.size > 0 and mask.size > 0, "输入图像或掩码尺寸异常"
52
-
53
- # 数据类型强制转换(根据网页2的浮点型要求)
54
- mask_float = mask.astype(np.float32) if mask.dtype != np.float32 else mask
55
-
56
- # 维度压缩(应对4D数组问题)
57
- if mask_float.ndim == 4:
58
- mask_float = np.squeeze(mask_float, axis=(0,1)) # 去除批次和通道维度
59
-
60
- # 尺寸调整(参考网页3的最佳实践)
61
- try:
62
- mask_resized = cv2.resize(
63
- mask_float,
64
- (img.shape[1], img.shape[0]),
65
- interpolation=cv2.INTER_CUBIC # 使用高阶插值保持缺陷边缘
66
- )
67
- except cv2.error as e:
68
- print(f"Resize参数异常: 原图尺寸{img.shape} 掩码尺寸{mask.shape}")
69
- raise
70
-
71
- # 通道统一(三通道对齐)
72
- if len(mask_resized.shape) == 2:
73
- mask_rgb = cv2.cvtColor(mask_resized, cv2.COLOR_GRAY2BGR)
74
- else:
75
- mask_rgb = mask_resized
76
-
77
- # 归一化处理
78
- return (cv2.addWeighted(
79
- img.astype(np.float32)/255,
80
- 0.7,
81
- mask_rgb.astype(np.float32)/255,
82
- 0.3,
83
- 0
84
- )*255).astype(np.uint8)
85
-
86
  def create_mode2_interface():
87
  with gr.Blocks() as mode2:
88
  with gr.Column():
@@ -115,6 +76,39 @@ def create_mode2_interface():
115
  outputs=mask_output
116
  )
117
  return mode2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  #import argparse
119
  device='cpu'
120
  net = build_model(device).to(device)
@@ -227,7 +221,7 @@ def create_mode1_interface():
227
 
228
  with gr.Blocks(title="交互式图像组分割系统") as demo:
229
  # 模式选择器
230
- mode = gr.Radio(["多图协同分割", "点提示交互分割"],
231
  value="多图协同分割",
232
  label="运行模式")
233
 
@@ -246,13 +240,29 @@ with gr.Blocks(title="交互式图像组分割系统") as demo:
246
  outputs=output_img
247
  )
248
 
249
- with gr.Tab("点选模式", id=1) as tab2:
250
  # 模式2界面组件
251
  img_input = gr.Image(type="numpy", label="点击上传图片并选择点")
252
  coord_store = gr.Textbox(visible=False)
253
  mask_btn = gr.Button("生成分割掩码")
254
  mask_output = gr.Image(label="分割结果")
255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  @img_input.select(inputs=[], outputs=coord_store)
257
  def store_coordinate(evt: gr.SelectData):
258
  return f"{evt.index[0]},{evt.index[1]}"
@@ -265,9 +275,9 @@ with gr.Blocks(title="交互式图像组分割系统") as demo:
265
 
266
  # 动态显示控制
267
  mode.change(
268
- lambda x: (gr.update(visible=x=="多图协同分割"), gr.update(visible=x=="点提示交互分割")),
269
  inputs=mode,
270
- outputs=[tab1, tab2]
271
  )
272
 
273
  demo.launch(debug=True)
 
30
 
31
  def generate_mask(img, coord):
32
  #x, y = map(int, coord.split(','))
 
33
  #
34
  mask = sepia((img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8),(img*0.999999).astype(np.uint8), stack_image=False)
35
  mask = F.interpolate(torch.from_numpy(mask).unsqueeze(0).unsqueeze(0),size=[img.shape[0],img.shape[1]],mode='bilinear').squeeze().numpy()
 
44
  mix = (1-mask_torch)*img+mask_torch*img*0.5+mask_torch*col*0.5
45
  return mix.numpy().astype(np.uint8)#overlay_mask(img, mask)
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def create_mode2_interface():
48
  with gr.Blocks() as mode2:
49
  with gr.Column():
 
76
  outputs=mask_output
77
  )
78
  return mode2
79
+
80
+ def create_mode3_interface():
81
+ with gr.Blocks() as mode2:
82
+ with gr.Column():
83
+ img_input = gr.Image(
84
+ type="numpy",
85
+ sources=["upload"], # 正确复数形式参数[2](@ref)
86
+ label="点击上传图片并选择框",
87
+ interactive=True
88
+ )
89
+
90
+ # 坐标存储组件
91
+ coord_store = gr.Textbox(visible=False)
92
+
93
+ # 绑定点击事件
94
+ @img_input.select(inputs=[], outputs=coord_store)
95
+ def capture_coordinates(evt: gr.SelectData):
96
+ return f"{evt.index[0]},{evt.index[1]}"
97
+
98
+ # 修改3:正确绑定点击事件
99
+ @img_input.select(inputs=img_input, outputs=coord_store)
100
+ def store_coordinate(evt: gr.SelectData):
101
+ return f"{evt.index[0]},{evt.index[1]}"
102
+
103
+ btn = gr.Button("生成分割掩码")
104
+ mask_output = gr.Image(label="分割结果")
105
+
106
+ btn.click(
107
+ generate_mask,
108
+ inputs=[img_input, coord_store],
109
+ outputs=mask_output
110
+ )
111
+ return mode2
112
  #import argparse
113
  device='cpu'
114
  net = build_model(device).to(device)
 
221
 
222
  with gr.Blocks(title="交互式图像组分割系统") as demo:
223
  # 模式选择器
224
+ mode = gr.Radio(["多图协同分割", "点提示交互分割","框提示交互分割"],
225
  value="多图协同分割",
226
  label="运行模式")
227
 
 
240
  outputs=output_img
241
  )
242
 
243
+ with gr.Tab("点选交互模式", id=1) as tab2:
244
  # 模式2界面组件
245
  img_input = gr.Image(type="numpy", label="点击上传图片并选择点")
246
  coord_store = gr.Textbox(visible=False)
247
  mask_btn = gr.Button("生成分割掩码")
248
  mask_output = gr.Image(label="分割结果")
249
 
250
+ @img_input.select(inputs=[], outputs=coord_store)
251
+ def store_coordinate(evt: gr.SelectData):
252
+ return f"{evt.index[0]},{evt.index[1]}"
253
+
254
+ mask_btn.click(
255
+ generate_mask,
256
+ inputs=[img_input, coord_store],
257
+ outputs=mask_output
258
+ )
259
+ with gr.Tab("框选交互模式", id=2) as tab3:
260
+ # 模式2界面组件
261
+ img_input = gr.Image(type="numpy", label="点击上传图片并选择框")
262
+ coord_store = gr.Textbox(visible=False)
263
+ mask_btn = gr.Button("生成分割掩码")
264
+ mask_output = gr.Image(label="分割结果")
265
+
266
  @img_input.select(inputs=[], outputs=coord_store)
267
  def store_coordinate(evt: gr.SelectData):
268
  return f"{evt.index[0]},{evt.index[1]}"
 
275
 
276
  # 动态显示控制
277
  mode.change(
278
+ lambda x: (gr.update(visible=x=="多图协同分割"), gr.update(visible=x=="点提示交互分割"), gr.update(visible=x=="框提示交互分割")),
279
  inputs=mode,
280
+ outputs=[tab1, tab2, tab3]
281
  )
282
 
283
  demo.launch(debug=True)