Shengxiao0709 commited on
Commit
cf8c65d
·
verified ·
1 Parent(s): f4db8ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -97
app.py CHANGED
@@ -289,6 +289,7 @@ import tempfile
289
  from skimage import measure
290
  from matplotlib import cm
291
 
 
292
  # ===== 导入三个推理模块 =====
293
  from inference_seg import load_model as load_seg_model, run as run_seg
294
  from inference_count import load_model as load_count_model, run as run_count
@@ -474,61 +475,32 @@ def segment_with_choice(use_box_choice, annot_value, mode="Overlay"):
474
  return Image.fromarray(overlay), result_text
475
 
476
  # ===== 计数功能 =====
477
- def count_cells_handler(image_path):
478
- """计数处理函数"""
479
- if image_path is None:
480
- return None, "⚠️ 请先上传图像"
481
-
482
- if COUNT_MODEL is None:
483
- return None, "❌ 计数模型未加载"
484
-
485
- try:
486
- print(f"🔢 Counting - Image: {image_path}")
487
-
488
- result = run_count(
489
- COUNT_MODEL,
490
- image_path,
491
- box=None,
492
- device=COUNT_DEVICE,
493
- visualize=True
494
- )
495
-
496
- if 'error' in result:
497
- return None, f"❌ 计数失败: {result['error']}"
498
-
499
- count = result['count']
500
- viz_path = result['visualized_path']
501
- result_text = f"✅ 检测到 {count:.1f} 个细胞"
502
-
503
- print(f"✅ Counting done - Count: {count:.1f}")
504
-
505
- return viz_path, result_text
506
-
507
- except Exception as e:
508
- print(f"❌ Counting error: {e}")
509
- import traceback
510
- traceback.print_exc()
511
- return None, f"❌ 计数失败: {str(e)}"
512
 
513
- # ===== 跟踪功能 =====
514
- def track_video_handler(video_dir_input):
515
- """跟踪处理函数"""
516
- if video_dir_input is None or video_dir_input.strip() == "":
517
- return None, "⚠️ 请输入视频帧目录路径"
518
 
519
  if TRACK_MODEL is None:
520
  return None, "❌ 跟踪模型未加载"
521
 
522
- # 检查目录是否存在
523
- if not os.path.exists(video_dir_input):
524
- return None, f"❌ 目录不存在: {video_dir_input}"
525
-
526
  try:
527
- print(f"🎬 Tracking - Video dir: {video_dir_input}")
 
 
 
 
 
 
 
 
528
 
