John Ho commited on
Commit
74821c0
·
1 Parent(s): 51b87e3

added missing cv2 requirement and new function to download model weights

Browse files
app.py CHANGED
@@ -1,10 +1,42 @@
1
  import gradio as gr
2
- import spaces, torch
 
 
3
  from samv2_handler import load_sam_image_model, run_sam_im_inference
4
  from PIL import Image
5
  from typing import Union
6
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  @spaces.GPU
9
  def load_im_model(variant, auto_mask_gen: bool = False):
10
  return load_sam_image_model(
@@ -55,6 +87,8 @@ with gr.Blocks() as demo:
55
  outputs=gr.JSON(label="Output JSON"),
56
  title="SAM2 for Images",
57
  )
 
 
58
  demo.launch(
59
  mcp_server=True, app_kwargs={"docs_url": "/docs"} # add FastAPI Swagger API Docs
60
  )
 
1
  import gradio as gr
2
+ import spaces, torch, os, requests, json
3
+ from pathlib import Path
4
+ from tqdm import tqdm
5
  from samv2_handler import load_sam_image_model, run_sam_im_inference
6
  from PIL import Image
7
  from typing import Union
8
 
9
 
10
+ def download_checkpoints():
11
+ checkpoint_dir = Path("checkpoints")
12
+ checkpoint_dir.mkdir(exist_ok=True)
13
+
14
+ # Read URLs from the file
15
+ with open(checkpoint_dir / "sam2_checkpoints_url.txt", "r") as f:
16
+ urls = [url.strip() for url in f.readlines() if url.strip()]
17
+
18
+ for url in urls:
19
+ filename = url.split("/")[-1]
20
+ output_path = checkpoint_dir / filename
21
+
22
+ if output_path.exists():
23
+ print(f"Checkpoint {filename} already exists, skipping...")
24
+ continue
25
+
26
+ print(f"Downloading {filename}...")
27
+ response = requests.get(url, stream=True)
28
+ total_size = int(response.headers.get("content-length", 0))
29
+
30
+ with open(output_path, "wb") as f:
31
+ with tqdm(total=total_size, unit="B", unit_scale=True) as pbar:
32
+ for chunk in response.iter_content(chunk_size=8192):
33
+ if chunk:
34
+ f.write(chunk)
35
+ pbar.update(len(chunk))
36
+
37
+ print(f"Downloaded {filename} successfully!")
38
+
39
+
40
  @spaces.GPU
41
  def load_im_model(variant, auto_mask_gen: bool = False):
42
  return load_sam_image_model(
 
87
  outputs=gr.JSON(label="Output JSON"),
88
  title="SAM2 for Images",
89
  )
90
+ # Download checkpoints before launching the app
91
+ download_checkpoints()
92
  demo.launch(
93
  mcp_server=True, app_kwargs={"docs_url": "/docs"} # add FastAPI Swagger API Docs
94
  )
checkpoints/sam2_checkpoints_url.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_tiny.pt
2
+ https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_small.pt
3
+ https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_base_plus.pt
4
+ https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_large.pt
requirements.txt CHANGED
@@ -6,3 +6,4 @@ pytest>=8.3.5
6
  retrying>=1.3.4
7
  samv2==0.0.4
8
  validators>=0.35.0
 
 
6
  retrying>=1.3.4
7
  samv2==0.0.4
8
  validators>=0.35.0
9
+ opencv-python-headless>=4.11.0.86