2022-12-10 15:06:15 +01:00
|
|
|
import cv2
|
|
|
|
import pytest
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from lama_cleaner.model_manager import ModelManager
|
|
|
|
from lama_cleaner.schema import HDStrategy
|
2023-12-28 03:48:52 +01:00
|
|
|
from lama_cleaner.tests.utils import (
|
|
|
|
current_dir,
|
|
|
|
get_config,
|
|
|
|
get_data,
|
|
|
|
save_dir,
|
|
|
|
check_device,
|
|
|
|
)
|
2022-12-10 15:06:15 +01:00
|
|
|
|
2023-12-19 06:16:30 +01:00
|
|
|
model_name = "Fantasy-Studio/Paint-by-Example"
|
2022-12-10 15:06:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assert_equal(
|
2023-12-19 06:16:30 +01:00
|
|
|
model,
|
|
|
|
config,
|
2023-12-28 03:48:52 +01:00
|
|
|
save_name: str,
|
2023-12-19 06:16:30 +01:00
|
|
|
fx: float = 1,
|
|
|
|
fy: float = 1,
|
2022-12-10 15:06:15 +01:00
|
|
|
img_p=current_dir / "overture-creations-5sI6fQgYIuo.png",
|
|
|
|
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
|
2022-12-11 15:03:36 +01:00
|
|
|
example_p=current_dir / "bunny.jpeg",
|
2022-12-10 15:06:15 +01:00
|
|
|
):
|
|
|
|
img, mask = get_data(fx=fx, fy=fy, img_p=img_p, mask_p=mask_p)
|
|
|
|
|
|
|
|
example_image = cv2.imread(str(example_p))
|
|
|
|
example_image = cv2.cvtColor(example_image, cv2.COLOR_BGRA2RGB)
|
2023-12-19 06:16:30 +01:00
|
|
|
example_image = cv2.resize(
|
|
|
|
example_image, None, fx=fx, fy=fy, interpolation=cv2.INTER_AREA
|
|
|
|
)
|
2022-12-10 15:06:15 +01:00
|
|
|
|
|
|
|
print(f"Input image shape: {img.shape}, example_image: {example_image.shape}")
|
|
|
|
config.paint_by_example_example_image = Image.fromarray(example_image)
|
|
|
|
res = model(img, mask, config)
|
2023-12-28 03:48:52 +01:00
|
|
|
cv2.imwrite(str(save_dir / save_name), res)
|
2022-12-10 15:06:15 +01:00
|
|
|
|
|
|
|
|
2023-12-28 03:48:52 +01:00
|
|
|
@pytest.mark.parametrize("device", ["cuda", "mps", "cpu"])
|
|
|
|
def test_paint_by_example(device):
|
|
|
|
sd_steps = check_device(device)
|
2023-12-19 06:16:30 +01:00
|
|
|
model = ModelManager(name=model_name, device=device, disable_nsfw=True)
|
2023-12-28 03:48:52 +01:00
|
|
|
cfg = get_config(strategy=HDStrategy.ORIGINAL, sd_steps=sd_steps)
|
2023-01-07 01:52:11 +01:00
|
|
|
assert_equal(
|
|
|
|
model,
|
|
|
|
cfg,
|
2023-12-28 03:48:52 +01:00
|
|
|
f"paint_by_example_device_{device}.png",
|
2023-01-07 01:52:11 +01:00
|
|
|
img_p=current_dir / "overture-creations-5sI6fQgYIuo.png",
|
|
|
|
mask_p=current_dir / "overture-creations-5sI6fQgYIuo_mask.png",
|
|
|
|
fy=0.9,
|
2023-12-19 06:16:30 +01:00
|
|
|
fx=1.3,
|
2023-01-07 01:52:11 +01:00
|
|
|
)
|