Spaces:
Running
Running
File size: 22,037 Bytes
cfa5142 0f36b51 cfa5142 17abb6a cfa5142 b0345e8 cfa5142 888e2b8 cfa5142 888e2b8 cfa5142 6df697d cfa5142 3423be2 5929ef8 e56e825 5f8864d cfa5142 5f8864d cfa5142 6df697d cfa5142 e56e825 cfa5142 60434a4 2878798 3423be2 cfa5142 0f36b51 6df697d 0f36b51 17abb6a 6df697d b109f8e 6df697d 888e2b8 cfa5142 888e2b8 6df697d 888e2b8 b0345e8 cfa5142 0f36b51 2878798 b109f8e 0f36b51 888e2b8 0f36b51 cfa5142 b109f8e cfa5142 0f36b51 1de582e 8d52a7d 2878798 e56e825 17abb6a e56e825 6df697d 2878798 3423be2 2878798 6df697d 2878798 3423be2 2878798 cfa5142 2c719e3 17abb6a 6df697d 2c719e3 87a101a 1de582e 87a101a cfa5142 2c719e3 41938cd 17abb6a 6df697d 2c719e3 cfa5142 87a101a 41938cd 87a101a e56e825 1de582e 2c719e3 e56e825 17abb6a e56e825 2878798 e56e825 2878798 6df697d 2878798 6df697d 2878798 e56e825 1de582e 2878798 6df697d 2878798 e56e825 17abb6a e56e825 1de582e e56e825 0f36b51 2878798 6df697d 2878798 17abb6a 6df697d e56e825 6df697d e56e825 6df697d e56e825 6df697d 2878798 3423be2 17abb6a e56e825 1de582e e56e825 888e2b8 e56e825 5929ef8 719689e e56e825 075107e e56e825 a8fcc23 888e2b8 a8fcc23 3423be2 719689e 3423be2 888e2b8 3423be2 2c719e3 17abb6a 2c719e3 0f36b51 002d880 2c719e3 002d880 2c719e3 16bf670 41938cd ebdd2a9 41938cd 6df697d 2c719e3 16bf670 2c719e3 6df697d 002d880 2c719e3 16bf670 8d52a7d 2c719e3 0f36b51 cfa5142 0f36b51 16bf670 17abb6a 16bf670 ebdd2a9 002d880 16bf670 6df697d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
from sam2.build_sam import build_sam2, build_sam2_video_predictor
from sam2.sam2_image_predictor import SAM2ImagePredictor
from typing import Dict, List, Optional, Tuple, Any
import torch
import os
from datetime import datetime
import numpy as np
import gradio as gr
from modules.model_downloader import (
AVAILABLE_MODELS, DEFAULT_MODEL_TYPE,
is_sam_exist,
download_sam_model_url
)
from modules.paths import (MODELS_DIR, TEMP_OUT_DIR, TEMP_DIR, MODEL_CONFIGS, OUTPUT_DIR)
from modules.constants import (BOX_PROMPT_MODE, AUTOMATIC_MODE, COLOR_FILTER, PIXELIZE_FILTER, IMAGE_FILE_EXT)
from modules.mask_utils import (
save_psd_with_masks,
create_mask_combined_images,
create_mask_gallery,
create_mask_pixelized_image,
create_solid_color_mask_image
)
from modules.video_utils import (get_frames_from_dir, create_video_from_frames, get_video_info, extract_frames,
extract_sound, clean_temp_dir, clean_files_with_extension)
from modules.utils import save_image
from modules.logger_util import get_logger
logger = get_logger()
class SamInference:
def __init__(self,
model_dir: str = MODELS_DIR,
output_dir: str = OUTPUT_DIR
):
self.model = None
self.available_models = list(AVAILABLE_MODELS.keys())
self.current_model_type = DEFAULT_MODEL_TYPE
self.model_dir = model_dir
self.output_dir = output_dir
self.model_path = os.path.join(self.model_dir, AVAILABLE_MODELS[DEFAULT_MODEL_TYPE][0])
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.dtype = torch.float16 if torch.cuda.is_available() else torch.float32
self.mask_generator = None
self.image_predictor = None
self.video_predictor = None
self.video_inference_state = None
self.video_info = None
def load_model(self,
model_type: Optional[str] = None,
load_video_predictor: bool = False):
"""
Load the model from the model directory. If the model is not found, download it from the URL.
Args:
model_type (str): The model type to load.
load_video_predictor (bool): Load the video predictor model.
"""
if model_type is None:
model_type = DEFAULT_MODEL_TYPE
config_path = MODEL_CONFIGS[model_type]
config_dir, config_name = os.path.split(config_path)
filename, url = AVAILABLE_MODELS[model_type]
model_path = os.path.join(self.model_dir, filename)
if not is_sam_exist(model_dir=self.model_dir, model_type=model_type):
logger.info(f"No SAM2 model found, downloading {model_type} model...")
download_sam_model_url(model_dir=self.model_dir, model_type=model_type)
logger.info(f"Applying configs to {model_type} model..")
if load_video_predictor:
try:
self.model = None
self.video_predictor = build_sam2_video_predictor(
config_file=config_name,
ckpt_path=model_path,
device=self.device
)
return
except Exception as e:
logger.exception("Error while loading SAM2 model for video predictor")
try:
self.model = build_sam2(
config_file=config_name,
ckpt_path=model_path,
device=self.device
)
except Exception as e:
logger.exception("Error while loading SAM2 model")
raise RuntimeError(f"Failed to load model") from e
def init_video_inference_state(self,
vid_input: str,
model_type: Optional[str] = None):
"""
Initialize the video inference state for the video predictor.
Args:
vid_input (str): The video frames directory.
model_type (str): The model type to load.
"""
if model_type is None:
model_type = self.current_model_type
if self.video_predictor is None or model_type != self.current_model_type:
self.current_model_type = model_type
self.load_model(model_type=model_type, load_video_predictor=True)
self.video_info = get_video_info(vid_input)
frames_temp_dir = TEMP_DIR
clean_temp_dir(frames_temp_dir)
extract_frames(vid_input, frames_temp_dir)
if self.video_info.has_sound:
extract_sound(vid_input, frames_temp_dir)
if self.video_inference_state is not None:
self.video_predictor.reset_state(self.video_inference_state)
self.video_inference_state = None
self.video_inference_state = self.video_predictor.init_state(video_path=frames_temp_dir)
def generate_mask(self,
image: np.ndarray,
model_type: str,
**params) -> List[Dict[str, Any]]:
"""
Generate masks with Automatic segmentation. Default hyperparameters are in './configs/default_hparams.yaml.'
Args:
image (np.ndarray): The input image.
model_type (str): The model type to load.
**params: The hyperparameters for the mask generator.
Returns:
List[Dict[str, Any]]: The auto-generated mask data.
"""
if self.model is None or self.current_model_type != model_type:
self.current_model_type = model_type
self.load_model(model_type=model_type)
self.mask_generator = SAM2AutomaticMaskGenerator(
model=self.model,
**params
)
try:
generated_masks = self.mask_generator.generate(image)
except Exception as e:
logger.exception(f"Error while auto generating masks : {e}")
raise RuntimeError(f"Failed to generate masks") from e
return generated_masks
def predict_image(self,
image: np.ndarray,
model_type: str,
box: Optional[np.ndarray] = None,
point_coords: Optional[np.ndarray] = None,
point_labels: Optional[np.ndarray] = None,
**params) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""
Predict image with prompt data.
Args:
image (np.ndarray): The input image.
model_type (str): The model type to load.
box (np.ndarray): The box prompt data.
point_coords (np.ndarray): The point coordinates prompt data.
point_labels (np.ndarray): The point labels prompt data.
**params: The hyperparameters for the mask generator.
Returns:
np.ndarray: The predicted masks output in CxHxW format.
np.ndarray: Array of scores for each mask.
np.ndarray: Array of logits in CxHxW format.
"""
if self.model is None or self.current_model_type != model_type:
self.current_model_type = model_type
self.load_model(model_type=model_type)
self.image_predictor = SAM2ImagePredictor(sam_model=self.model)
self.image_predictor.set_image(image)
try:
masks, scores, logits = self.image_predictor.predict(
box=box,
point_coords=point_coords,
point_labels=point_labels,
multimask_output=params["multimask_output"],
)
except Exception as e:
logger.exception(f"Error while predicting image with prompt: {str(e)}")
raise RuntimeError(f"Failed to predict image with prompt") from e
return masks, scores, logits
def add_prediction_to_frame(self,
frame_idx: int,
obj_id: int,
inference_state: Optional[Dict] = None,
points: Optional[np.ndarray] = None,
labels: Optional[np.ndarray] = None,
box: Optional[np.ndarray] = None) -> Tuple[int, int, torch.Tensor]:
"""
Add prediction to the current video inference state. inference state must be initialized before calling this method.
Args:
frame_idx (int): The frame index of the video.
obj_id (int): The object id for the frame.
inference_state (Dict): The inference state for the video predictor.
points (np.ndarray): The point coordinates prompt data.
labels (np.ndarray): The point labels prompt data.
box (np.ndarray): The box prompt data.
Returns:
int: The frame index of the corresponding prediction.
int: The object id of the corresponding prediction.
torch.Tensor: The mask logits output in CxHxW format.
"""
if (self.video_predictor is None or
inference_state is None and self.video_inference_state is None):
logger.exception("Error while predicting frame from video, load video predictor first")
if inference_state is None:
inference_state = self.video_inference_state
try:
out_frame_idx, out_obj_ids, out_mask_logits = self.video_predictor.add_new_points_or_box(
inference_state=inference_state,
frame_idx=frame_idx,
obj_id=obj_id,
points=points,
labels=labels,
box=box
)
except Exception as e:
logger.exception(f"Error while predicting frame with prompt: {str(e)}")
raise RuntimeError(f"Failed to predicting frame with prompt") from e
return out_frame_idx, out_obj_ids, out_mask_logits
def propagate_in_video(self,
inference_state: Optional[Dict] = None,):
"""
Propagate in the video with the tracked predictions for each frame. Currently only supports
single frame tracking.
Args:
inference_state (Dict): The inference state for the video predictor. Use self.video_inference_state if None.
Returns:
Dict: The video segments with the image and mask data. It has frame index as each key and each key has
"image" and "mask" data. "image" key contains the path of the original image file and "mask" key contains
the np.ndarray mask output.
"""
if inference_state is None and self.video_inference_state is None:
logger.exception("Error while propagating in video, load video predictor first")
if inference_state is None:
inference_state = self.video_inference_state
video_segments = {}
try:
generator = self.video_predictor.propagate_in_video(
inference_state=inference_state,
start_frame_idx=0
)
images = get_frames_from_dir(vid_dir=TEMP_DIR, as_numpy=True)
with torch.autocast(device_type=self.device, dtype=torch.float16):
for out_frame_idx, out_obj_ids, out_mask_logits in generator:
mask = (out_mask_logits[0] > 0.0).cpu().numpy()
video_segments[out_frame_idx] = {
"image": images[out_frame_idx],
"mask": mask
}
except Exception as e:
logger.exception(f"Error while propagating in video: {str(e)}")
raise RuntimeError(f"Failed to propagate in video") from e
return video_segments
def add_filter_to_preview(self,
image_prompt_input_data: Dict,
filter_mode: str,
frame_idx: int,
pixel_size: Optional[int] = None,
color_hex: Optional[str] = None,
):
"""
Add filter to the preview image with the prompt data. Specially made for gradio app.
It adds prediction tracking to the self.video_inference_state and returns the filtered image.
Args:
image_prompt_input_data (Dict): The image prompt data.
filter_mode (str): The filter mode to apply. ["Solid Color", "Pixelize"]
frame_idx (int): The frame index of the video.
pixel_size (int): The pixel size for the pixelize filter.
color_hex (str): The color hex code for the solid color filter.
Returns:
np.ndarray: The filtered image output.
"""
if self.video_predictor is None or self.video_inference_state is None:
logger.exception("Error while adding filter to preview, load video predictor first")
raise f"Error while adding filter to preview"
if not image_prompt_input_data["points"]:
error_message = ("No prompt data provided. If this is an incorrect flag, "
"Please press the eraser button (on the image prompter) and add your prompts again.")
logger.error(error_message)
raise gr.Error(error_message, duration=20)
image, prompt = image_prompt_input_data["image"], image_prompt_input_data["points"]
image = np.array(image.convert("RGB"))
point_labels, point_coords, box = self.handle_prompt_data(prompt)
obj_id = frame_idx
self.video_predictor.reset_state(self.video_inference_state)
idx, scores, logits = self.add_prediction_to_frame(
frame_idx=frame_idx,
obj_id=obj_id,
inference_state=self.video_inference_state,
points=point_coords,
labels=point_labels,
box=box
)
masks = (logits[0] > 0.0).cpu().numpy()
generated_masks = self.format_to_auto_result(masks)
if filter_mode == COLOR_FILTER:
image = create_solid_color_mask_image(image, generated_masks, color_hex)
elif filter_mode == PIXELIZE_FILTER:
image = create_mask_pixelized_image(image, generated_masks, pixel_size)
return image
def create_filtered_video(self,
image_prompt_input_data: Dict,
filter_mode: str,
frame_idx: int,
pixel_size: Optional[int] = None,
color_hex: Optional[str] = None
):
"""
Create a whole filtered video with video_inference_state. Currently only one frame tracking is supported.
This needs FFmpeg to run. Returns two output path because of the gradio app.
Args:
image_prompt_input_data (Dict): The image prompt data.
filter_mode (str): The filter mode to apply. ["Solid Color", "Pixelize"]
frame_idx (int): The frame index of the video.
pixel_size (int): The pixel size for the pixelize filter.
color_hex (str): The color hex code for the solid color filter.
Returns:
str: The output video path.
str: The output video path.
"""
if self.video_predictor is None or self.video_inference_state is None:
logger.exception("Error while adding filter to preview, load video predictor first")
raise RuntimeError("Error while adding filter to preview")
if not image_prompt_input_data["points"]:
error_message = ("No prompt data provided. If this is an incorrect flag, "
"Please press the eraser button (on the image prompter) and add your prompts again.")
logger.error(error_message)
raise gr.Error(error_message, duration=20)
output_dir = os.path.join(self.output_dir, "filter")
clean_files_with_extension(TEMP_OUT_DIR, IMAGE_FILE_EXT)
self.video_predictor.reset_state(self.video_inference_state)
prompt_frame_image, prompt = image_prompt_input_data["image"], image_prompt_input_data["points"]
point_labels, point_coords, box = self.handle_prompt_data(prompt)
obj_id = frame_idx
idx, scores, logits = self.add_prediction_to_frame(
frame_idx=frame_idx,
obj_id=obj_id,
inference_state=self.video_inference_state,
points=point_coords,
labels=point_labels,
box=box
)
video_segments = self.propagate_in_video(inference_state=self.video_inference_state)
for frame_index, info in video_segments.items():
orig_image, masks = info["image"], info["mask"]
masks = self.format_to_auto_result(masks)
if filter_mode == COLOR_FILTER:
filtered_image = create_solid_color_mask_image(orig_image, masks, color_hex)
elif filter_mode == PIXELIZE_FILTER:
filtered_image = create_mask_pixelized_image(orig_image, masks, pixel_size)
save_image(image=filtered_image, output_dir=TEMP_OUT_DIR)
if len(video_segments) == 1:
out_image = save_image(image=filtered_image, output_dir=output_dir)
return None, out_image
out_video = create_video_from_frames(
frames_dir=TEMP_OUT_DIR,
frame_rate=self.video_info.frame_rate,
output_dir=output_dir,
)
return out_video, out_video
def divide_layer(self,
image_input: np.ndarray,
image_prompt_input_data: Dict,
input_mode: str,
model_type: str,
*params):
"""
Divide the layer with the given prompt data and save psd file.
Args:
image_input (np.ndarray): The input image.
image_prompt_input_data (Dict): The image prompt data.
input_mode (str): The input mode for the image prompt data. ["Automatic", "Box Prompt"]
model_type (str): The model type to load.
*params: The hyperparameters for the mask generator.
Returns:
List[np.ndarray]: List of images by predicted masks.
str: The output path of the psd file.
"""
timestamp = datetime.now().strftime("%m%d%H%M%S")
output_file_name = f"result-{timestamp}.psd"
output_path = os.path.join(self.output_dir, "psd", output_file_name)
# Pre-processed gradio components
hparams = {
'points_per_side': int(params[0]),
'points_per_batch': int(params[1]),
'pred_iou_thresh': float(params[2]),
'stability_score_thresh': float(params[3]),
'stability_score_offset': float(params[4]),
'crop_n_layers': int(params[5]),
'box_nms_thresh': float(params[6]),
'crop_n_points_downscale_factor': int(params[7]),
'min_mask_region_area': int(params[8]),
'use_m2m': bool(params[9]),
'multimask_output': bool(params[10])
}
if input_mode == AUTOMATIC_MODE:
image = image_input
generated_masks = self.generate_mask(
image=image,
model_type=model_type,
**hparams
)
elif input_mode == BOX_PROMPT_MODE:
image = image_prompt_input_data["image"]
image = np.array(image.convert("RGB"))
prompt = image_prompt_input_data["points"]
if len(prompt) == 0:
return [image], []
point_labels, point_coords, box = self.handle_prompt_data(prompt)
predicted_masks, scores, logits = self.predict_image(
image=image,
model_type=model_type,
box=box,
point_coords=point_coords,
point_labels=point_labels,
multimask_output=hparams["multimask_output"]
)
generated_masks = self.format_to_auto_result(predicted_masks)
save_psd_with_masks(image, generated_masks, output_path)
mask_combined_image = create_mask_combined_images(image, generated_masks)
gallery = create_mask_gallery(image, generated_masks)
gallery = [mask_combined_image] + gallery
return gallery, output_path
@staticmethod
def format_to_auto_result(
masks: np.ndarray
):
"""Format the masks to auto result format for convenience."""
place_holder = 0
if len(masks.shape) <= 3:
masks = np.expand_dims(masks, axis=0)
result = [{"segmentation": mask[0], "area": place_holder} for mask in masks]
return result
@staticmethod
def handle_prompt_data(
prompt_data: List
):
"""
Handle data from ImageInputPrompter.
Args:
prompt_data (Dict): A dictionary containing the 'prompt' key with a list of prompts.
Returns:
point_labels (List): list of points labels.
point_coords (List): list of points coords.
box (List): list of box datas.
"""
point_labels, point_coords, box = [], [], []
for x1, y1, left_click_indicator, x2, y2, point_indicator in prompt_data:
is_point = point_indicator == 4.0
if is_point:
point_labels.append(left_click_indicator)
point_coords.append([x1, y1])
else:
box.append([x1, y1, x2, y2])
point_labels = np.array(point_labels) if point_labels else None
point_coords = np.array(point_coords) if point_coords else None
box = np.array(box) if box else None
return point_labels, point_coords, box
|