Shengxiao0709 commited on
Commit
a593bb4
·
verified ·
1 Parent(s): ea77d47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -30
app.py CHANGED
@@ -156,10 +156,10 @@ def overlay_instances(img, mask, alpha=0.5, cmap_name="tab20"):
156
  return overlay
157
 
158
  # ===== 分割功能 =====
159
- def segment_with_choice(use_box_choice, annot_value, mode="Overlay"):
160
- """分割主函数"""
161
  if annot_value is None or len(annot_value) < 1:
162
- return None, "⚠️ 请上传图像"
163
 
164
  img_path = annot_value[0]
165
  bboxes = annot_value[1] if len(annot_value) > 1 else []
@@ -178,7 +178,8 @@ def segment_with_choice(use_box_choice, annot_value, mode="Overlay"):
178
  mask = run_seg(SEG_MODEL, img_path, box=box_array, device=SEG_DEVICE)
179
  print("📏 mask shape:", mask.shape, "unique ids:", np.unique(mask))
180
  except Exception as e:
181
- return None, f"❌ 推理失败: {str(e)}"
 
182
 
183
  # 读取原图
184
  try:
@@ -187,7 +188,8 @@ def segment_with_choice(use_box_choice, annot_value, mode="Overlay"):
187
  if img_np.max() > 1.5:
188
  img_np /= 255.0
189
  except Exception as e:
190
- return None, f"❌ 图像读取失败: {str(e)}"
 
191
 
192
  inst_mask = mask.astype(np.int32)
193
  unique_ids = np.unique(inst_mask)
@@ -195,18 +197,12 @@ def segment_with_choice(use_box_choice, annot_value, mode="Overlay"):
195
  print(f"✅ 实例数量: {num_instances}")
196
 
197
  if num_instances == 0:
198
- return Image.new("RGB", mask.shape[::-1], (255, 0, 0)), "⚠️ 未检测到实例"
199
-
200
- # 可视化
201
- if mode == "Overlay":
202
- overlay = overlay_instances(img_np, inst_mask, alpha=0.5, cmap_name="tab20")
203
- overlay_img = Image.fromarray((overlay * 255).astype(np.uint8))
204
- return overlay_img, f"✅ 检测到 {num_instances} 个细胞"
205
- elif mode == "Instance Mask Only":
206
- color_mask = colorize_mask(inst_mask, num_colors=512)
207
- return Image.fromarray(color_mask), f"✅ 检测到 {num_instances} 个细胞"
208
 
209
- return None, "❓ 无效显示模式"
 
 
 
210
 
211
  # ===== 计数功能 =====
212
  def count_cells_handler(image_path):
@@ -354,11 +350,6 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
354
  value="No",
355
  label="🔲 使用边界框?"
356
  )
357
- mode_radio = gr.Radio(
358
- choices=["Overlay", "Instance Mask Only"],
359
- value="Overlay",
360
- label="🎨 显示模式"
361
- )
362
 
363
  run_seg_btn = gr.Button("▶️ 运行分割", variant="primary", size="lg")
364
 
@@ -367,8 +358,7 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
367
  **使用说明:**
368
  1. 上传图像或从Gallery选择示例
369
  2. (可选) 标注边界框并选择 "Yes"
370
- 3. 选择显示模式
371
- 4. 点击 "运行分割"
372
  """
373
  )
374
 
@@ -378,17 +368,13 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
378
  label="📸 分割结果",
379
  height=400
380
  )
381
- seg_status = gr.Textbox(
382
- label="📊 状态信息",
383
- lines=2
384
- )
385
 
386
  # 满意度评分
387
  score_slider = gr.Slider(
388
  minimum=1,
389
  maximum=5,
390
  step=1,
391
- value=3,
392
  label="🌟 满意度评分 (1-5)"
393
  )
394
 
@@ -411,8 +397,8 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
411
  # 绑定事件: 运行分割
412
  run_seg_btn.click(
413
  fn=segment_with_choice,
414
- inputs=[use_box_radio, annotator, mode_radio],
415
- outputs=[seg_output, seg_status]
416
  )
417
 
418
  # 初始化Gallery显示
 
156
  return overlay
157
 
158
  # ===== 分割功能 =====
159
+ def segment_with_choice(use_box_choice, annot_value):
160
+ """分割主函数 - 固定使用Overlay模式"""
161
  if annot_value is None or len(annot_value) < 1:
162
+ return None
163
 
164
  img_path = annot_value[0]
165
  bboxes = annot_value[1] if len(annot_value) > 1 else []
 
178
  mask = run_seg(SEG_MODEL, img_path, box=box_array, device=SEG_DEVICE)
179
  print("📏 mask shape:", mask.shape, "unique ids:", np.unique(mask))
180
  except Exception as e:
181
+ print(f"❌ 推理失败: {str(e)}")
182
+ return None
183
 
184
  # 读取原图
185
  try:
 
188
  if img_np.max() > 1.5:
189
  img_np /= 255.0
190
  except Exception as e:
191
+ print(f"❌ 图像读取失败: {str(e)}")
192
+ return None
193
 
194
  inst_mask = mask.astype(np.int32)
195
  unique_ids = np.unique(inst_mask)
 
197
  print(f"✅ 实例数量: {num_instances}")
198
 
199
  if num_instances == 0:
200
+ return Image.new("RGB", mask.shape[::-1], (255, 0, 0))
 
 
 
 
 
 
 
 
 
201
 
202
+ # 使用Overlay模式可视化
203
+ overlay = overlay_instances(img_np, inst_mask, alpha=0.5, cmap_name="tab20")
204
+ overlay_img = Image.fromarray((overlay * 255).astype(np.uint8))
205
+ return overlay_img
206
 
207
  # ===== 计数功能 =====
208
  def count_cells_handler(image_path):
 
350
  value="No",
351
  label="🔲 使用边界框?"
352
  )
 
 
 
 
 
353
 
354
  run_seg_btn = gr.Button("▶️ 运行分割", variant="primary", size="lg")
355
 
 
358
  **使用说明:**
359
  1. 上传图像或从Gallery选择示例
360
  2. (可选) 标注边界框并选择 "Yes"
361
+ 3. 点击 "运行分割"
 
362
  """
363
  )
364
 
 
368
  label="📸 分割结果",
369
  height=400
370
  )
 
 
 
 
371
 
372
  # 满意度评分
373
  score_slider = gr.Slider(
374
  minimum=1,
375
  maximum=5,
376
  step=1,
377
+ value=5,
378
  label="🌟 满意度评分 (1-5)"
379
  )
380
 
 
397
  # 绑定事件: 运行分割
398
  run_seg_btn.click(
399
  fn=segment_with_choice,
400
+ inputs=[use_box_radio, annotator],
401
+ outputs=seg_output
402
  )
403
 
404
  # 初始化Gallery显示