geshang commited on
Commit
12c4eca
·
verified ·
1 Parent(s): d70014a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -15,39 +15,39 @@ from typing import List, Tuple, Optional
15
 
16
  # ------------------ 安装SAM2依赖 ------------------
17
  def install_sam2():
18
- """安装SAM2及其依赖,并下载检查点"""
19
  sam2_dir = "third_party/sam2"
20
- checkpoints_dir = os.path.join(sam2_dir, "checkpoints")
21
-
22
- # 创建目录
23
- os.makedirs(checkpoints_dir, exist_ok=True)
24
-
25
- # 克隆仓库(如果不存在)
26
  if not os.path.exists(sam2_dir):
27
- print("Cloning SAM2 repository...")
 
 
 
28
  subprocess.run(["git", "clone", "https://github.com/facebookresearch/sam2.git", sam2_dir], check=True)
29
-
30
- # 安装依赖
31
- print("Installing SAM2 dependencies...")
32
- subprocess.run(["pip", "install", "-e", sam2_dir], check=True)
33
-
34
- # 直接下载检查点文件(避免使用脚本)
35
- checkpoint_urls = {
36
- "sam2.1_hiera_large.pt": "https://dl.fbaipublicfiles.com/sam2/sam2.1_hiera_large.pt",
37
- "sam2.1_hiera_base.pt": "https://dl.fbaipublicfiles.com/sam2/sam2.1_hiera_base.pt"
38
- }
39
-
40
- print("Downloading SAM2 checkpoints...")
41
- for filename, url in checkpoint_urls.items():
42
- filepath = os.path.join(checkpoints_dir, filename)
43
- if not os.path.exists(filepath):
44
- print(f"Downloading {filename}...")
45
- response = requests.get(url, stream=True)
46
- with open(filepath, "wb") as f:
47
- for chunk in response.iter_content(chunk_size=8192):
48
- f.write(chunk)
49
-
50
- print("SAM2 installation completed successfully!")
 
 
 
51
 
52
  # 确保安装SAM2
53
  install_sam2()
 
15
 
16
  # ------------------ 安装SAM2依赖 ------------------
17
  def install_sam2():
18
+ """检查并安装SAM2及其依赖"""
19
  sam2_dir = "third_party/sam2"
 
 
 
 
 
 
20
  if not os.path.exists(sam2_dir):
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(["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()