Skip to content

Commit

Permalink
fix: Unified use of RGB color values(#3)
Browse files Browse the repository at this point in the history
- Unified use of RGB color values
- Remove redundant unused background color replacement schemes
  • Loading branch information
aoguai committed Aug 7, 2024
1 parent eeadca8 commit c1749b2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 176 deletions.
2 changes: 1 addition & 1 deletion docs/README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Options:
-y, --yolov8-model-path PATH Path to YOLOv8 model
-u, --yunet-model-path PATH Path to YuNet model
-r, --rmbg-model-path PATH Path to RMBG model
-b, --bgr-list BGR_LIST BGR channel values list (comma-separated)
-b, --rgb-list RGB_LIST RGB channel values list (comma-separated)
for image composition
-s, --save-path PATH Path to save the output image
-p, --photo-type TEXT Type of photo
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Options:
-y, --yolov8-model-path PATH YOLOv8 模型路径
-u, --yunet-model-path PATH YuNet 模型路径
-r, --rmbg-model-path PATH RMBG 模型路径
-b, --bgr-list BGR_LIST BGR 通道值列表(英文逗号分隔),用于图像合成
-b, --rgb-list RGB_LIST RGB 通道值列表(英文逗号分隔),用于图像合成
-s, --save-path PATH 保存路径
-p, --photo-type TEXT 照片类型
--photo-sheet-size TEXT 选择照片表格的尺寸
Expand Down
28 changes: 7 additions & 21 deletions run_en.bat
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ if /i "!change_background!"=="no" (
) else (
set change_background=--change-background
REM Ask for background color
set /p "bgr_list=Enter BGR channel values (comma separated, default is 1.0,1.0,1.0):"
if "!bgr_list!"=="red" set bgr_list=1.0,0.0,0.0
if "!bgr_list!"=="blue" set bgr_list=0.05,0.36,0.647
if "!bgr_list!"=="white" set bgr_list=1.0,1.0,1.0
if "!bgr_list!"=="" set bgr_list=1.0,1.0,1.0
set /p "rgb_list=Enter RGB channel values (comma separated, default is 255,255,255):"
if "!rgb_list!"=="red" set rgb_list=255,0,0
if "!rgb_list!"=="blue" set rgb_list=12,92,165
if "!rgb_list!"=="white" set rgb_list=255,255,255
if "!rgb_list!"=="" set rgb_list=255,255,255
REM Ask whether to save the changed background image
set /p "save_background=Save images with changed background (yes/no, default is no):"
if /i "!save_background!"=="yes" (
Expand Down Expand Up @@ -97,14 +97,7 @@ if exist "%INPUT_PATH%\" (
set "OUTPUT_PATH=%%~dpnf_output%%~xf"

REM Execute Python script to process the image
"%PYTHON_EXE%" "%SCRIPT_PATH%" "%%~ff" ^
-b "%bgr_list%" ^
-s "%%~dpnf_output%%~xf" ^
-p "%photo_type%" ^
--photo-sheet-size "%photo-sheet-size%"^
%compress% %save_corrected% %change_background% %save_background% ^
-sr %sheet_rows% -sc %sheet_cols% ^
%rotate% %resize% %save_resized%
start "" cmd /k "%PYTHON_EXE% %SCRIPT_PATH% %%~ff -b %rgb_list% -s %%~dpnf_output%%~xf -p %photo_type% --photo-sheet-size %photo-sheet-size% %compress% %save_corrected% %change_background% %save_background% -sr %sheet_rows% -sc %sheet_cols% %rotate% %resize% %save_resized% & pause"
)
) else (
REM If it's a file, process the file directly
Expand All @@ -113,14 +106,7 @@ if exist "%INPUT_PATH%\" (
set OUTPUT_PATH=%INPUT_DIR%%~n1_output%~x1

REM Due to setlocal enabledelayedexpansion, use !variable_name! to reference variables
start "" "%PYTHON_EXE%" "%SCRIPT_PATH%" "!INPUT_PATH!" ^
-b "%bgr_list%" ^
-s "!OUTPUT_PATH!" ^
-p "%photo_type%" ^
--photo-sheet-size "%photo-sheet-size%"^
%compress% %save_corrected% %change_background% %save_background% ^
-sr %sheet_rows% -sc %sheet_cols% ^
%rotate% %resize% %save_resized%
start "" cmd /k "%PYTHON_EXE% %SCRIPT_PATH% !INPUT_PATH! -b %rgb_list% -s !OUTPUT_PATH! -p %photo_type% --photo-sheet-size %photo-sheet-size% %compress% %save_corrected% %change_background% %save_background% -sr %sheet_rows% -sc %sheet_cols% %rotate% %resize% %save_resized% & pause"
)

pause
31 changes: 9 additions & 22 deletions run_zh.bat
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set INPUT_PATH=%~1

echo LiYing
echo Github: https://github.com/aoguai/LiYing
echo LICENSE AGPL-3.0 license
echo LICENSE AGPL-3 license
echo ----------------------------------------
REM 询问用户输入参数
REM 询问用户是否调整图像尺寸
Expand Down Expand Up @@ -64,11 +64,11 @@ if /i "!change_background!"=="no" (
) else (
set change_background=--change-background
REM 询问背景颜色
set /p "bgr_list=请输入 BGR 通道值列表(逗号分隔,默认为 1.0,1.0,1.0):"
if "!bgr_list!"=="" set bgr_list=1.0,0.0,0.0
if "!bgr_list!"=="" set bgr_list=0.05,0.36,0.647
if "!bgr_list!"=="" set bgr_list=1.0,1.0,1.0
if "!bgr_list!"=="" set bgr_list=1.0,1.0,1.0
set /p "rgb_list=请输入 RGB 通道值列表(逗号分隔,默认为 255,255,255):"
if "!rgb_list!"=="" set rgb_list=255,0,0
if "!rgb_list!"=="" set rgb_list=12,92,165
if "!rgb_list!"=="" set rgb_list=255,255,255
if "!rgb_list!"=="" set rgb_list=255,255,255
REM 询问是否保存调整后的图像
set /p "save_background=是否保存替换背景后的图像(yes/no,默认为 no):"
if /i "!save_background!"=="yes" (
Expand Down Expand Up @@ -97,14 +97,8 @@ if exist "%INPUT_PATH%\" (
set "OUTPUT_PATH=%%~dpnf_output%%~xf"

REM 执行Python脚本处理图像
"%PYTHON_EXE%" "%SCRIPT_PATH%" "%%~ff" ^
-b "%bgr_list%" ^
-s "%%~dpnf_output%%~xf" ^
-p "%photo_type%" ^
--photo-sheet-size "%photo-sheet-size%"^
%compress% %save_corrected% %change_background% %save_background% ^
-sr %sheet_rows% -sc %sheet_cols% ^
%rotate% %resize% %save_resized%
start "" cmd /k "%PYTHON_EXE% %SCRIPT_PATH% %%~ff -b %rgb_list% -s %%~dpnf_output%%~xf -p %photo_type% --photo-sheet-size %photo-sheet-size% %compress% %save_corrected% %change_background% %save_background% -sr %sheet_rows% -sc %sheet_cols% %rotate% %resize% %save_resized% & pause"

)
) else (
REM 如果是文件,则直接处理该文件
Expand All @@ -113,14 +107,7 @@ if exist "%INPUT_PATH%\" (
set OUTPUT_PATH=%INPUT_DIR%%~n1_output%~x1

REM 由于使用了 setlocal enabledelayedexpansion 需要使用 !变量名! 来引用变量
start "" "%PYTHON_EXE%" "%SCRIPT_PATH%" "!INPUT_PATH!" ^
-b "%bgr_list%" ^
-s "!OUTPUT_PATH!" ^
-p "%photo_type%" ^
--photo-sheet-size "%photo-sheet-size%"^
%compress% %save_corrected% %change_background% %save_background% ^
-sr %sheet_rows% -sc %sheet_cols% ^
%rotate% %resize% %save_resized%
start "" cmd /k "%PYTHON_EXE% %SCRIPT_PATH% !INPUT_PATH! -b %rgb_list% -s !OUTPUT_PATH! -p %photo_type% --photo-sheet-size %photo-sheet-size% %compress% %save_corrected% %change_background% %save_background% -sr %sheet_rows% -sc %sheet_cols% %rotate% %resize% %save_resized% & pause"
)

pause
Expand Down
18 changes: 9 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
from tool.PhotoRequirements import PhotoRequirements


class BGRListType(click.ParamType):
name = 'bgr_list'
class RGBListType(click.ParamType):
name = 'rgb_list'

def convert(self, value, param, ctx):
if value:
try:
return tuple(float(x) for x in value.split(','))
return tuple(int(x) for x in value.split(','))
except ValueError:
self.fail(f'{value} is not a valid BGR list format. Expected format: FLOAT,FLOAT,FLOAT.')
return 1.0, 1.0, 1.0 # Default value
self.fail(f'{value} is not a valid RGB list format. Expected format: INTEGER,INTEGER,INTEGER.')
return 0, 0, 0 # Default value


def get_language():
Expand Down Expand Up @@ -81,8 +81,8 @@ def echo_message(key, **kwargs):
@click.option('-r', '--rmbg-model-path', type=click.Path(),
default=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'model/RMBG-1.4-model.onnx'),
help='Path to RMBG model' if get_language() == 'en' else 'RMBG 模型路径')
@click.option('-b', '--bgr-list', type=BGRListType(), default='1.0,1.0,1.0',
help='BGR channel values list (comma-separated) for image composition' if get_language() == 'en' else 'BGR 通道值列表(英文逗号分隔),用于图像合成')
@click.option('-b', '--rgb-list', type=RGBListType(), default='0,0,0',
help='RGB channel values list (comma-separated) for image composition' if get_language() == 'en' else 'RGB 通道值列表(英文逗号分隔),用于图像合成')
@click.option('-s', '--save-path', type=click.Path(), default='output.jpg',
help='Path to save the output image' if get_language() == 'en' else '保存路径')
@click.option('-p', '--photo-type', type=str, default='one_inch_photo' if get_language() == 'en' else '一寸照片',
Expand All @@ -107,11 +107,11 @@ def echo_message(key, **kwargs):
help='Whether to resize the image' if get_language() == 'en' else '是否调整图像尺寸')
@click.option('-srz', '--save-resized/--no-save-resized', default=False,
help='Whether to save the resized image' if get_language() == 'en' else '是否保存调整尺寸后的图像')
def cli(img_path, yolov8_model_path, yunet_model_path, rmbg_model_path, bgr_list, save_path, photo_type,
def cli(img_path, yolov8_model_path, yunet_model_path, rmbg_model_path, rgb_list, save_path, photo_type,
photo_sheet_size, compress, save_corrected,
change_background, save_background, sheet_rows, sheet_cols, rotate, resize, save_resized):
# Create an instance of the image processor
processor = ImageProcessor(img_path, yolov8_model_path, yunet_model_path, rmbg_model_path, bgr_list, y_b=compress)
processor = ImageProcessor(img_path, yolov8_model_path, yunet_model_path, rmbg_model_path, rgb_list, y_b=compress)
photo_requirements_detector = PhotoRequirements()
# Crop and correct image
processor.crop_and_correct_image()
Expand Down
93 changes: 0 additions & 93 deletions src/tool/ImageInference.py

This file was deleted.

19 changes: 9 additions & 10 deletions src/tool/ImageProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, img_path,
yolov8_model_path=get_model_file('yolov8n-pose.onnx'),
yunet_model_path=get_model_file('face_detection_yunet_2023mar.onnx'),
RMBG_model_path=get_model_file('RMBG-1.4-model.onnx'),
bgr_list=None,
rgb_list=None,
y_b=False):
"""
Initialize ImageProcessor instance
Expand All @@ -30,7 +30,7 @@ def __init__(self, img_path,
:param yolov8_model_path: Path to the YOLOv8 model
:param yunet_model_path: Path to the YuNet model
:param RMBG_model_path: Path to the RMBG model
:param bgr_list: List of BGR channel values for image composition
:param rgb_list: List of rgb channel values for image composition
"""
if not os.path.exists(img_path):
raise FileNotFoundError(f"Image path does not exist: {img_path}")
Expand All @@ -42,9 +42,8 @@ def __init__(self, img_path,
raise FileNotFoundError(f"RMBG model path does not exist: {RMBG_model_path}")

self.photo = PhotoEntity(img_path, yolov8_model_path, yunet_model_path, y_b)
# self.inference = ImageInference(RVM_model_path, bgr_list if bgr_list is not None else [1.0, 1.0, 1.0])
self.segmentation = ImageSegmentation(model_path=RMBG_model_path, model_input_size=[1024, 1024],
bgr_list=bgr_list if bgr_list is not None else [1.0, 1.0, 1.0])
rgb_list=rgb_list if rgb_list is not None else [255, 255, 255])
self.photo_requirements_detector = PhotoRequirements()

@staticmethod
Expand Down Expand Up @@ -173,18 +172,18 @@ def crop_and_correct_image(self) -> PhotoEntity:
else:
raise ValueError('No single person detected.')

def change_background(self, bgr_list=None) -> PhotoEntity:
def change_background(self, rgb_list=None) -> PhotoEntity:
"""
Replace the background of the human region in the image
:param bgr_list: New list of BGR channel values
:param rgb_list: New list of RGB channel values
:return: Updated PhotoEntity instance
:rtype: PhotoEntity
"""
if bgr_list is not None:
if not (isinstance(bgr_list, list) and len(bgr_list) == 3):
raise ValueError("The BGR value format is incorrect")
self.segmentation.bgr_list = bgr_list
if rgb_list is not None:
if not (isinstance(rgb_list, list) and len(rgb_list) == 3):
raise ValueError("The RGB value format is incorrect")
self.segmentation.rgb_list = rgb_list

self.photo.image = self.segmentation.infer(self.photo.image)
return self.photo
Expand Down
Loading

0 comments on commit c1749b2

Please sign in to comment.