geshang commited on
Commit
d70014a
·
verified ·
1 Parent(s): 7dacac2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -15,23 +15,39 @@ from typing import List, Tuple, Optional
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
  subprocess.run(["git", "clone", "https://github.com/facebookresearch/sam2.git", sam2_dir], check=True)
24
-
25
- # 安装依赖
26
- subprocess.run(["pip", "install", "-e", sam2_dir], check=True)
27
-
28
- # 下载检查点
29
- checkpoints_dir = os.path.join(sam2_dir, "checkpoints")
30
- os.makedirs(checkpoints_dir, exist_ok=True)
31
- subprocess.run([os.path.join(checkpoints_dir, "download_ckpts.sh")], cwd=checkpoints_dir, shell=True, check=True)
32
- print("SAM2 installed successfully!")
33
- else:
34
- print("SAM2 already installed.")
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # 确保安装SAM2
37
  install_sam2()
 
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()