danylgao commited on
Commit
73441d8
·
1 Parent(s): 23cb1a2
Files changed (2) hide show
  1. app.py +13 -47
  2. requirements.txt +11 -7
app.py CHANGED
@@ -67,21 +67,13 @@ plot_results = None
67
 
68
  try:
69
  import sam3
 
70
  SAM3_INSTALLED = True
71
  print("✅ sam3 库已安装")
72
 
73
- # 尝试导入主要组件,使用更灵活的方式
74
- try:
75
- from sam3 import build_sam3_image_model
76
- print("✅ build_sam3_image_model 导入成功")
77
- except ImportError as e:
78
- print(f"⚠️ build_sam3_image_model 导入失败: {e}")
79
- build_sam3_image_model = None
80
-
81
  # 尝试导入其他组件
82
  try:
83
  from sam3.model.box_ops import box_xywh_to_cxcywh
84
- print("✅ box_ops 导入成功")
85
  except ImportError as e:
86
  print(f"⚠️ box_ops 导入失败: {e}")
87
  # 定义一个简单的替代函数
@@ -94,14 +86,12 @@ try:
94
 
95
  try:
96
  from sam3.model.sam3_image_processor import Sam3Processor
97
- print("✅ Sam3Processor 导入成功")
98
  except ImportError as e:
99
  print(f"⚠️ Sam3Processor 导入失败: {e}")
100
  Sam3Processor = None
101
 
102
  try:
103
  from sam3.visualization_utils import normalize_bbox, draw_box_on_image, plot_mask, plot_bbox, COLORS, plot_results
104
- print("✅ visualization_utils 导入成功")
105
  except ImportError as e:
106
  print(f"⚠️ visualization_utils 导入失败: {e}")
107
  # 定义简单的替代函数
@@ -142,37 +132,7 @@ try:
142
  except ImportError as e:
143
  print(f"❌ sam3 库导入失败: {e}")
144
  print("请确保 requirements.txt 中包含: git+https://github.com/facebookresearch/sam3.git")
145
- # 提供更详细的错误信息
146
- import traceback
147
- traceback.print_exc()
148
-
149
- # 检查是否是因为sam3库结构问题
150
- print("\n尝试检查sam3库结构...")
151
- try:
152
- import importlib.util
153
- spec = importlib.util.find_spec("sam3")
154
- if spec and spec.origin:
155
- import os
156
- sam3_path = os.path.dirname(spec.origin)
157
- print(f"sam3库路径: {sam3_path}")
158
- if os.path.exists(sam3_path):
159
- print("sam3目录内容:", os.listdir(sam3_path))
160
- except Exception as ex:
161
- print(f"检查sam3库结构失败: {ex}")
162
-
163
- # 增强 ImagePrompter 兼容性处理
164
- def create_image_input(label="🖼️ 示例图像", type="pil"):
165
- """创建图像输入组件,处理 ImagePrompter 兼容性问题"""
166
- if IMAGE_PROMPTER_AVAILABLE:
167
- try:
168
- # 尝试使用最小化参数初始化 ImagePrompter
169
- return ImagePrompter(label=label, type=type)
170
- except Exception as e:
171
- print(f"⚠️ ImagePrompter 初始化失败,使用标准 gr.Image: {e}")
172
- # 回退到标准 gr.Image
173
- return gr.Image(label=label, type=type)
174
- else:
175
- return gr.Image(label=label, type=type)
176
 
177
  # --- 0. ImagePrompter 数据解析函数 ---
178
 
@@ -1128,10 +1088,16 @@ with gr.Blocks() as demo:
1128
  - **线条/点宽度**: 调整边框线宽或中心点大小
