geshang commited on
Commit
fedfd3d
·
verified ·
1 Parent(s): 433a03e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -11
app.py CHANGED
@@ -13,6 +13,15 @@ import base64
13
  import cv2
14
  from typing import List, Tuple, Optional
15
 
 
 
 
 
 
 
 
 
 
16
  # ------------------ 安装SAM2依赖 ------------------
17
  def install_sam2():
18
  """检查并安装SAM2及其依赖"""
@@ -21,37 +30,66 @@ def install_sam2():
21
  print("Installing SAM2...")
22
  os.makedirs("third_party", exist_ok=True)
23
 
24
- # 克隆仓库
25
- subprocess.run(["git", "clone", "https://github.com/facebookresearch/sam2.git", sam2_dir], check=True)
 
 
 
 
 
26
 
27
  # 切换到sam2目录安装
28
  original_dir = os.getcwd()
29
  try:
30
  os.chdir(sam2_dir)
31
 
32
- # 安装依赖
33
- subprocess.run(["python","-m","pip", "install", "-e", "."], check=True)
 
 
 
34
 
35
  # 切换到checkpoints目录下载模型
36
  os.chdir("checkpoints")
37
  if not os.path.exists("download_ckpts.sh"):
38
- # 如果脚本不存在,可能是权限问题,尝试直接下载
39
- subprocess.run(["wget", "https://raw.githubusercontent.com/facebookresearch/sam2/main/checkpoints/download_ckpts.sh"], check=True)
 
 
40
  subprocess.run(["chmod", "+x", "download_ckpts.sh"], check=True)
41
 
42
- subprocess.run(["./download_ckpts.sh"], check=True)
 
 
 
 
 
 
 
 
43
 
 
 
 
44
  finally:
45
- # 确保切换回原始目录
46
  os.chdir(original_dir)
47
 
48
- print("SAM2 installed successfully!")
49
  else:
50
- print("SAM2 already installed.")
51
 
52
- # 确保安装SAM2
 
53
  install_sam2()
54
 
 
 
 
 
 
 
 
 
55
  # ------------------ 初始化模型 ------------------
56
  # 使用相对路径
57
  MODEL_PATH = "geshang/Seg-R1-COD"
 
13
  import cv2
14
  from typing import List, Tuple, Optional
15
 
16
+ # ------------------ 安装SAM2依赖 ------------------
17
+ # ------------------ 修复SAM2模块路径问题 ------------------
18
+ def add_sam2_to_path():
19
+ """将SAM2安装目录添加到Python路径"""
20
+ sam2_dir = os.path.abspath("third_party/sam2")
21
+ if sam2_dir not in sys.path:
22
+ sys.path.insert(0, sam2_dir)
23
+ return sam2_dir
24
+
25
  # ------------------ 安装SAM2依赖 ------------------
26
  def install_sam2():
27
  """检查并安装SAM2及其依赖"""
 
30
  print("Installing SAM2...")
31
  os.makedirs("third_party", exist_ok=True)
32
 
33
+ # 克隆仓库(添加--recursive以防有子模块)
34
+ subprocess.run([
35
+ "git", "clone",
36
+ "--recursive",
37
+ "https://github.com/facebookresearch/sam2.git",
38
+ sam2_dir
39
+ ], check=True)
40
 
41
  # 切换到sam2目录安装
42
  original_dir = os.getcwd()
43
  try:
44
  os.chdir(sam2_dir)
45
 
46
+ # 先安装核心依赖
47
+ subprocess.run(["pip", "install", "-r", "requirements.txt"], check=True)
48
+
49
+ # 以可编辑模式安装SAM2
50
+ subprocess.run(["pip", "install", "-e", "."], check=True)
51
 
52
  # 切换到checkpoints目录下载模型
53
  os.chdir("checkpoints")
54
  if not os.path.exists("download_ckpts.sh"):
55
+ subprocess.run([
56
+ "wget",
57
+ "https://raw.githubusercontent.com/facebookresearch/sam2/main/checkpoints/download_ckpts.sh"
58
+ ], check=True)
59
  subprocess.run(["chmod", "+x", "download_ckpts.sh"], check=True)
60
 
61
+ # 下载检查点(添加超时和重试)
62
+ result = subprocess.run(["./download_ckpts.sh"], check=False)
63
+ if result.returncode != 0:
64
+ print("Warning: Checkpoint download failed, trying alternative method...")
65
+ subprocess.run([
66
+ "wget",
67
+ "https://dl.fbaipublicfiles.com/sam2/sam2.1_hiera_large.pt",
68
+ "-O", "sam2.1_hiera_large.pt"
69
+ ], check=True)
70
 
71
+ except Exception as e:
72
+ print(f"Error during SAM2 installation: {str(e)}")
73
+ raise
74
  finally:
 
75
  os.chdir(original_dir)
76
 
77
+ print("SAM2 installed successfully!")
78
  else:
79
+ print("SAM2 already exists, skipping installation.")
80
 
81
+
82
+ # 1. 安装SAM2
83
  install_sam2()
84
 
85
+ # 2. 确保路径正确
86
+ sam2_dir = add_sam2_to_path()
87
+
88
+ # 3. 现在可以安全导入SAM2模块
89
+ from sam2.build_sam import build_sam2
90
+ from sam2.sam2_image_predictor import SAM2ImagePredictor
91
+ print("🎉 SAM2 modules imported successfully!")
92
+
93
  # ------------------ 初始化模型 ------------------
94
  # 使用相对路径
95
  MODEL_PATH = "geshang/Seg-R1-COD"