Spaces:
Runtime error
Runtime error
Update diffusion_webui/utils/preprocces_utils.py
Browse files
diffusion_webui/utils/preprocces_utils.py
CHANGED
|
@@ -14,6 +14,10 @@ from controlnet_aux import (
|
|
| 14 |
ZoeDetector,
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
PREPROCCES_DICT = {
|
| 18 |
"Hed": HEDdetector.from_pretrained("lllyasviel/Annotators"),
|
| 19 |
"Midas": MidasDetector.from_pretrained("lllyasviel/Annotators"),
|
|
@@ -30,3 +34,59 @@ PREPROCCES_DICT = {
|
|
| 30 |
"ContentShuffle": ContentShuffleDetector(),
|
| 31 |
"MediapipeFace": MediapipeFaceDetector(),
|
| 32 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
ZoeDetector,
|
| 15 |
)
|
| 16 |
|
| 17 |
+
import numpmy as np
|
| 18 |
+
import cv2
|
| 19 |
+
|
| 20 |
+
|
| 21 |
PREPROCCES_DICT = {
|
| 22 |
"Hed": HEDdetector.from_pretrained("lllyasviel/Annotators"),
|
| 23 |
"Midas": MidasDetector.from_pretrained("lllyasviel/Annotators"),
|
|
|
|
| 34 |
"ContentShuffle": ContentShuffleDetector(),
|
| 35 |
"MediapipeFace": MediapipeFaceDetector(),
|
| 36 |
}
|
| 37 |
+
|
| 38 |
+
def pad64(x):
|
| 39 |
+
return int(np.ceil(float(x) / 64.0) * 64 - x)
|
| 40 |
+
|
| 41 |
+
def HWC3(x):
|
| 42 |
+
assert x.dtype == np.uint8
|
| 43 |
+
if x.ndim == 2:
|
| 44 |
+
x = x[:, :, None]
|
| 45 |
+
assert x.ndim == 3
|
| 46 |
+
H, W, C = x.shape
|
| 47 |
+
assert C == 1 or C == 3 or C == 4
|
| 48 |
+
if C == 3:
|
| 49 |
+
return x
|
| 50 |
+
if C == 1:
|
| 51 |
+
return np.concatenate([x, x, x], axis=2)
|
| 52 |
+
if C == 4:
|
| 53 |
+
color = x[:, :, 0:3].astype(np.float32)
|
| 54 |
+
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
|
| 55 |
+
y = color * alpha + 255.0 * (1.0 - alpha)
|
| 56 |
+
y = y.clip(0, 255).astype(np.uint8)
|
| 57 |
+
return y
|
| 58 |
+
|
| 59 |
+
def safer_memory(x):
|
| 60 |
+
return np.ascontiguousarray(x.copy()).copy()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def resize_image_with_pad(input_image, resolution, skip_hwc3=False):
|
| 64 |
+
if skip_hwc3:
|
| 65 |
+
img = input_image
|
| 66 |
+
else:
|
| 67 |
+
img = HWC3(input_image)
|
| 68 |
+
|
| 69 |
+
H_raw, W_raw, _ = img.shape
|
| 70 |
+
k = float(resolution) / float(min(H_raw, W_raw))
|
| 71 |
+
interpolation = cv2.INTER_CUBIC if k > 1 else cv2.INTER_AREA
|
| 72 |
+
H_target = int(np.round(float(H_raw) * k))
|
| 73 |
+
W_target = int(np.round(float(W_raw) * k))
|
| 74 |
+
img = cv2.resize(img, (W_target, H_target), interpolation=interpolation)
|
| 75 |
+
H_pad, W_pad = pad64(H_target), pad64(W_target)
|
| 76 |
+
img_padded = np.pad(img, [[0, H_pad], [0, W_pad], [0, 0]], mode='edge')
|
| 77 |
+
|
| 78 |
+
def remove_pad(x):
|
| 79 |
+
return safer_memory(x[:H_target, :W_target])
|
| 80 |
+
|
| 81 |
+
return safer_memory(img_padded), remove_pad
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def scribble_xdog(img, res=512, thr_a=32, **kwargs):
|
| 85 |
+
img, remove_pad = resize_image_with_pad(img, res)
|
| 86 |
+
g1 = cv2.GaussianBlur(img.astype(np.float32), (0, 0), 0.5)
|
| 87 |
+
g2 = cv2.GaussianBlur(img.astype(np.float32), (0, 0), 5.0)
|
| 88 |
+
dog = (255 - np.min(g2 - g1, axis=2)).clip(0, 255).astype(np.uint8)
|
| 89 |
+
result = np.zeros_like(img, dtype=np.uint8)
|
| 90 |
+
result[2 * (255 - dog) > thr_a] = 255
|
| 91 |
+
return remove_pad(result), True
|
| 92 |
+
|