1129
  """)
1130
 
1131
- unified_image_input = create_image_input(
1132
- label="🖼️ 示例图像",
1133
- type="pil"
1134
- )
 
 
 
 
 
 
1135
 
1136
  prompt_type = gr.Radio(
1137
  ["Text", "Single Box", "Multi Box"],
@@ -1321,5 +1287,5 @@ if __name__ == "__main__":
1321
  # Hugging Face Spaces 会自动处理端口、地址和共享设置
1322
  # 不要指定 server_name 和 server_port,让平台自动配置
1323
  demo.launch(
1324
- show_error=True, # 显示详细错误信息,方便调试
1325
  )
 
67
 
68
  try:
69
  import sam3
70
+ from sam3 import build_sam3_image_model
71
  SAM3_INSTALLED = True
72
  print("✅ sam3 库已安装")
73
 
 
 
 
 
 
 
 
 
74
  # 尝试导入其他组件
75
  try:
76
  from sam3.model.box_ops import box_xywh_to_cxcywh
 
77
  except ImportError as e:
78
  print(f"⚠️ box_ops 导入失败: {e}")
79
  # 定义一个简单的替代函数
 
86
 
87
  try:
88
  from sam3.model.sam3_image_processor import Sam3Processor
 
89
  except ImportError as e:
90
  print(f"⚠️ Sam3Processor 导入失败: {e}")
91
  Sam3Processor = None
92
 
93
  try:
94
  from sam3.visualization_utils import normalize_bbox, draw_box_on_image, plot_mask, plot_bbox, COLORS, plot_results
 
95
  except ImportError as e:
96
  print(f"⚠️ visualization_utils 导入失败: {e}")
97
  # 定义简单的替代函数
 
132
  except ImportError as e:
133
  print(f"❌ sam3 库导入失败: {e}")
134
  print("请确保 requirements.txt 中包含: git+https://github.com/facebookresearch/sam3.git")
135
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  # --- 0. ImagePrompter 数据解析函数 ---
138
 
 
1088
  - **线条/点宽度**: 调整边框线宽或中心点大小
1089
  """)
1090
 
1091
+ if IMAGE_PROMPTER_AVAILABLE:
1092
+ unified_image_input = ImagePrompter(
1093
+ label="🖼️ 示例图像",
1094
+ type="pil"
1095
+ )
1096
+ else:
1097
+ unified_image_input = gr.Image(
1098
+ label="🖼️ 示例图像",
1099
+ type="pil"
1100
+ )
1101
 
1102
  prompt_type = gr.Radio(
1103
  ["Text", "Single Box", "Multi Box"],
 
1287
  # Hugging Face Spaces 会自动处理端口、地址和共享设置
1288
  # 不要指定 server_name 和 server_port,让平台自动配置
1289
  demo.launch(
1290
+ show_error=True # 显示详细错误信息,方便调试
1291
  )
requirements.txt CHANGED
@@ -8,14 +8,18 @@
8
  git+https://github.com/facebookresearch/sam3.git
9
 
10
  # ========== PyTorch (HF Spaces 通常已预装) ==========
11
- torch>=2.0.0
12
- torchvision>=0.15.0
13
 
14
  # ========== Gradio 相关 ==========
15
- # 注意:gradio-image-prompter 0.1.0 需要 gradio<5.0,>=4.0
16
- gradio>=4.0,<5.0
17
- gradio-image-prompter
18
 
19
  # ========== 图像处理 ==========
20
- Pillow>=9.0.0
21
- matplotlib>=3.5.0
 
 
 
 
 
8
  git+https://github.com/facebookresearch/sam3.git
9
 
10
  # ========== PyTorch (HF Spaces 通常已预装) ==========
11
+ torch==2.2.2
12
+ torchvision==0.17.2
13
 
14
  # ========== Gradio 相关 ==========
15
+ # 注意:HF Spaces 可能预装特定版本,建议不强制指定版本
16
+ gradio==4.43.0
17
+ gradio_image_prompter==0.1.0
18
 
19
  # ========== 图像处理 ==========
20
+ Pillow==10.4.0
21
+ matplotlib==3.10.7
22
+
23
+ # ========== 其他关键依赖 ==========
24
+ pydantic==2.7.1
25
+ huggingface-hub==0.36.0