Shengxiao0709 commited on
Commit
298c6f8
·
verified ·
1 Parent(s): 76ab0f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -12
app.py CHANGED
@@ -222,18 +222,30 @@ def segment_with_choice(use_box_choice, annot_value):
222
  return Image.fromarray(overlay), temp_mask_file.name
223
 
224
  # ===== 计数功能 =====
225
- def count_cells_handler(image_path):
226
- """计数处理函数 - 只返回密度图"""
227
- if image_path is None:
228
  return None, "⚠️ 请先上传图像"
229
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  try:
231
  print(f"🔢 Counting - Image: {image_path}")
232
 
233
  result = run_count(
234
  COUNT_MODEL,
235
  image_path,
236
- box=None,
237
  device=COUNT_DEVICE,
238
  visualize=True
239
  )
@@ -514,9 +526,9 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
514
 
515
  with gr.Row():
516
  with gr.Column(scale=1):
517
- count_input = gr.Image(
518
- label="🖼️ 上传图像",
519
- type="filepath"
520
  )
521
 
522
  # 示例图片Gallery (与Segmentation相同)
@@ -533,14 +545,21 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
533
  type="filepath"
534
  )
535
 
 
 
 
 
 
 
 
536
  count_btn = gr.Button("▶️ 运行计数", variant="primary", size="lg")
537
 
538
  gr.Markdown(
539
  """
540
  **使用说明:**
541
  1. 上传图像或从Gallery选择示例
542
- 2. 点击 "运行计数"
543
- 3. 查看密度图和计数结果
544
  """
545
  )
546
 
@@ -558,7 +577,7 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
558
  # 绑定事件
559
  count_btn.click(
560
  fn=count_cells_handler,
561
- inputs=count_input,
562
  outputs=[count_output, count_status]
563
  )
564
 
@@ -591,7 +610,7 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
591
  outputs=count_example_gallery
592
  )
593
 
594
- # 绑定事件: 点击Gallery加载到count_input
595
  def load_from_count_gallery(evt: gr.SelectData, all_imgs):
596
  if evt.index is not None and evt.index < len(all_imgs):
597
  return all_imgs[evt.index]
@@ -600,7 +619,7 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
600
  count_example_gallery.select(
601
  fn=load_from_count_gallery,
602
  inputs=count_user_examples,
603
- outputs=count_input
604
  )
605
 
606
  # ===== Tab 3: Tracking =====
 
222
  return Image.fromarray(overlay), temp_mask_file.name
223
 
224
  # ===== 计数功能 =====
225
+ def count_cells_handler(use_box_choice, annot_value):
226
+ """计数处理函数 - 支持边界框,只返回密度图"""
227
+ if annot_value is None or len(annot_value) < 1:
228
  return None, "⚠️ 请先上传图像"
229
 
230
+ image_path = annot_value[0]
231
+ bboxes = annot_value[1] if len(annot_value) > 1 else []
232
+
233
+ print(f"🖼️ 图像路径: {image_path}")
234
+ box_array = None
235
+ if use_box_choice == "Yes" and bboxes:
236
+ box = parse_first_bbox(bboxes)
237
+ if box:
238
+ xmin, ymin, xmax, ymax = map(int, box)
239
+ box_array = [[xmin, ymin, xmax, ymax]]
240
+ print(f"📦 使用边界框: {box_array}")
241
+
242
  try:
243
  print(f"🔢 Counting - Image: {image_path}")
244
 
245
  result = run_count(
246
  COUNT_MODEL,
247
  image_path,
248
+ box=box_array,
249
  device=COUNT_DEVICE,
250
  visualize=True
251
  )
 
526
 
527
  with gr.Row():
528
  with gr.Column(scale=1):
529
+ count_annotator = BBoxAnnotator(
530
+ label="🖼️ 上传图像 (可选标注边界框)",
531
+ categories=["cell"]
532
  )
533
 
534
  # 示例图片Gallery (与Segmentation相同)
 
545
  type="filepath"
546
  )
547
 
548
+ with gr.Row():
549
+ count_use_box_radio = gr.Radio(
550
+ choices=["Yes", "No"],
551
+ value="No",
552
+ label="🔲 使用边界框?"
553
+ )
554
+
555
  count_btn = gr.Button("▶️ 运行计数", variant="primary", size="lg")
556
 
557
  gr.Markdown(
558
  """
559
  **使用说明:**
560
  1. 上传图像或从Gallery选择示例
561
+ 2. (可选) 标注边界框并选择 "Yes"
562
+ 3. 点击 "运行计数"
563
  """
564
  )
565
 
 
577
  # 绑定事件
578
  count_btn.click(
579
  fn=count_cells_handler,
580
+ inputs=[count_use_box_radio, count_annotator],
581
  outputs=[count_output, count_status]
582
  )
583
 
 
610
  outputs=count_example_gallery
611
  )
612
 
613
+ # 绑定事件: 点击Gallery加载到count_annotator
614
  def load_from_count_gallery(evt: gr.SelectData, all_imgs):
615
  if evt.index is not None and evt.index < len(all_imgs):
616
  return all_imgs[evt.index]
 
619
  count_example_gallery.select(
620
  fn=load_from_count_gallery,
621
  inputs=count_user_examples,
622
+ outputs=count_annotator
623
  )
624
 
625
  # ===== Tab 3: Tracking =====