forked from CartoonSegmentation/CartoonSegmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_style.py
273 lines (238 loc) · 12.2 KB
/
run_style.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import json
import base64
import io
import requests
from PIL import Image
import numpy as np
import argparse
import random
from tqdm import tqdm
import os.path as osp
from omegaconf import OmegaConf
from utils.io_utils import find_all_imgs, submit_request, img2b64, save_encoded_image
from random import randint
from requests.auth import HTTPBasicAuth
import cv2
from copy import deepcopy
from pathlib import Path
import math
INPAINTING_FILL_METHODS = ['fill', 'original', 'latent_noise', 'latent_nothing']
def run_sdinpaint(img: Image.Image, mask: Image.Image, data: dict, prompt: str = '', nprompt: str = '', url='', auth=None) -> str:
if isinstance(img, Image.Image):
img_b64 = img2b64(img)
else:
assert isinstance(img, str)
img_b64 = img
mask_b64 = img2b64(mask)
data['init_images'] = [img_b64]
data['alwayson_scripts']['controlnet']['args'][0]['input_image'] = img_b64
data['mask'] = mask_b64
data['negative_prompt'] = nprompt
data['prompt'] = prompt
response = submit_request(url, json.dumps(data), auth=auth)
img_b64 = response.json()['images'][0]
return img_b64
def long_side_to(H, W, long_side):
asp = H / W
if asp > 1:
H = long_side
H = int(round(H / 32)) * 32
W = int(round(H / asp / 32)) * 32
else:
W = long_side
W = int(round(W / 32)) * 32
H = int(round(W * asp / 32)) * 32
return H, W
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Inpaint instances of people using stable '
'diffusion.')
# workspace\forpaper\eval\kenburns\original\1.png
parser.add_argument('--img_path', type=str, help='Path to input image. Can be directory.')
# parser.add_argument('--img_path', type=str, default=r'workspace/style/original/', help='Path to input image.')
parser.add_argument('--onebyone', type=bool, default=True, help='repainting person one by one')
parser.add_argument('-n', '--negative_prompt', type=str, default='',
help='Stable diffusion negative prompt.')
parser.add_argument('-W', '--width', type=int, default=768, help='Width of output image.')
parser.add_argument('-H', '--height', type=int, default=768, help='Height of output image.')
parser.add_argument('-s', '--steps', type=int, default=24, help='Number of diffusion steps.')
parser.add_argument('-c', '--cfg_scale', type=int, default=9, help='Classifier free guidance '
'scale, i.e. how strongly the image should conform to prompt.')
parser.add_argument('-S', '--sample_name', type=str, default='Euler a', help='Name of sampler '
'to use.')
parser.add_argument('-d', '--denoising_strength', type=float, default=0.75, help='How much to '
'disregard original image.')
parser.add_argument('-f', '--fill', type=str, default=INPAINTING_FILL_METHODS[1],
help='The fill method to use for inpainting.')
parser.add_argument('-b', '--mask_blur', type=int, default=4, help='Blur radius of Gaussian '
'filter to apply to mask.')
parser.add_argument('-r', '--resolution', type=int, default=640, help='inpainting resolution')
parser.add_argument('--save_dir', type=str, default='repaint_output')
parser.add_argument('--url', type=str, default='http://127.0.0.1:7860/sdapi/v1/txt2img', help='img2img url')
parser.add_argument('--cfg', type=str, default='configs/3d_pixar.yaml', help='repaint config path')
parser.add_argument('--bg_nprompt', type=str, default='', help='background negative prompt')
parser.add_argument('--inpaint_full_res', type=int, default=1)
parser.add_argument('--inpaint_full_res_padding', type=int, default=32)
parser.add_argument('--detector_ckpt', type=str, default='models/AnimeInstanceSegmentation/rtmdetl_e60.ckpt')
parser.add_argument('--save_intermediate', type=bool, default=False)
parser.add_argument('--to-grey', type=bool, default=False)
parser.add_argument('--infer-tagger', type=bool, default=True)
parser.add_argument('--style-prompt', default='')
parser.add_argument('--global-nprompt', default='')
parser.add_argument('--apply-bg-tagger', default=False)
parser.add_argument('--apply-fg-tagger', default=True)
args = parser.parse_args()
args = OmegaConf.create(vars(args))
args.merge_with(OmegaConf.load(args.cfg))
data = {
**OmegaConf.to_container(args.sd_params),
# "init_images": [img_b64]
}
auth = None
if 'username' in args:
username = args.pop('username')
password = args.pop('password')
auth = HTTPBasicAuth(username, password)
img_path = args.img_path
if osp.isfile(img_path):
imglist = [img_path]
else:
imglist = find_all_imgs(img_path, abs_path=True)
imglist = imglist[::-1]
detector = None
for ii, img_path in enumerate(imglist):
print(f'repainting {img_path} ... {ii+1}/{len(imglist)}')
imname = osp.basename(img_path).replace(Path(img_path).suffix, '')
cimg = Image.open(img_path).convert('RGB')
W, H = cimg.width, cimg.height
H, W = long_side_to(H, W, args.long_side)
data['width'], data['height'] = W, H
img_resized = cimg.resize((W, H), resample=Image.Resampling.LANCZOS)
if not osp.exists(args.save_dir):
os.makedirs(args.save_dir)
if args.onebyone:
repaint_args = {
'mask_blur': args.mask_blur,
'inpainting_fill': INPAINTING_FILL_METHODS.index(args.fill),
'inpaint_full_res': args.inpaint_full_res,
'inpaint_full_res_padding': args.inpaint_full_res_padding,
'denoising_strength': args.denoising_strength
}
data_inpaint = deepcopy(data)
data_inpaint.update(repaint_args)
from utils.io_utils import json2dict, dict2json, find_all_imgs
cache_masks_dir = args.cache_masks_dir
mask_fg = None
masks = []
if not osp.exists(cache_masks_dir):
os.makedirs(cache_masks_dir)
promptp = osp.join(cache_masks_dir, f'{imname}_prompts.json')
bg_prompt = ''
fg_prompts = []
if not osp.exists(promptp):
if detector is None:
from animeinsseg import AnimeInsSeg
import numpy as np
from animeinsseg.inpainting import patch_match
detector = AnimeInsSeg(args.detector_ckpt, device='cuda')
detector.init_tagger()
instances = detector.infer(img_path, output_type='numpy', infer_tags=True)
if not instances.is_empty:
prompts_dict = {}
for ii, mask in enumerate(instances.masks):
mask = cv2.resize(mask.astype(np.uint8) * 255, (W, H), interpolation=cv2.INTER_AREA)
mask = Image.fromarray(mask)
savename = imname + '_' + str(ii).zfill(3) + '.png'
mask.save(osp.join(cache_masks_dir, savename))
masks.append(mask)
tags = instances.tags[ii].split(' ')
ctags = instances.character_tags[ii]
for ctag in ctags:
if ctag in tags:
tags.remove(ctag)
prompt = ','.join(tags).replace('_', ' ')
prompts_dict[savename] = prompt
fg_prompts.append(prompt)
mask_fg = cv2.resize(instances.compose_masks().astype(np.uint8) * 255, (W, H), interpolation=cv2.INTER_AREA)
bg = patch_match.inpaint(np.array(img_resized), mask_fg, patch_size=3)
savep = osp.join(cache_masks_dir, f'{imname}_bg_repaint.png')
Image.fromarray(bg).save(savep)
mask_fg = Image.fromarray(mask_fg)
mask_fg.save(osp.join(cache_masks_dir, f'{imname}_mask_fg.png'))
bg_tags, character_tags = detector.tagger.label_cv2_bgr(cv2.cvtColor(bg, cv2.COLOR_BGR2RGB))
for ii, t in enumerate(bg_tags):
bg_tags[ii] = t.replace('_', ' ')
bg_prompt = ','.join(bg_tags)
prompts_dict[f'{imname}_bg_repaint.png'] = bg_prompt
dict2json(prompts_dict, promptp)
else:
maskp_list = find_all_imgs(cache_masks_dir, abs_path=False)
prompts_dict = json2dict(promptp)
for maskn in prompts_dict.keys():
maskp = osp.join(cache_masks_dir, maskn)
mask = Image.open(maskp)
if maskn.endswith('bg_repaint.png'):
bg_prompt = prompts_dict[maskn]
bg = mask
else:
mask = mask.convert('L')
fg_prompts.append(prompts_dict[maskn])
masks.append(mask)
mask_fg = osp.join(cache_masks_dir, f'{imname}_mask_fg.png')
mask_fg = Image.open(mask_fg).convert('L')
if len(masks) == 0:
print('no fg is found')
continue
for ii in tqdm(range(args.niter)):
if args.random_seed:
data['seed'] = randint(0, 65536)
else:
data['seed'] += ii
seed = data['seed']
if args.onebyone:
data_inpaint['seed'] = seed
if ii == 0:
img_b64 = img2b64(bg)
img_repainted = img_resized
else:
img_b64 = output_img_b64
if ii == 0:
nprompt = args.bg_nprompt
prompt = args.style_prompt + ','
if args.apply_bg_tagger:
prompt += bg_prompt + ','
prompt = prompt.strip(',')
data['alwayson_scripts']['controlnet']['args'][0]['input_image'] = img_b64
data['init_images'] = [img_b64]
data['negative_prompt'] = nprompt
data['prompt'] = prompt
response = submit_request(args.url, json.dumps(data), auth=auth)
output_img_b64 = response.json()['images'][0]
bg_repainted = Image.open(io.BytesIO(base64.b64decode(output_img_b64)))
# bg_repainted.show()
img_repainted = Image.composite(img_repainted, bg_repainted, mask_fg)
# img_repainted.show()
for jj, (fg_prompt, mask) in enumerate(zip(fg_prompts, masks)):
nprompt = args.global_nprompt
prompt = args.style_prompt + ','
if args.apply_fg_tagger:
prompt += fg_prompt + ','
print(prompt)
prompt = prompt.strip(',')
output_img_b64 = run_sdinpaint(img_repainted, mask, data_inpaint, prompt=prompt, nprompt=nprompt, url=args.url, auth=auth)
img_repainted = Image.open(io.BytesIO(base64.b64decode(output_img_b64)))
# img_repainted.show()
save_encoded_image(output_img_b64, osp.join(args.save_dir, f'{imname}_onebyone_niter{ii}_output_{seed}.png'))
else:
img_b64 = img2b64(cimg)
data['alwayson_scripts']['controlnet']['args'][0]['input_image'] = img_b64
data['init_images'] = [img_b64]
prompt = args.style_prompt + ','
prompt = prompt.strip(',')
data['prompt'] = prompt
data['negative_prompt'] = args.global_nprompt
response = submit_request(args.url, json.dumps(data), auth=auth)
output_img_b64 = response.json()['images'][0]
imgsavep = osp.join(args.save_dir, f'{imname}_niter{ii}_output_{seed}.png')
save_encoded_image(output_img_b64, imgsavep)
cimg = Image.open(io.BytesIO(base64.b64decode(output_img_b64)))