529
  result = run_track(
530
  TRACK_MODEL,
531
- video_dir=video_dir_input,
532
  box=None,
533
  device=TRACK_DEVICE,
534
  output_dir="tracked_results"
@@ -549,13 +521,15 @@ def track_video_handler(video_dir_input):
549
  - res_track.txt (CTC格式轨迹)
550
  - 其他跟踪数据文件
551
  """
552
-
553
  print(f"✅ Tracking done - {num_tracks} tracks")
554
-
555
  return None, result_text
556
-
 
 
 
557
  except Exception as e:
558
- print(f"❌ Tracking error: {e}")
559
  import traceback
560
  traceback.print_exc()
561
  return None, f"❌ 跟踪失败: {str(e)}"
@@ -673,55 +647,50 @@ with gr.Blocks(title="Microscopy Analysis Suite", theme=gr.themes.Soft()) as dem
673
 
674
  # ===== Tab 3: Tracking =====
675
  with gr.Tab("🎬 跟踪 (Tracking)"):
676
- gr.Markdown("## 视频细胞跟踪 - 时间序列分析")
677
-
678
- with gr.Row():
679
- with gr.Column(scale=1):
680
- track_input = gr.Textbox(
681
- label="📁 视频帧目录路径",
682
- placeholder="例如: example_imgs/2D+Time/Fluo-N2DL-HeLa/train/02/",
683
- lines=2
684
- )
685
- track_btn = gr.Button("▶️ 运行跟踪", variant="primary", size="lg")
686
-
687
- gr.Markdown(
688
- """
689
- **使用说明:**
690
- 1. 输入包含视频帧序列的目录路径
691
- 2. 目录应包含: t000.tif, t001.tif, ...
692
- 3. 点击 "运行跟踪"
693
- 4. 结果将保存到 `tracked_results/` 目录
694
-
695
- **输入格式:**
696
- ```
697
- video_dir/
698
- ├── t000.tif
699
- ├── t001.tif
700
- ├── t002.tif
701
- └── ...
702
- ```
703
-
704
- **跟踪模式:** Greedy (快速)
705
- """
706
- )
707
 
708
- with gr.Column(scale=2):
709
- track_output = gr.Video(
710
- label="📹 跟踪结果视频 (暂不支持)",
711
- visible=False
712
- )
713
- track_status = gr.Textbox(
714
- label="📊 跟踪信息",
715
- lines=10
716
- )
 
 
 
 
 
 
 
 
 
 
 
717
 
718
- # 绑定事件
719
- track_btn.click(
720
- fn=track_video_handler,
721
- inputs=track_input,
722
- outputs=[track_output, track_status]
723
- )
724
-
 
 
 
 
 
 
725
  gr.Markdown(
726
  """
727
  ---
 
289
  from skimage import measure
290
  from matplotlib import cm
291
 
292
+
293
  # ===== 导入三个推理模块 =====
294
  from inference_seg import load_model as load_seg_model, run as run_seg
295
  from inference_count import load_model as load_count_model, run as run_count
 
475
  return Image.fromarray(overlay), result_text
476
 
477
  # ===== 计数功能 =====
478
+ import zipfile
479
+ import tempfile
480
+ import shutil
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481
 
482
+ def track_video_handler(zip_file_obj):
483
+ """支持 ZIP 压缩包上传的 Tracking 处理函数"""
484
+ if zip_file_obj is None:
485
+ return None, "⚠️ 请上传包含视频帧的压缩包 (.zip)"
 
486
 
487
  if TRACK_MODEL is None:
488
  return None, "❌ 跟踪模型未加载"
489
 
 
 
 
 
490
  try:
491
+ # 创建临时目录
492
+ temp_dir = tempfile.mkdtemp()
493
+ print(f"📦 解压到临时目录: {temp_dir}")
494
+
495
+ # 解压 zip 文件
496
+ with zipfile.ZipFile(zip_file_obj.name, 'r') as zip_ref:
497
+ zip_ref.extractall(temp_dir)
498
+
499
+ print(f"🎬 Tracking - Video frame folder: {temp_dir}")
500
 
501
  result = run_track(
502
  TRACK_MODEL,
503
+ video_dir=temp_dir, # 传入解压目录
504
  box=None,
505
  device=TRACK_DEVICE,
506
  output_dir="tracked_results"
 
521
  - res_track.txt (CTC格式轨迹)
522
  - 其他跟踪数据文件
523
  """
524
+
525
  print(f"✅ Tracking done - {num_tracks} tracks")
526
+
527
  return None, result_text
528
+
529
+ except zipfile.BadZipFile:
530
+ return None, "❌ 上传的文件不是有效的 ZIP 压缩包"
531
+
532
  except Exception as e:
 
533
  import traceback
534
  traceback.print_exc()
535
  return None, f"❌ 跟踪失败: {str(e)}"
 
647
 
648
  # ===== Tab 3: Tracking =====
649
  with gr.Tab("🎬 跟踪 (Tracking)"):
650
+ gr.Markdown("## 视频细胞跟踪 - 支持 ZIP 压缩包上传")
651
+
652
+ with gr.Row():
653
+ with gr.Column(scale=1):
654
+ track_zip_upload = gr.File(
655
+ label="📦 上传视频帧 ZIP 文件",
656
+ file_types=[".zip"]
657
+ )
658
+ track_btn = gr.Button("▶️ 运行跟踪", variant="primary", size="lg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
659
 
660
+ gr.Markdown(
661
+ """
662
+ **使用说明:**
663
+ 1. 上传包含视频帧序列的压缩包 `.zip`
664
+ 2. 压缩包应直接包含 `.tif` 格式图像,如 t000.tif, t001.tif, ...
665
+ 3. 点击 "运行跟踪"
666
+ 4. 结果将保存到 `tracked_results/` 目录
667
+
668
+ **压缩包示例结构:**
669
+ ```
670
+ frames.zip
671
+ ├── t000.tif
672
+ ├── t001.tif
673
+ ├── t002.tif
674
+ └── ...
675
+ ```
676
+
677
+ **跟踪模式:** Greedy (快速)
678
+ """
679
+ )
680
 
681
+ with gr.Column(scale=2):
682
+ track_output = gr.Textbox(
683
+ label="📊 跟踪信息",
684
+ lines=12,
685
+ interactive=False
686
+ )
687
+
688
+ # 绑定事件:上传zip → 解压 → Tracking
689
+ track_btn.click(
690
+ fn=track_video_handler, # 你刚才改好的函数
691
+ inputs=track_zip_upload, # 文件上传
692
+ outputs=[None, track_output] # 第二个是 Textbox 输出
693
+ )
694
  gr.Markdown(
695
  """
696
  ---