Spaces:
Running
on
Zero
Running
on
Zero
File size: 14,202 Bytes
f460ce6 |
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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
import warnings
import cv2
import numpy as np
from PIL import Image
import torch
from torch import nn
from torch.nn import functional as F
import os
from einops import rearrange
from .util import HWC3, nms, safe_step, resize_image_with_pad, common_input_validate, get_intensity_mask, combine_layers
from .pidi import pidinet
from .ted import TED
from .lineart import Generator as LineartGenerator
from .informative_drawing import Generator
from .hed import ControlNetHED_Apache2
from pathlib import Path
from skimage import morphology
import argparse
from tqdm import tqdm
PREPROCESSORS_ROOT = os.getenv("PREPROCESSORS_ROOT", os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))), "models/preprocessors"))
class HEDDetector:
def __init__(self, netNetwork):
self.netNetwork = netNetwork
self.device = "cpu"
@classmethod
def from_pretrained(cls, filename="ControlNetHED.pth"):
model_path = os.path.join(PREPROCESSORS_ROOT, filename)
netNetwork = ControlNetHED_Apache2()
netNetwork.load_state_dict(torch.load(model_path, map_location='cpu'))
netNetwork.float().eval()
return cls(netNetwork)
def to(self, device):
self.netNetwork.to(device)
self.device = device
return self
def __call__(self, input_image, detect_resolution=512, safe=False, output_type=None, scribble=True, upscale_method="INTER_CUBIC", **kwargs):
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
input_image, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
assert input_image.ndim == 3
H, W, C = input_image.shape
with torch.no_grad():
image_hed = torch.from_numpy(input_image).float().to(self.device)
image_hed = rearrange(image_hed, 'h w c -> 1 c h w')
edges = self.netNetwork(image_hed)
edges = [e.detach().cpu().numpy().astype(np.float32)[0, 0] for e in edges]
edges = [cv2.resize(e, (W, H), interpolation=cv2.INTER_LINEAR) for e in edges]
edges = np.stack(edges, axis=2)
edge = 1 / (1 + np.exp(-np.mean(edges, axis=2).astype(np.float64)))
if safe:
edge = safe_step(edge)
edge = (edge * 255.0).clip(0, 255).astype(np.uint8)
detected_map = edge
if scribble:
detected_map = nms(detected_map, 127, 3.0)
detected_map = cv2.GaussianBlur(detected_map, (0, 0), 3.0)
detected_map[detected_map > 4] = 255
detected_map[detected_map < 255] = 0
detected_map = HWC3(remove_pad(detected_map))
if output_type == "pil":
detected_map = Image.fromarray(detected_map)
return detected_map
class CannyDetector:
def __call__(self, input_image=None, low_threshold=100, high_threshold=200, detect_resolution=512, output_type=None, upscale_method="INTER_CUBIC", **kwargs):
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
detected_map, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
detected_map = cv2.Canny(detected_map, low_threshold, high_threshold)
detected_map = HWC3(remove_pad(detected_map))
if output_type == "pil":
detected_map = Image.fromarray(detected_map)
return detected_map
class PidiNetDetector:
def __init__(self, netNetwork):
self.netNetwork = netNetwork
self.device = "cpu"
@classmethod
def from_pretrained(cls, filename="table5_pidinet.pth"):
model_path = os.path.join(PREPROCESSORS_ROOT, filename)
netNetwork = pidinet()
netNetwork.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(model_path)['state_dict'].items()})
netNetwork.eval()
return cls(netNetwork)
def to(self, device):
self.netNetwork.to(device)
self.device = device
return self
def __call__(self, input_image, detect_resolution=512, safe=False, output_type=None, scribble=True, apply_filter=False, upscale_method="INTER_CUBIC", **kwargs):
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
detected_map, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
detected_map = detected_map[:, :, ::-1].copy()
with torch.no_grad():
image_pidi = torch.from_numpy(detected_map).float().to(self.device)
image_pidi = image_pidi / 255.0
image_pidi = rearrange(image_pidi, 'h w c -> 1 c h w')
edge = self.netNetwork(image_pidi)[-1]
edge = edge.cpu().numpy()
if apply_filter:
edge = edge > 0.5
if safe:
edge = safe_step(edge)
edge = (edge * 255.0).clip(0, 255).astype(np.uint8)
detected_map = edge[0, 0]
if scribble:
detected_map = nms(detected_map, 127, 3.0)
detected_map = cv2.GaussianBlur(detected_map, (0, 0), 3.0)
detected_map[detected_map > 4] = 255
detected_map[detected_map < 255] = 0
detected_map = HWC3(remove_pad(detected_map))
if output_type == "pil":
detected_map = Image.fromarray(detected_map)
return detected_map
class TEDDetector:
def __init__(self, model):
self.model = model
self.device = "cpu"
@classmethod
def from_pretrained(cls, filename="7_model.pth"):
model_path = os.path.join(PREPROCESSORS_ROOT, filename)
model = TED()
model.load_state_dict(torch.load(model_path, map_location="cpu"))
model.eval()
return cls(model)
def to(self, device):
self.model.to(device)
self.device = device
return self
def __call__(self, input_image, detect_resolution=512, safe_steps=2, upscale_method="INTER_CUBIC", output_type=None, **kwargs):
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
input_image, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
H, W, _ = input_image.shape
with torch.no_grad():
image_teed = torch.from_numpy(input_image.copy()).float().to(self.device)
image_teed = rearrange(image_teed, 'h w c -> 1 c h w')
edges = self.model(image_teed)
edges = [e.detach().cpu().numpy().astype(np.float32)[0, 0] for e in edges]
edges = [cv2.resize(e, (W, H), interpolation=cv2.INTER_LINEAR) for e in edges]
edges = np.stack(edges, axis=2)
edge = 1 / (1 + np.exp(-np.mean(edges, axis=2).astype(np.float64)))
if safe_steps != 0:
edge = safe_step(edge, safe_steps)
edge = (edge * 255.0).clip(0, 255).astype(np.uint8)
detected_map = remove_pad(HWC3(edge))
if output_type == "pil":
detected_map = Image.fromarray(detected_map[..., :3])
return detected_map
class LineartStandardDetector:
def __call__(self, input_image=None, guassian_sigma=6.0, intensity_threshold=8, detect_resolution=512, output_type=None, upscale_method="INTER_CUBIC", **kwargs):
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
input_image, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
x = input_image.astype(np.float32)
g = cv2.GaussianBlur(x, (0, 0), guassian_sigma)
intensity = np.min(g - x, axis=2).clip(0, 255)
intensity /= max(16, np.median(intensity[intensity > intensity_threshold]))
intensity *= 127
detected_map = intensity.clip(0, 255).astype(np.uint8)
detected_map = HWC3(remove_pad(detected_map))
if output_type == "pil":
detected_map = Image.fromarray(detected_map)
return detected_map
class AnyLinePreprocessor:
def __init__(self, mteed_model, lineart_standard_detector):
self.device = "cpu"
self.mteed_model = mteed_model
self.lineart_standard_detector = lineart_standard_detector
@classmethod
def from_pretrained(cls, mteed_filename="MTEED.pth"):
mteed_model = TEDDetector.from_pretrained(filename=mteed_filename)
lineart_standard_detector = LineartStandardDetector()
return cls(mteed_model, lineart_standard_detector)
def to(self, device):
self.mteed_model.to(device)
self.device = device
return self
def __call__(self, image, resolution=512, lineart_lower_bound=0, lineart_upper_bound=1, object_min_size=36, object_connectivity=1):
# Process the image with MTEED model
mteed_result = self.mteed_model(image, detect_resolution=resolution)
# Process the image with the lineart standard preprocessor
lineart_result = self.lineart_standard_detector(image, guassian_sigma=2, intensity_threshold=3, resolution=resolution)
_lineart_result = get_intensity_mask(lineart_result, lower_bound=lineart_lower_bound, upper_bound=lineart_upper_bound)
_cleaned = morphology.remove_small_objects(_lineart_result.astype(bool), min_size=object_min_size, connectivity=object_connectivity)
_lineart_result = _lineart_result * _cleaned
_mteed_result = mteed_result
result = combine_layers(_mteed_result, _lineart_result)
# print(result.shape)
return result
class LineartDetector:
def __init__(self, model, coarse_model):
self.model = model
self.model_coarse = coarse_model
self.device = "cpu"
@classmethod
def from_pretrained(cls, filename="sk_model.pth", coarse_filename="sk_model2.pth"):
model_path = os.path.join(PREPROCESSORS_ROOT, filename)
coarse_model_path = os.path.join(PREPROCESSORS_ROOT, coarse_filename)
model = LineartGenerator(3, 1, 3)
model.load_state_dict(torch.load(model_path, map_location="cpu"))
model.eval()
coarse_model = LineartGenerator(3, 1, 3)
coarse_model.load_state_dict(torch.load(coarse_model_path, map_location="cpu"))
coarse_model.eval()
return cls(model, coarse_model)
def to(self, device):
self.model.to(device)
self.model_coarse.to(device)
self.device = device
return self
def __call__(self, input_image, coarse=False, detect_resolution=512, output_type=None, upscale_method="INTER_CUBIC", **kwargs):
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
detected_map, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
model = self.model_coarse if coarse else self.model
assert detected_map.ndim == 3
with torch.no_grad():
image = torch.from_numpy(detected_map).float().to(self.device)
image = image / 255.0
image = rearrange(image, 'h w c -> 1 c h w')
line = model(image)[0][0]
line = line.cpu().numpy()
line = (line * 255.0).clip(0, 255).astype(np.uint8)
detected_map = HWC3(line)
detected_map = remove_pad(255 - detected_map)
if output_type == "pil":
detected_map = Image.fromarray(detected_map)
return detected_map
class InformativeDetector:
def __init__(self, anime_model, contour_model):
self.anime_model = anime_model
self.contour_model = contour_model
self.device = "cpu"
@classmethod
def from_pretrained(cls, anime_filename="anime_style.pth", contour_filename="contour_style.pth"):
anime_model_path = os.path.join(PREPROCESSORS_ROOT, anime_filename)
contour_model_path = os.path.join(PREPROCESSORS_ROOT, contour_filename)
# 创建两个Generator模型
anime_model = Generator(3, 1, 3) # input_nc=3, output_nc=1, n_blocks=3
anime_model.load_state_dict(torch.load(anime_model_path, map_location="cpu"))
anime_model.eval()
contour_model = Generator(3, 1, 3) # input_nc=3, output_nc=1, n_blocks=3
contour_model.load_state_dict(torch.load(contour_model_path, map_location="cpu"))
contour_model.eval()
return cls(anime_model, contour_model)
def to(self, device):
self.anime_model.to(device)
self.contour_model.to(device)
self.device = device
return self
def __call__(self, input_image, style="anime", detect_resolution=512, output_type=None, upscale_method="INTER_CUBIC", **kwargs):
"""
提取sketch
Args:
input_image: 输入图像
style: "anime" 或 "contour"
detect_resolution: 检测分辨率
output_type: 输出类型
upscale_method: 上采样方法
"""
input_image, output_type = common_input_validate(input_image, output_type, **kwargs)
detected_map, remove_pad = resize_image_with_pad(input_image, detect_resolution, upscale_method)
# 选择模型
model = self.anime_model if style == "anime" else self.contour_model
assert detected_map.ndim == 3
with torch.no_grad():
image = torch.from_numpy(detected_map).float().to(self.device)
image = image / 255.0
# 转换维度 (h, w, c) -> (1, c, h, w)
image = image.permute(2, 0, 1).unsqueeze(0)
# 生成sketch
sketch = model(image)
sketch = sketch[0][0] # 取出第一个batch的第一个通道
sketch = sketch.cpu().numpy()
sketch = (sketch * 255.0).clip(0, 255).astype(np.uint8)
detected_map = HWC3(sketch)
detected_map = remove_pad(255 - detected_map) # 反转颜色
if output_type == "pil":
detected_map = Image.fromarray(detected_map)
return detected_map |