Spaces:
Running
on
Zero
Running
on
Zero
Upload __init__.py
Browse files
controlnet_aux/anyline/__init__.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# code based in https://github.com/TheMistoAI/ComfyUI-Anyline/blob/main/anyline.py
|
2 |
+
import os
|
3 |
+
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import torch
|
7 |
+
from einops import rearrange
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
from PIL import Image
|
10 |
+
from skimage import morphology
|
11 |
+
|
12 |
+
from ..teed.ted import TED
|
13 |
+
from ..util import HWC3, resize_image, safe_step
|
14 |
+
|
15 |
+
|
16 |
+
class AnylineDetector:
|
17 |
+
def __init__(self, model):
|
18 |
+
self.model = model
|
19 |
+
|
20 |
+
@classmethod
|
21 |
+
def from_pretrained(cls, pretrained_model_or_path, filename=None, subfolder=None):
|
22 |
+
if os.path.isdir(pretrained_model_or_path):
|
23 |
+
model_path = os.path.join(pretrained_model_or_path, filename)
|
24 |
+
else:
|
25 |
+
model_path = hf_hub_download(
|
26 |
+
pretrained_model_or_path, filename, subfolder=subfolder
|
27 |
+
)
|
28 |
+
|
29 |
+
model = TED()
|
30 |
+
model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
31 |
+
|
32 |
+
return cls(model)
|
33 |
+
|
34 |
+
def to(self, device):
|
35 |
+
self.model.to(device)
|
36 |
+
return self
|
37 |
+
|
38 |
+
def __call__(
|
39 |
+
self,
|
40 |
+
input_image,
|
41 |
+
detect_resolution=1280,
|
42 |
+
guassian_sigma=2.0,
|
43 |
+
intensity_threshold=3,
|
44 |
+
output_type="pil",
|
45 |
+
):
|
46 |
+
device = next(iter(self.model.parameters())).device
|
47 |
+
|
48 |
+
if not isinstance(input_image, np.ndarray):
|
49 |
+
input_image = np.array(input_image, dtype=np.uint8)
|
50 |
+
output_type = output_type or "pil"
|
51 |
+
else:
|
52 |
+
output_type = output_type or "np"
|
53 |
+
|
54 |
+
original_height, original_width, _ = input_image.shape
|
55 |
+
|
56 |
+
input_image = HWC3(input_image)
|
57 |
+
input_image = resize_image(input_image, detect_resolution)
|
58 |
+
|
59 |
+
assert input_image.ndim == 3
|
60 |
+
height, width, _ = input_image.shape
|
61 |
+
with torch.no_grad():
|
62 |
+
image_teed = torch.from_numpy(input_image.copy()).float().to(device)
|
63 |
+
image_teed = rearrange(image_teed, "h w c -> 1 c h w")
|
64 |
+
edges = self.model(image_teed)
|
65 |
+
edges = [e.detach().cpu().numpy().astype(np.float32)[0, 0] for e in edges]
|
66 |
+
edges = [
|
67 |
+
cv2.resize(e, (width, height), interpolation=cv2.INTER_LINEAR)
|
68 |
+
for e in edges
|
69 |
+
]
|
70 |
+
edges = np.stack(edges, axis=2)
|
71 |
+
edge = 1 / (1 + np.exp(-np.mean(edges, axis=2).astype(np.float64)))
|
72 |
+
edge = safe_step(edge, 2)
|
73 |
+
edge = (edge * 255.0).clip(0, 255).astype(np.uint8)
|
74 |
+
|
75 |
+
mteed_result = edge
|
76 |
+
mteed_result = HWC3(mteed_result)
|
77 |
+
|
78 |
+
x = input_image.astype(np.float32)
|
79 |
+
g = cv2.GaussianBlur(x, (0, 0), guassian_sigma)
|
80 |
+
intensity = np.min(g - x, axis=2).clip(0, 255)
|
81 |
+
intensity /= max(16, np.median(intensity[intensity > intensity_threshold]))
|
82 |
+
intensity *= 127
|
83 |
+
lineart_result = intensity.clip(0, 255).astype(np.uint8)
|
84 |
+
|
85 |
+
lineart_result = HWC3(lineart_result)
|
86 |
+
|
87 |
+
lineart_result = self.get_intensity_mask(
|
88 |
+
lineart_result, lower_bound=0, upper_bound=255
|
89 |
+
)
|
90 |
+
|
91 |
+
cleaned = morphology.remove_small_objects(
|
92 |
+
lineart_result.astype(bool), min_size=36, connectivity=1
|
93 |
+
)
|
94 |
+
lineart_result = lineart_result * cleaned
|
95 |
+
final_result = self.combine_layers(mteed_result, lineart_result)
|
96 |
+
|
97 |
+
final_result = cv2.resize(
|
98 |
+
final_result,
|
99 |
+
(original_width, original_height),
|
100 |
+
interpolation=cv2.INTER_LINEAR,
|
101 |
+
)
|
102 |
+
|
103 |
+
if output_type == "pil":
|
104 |
+
final_result = Image.fromarray(final_result)
|
105 |
+
|
106 |
+
return final_result
|
107 |
+
|
108 |
+
def get_intensity_mask(self, image_array, lower_bound, upper_bound):
|
109 |
+
mask = image_array[:, :, 0]
|
110 |
+
mask = np.where((mask >= lower_bound) & (mask <= upper_bound), mask, 0)
|
111 |
+
mask = np.expand_dims(mask, 2).repeat(3, axis=2)
|
112 |
+
return mask
|
113 |
+
|
114 |
+
def combine_layers(self, base_layer, top_layer):
|
115 |
+
mask = top_layer.astype(bool)
|
116 |
+
temp = 1 - (1 - top_layer) * (1 - base_layer)
|
117 |
+
result = base_layer * (~mask) + temp * mask
|
118 |
+
return result
|