Spaces:
Running
on
Zero
Running
on
Zero
tori29umai
commited on
Commit
•
51838d1
1
Parent(s):
e09cb69
app.py
Browse files- app.py +10 -10
- utils/dl_utils.py +1 -1
- utils/image_utils.py +44 -0
app.py
CHANGED
@@ -7,7 +7,7 @@ import os
|
|
7 |
import time
|
8 |
|
9 |
from utils.dl_utils import dl_cn_model, dl_cn_config, dl_tagger_model, dl_lora_model
|
10 |
-
from utils.image_utils import resize_image_aspect_ratio, base_generation,
|
11 |
|
12 |
from utils.prompt_utils import execute_prompt, remove_color, remove_duplicates
|
13 |
from utils.tagger import modelLoad, analysis
|
@@ -32,6 +32,7 @@ def load_model(lora_dir, cn_dir):
|
|
32 |
dtype = torch.float16
|
33 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
34 |
controlnet = ControlNetModel.from_pretrained(cn_dir, torch_dtype=dtype, use_safetensors=True)
|
|
|
35 |
pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
|
36 |
"cagliostrolab/animagine-xl-3.1", controlnet=controlnet, vae=vae, torch_dtype=torch.float16
|
37 |
)
|
@@ -94,10 +95,10 @@ class Img2Img:
|
|
94 |
tags_list = remove_color(tags)
|
95 |
return tags_list
|
96 |
|
97 |
-
def
|
98 |
-
|
99 |
-
|
100 |
-
return
|
101 |
|
102 |
def layout(self):
|
103 |
css = """
|
@@ -113,9 +114,8 @@ class Img2Img:
|
|
113 |
self.input_image_path = gr.Image(label="input_image", type='filepath')
|
114 |
self.canny_image = gr.Image(label="canny_image", type='pil')
|
115 |
with gr.Row():
|
116 |
-
|
117 |
-
gr.
|
118 |
-
canny_threshold2 = gr.Slider(minimum=0, value=120, maximum=254, show_label=False)
|
119 |
canny_generate_button = gr.Button("canny_generate")
|
120 |
|
121 |
self.prompt = gr.Textbox(label="prompt", lines=3)
|
@@ -130,8 +130,8 @@ class Img2Img:
|
|
130 |
self.output_image = gr.Image(type="pil", label="output_image")
|
131 |
|
132 |
canny_generate_button.click(
|
133 |
-
self.
|
134 |
-
inputs=[self.input_image_path,
|
135 |
outputs=self.canny_image
|
136 |
)
|
137 |
|
|
|
7 |
import time
|
8 |
|
9 |
from utils.dl_utils import dl_cn_model, dl_cn_config, dl_tagger_model, dl_lora_model
|
10 |
+
from utils.image_utils import resize_image_aspect_ratio, base_generation, line_process
|
11 |
|
12 |
from utils.prompt_utils import execute_prompt, remove_color, remove_duplicates
|
13 |
from utils.tagger import modelLoad, analysis
|
|
|
32 |
dtype = torch.float16
|
33 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
34 |
controlnet = ControlNetModel.from_pretrained(cn_dir, torch_dtype=dtype, use_safetensors=True)
|
35 |
+
|
36 |
pipe = StableDiffusionXLControlNetImg2ImgPipeline.from_pretrained(
|
37 |
"cagliostrolab/animagine-xl-3.1", controlnet=controlnet, vae=vae, torch_dtype=torch.float16
|
38 |
)
|
|
|
95 |
tags_list = remove_color(tags)
|
96 |
return tags_list
|
97 |
|
98 |
+
def _make_line(self, img_path, sigma, gamma):
|
99 |
+
sigma = float(sigma )
|
100 |
+
gamma = float(gamma)
|
101 |
+
return line_process(img_path, sigma, gamma)
|
102 |
|
103 |
def layout(self):
|
104 |
css = """
|
|
|
114 |
self.input_image_path = gr.Image(label="input_image", type='filepath')
|
115 |
self.canny_image = gr.Image(label="canny_image", type='pil')
|
116 |
with gr.Row():
|
117 |
+
line_sigma = gr.Slider(minimum=0.1, value=1.4, maximum=3.0, show_label=False)
|
118 |
+
line_gamma = gr.Slider(minimum=0.5, value=0.98, maximum=2.0, show_label=False)
|
|
|
119 |
canny_generate_button = gr.Button("canny_generate")
|
120 |
|
121 |
self.prompt = gr.Textbox(label="prompt", lines=3)
|
|
|
130 |
self.output_image = gr.Image(type="pil", label="output_image")
|
131 |
|
132 |
canny_generate_button.click(
|
133 |
+
self._make_line,
|
134 |
+
inputs=[self.input_image_path, line_sigma, line_gamma],
|
135 |
outputs=self.canny_image
|
136 |
)
|
137 |
|
utils/dl_utils.py
CHANGED
@@ -11,7 +11,7 @@ import cv2
|
|
11 |
def dl_cn_model(model_dir):
|
12 |
folder = model_dir
|
13 |
file_name = 'diffusion_pytorch_model.safetensors'
|
14 |
-
url = "https://huggingface.co/
|
15 |
file_path = os.path.join(folder, file_name)
|
16 |
if not os.path.exists(file_path):
|
17 |
response = requests.get(url, allow_redirects=True)
|
|
|
11 |
def dl_cn_model(model_dir):
|
12 |
folder = model_dir
|
13 |
file_name = 'diffusion_pytorch_model.safetensors'
|
14 |
+
url = "https://huggingface.co/2vXpSwA7/iroiro-lora/resolve/main/test_controlnet2/CN-anytest_v3-50000_fp16.safetensors"
|
15 |
file_path = os.path.join(folder, file_name)
|
16 |
if not os.path.exists(file_path):
|
17 |
response = requests.get(url, allow_redirects=True)
|
utils/image_utils.py
CHANGED
@@ -27,6 +27,50 @@ def canny_process(image_path, threshold1, threshold2):
|
|
27 |
return canny
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def resize_image_aspect_ratio(image):
|
31 |
# 元の画像サイズを取得
|
32 |
original_width, original_height = image.size
|
|
|
27 |
return canny
|
28 |
|
29 |
|
30 |
+
def line_process(image_path, sigma, gamma):
|
31 |
+
def DoG_filter(image, kernel_size=0, sigma=1.0, k_sigma=2.0, gamma=1.5):
|
32 |
+
g1 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
|
33 |
+
g2 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma * k_sigma)
|
34 |
+
return g1 - gamma * g2
|
35 |
+
|
36 |
+
def XDoG_filter(image, kernel_size=0, sigma=1.4, k_sigma=1.6, epsilon=0, phi=10, gamma=0.98):
|
37 |
+
epsilon /= 255
|
38 |
+
dog = DoG_filter(image, kernel_size, sigma, k_sigma, gamma)
|
39 |
+
dog /= dog.max()
|
40 |
+
e = 1 + np.tanh(phi * (dog - epsilon))
|
41 |
+
e[e >= 1] = 1
|
42 |
+
return (e * 255).astype('uint8')
|
43 |
+
|
44 |
+
def binarize_image(image):
|
45 |
+
_, binarized = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
46 |
+
return binarized
|
47 |
+
|
48 |
+
def process_XDoG(image, kernel_size=0, sigma=1.4, k_sigma=1.6, epsilon=0, phi=10, gamma=0.98):
|
49 |
+
xdog_image = XDoG_filter(image, kernel_size, sigma, k_sigma, epsilon, phi, gamma)
|
50 |
+
binarized_image = binarize_image(xdog_image)
|
51 |
+
final_image_pil = Image.fromarray(binarized_image)
|
52 |
+
return final_image_pil
|
53 |
+
|
54 |
+
# 画像を開き、RGBA形式に変換して透過情報を保持
|
55 |
+
img = Image.open(image_path)
|
56 |
+
img = img.convert("RGBA")
|
57 |
+
|
58 |
+
canvas_image = Image.new('RGBA', img.size, (255, 255, 255, 255))
|
59 |
+
|
60 |
+
# 画像をキャンバスにペーストし、透過部分が白色になるように設定
|
61 |
+
canvas_image.paste(img, (0, 0), img)
|
62 |
+
|
63 |
+
# RGBAからRGBに変換し、透過部分を白色にする
|
64 |
+
image_pil = canvas_image.convert("RGB")
|
65 |
+
|
66 |
+
# OpenCVが扱える形式に変換
|
67 |
+
image_cv = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
|
68 |
+
image_gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY)
|
69 |
+
inv_Line = process_XDoG(image_gray, kernel_size=0, sigma=sigma, k_sigma=1.6, epsilon=0, phi=10, gamma=gamma)
|
70 |
+
return inv_Line
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
def resize_image_aspect_ratio(image):
|
75 |
# 元の画像サイズを取得
|
76 |
original_width, original_height = image.size
|