JasonYinnnn commited on
Commit
72400de
·
1 Parent(s): a6f6488

try lazy initialization for SAM2

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -101,13 +101,7 @@ if not os.path.exists("./checkpoints/sam2.1_hiera_large.pt"):
101
  SAM2_CHECKPOINT = "./checkpoints/sam2.1_hiera_large.pt"
102
  SAM2_CONFIG = "configs/sam2.1/sam2.1_hiera_l.yaml"
103
 
104
- sam2_model = build_sam2(
105
- config_file=SAM2_CONFIG,
106
- ckpt_path=SAM2_CHECKPOINT,
107
- device='cpu'
108
- )
109
- sam2_predictor = SAM2ImagePredictor(sam2_model)
110
- # sam2_predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-large", device=torch.device("cpu"))
111
 
112
  ############## 3D-Fixer model
113
  model_dir = 'HorizonRobotics/3D-Fixer'
@@ -231,6 +225,14 @@ def run_segmentation(
231
  rgb_image = image_prompts["image"].convert("RGB")
232
 
233
  global work_space
 
 
 
 
 
 
 
 
234
 
235
  # pre-process the layers and get the xyxy boxes of each layer
236
  if len(image_prompts["points"]) == 0:
 
101
  SAM2_CHECKPOINT = "./checkpoints/sam2.1_hiera_large.pt"
102
  SAM2_CONFIG = "configs/sam2.1/sam2.1_hiera_l.yaml"
103
 
104
+ sam2_predictor = None
 
 
 
 
 
 
105
 
106
  ############## 3D-Fixer model
107
  model_dir = 'HorizonRobotics/3D-Fixer'
 
225
  rgb_image = image_prompts["image"].convert("RGB")
226
 
227
  global work_space
228
+ global sam2_predictor
229
+
230
+ if sam2_predictor is None:
231
+ sam2_model = build_sam2(
232
+ config_file=SAM2_CONFIG,
233
+ ckpt_path=SAM2_CHECKPOINT,
234
+ )
235
+ sam2_predictor = SAM2ImagePredictor(sam2_model)
236
 
237
  # pre-process the layers and get the xyxy boxes of each layer
238
  if len(image_prompts["points"]) == 0: