Spaces:
Running
Running
jhj0517
commited on
Commit
•
888e2b8
1
Parent(s):
64b615e
Update output path
Browse files- modules/sam_inference.py +10 -15
modules/sam_inference.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
|
2 |
from sam2.build_sam import build_sam2, build_sam2_video_predictor
|
3 |
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
4 |
-
from sam2.sam2_video_predictor import SAM2VideoPredictor
|
5 |
from typing import Dict, List, Optional
|
6 |
import torch
|
7 |
import os
|
@@ -10,12 +9,12 @@ import numpy as np
|
|
10 |
import gradio as gr
|
11 |
|
12 |
from modules.model_downloader import (
|
13 |
-
AVAILABLE_MODELS, DEFAULT_MODEL_TYPE,
|
14 |
is_sam_exist,
|
15 |
download_sam_model_url
|
16 |
)
|
17 |
-
from modules.paths import
|
18 |
-
from modules.constants import BOX_PROMPT_MODE, AUTOMATIC_MODE, COLOR_FILTER, PIXELIZE_FILTER, IMAGE_FILE_EXT
|
19 |
from modules.mask_utils import (
|
20 |
save_psd_with_masks,
|
21 |
create_mask_combined_images,
|
@@ -28,12 +27,6 @@ from modules.video_utils import (get_frames_from_dir, create_video_from_frames,
|
|
28 |
from modules.utils import save_image
|
29 |
from modules.logger_util import get_logger
|
30 |
|
31 |
-
MODEL_CONFIGS = {
|
32 |
-
"sam2_hiera_tiny": os.path.join(SAM2_CONFIGS_DIR, "sam2_hiera_t.yaml"),
|
33 |
-
"sam2_hiera_small": os.path.join(SAM2_CONFIGS_DIR, "sam2_hiera_s.yaml"),
|
34 |
-
"sam2_hiera_base_plus": os.path.join(SAM2_CONFIGS_DIR, "sam2_hiera_b+.yaml"),
|
35 |
-
"sam2_hiera_large": os.path.join(SAM2_CONFIGS_DIR, "sam2_hiera_l.yaml"),
|
36 |
-
}
|
37 |
logger = get_logger()
|
38 |
|
39 |
|
@@ -64,11 +57,12 @@ class SamInference:
|
|
64 |
|
65 |
config = MODEL_CONFIGS[model_type]
|
66 |
filename, url = AVAILABLE_MODELS[model_type]
|
|
|
67 |
model_path = os.path.join(self.model_dir, filename)
|
68 |
|
69 |
-
if not is_sam_exist(model_type):
|
70 |
logger.info(f"No SAM2 model found, downloading {model_type} model...")
|
71 |
-
download_sam_model_url(model_type)
|
72 |
logger.info(f"Applying configs to {model_type} model..")
|
73 |
|
74 |
if load_video_predictor:
|
@@ -79,6 +73,7 @@ class SamInference:
|
|
79 |
ckpt_path=model_path,
|
80 |
device=self.device
|
81 |
)
|
|
|
82 |
except Exception as e:
|
83 |
logger.exception("Error while loading SAM2 model for video predictor")
|
84 |
|
@@ -276,6 +271,7 @@ class SamInference:
|
|
276 |
"Please press the eraser button (on the image prompter) and add your prompts again.")
|
277 |
logger.error(error_message)
|
278 |
raise gr.Error(error_message, duration=20)
|
|
|
279 |
|
280 |
clean_files_with_extension(TEMP_OUT_DIR, IMAGE_FILE_EXT)
|
281 |
self.video_predictor.reset_state(self.video_inference_state)
|
@@ -308,13 +304,13 @@ class SamInference:
|
|
308 |
save_image(image=filtered_image, output_dir=TEMP_OUT_DIR)
|
309 |
|
310 |
if len(video_segments) == 1:
|
311 |
-
out_image = save_image(image=filtered_image, output_dir=
|
312 |
return None, out_image
|
313 |
|
314 |
out_video = create_video_from_frames(
|
315 |
frames_dir=TEMP_OUT_DIR,
|
316 |
frame_rate=self.video_info.frame_rate,
|
317 |
-
output_dir=
|
318 |
)
|
319 |
|
320 |
return out_video, out_video
|
@@ -328,7 +324,6 @@ class SamInference:
|
|
328 |
timestamp = datetime.now().strftime("%m%d%H%M%S")
|
329 |
output_file_name = f"result-{timestamp}.psd"
|
330 |
output_path = os.path.join(self.output_dir, "psd", output_file_name)
|
331 |
-
|
332 |
# Pre-processed gradio components
|
333 |
hparams = {
|
334 |
'points_per_side': int(params[0]),
|
|
|
1 |
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
|
2 |
from sam2.build_sam import build_sam2, build_sam2_video_predictor
|
3 |
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
|
4 |
from typing import Dict, List, Optional
|
5 |
import torch
|
6 |
import os
|
|
|
9 |
import gradio as gr
|
10 |
|
11 |
from modules.model_downloader import (
|
12 |
+
AVAILABLE_MODELS, DEFAULT_MODEL_TYPE,
|
13 |
is_sam_exist,
|
14 |
download_sam_model_url
|
15 |
)
|
16 |
+
from modules.paths import (MODELS_DIR, TEMP_OUT_DIR, TEMP_DIR, MODEL_CONFIGS, OUTPUT_DIR)
|
17 |
+
from modules.constants import (BOX_PROMPT_MODE, AUTOMATIC_MODE, COLOR_FILTER, PIXELIZE_FILTER, IMAGE_FILE_EXT)
|
18 |
from modules.mask_utils import (
|
19 |
save_psd_with_masks,
|
20 |
create_mask_combined_images,
|
|
|
27 |
from modules.utils import save_image
|
28 |
from modules.logger_util import get_logger
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
logger = get_logger()
|
31 |
|
32 |
|
|
|
57 |
|
58 |
config = MODEL_CONFIGS[model_type]
|
59 |
filename, url = AVAILABLE_MODELS[model_type]
|
60 |
+
|
61 |
model_path = os.path.join(self.model_dir, filename)
|
62 |
|
63 |
+
if not is_sam_exist(model_dir=self.model_dir, model_type=model_type):
|
64 |
logger.info(f"No SAM2 model found, downloading {model_type} model...")
|
65 |
+
download_sam_model_url(model_dir=self.model_dir, model_type=model_type)
|
66 |
logger.info(f"Applying configs to {model_type} model..")
|
67 |
|
68 |
if load_video_predictor:
|
|
|
73 |
ckpt_path=model_path,
|
74 |
device=self.device
|
75 |
)
|
76 |
+
return
|
77 |
except Exception as e:
|
78 |
logger.exception("Error while loading SAM2 model for video predictor")
|
79 |
|
|
|
271 |
"Please press the eraser button (on the image prompter) and add your prompts again.")
|
272 |
logger.error(error_message)
|
273 |
raise gr.Error(error_message, duration=20)
|
274 |
+
output_dir = os.path.join(self.output_dir, "filter")
|
275 |
|
276 |
clean_files_with_extension(TEMP_OUT_DIR, IMAGE_FILE_EXT)
|
277 |
self.video_predictor.reset_state(self.video_inference_state)
|
|
|
304 |
save_image(image=filtered_image, output_dir=TEMP_OUT_DIR)
|
305 |
|
306 |
if len(video_segments) == 1:
|
307 |
+
out_image = save_image(image=filtered_image, output_dir=output_dir)
|
308 |
return None, out_image
|
309 |
|
310 |
out_video = create_video_from_frames(
|
311 |
frames_dir=TEMP_OUT_DIR,
|
312 |
frame_rate=self.video_info.frame_rate,
|
313 |
+
output_dir=output_dir,
|
314 |
)
|
315 |
|
316 |
return out_video, out_video
|
|
|
324 |
timestamp = datetime.now().strftime("%m%d%H%M%S")
|
325 |
output_file_name = f"result-{timestamp}.psd"
|
326 |
output_path = os.path.join(self.output_dir, "psd", output_file_name)
|
|
|
327 |
# Pre-processed gradio components
|
328 |
hparams = {
|
329 |
'points_per_side': int(params[0]),
|