Skip to content

Commit

Permalink
commit TransparentBackgroundUltra node, add install_requirements.bat …
Browse files Browse the repository at this point in the history
…to easy install dependency package
  • Loading branch information
chflame163 committed Jul 20, 2024
1 parent 15b5814 commit ddd0472
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 36 deletions.
46 changes: 42 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Some JSON workflow files in the ```workflow``` directory, That's examples of how


## How to install
(Taking ComfyUI official portable package and Aki ComfyUI package as examples, please modify the dependency environment directory for other ComfyUI environments)

### Install plugin
* Recommended use ComfyUI Manager for installation.

* Or open the cmd window in the plugin directory of ComfyUI, like ```ComfyUI\custom_nodes```,type
Expand All @@ -29,13 +31,26 @@ git clone https://github.com/chflame163/ComfyUI_LayerStyle.git
```
* Or download the zip file and extracted, copy the resulting folder to ```ComfyUI\custom_ Nodes```

* Install dependency packages, open the cmd window in the ComfyUI_LayerStyle plugin directory like
```ComfyUI\custom_ Nodes\ComfyUI_LayerStyle``` and enter the following command:
### Install dependency packages
* for ComfyUI official portable package, double-click the ```install_requirements.bat``` in the plugin directory, for Aki ComfyUI package double-click on the ```install_requirements_aki.bat``` in the plugin directory, and wait for the installation to complete.

* Or install dependency packages, open the cmd window in the ComfyUI_LayerStyle plugin directory like
```ComfyUI\custom_ Nodes\ComfyUI_LayerStyle``` and enter the following command,

  for ComfyUI official portable package, type:

```
..\..\..\python_embeded\python.exe -m pip install -r requirements.txt
..\..\..\python_embeded\python.exe -s -m pip install .\whl\docopt-0.6.2-py2.py3-none-any.whl
..\..\..\python_embeded\python.exe -s -m pip install -r requirements.txt
.\repair_dependency.bat
```
  for Aki ComfyUI package, type:

```
..\..\python\python.exe -s -m pip install .\whl\docopt-0.6.2-py2.py3-none-any.whl
..\..\python\python.exe -s -m pip install -r requirements.txt
.\repair_dependency.bat
```
* Restart ComfyUI.

## Common Issues
Expand Down Expand Up @@ -65,7 +80,7 @@ This error is caused by the low version of ```transformers``` package.
### insightface Loading very slow
This error is caused by the low version of ```protobuf``` package.

## For the issues with the above three dependency packages, please double click ```repair_dependency.bat``` (for Official ComfyUI Protable) or ```repair_dependency_aki.bat``` (for ComfyUI-aki-v1.x) in the plugin folder to automatically fix them.
#### For the issues with the above three dependency packages, please double click ```repair_dependency.bat``` (for Official ComfyUI Protable) or ```repair_dependency_aki.bat``` (for ComfyUI-aki-v1.x) in the plugin folder to automatically fix them.

### ValueError: Trimap did not contain foreground values (xxxx...)
This error is caused by the mask area being too large or too small when using the ```PyMatting``` method to handle the mask edges.
Expand All @@ -80,6 +95,7 @@ When this error has occurred, please check the network environment.
## Update
<font size="4">**If the dependency package error after updating, please reinstall the relevant dependency packages. </font><br />

* Commit [TransparentBackgroundUltra](#TransparentBackgroundUltra) node, it remove background based on transparent-background model.
* Change the VitMatte model of the [Ultra](#Ultra) node to a local call. Please download [all files of vitmatte model](https://huggingface.co/hustvl/vitmatte-small-composition-1k/tree/main) to the ```ComfyUI/models/vitmatte``` folder.
* [GetColorToneV2](#GetColorToneV2) node add the ```mask``` method to the color selection option, which can accurately obtain the main color and average color within the mask.
* [ImageScaleByAspectRatioV2](#ImageScaleByAspectRatioV2) node add the "background_color" option.
Expand Down Expand Up @@ -1482,6 +1498,28 @@ Node options:
* device: Set whether the VitMatte to use cuda.
* max_megapixels: Set the maximum size for VitMate operations.

### <a id="table1">TransparentBackgroundUltra</a>
Using the transparent-background model to remove background has better recognition ability and speed, while also having ultra-high edge details.

*Download [ckpt_base.pth](https://github.com/plemeri/transparent-background/releases/download/1.2.12/ckpt_base.pth),
[ckpt_fast.pth](https://github.com/plemeri/transparent-background/releases/download/1.2.12/ckpt_fast.pth) and
[ckpt_base_nightly.pth](https://github.com/plemeri/transparent-background/releases/download/1.2.12/ckpt_base_nightly.pth)
3 files to ```ComfyUI/models/transparent-background``` folder.

![image](image/transparent_background_ultra_example.jpg)

Node Options:
![image](image/transparent_background_ultra_node.jpg)
* model: Select the model.
* detail_method: Edge processing methods. provides VITMatte, VITMatte(local), PyMatting, GuidedFilter. If the model has been downloaded after the first use of VITMatte, you can use VITMatte (local) afterwards.
* detail_erode: Mask the erosion range inward from the edge. the larger the value, the larger the range of inward repair.
* detail_dilate: The edge of the mask expands outward. the larger the value, the wider the range of outward repair.
* black_point: Edge black sampling threshold.
* white_point: Edge white sampling threshold.
* process_detail: Set to false here will skip edge processing to save runtime.
* device: Set whether the VitMatte to use cuda.
* max_megapixels: Set the maximum size for VitMate operations.

### <a id="table1">PersonMaskUltra</a>
Generate masks for portrait's face, hair, body skin, clothing, or accessories. Compared to the previous A Person Mask Generator node, this node has ultra-high edge details.
The model code for this node comes from [a-person-mask-generator](https://github.com/djbielejeski/a-person-mask-generator), edge processing code from [ComfyUI-Image-Filters](https://github.com/spacepxl/ComfyUI-Image-Filters),thanks to the original author.
Expand Down
50 changes: 45 additions & 5 deletions README_CN.MD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@


## 安装方法
(以ComfyUI官方便携包和秋叶整合包为例,其他ComfyUI环境请修改依赖环境目录)

### 安装插件
* 推荐使用 ComfyUI Manager 安装。
* 或者在CompyUI插件目录(例如“CompyUI\custom_nodes\”)中打开cmd窗口,键入
```
Expand All @@ -24,13 +26,27 @@ git clone https://github.com/chflame163/ComfyUI_LayerStyle.git
* 或者下载解压zip文件,将得到的文件夹复制到 ```ComfyUI\custom_nodes\```

* 安装依赖包,在资源管理器```ComfyUI\custom_nodes\ComfyUI_LayerStyle``` 插件目录位置打开cmd窗口,输入以下命令:
### 安装依赖包

* 官方便携包请双击运行插件目录下的```install_requirements.bat```,秋叶整合包请双击运行插件目录下的```install_requirements_aki.bat```,然后等待安装完成。

* 或者在资源管理器```ComfyUI\custom_nodes\ComfyUI_LayerStyle``` 插件目录位置打开cmd窗口,

&emsp;&emsp;官方便携包输入以下命令:

```
..\..\..\python_embeded\python.exe -m pip install -r requirements.txt
..\..\..\python_embeded\python.exe -s -m pip install .\whl\docopt-0.6.2-py2.py3-none-any.whl
..\..\..\python_embeded\python.exe -s -m pip install -r requirements.txt
.\repair_dependency.bat
```
&emsp;&emsp;秋叶整合包输入以下命令:


重新打开ComfyUI。
```
..\..\python\python.exe -s -m pip install .\whl\docopt-0.6.2-py2.py3-none-any.whl
..\..\python\python.exe -s -m pip install -r requirements.txt
.\repair_dependency.bat
```
* 重新打开ComfyUI。

## 常见问题
如果节点不能正常加载,或者使用中出现错误,请在ComfyUI终端窗口查看报错信息。以下是常见的错误及解决方法。
Expand All @@ -57,7 +73,7 @@ git clone https://github.com/chflame163/ComfyUI_LayerStyle.git
### insightface 加载缓慢
这是由于```protobuf``` 版本过低造成的。

## 以上三个依赖包的问题,请双击运行插件目录下的```repair_dependency.bat```(官方便携包)或者```repair_dependency_aki.bat```(秋叶整合包)自动修复。
#### 以上三个依赖包的问题,请双击运行插件目录下的```repair_dependency.bat```(官方便携包)或者```repair_dependency_aki.bat```(秋叶整合包)自动修复。

### ValueError: Trimap did not contain foreground values (xxxx...)
这个错误是由于使用PyMatting方法处理遮罩边缘时,遮罩面积过大或者过小引起的。
Expand All @@ -80,6 +96,7 @@ git clone https://github.com/chflame163/ComfyUI_LayerStyle.git
## 更新说明
<font size="4">**如果本插件更新后出现依赖包错误,请重新安装相关依赖包。

* 添加[TransparentBackgroundUltra](#TransparentBackgroundUltra) 节点,基于transparent-background模型,用于去除背景。
* [Ultra](#Ultra) 节点的VitMatte模型改为本地调用,请下载[所有的vitmatte模型文件](https://huggingface.co/hustvl/vitmatte-small-composition-1k/tree/main)```ComfyUI/models/vitmatte```文件夹。
* [GetColorToneV2](#GetColorToneV2) 节点的取色选项增加```mask```方法,可精确获取遮罩内的主色和平均色。
* [ImageScaleByAspectRatioV2](#ImageScaleByAspectRatioV2) 节点增加background_color选项。
Expand Down Expand Up @@ -1462,6 +1479,29 @@ RemBgUltra的V2升级版,增加了VITMatte边缘处理方法。
* device: 设置是否使用cuda。
* max_megapixels: 设置vitmatte运算的最大尺寸。

### <a id="table1">TransparentBackgroundUltra</a>
使用transparent-background模型去除背景,有更好的识别能力和识别速度,同时具有超高的边缘细节。
本节点模型部分的代码来自[github.com/plemeri/transparent-background](https://github.com/plemeri/transparent-background),感谢原作者。

*下载 [ckpt_base.pth](https://github.com/plemeri/transparent-background/releases/download/1.2.12/ckpt_base.pth),
[ckpt_fast.pth](https://github.com/plemeri/transparent-background/releases/download/1.2.12/ckpt_fast.pth)
[ckpt_base_nightly.pth](https://github.com/plemeri/transparent-background/releases/download/1.2.12/ckpt_base_nightly.pth)
3 个文件至```ComfyUI/models/transparent-background```文件夹。

![image](image/transparent_background_ultra_example.jpg)

节点选项说明:
![image](image/transparent_background_ultra_node.jpg)
* model: 选择模型。
* detail_method: 边缘处理方法。提供了VITMatte, VITMatte(local), PyMatting, GuidedFilter。如果首次使用VITMatte后模型已经下载,之后可以使用VITMatte(local)。
* detail_erode: 遮罩边缘向内侵蚀范围。数值越大,向内修复的范围越大。
* detail_dilate: 遮罩边缘向外扩张范围。数值越大,向外修复的范围越大。
* black_point: 边缘黑色采样阈值。
* white_point: 边缘黑色采样阈值。
* process_detail: 此处设为False将跳过边缘处理以节省运行时间。
* device: 设置是否使用cuda。
* max_megapixels: 设置vitmatte运算的最大尺寸。

### <a id="table1">PersonMaskUltra</a>
为人物生成脸、头发、身体皮肤、衣服或配饰的遮罩。与之前的A Person Mask Generator节点相比,这个节点具有超高的边缘细节。
本节点的模型代码来自[a-person-mask-generator](https://github.com/djbielejeski/a-person-mask-generator),边缘处理代码来自spacepxl的[ComfyUI-Image-Filters](https://github.com/spacepxl/ComfyUI-Image-Filters),感谢原作者。
Expand Down
Binary file added image/transparent_background_ultra_example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/transparent_background_ultra_node.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions install_requirements.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@echo off

set "python_exec=..\..\..\python_embeded\python.exe"
set "repair_dependency_txt=%~dp0\repair_dependency_list.txt"

echo Installing with ComfyUI Portable
echo .
echo Install whl...
%python_exec% -s -m pip install ./whl/docopt-0.6.2-py2.py3-none-any.whl

echo .
echo Install requirement.txt...
%python_exec% -s -m pip install -r ./requirements.txt

echo .
echo Fixing Dependency Package...
%python_exec% -s -m pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless opencv-contrib-python-headless
for /f "delims=" %%i in (%repair_dependency_txt%) do (
%python_exec% -s -m pip install "%%i"
)

echo .
echo Install Finish!
pause

25 changes: 25 additions & 0 deletions install_requirements_aki.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@echo off

set "python_exec=..\..\python\python.exe"
set "repair_dependency_txt=%~dp0\repair_dependency_list.txt"

echo Installing with ComfyUI Portable
echo .
echo Install whl...
%python_exec% -s -m pip install ./whl/docopt-0.6.2-py2.py3-none-any.whl

echo .
echo Install requirement.txt...
%python_exec% -s -m pip install -r ./requirements.txt

echo .
echo Fixing Dependency Package...
%python_exec% -s -m pip uninstall -y opencv-python opencv-contrib-python opencv-python-headless opencv-contrib-python-headless
for /f "delims=" %%i in (%repair_dependency_txt%) do (
%python_exec% -s -m pip install "%%i"
)

echo .
echo Install Finish!
pause

25 changes: 1 addition & 24 deletions py/imagefunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,29 +1097,6 @@ def gamma_trans(image:Image, gamma:float) -> Image:
_corrected = cv2.LUT(cv2_image,gamma_table)
return cv22pil(_corrected)

# def apply_lut(image:Image, lut_file:str, log:bool=False) -> Image:
# from colour.io.luts.iridas_cube import read_LUT_IridasCube, LUT3D, LUT3x1D
# lut: Union[LUT3x1D, LUT3D] = read_LUT_IridasCube(lut_file)
# lut.name = os.path.splitext(os.path.basename(lut_file))[0] # use base filename instead of internal LUT name
#
# im_array = np.asarray(image.convert('RGB'), dtype=np.float32) / 255
# is_non_default_domain = not np.array_equal(lut.domain, np.array([[0., 0., 0.], [1., 1., 1.]]))
# dom_scale = None
# if is_non_default_domain:
# dom_scale = lut.domain[1] - lut.domain[0]
# im_array = im_array * dom_scale + lut.domain[0]
# if log:
# im_array = im_array ** (1 / 2.2)
# im_array = lut.apply(im_array)
# if log:
# im_array = im_array ** (2.2)
# if is_non_default_domain:
# im_array = (im_array - lut.domain[0]) / dom_scale
# im_array = im_array * 255
# ret_image = Image.fromarray(np.uint8(im_array))
#
# return ret_image

def apply_lut(image:Image, lut_file:str, colorspace:str, strength:int, clip_values:bool=True) -> Image:
"""
Apply a LUT to an image.
Expand Down Expand Up @@ -2071,7 +2048,7 @@ def chat_response(self, message, history, image_path):
**model_inputs,
max_new_tokens=512,
do_sample=True,
temperature=0.7,
temperature=0.3,
repetition_penalty=1.2,
stopping_criteria=StoppingCriteriaList([stop])
)
Expand Down
85 changes: 85 additions & 0 deletions py/transparent_background_ultra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from transparent_background import Remover
from .imagefunc import *

NODE_NAME = 'TransparentBackgroundUltra'
model_file_list = glob.glob(os.path.join(folder_paths.models_dir, "transparent-background") + '/*.pth')
model_dict = {}
for i in range(len(model_file_list)):
_, __filename = os.path.split(model_file_list[i])
model_dict[__filename] = model_file_list[i]
model_list = list(model_dict.keys())
mode_dict = {"ckpt_base.pth": "base", "ckpt_base_nightly.pth": "base-nightly", "ckpt_fast.pth": "fast"}
class TransparentBackgroundUltra:

@classmethod
def INPUT_TYPES(cls):
method_list = ['VITMatte', 'VITMatte(local)', 'PyMatting', 'GuidedFilter', ]
device_list = ['cuda','cpu']
return {
"required": {
"image": ("IMAGE",),
"model": (model_list,),
"detail_method": (method_list,),
"detail_erode": ("INT", {"default": 6, "min": 1, "max": 255, "step": 1}),
"detail_dilate": ("INT", {"default": 6, "min": 1, "max": 255, "step": 1}),
"black_point": ("FLOAT", {"default": 0.01, "min": 0.01, "max": 0.98, "step": 0.01, "display": "slider"}),
"white_point": ("FLOAT", {"default": 0.99, "min": 0.02, "max": 0.99, "step": 0.01, "display": "slider"}),
"process_detail": ("BOOLEAN", {"default": True}),
"device": (device_list,),
"max_megapixels": ("FLOAT", {"default": 2.0, "min": 1, "max": 999, "step": 0.1}),
},
"optional": {
}
}

RETURN_TYPES = ("IMAGE", "MASK", )
RETURN_NAMES = ("image", "mask", )
FUNCTION = "transparent_background_ultra"
CATEGORY = '😺dzNodes/LayerMask'

def transparent_background_ultra(self, image, model, detail_method, detail_erode, detail_dilate,
black_point, white_point, process_detail, device, max_megapixels):
ret_images = []
ret_masks = []

if detail_method == 'VITMatte(local)':
local_files_only = True
else:
local_files_only = False

remover = Remover(mode=mode_dict[model], jit=False, device=device, ckpt=model_dict[model])
for i in image:
i = torch.unsqueeze(i, 0)
orig_image = tensor2pil(i).convert('RGB')
ret_image = remover.process(orig_image, type='rgba')
_mask = ret_image.split()[3]
_mask = adjust_levels(_mask, 64, 192)

if process_detail:
detail_range = detail_erode + detail_dilate
_mask = pil2tensor(_mask)
if detail_method == 'GuidedFilter':
_mask = guided_filter_alpha(i, _mask, detail_range // 6 + 1)
_mask = tensor2pil(histogram_remap(_mask, black_point, white_point))
elif detail_method == 'PyMatting':
_mask = tensor2pil(mask_edge_detail(i, _mask, detail_range // 8 + 1, black_point, white_point))
else:
_trimap = generate_VITMatte_trimap(_mask, detail_erode, detail_dilate)
_mask = generate_VITMatte(orig_image, _trimap, local_files_only=local_files_only, device=device, max_megapixels=max_megapixels)
_mask = tensor2pil(histogram_remap(pil2tensor(_mask), black_point, white_point))
ret_image = RGB2RGBA(orig_image, _mask.convert('L'))

ret_images.append(pil2tensor(ret_image))
ret_masks.append(image2mask(_mask))

log(f"{NODE_NAME} Processed {len(ret_images)} image(s).", message_type='finish')

return (torch.cat(ret_images, dim=0), torch.cat(ret_masks, dim=0),)

NODE_CLASS_MAPPINGS = {
"LayerMask: TransparentBackgroundUltra": TransparentBackgroundUltra,
}

NODE_DISPLAY_NAME_MAPPINGS = {
"LayerMask: TransparentBackgroundUltra": "LayerMask: Transparent Background Ultra",
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui_layerstyle"
description = "A set of nodes for ComfyUI it generate image like Adobe Photoshop's Layer Style. the Drop Shadow is first completed node, and follow-up work is in progress."
version = "1.0.16"
version = "1.0.17"
license = "MIT"
dependencies = ["numpy", "pillow", "torch", "matplotlib", "Scipy", "scikit_image", "opencv-contrib-python", "pymatting", "segment_anything", "timm", "addict", "yapf", "colour-science", "wget", "mediapipe", "loguru", "typer_config", "fastapi", "rich", "google-generativeai", "diffusers", "omegaconf", "tqdm", "transformers", "kornia", "image-reward", "ultralytics", "blend_modes", "blind-watermark", "qrcode", "pyzbar", "psd-tools"]

Expand Down
5 changes: 3 additions & 2 deletions repair_dependency_list.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
opencv-contrib-python>=4.9.0.80
huggingface_hub==0.23.3
transformers>=4.38.1
protobuf>=4.25.3
protobuf>=4.25.3
opencv-contrib-python>=4.9.0.80
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ blend_modes
blind-watermark
qrcode
pyzbar
transparent-background
psd-tools
Binary file added whl/docopt-0.6.2-py2.py3-none-any.whl
Binary file not shown.

0 comments on commit ddd0472

Please sign in to comment.