diff --git a/scripts/api.py b/scripts/api.py index ccdf13d..0dcc66e 100644 --- a/scripts/api.py +++ b/scripts/api.py @@ -6,7 +6,7 @@ from pydantic import BaseModel, Field from modules.api.api import encode_pil_to_base64, decode_base64_to_image -from scripts.main import process_image, model_list +from scripts.utils import process_image, model_list class PBRRemRequest(BaseModel): diff --git a/scripts/main.py b/scripts/main.py index 06fd08d..67dcd71 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -2,7 +2,6 @@ import io import json import numpy as np -import cv2 import re import gradio as gr @@ -12,24 +11,11 @@ import modules.shared as shared from modules import images -from scripts.td_abg import get_foreground -from scripts.convertor import pil2cv from scripts.batch_dir import save_image_dir, modify_basename +from scripts.utils import process_image, model_list -try: - from modules.paths_internal import extensions_dir -except Exception: - from modules.extensions import extensions_dir - -from collections import OrderedDict from PIL import Image -model_cache = OrderedDict() -sam_model_dir = os.path.join( - extensions_dir, "PBRemTools/models/") -model_list = [f for f in os.listdir(sam_model_dir) if os.path.isfile( - os.path.join(sam_model_dir, f)) and f.split('.')[-1] != 'txt'] - def processing(single_image, batch_image, input_dir, output_dir, output_mask_dir, show_result, input_tab_state, *rem_args): # 0: single if (input_tab_state == 0): @@ -78,12 +64,6 @@ def processing(single_image, batch_image, input_dir, output_dir, output_mask_dir else: return None -def process_image(target_image, *rem_args): - image = pil2cv(target_image) - image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - mask, image = get_foreground(image, *rem_args) - return image, mask - class Script(scripts.Script): def __init__(self) -> None: super().__init__() diff --git a/scripts/utils.py b/scripts/utils.py new file mode 100644 index 0000000..299b7f3 --- /dev/null +++ b/scripts/utils.py @@ -0,0 +1,24 @@ +import os +import cv2 + +from scripts.td_abg import get_foreground +from scripts.convertor import pil2cv + +try: + from modules.paths_internal import extensions_dir +except Exception: + from modules.extensions import extensions_dir + +from collections import OrderedDict + +model_cache = OrderedDict() +sam_model_dir = os.path.join( + extensions_dir, "PBRemTools/models/") +model_list = [f for f in os.listdir(sam_model_dir) if os.path.isfile( + os.path.join(sam_model_dir, f)) and f.split('.')[-1] != 'txt'] + +def process_image(target_image, *rem_args): + image = pil2cv(target_image) + image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + mask, image = get_foreground(image, *rem_args) + return image, mask \ No newline at end of file