Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing-su committed Mar 21, 2024
2 parents cfd0e95 + 3d6a5a5 commit 0a07045
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Changelog

## 2024-03-21

- v24.3.2
- UI를 거치지 않은 입력에 대해, image_mask를 입력했을 때 opencv 에러가 발생하는 것 수정
- img2img inpaint에서 skip img2img 옵션을 활성화할 경우, adetailer를 비활성화함
- 마스크 크기에 대해 해결하기 힘든 문제가 있음

## 2024-03-16

- v24.3.1
- YOLO World v2, YOLO9 지원가능한 버전으로 ultralytics 업데이트
- inpaint full res인 경우 인페인트 모드에서 동작하게 변경
- inpaint full res가 아닌 경우, 사용자가 입력한 마스크와 교차점이 있는 마스크만 선택하여 사용함
Expand Down
2 changes: 1 addition & 1 deletion adetailer/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "24.3.1"
__version__ = "24.3.2"
2 changes: 1 addition & 1 deletion adetailer/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def offset(img: Image.Image, x: int = 0, y: int = 0) -> Image.Image:

def is_all_black(img: Image.Image | np.ndarray) -> bool:
if isinstance(img, Image.Image):
img = np.array(img)
img = np.array(ensure_pil_image(img, "L"))
return cv2.countNonZero(img) == 0


Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ readme = "README.md"
license = { text = "AGPL-3.0" }
dependencies = [
"ultralytics>=8.1",
"mediapipe>=10",
"mediapipe>=0.10",
"pydantic<3",
"rich>=13",
"huggingface_hub",
Expand All @@ -18,6 +18,10 @@ keywords = [
"adetailer",
"ultralytics",
]
classifiers = [
"License :: OSI Approved :: GNU Affero General Public License v3",
"Topic :: Scientific/Engineering :: Image Recognition",
]
dynamic = ["version"]

[project.urls]
Expand Down
90 changes: 58 additions & 32 deletions scripts/!adetailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from modules.processing import (
Processed,
StableDiffusionProcessingImg2Img,
create_binary_mask,
create_infotext,
process_images,
)
Expand Down Expand Up @@ -200,7 +201,7 @@ def is_ad_enabled(self, *args_) -> bool:
not_none = any(arg.get("ad_model", "None") != "None" for arg in arg_list)
return ad_enabled and not_none

def check_skip_img2img(self, p, *args_) -> None:
def set_skip_img2img(self, p, *args_) -> None:
if (
hasattr(p, "_ad_skip_img2img")
or not hasattr(p, "init_images")
Expand All @@ -210,20 +211,29 @@ def check_skip_img2img(self, p, *args_) -> None:

if len(args_) >= 2 and isinstance(args_[1], bool):
p._ad_skip_img2img = args_[1]
if args_[1]:
p._ad_orig = SkipImg2ImgOrig(
steps=p.steps,
sampler_name=p.sampler_name,
width=p.width,
height=p.height,
)
p.steps = 1
p.sampler_name = "Euler"
p.width = 128
p.height = 128
else:
p._ad_skip_img2img = False

if not p._ad_skip_img2img:
return

if self.is_img2img_inpaint(p):
p._ad_disabled = True
msg = "[-] ADetailer: img2img inpainting with skip img2img is not supported. (because it's buggy)"
print(msg)
return

p._ad_orig = SkipImg2ImgOrig(
steps=p.steps,
sampler_name=p.sampler_name,
width=p.width,
height=p.height,
)
p.steps = 1
p.sampler_name = "Euler"
p.width = 128
p.height = 128

@staticmethod
def get_i(p) -> int:
it = p.iteration
Expand Down Expand Up @@ -577,10 +587,10 @@ def pred_preprocessing(self, p, pred: PredictOutput, args: ADetailerArgs):
y_offset=args.ad_y_offset,
merge_invert=args.ad_mask_merge_invert,
)

if self.is_img2img_inpaint(p) and not self.is_inpaint_only_masked(p):
invert = p.inpainting_mask_invert
image_mask = ensure_pil_image(p.image_mask, mode="L")
masks = self.inpaint_mask_filter(image_mask, masks, invert)
image_mask = self.get_image_mask(p)
masks = self.inpaint_mask_filter(image_mask, masks)
return masks

@staticmethod
Expand Down Expand Up @@ -641,40 +651,56 @@ def is_inpaint_only_masked(p) -> bool:

@staticmethod
def inpaint_mask_filter(
img2img_mask: Image.Image, ad_mask: list[Image.Image], invert: int = 0
img2img_mask: Image.Image, ad_mask: list[Image.Image]
) -> list[Image.Image]:
if invert:
img2img_mask = ImageChops.invert(img2img_mask)
return [mask for mask in ad_mask if has_intersection(img2img_mask, mask)]

@staticmethod
def get_image_mask(p) -> Image.Image:
mask = p.image_mask
if p.inpainting_mask_invert:
mask = ImageChops.invert(mask)
mask = create_binary_mask(mask)

if getattr(p, "_ad_skip_img2img", False):
width, height = p.init_images[0].size
else:
width, height = p.width, p.height
return images.resize_image(p.resize_mode, mask, width, height)

@rich_traceback
def process(self, p, *args_):
if getattr(p, "_ad_disabled", False):
return

if self.is_img2img_inpaint(p) and is_all_black(p.image_mask):
if self.is_img2img_inpaint(p) and is_all_black(self.get_image_mask(p)):
p._ad_disabled = True
msg = (
"[-] ADetailer: img2img inpainting with no mask -- adetailer disabled."
)
print(msg)
return

if self.is_ad_enabled(*args_):
arg_list = self.get_args(p, *args_)
self.check_skip_img2img(p, *args_)
if not self.is_ad_enabled(*args_):
p._ad_disabled = True
return

if hasattr(p, "_ad_xyz_prompt_sr"):
replaced_positive_prompt, replaced_negative_prompt = self.get_prompt(
p, arg_list[0]
)
arg_list[0].ad_prompt = replaced_positive_prompt[0]
arg_list[0].ad_negative_prompt = replaced_negative_prompt[0]
self.set_skip_img2img(p, *args_)
if getattr(p, "_ad_disabled", False):
# case when img2img inpainting with skip img2img
return

extra_params = self.extra_params(arg_list)
p.extra_generation_params.update(extra_params)
else:
p._ad_disabled = True
arg_list = self.get_args(p, *args_)

if hasattr(p, "_ad_xyz_prompt_sr"):
replaced_positive_prompt, replaced_negative_prompt = self.get_prompt(
p, arg_list[0]
)
arg_list[0].ad_prompt = replaced_positive_prompt[0]
arg_list[0].ad_negative_prompt = replaced_negative_prompt[0]

extra_params = self.extra_params(arg_list)
p.extra_generation_params.update(extra_params)

def _postprocess_image_inner(
self, p, pp, args: ADetailerArgs, *, n: int = 0
Expand Down
20 changes: 20 additions & 0 deletions tests/test_mask.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import cv2
import numpy as np
import pytest
from PIL import Image, ImageDraw

from adetailer.mask import dilate_erode, has_intersection, is_all_black, offset
Expand Down Expand Up @@ -78,6 +80,24 @@ def test_is_all_black_2():
assert not is_all_black(img)


def test_is_all_black_rgb_image_pil():
img = Image.new("RGB", (10, 10), color="red")
assert not is_all_black(img)

img = Image.new("RGBA", (10, 10), color="red")
assert not is_all_black(img)


def test_is_all_black_rgb_image_numpy():
img = np.full((10, 10, 4), 127, dtype=np.uint8)
with pytest.raises(cv2.error):
is_all_black(img)

img = np.full((4, 10, 10), 0.5, dtype=np.float32)
with pytest.raises(cv2.error):
is_all_black(img)


def test_has_intersection_1():
arr1 = np.array(
[
Expand Down
2 changes: 2 additions & 0 deletions tests/test_ultralytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"face_yolov8n.pt",
"face_yolov8n_v2.pt",
"face_yolov8s.pt",
"face_yolov9c.pt",
"hand_yolov8n.pt",
"hand_yolov8s.pt",
"hand_yolov9c.pt",
"person_yolov8n-seg.pt",
"person_yolov8s-seg.pt",
"person_yolov8m-seg.pt",
Expand Down

0 comments on commit 0a07045

Please sign in to comment.