ColamanAI commited on
Commit
b7a53b2
·
verified ·
1 Parent(s): 9c5c26b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -4
app.py CHANGED
@@ -719,8 +719,22 @@ def run_model(
719
  # Load segmentation models if enabled (使用CPU节省GPU资源)
720
  if enable_segmentation:
721
  progress(0.1, desc="🎯 加载分割模型 (CPU)...")
 
 
 
722
  load_grounding_dino_model("cpu") # 分割使用CPU
723
  load_sam_model("cpu") # MobileSAM在CPU上运行良好
 
 
 
 
 
 
 
 
 
 
 
724
 
725
  # Load images
726
  progress(0.15, desc="📷 加载图片...")
@@ -795,8 +809,12 @@ def run_model(
795
  segmented_glb = None
796
  if enable_segmentation and grounding_dino_model is not None:
797
  progress(0.65, desc="🎯 开始物体分割...")
798
- print("\n🎯 Starting segmentation...")
799
- print(f"🔍 Detection prompt: {text_prompt[:100]}...")
 
 
 
 
800
 
801
  all_view_detections = []
802
  all_view_masks = []
@@ -812,8 +830,11 @@ def run_model(
812
  ref_image_np = ref_image
813
 
814
  detections = run_grounding_dino_detection(ref_image_np, text_prompt, "cpu") # 使用CPU进行检测
815
-
 
816
  if len(detections) > 0:
 
 
817
  boxes = [d['bbox'] for d in detections]
818
  masks = run_sam_refinement(ref_image_np, boxes)
819
 
@@ -850,7 +871,14 @@ def run_model(
850
  else:
851
  print(f"⚠️ 分割3D模型生成失败")
852
  else:
 
853
  print(f"⚠️ 未检测到任何物体,无法生成分割模型")
 
 
 
 
 
 
854
 
855
  # Cleanup
856
  progress(0.95, desc="🧹 清理内存...")
@@ -1685,6 +1713,26 @@ CUSTOM_CSS = GRADIO_CSS + """
1685
  .file-upload-enhanced {
1686
  position: relative;
1687
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1688
  """
1689
 
1690
  # JavaScript for paste support
@@ -1799,8 +1847,19 @@ with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="MapAnything V2 - 3D重建与
1799
  )
1800
 
1801
  with gr.Tab("🎨 分割3D"):
 
 
 
 
 
 
 
 
 
 
 
1802
  segmented_output = gr.Model3D(
1803
- height=550, zoom_speed=0.5, pan_speed=0.5,
1804
  clear_color=[0.0, 0.0, 0.0, 0.0]
1805
  )
1806
 
@@ -1923,6 +1982,18 @@ with gr.Blocks(theme=theme, css=CUSTOM_CSS, title="MapAnything V2 - 3D重建与
1923
  target_dir_output, image_gallery, log_output
1924
  ]
1925
  )
 
 
 
 
 
 
 
 
 
 
 
 
1926
 
1927
  # === 事件绑定 ===
1928
 
 
719
  # Load segmentation models if enabled (使用CPU节省GPU资源)
720
  if enable_segmentation:
721
  progress(0.1, desc="🎯 加载分割模型 (CPU)...")
722
+ print(f"\n{'='*70}")
723
+ print("🎯 分割模型加载开始...")
724
+ print(f"{'='*70}")
725
  load_grounding_dino_model("cpu") # 分割使用CPU
726
  load_sam_model("cpu") # MobileSAM在CPU上运行良好
727
+
728
+ # 验证模型是否成功加载
729
+ if grounding_dino_model is None:
730
+ print("❌ GroundingDINO 模型加载失败!")
731
+ return predictions, processed_data, None
732
+ if sam_predictor is None:
733
+ print("❌ SAM 模型加载失败!")
734
+ return predictions, processed_data, None
735
+
736
+ print(f"✅ 所有分割模型加载成功")
737
+ print(f"{'='*70}\n")
738
 
739
  # Load images
740
  progress(0.15, desc="📷 加载图片...")
 
809
  segmented_glb = None
810
  if enable_segmentation and grounding_dino_model is not None:
811
  progress(0.65, desc="🎯 开始物体分割...")
812
+ print(f"\n{'='*70}")
813
+ print("🎯 开始物体分割...")
814
+ print(f"🔍 检测提示词: {text_prompt[:100]}...")
815
+ print(f"📊 置信度阈值: {GROUNDING_DINO_BOX_THRESHOLD}")
816
+ print(f"📐 最小掩码面积: {MIN_MASK_AREA} px")
817
+ print(f"{'='*70}\n")
818
 
819
  all_view_detections = []
820
  all_view_masks = []
 
830
  ref_image_np = ref_image
831
 
832
  detections = run_grounding_dino_detection(ref_image_np, text_prompt, "cpu") # 使用CPU进行检测
833
+ print(f" ✓ 检测到 {len(detections)} 个物体")
834
+
835
  if len(detections) > 0:
836
+ for i, det in enumerate(detections):
837
+ print(f" 物体 {i+1}: {det['label']} (置信度: {det['confidence']:.2f})")
838
  boxes = [d['bbox'] for d in detections]
839
  masks = run_sam_refinement(ref_image_np, boxes)
840
 
 
871
  else:
872
  print(f"⚠️ 分割3D模型生成失败")
873
  else:
874
+ print(f"\n{'='*70}")
875
  print(f"⚠️ 未检测到任何物体,无法生成分割模型")
876
+ print(f"\n💡 调试提示:")
877
+ print(f" 1. 检查检测提示词是否准确(当前: {text_prompt[:50]}...)")
878
+ print(f" 2. 当前置信度阈值: {GROUNDING_DINO_BOX_THRESHOLD}")
879
+ print(f" 3. 尝试更通用的提示词,如: {COMMON_OBJECTS_PROMPT[:80]}...")
880
+ print(f" 4. 确保图片中有清晰可见的物体")
881
+ print(f"{'='*70}\n")
882
 
883
  # Cleanup
884
  progress(0.95, desc="🧹 清理内存...")
 
1713
  .file-upload-enhanced {
1714
  position: relative;
1715
  }
1716
+
1717
+ /* 减少Accordion和后续内容的间距 */
1718
+ .accordion {
1719
+ margin-bottom: 10px !important;
1720
+ }
1721
+
1722
+ /* 页脚样式 */
1723
+ .footer {
1724
+ margin-top: 20px !important;
1725
+ padding: 10px 0 !important;
1726
+ }
1727
+
1728
+ /* 信息提示框 */
1729
+ .info-box {
1730
+ background-color: #E3F2FD !important;
1731
+ border-left: 4px solid #2196F3 !important;
1732
+ padding: 15px !important;
1733
+ margin-bottom: 15px !important;
1734
+ border-radius: 4px !important;
1735
+ }
1736
  """
1737
 
1738
  # JavaScript for paste support
 
1847
  )
1848
 
1849
  with gr.Tab("🎨 分割3D"):
1850
+ gr.Markdown(
1851
+ """
1852
+ 💡 **使用说明**:
1853
+ 1. 在下方「⚙️ 高级选项」中勾选「启用语义分割 (CPU)」
1854
+ 2. 点击「开始重建」按钮
1855
+ 3. 等待处理完成后,分割结果将显示在此处
1856
+
1857
+ 📌 如果没有显示分割结果,请查看控制台日志查找原因
1858
+ """,
1859
+ elem_classes=["info-box"]
1860
+ )
1861
  segmented_output = gr.Model3D(
1862
+ height=450, zoom_speed=0.5, pan_speed=0.5,
1863
  clear_color=[0.0, 0.0, 0.0, 0.0]
1864
  )
1865
 
 
1982
  target_dir_output, image_gallery, log_output
1983
  ]
1984
  )
1985
+
1986
+ # 页脚
1987
+ gr.Markdown("---") # 分隔线
1988
+ gr.Markdown(
1989
+ """
1990
+ <div style="text-align: center; padding: 20px 0; color: #666;">
1991
+ <p>MapAnything V2 - 3D重建与物体分割系统</p>
1992
+ <p style="font-size: 14px;">基于 DBSCAN 聚类的多视图融合 | CPU优化的轻量级分割</p>
1993
+ </div>
1994
+ """,
1995
+ elem_classes=["footer"]
1996
+ )
1997
 
1998
  # === 事件绑定 ===
1999