Create block.py
Browse files
block.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers.modular_pipelines import ModularPiplineBlocks, PipelineState, InputParam, OutputParam
|
2 |
+
from diffusers.utils import load_image
|
3 |
+
from PIL import Image
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
class GetImageStep(ModularPiplineBlocks):
|
8 |
+
|
9 |
+
PROCESSOR_IDS = set([
|
10 |
+
"canny", "lineart_anime",
|
11 |
+
])
|
12 |
+
|
13 |
+
def __init__(self):
|
14 |
+
super().__init__()
|
15 |
+
from controlnet_aux.processor import Processor
|
16 |
+
self.processor = Processor
|
17 |
+
|
18 |
+
@staticmethod
|
19 |
+
def make_canny(image):
|
20 |
+
image = np.array(image)
|
21 |
+
image = cv2.Canny(image, 100, 200)
|
22 |
+
image = image[:, :, None]
|
23 |
+
image = np.concatenate([image, image, image], axis=2)
|
24 |
+
return Image.fromarray(image)
|
25 |
+
|
26 |
+
def make_lineart_anime(self, image):
|
27 |
+
return self.processor("lineart_anime")(image)
|
28 |
+
|
29 |
+
|
30 |
+
def check_inputs(self, data) -> None:
|
31 |
+
"""
|
32 |
+
Validates that `processor_id` is one of the supported processors.
|
33 |
+
Raises:
|
34 |
+
ValueError: if `processor_id` is not in PROCESSOR_IDS.
|
35 |
+
"""
|
36 |
+
|
37 |
+
if data.image_url is None and data.image is None:
|
38 |
+
raise ValueError("Either `image_url` or `image` must be provided.")
|
39 |
+
|
40 |
+
if data.image_url is not None and data.image is not None:
|
41 |
+
raise ValueError("Only one of `image_url` or `image` must be provided.")
|
42 |
+
|
43 |
+
if data.processor_id is not None and data.processor_id not in self.PROCESSOR_IDS:
|
44 |
+
raise ValueError(
|
45 |
+
f"Processor id '{data.processor_id}' not found. "
|
46 |
+
f"Please use one of the following: {self.PROCESSOR_IDS}"
|
47 |
+
)
|
48 |
+
|
49 |
+
@property
|
50 |
+
def inputs(self):
|
51 |
+
return [
|
52 |
+
InputParam("image", type_hint=Image.Image),
|
53 |
+
InputParam("image_url", type_hint=str, description="The url of the image to load"),
|
54 |
+
InputParam("size", description="The size of the image"),
|
55 |
+
InputParam("processor_id", type_hint=str, description="The id of the processor to use for controlnet")
|
56 |
+
]
|
57 |
+
|
58 |
+
@property
|
59 |
+
def intermediate_outputs(self):
|
60 |
+
return [
|
61 |
+
OutputParam("image", type_hint=Image.Image),
|
62 |
+
]
|
63 |
+
|
64 |
+
def __call__(self, pipeline, state: PipelineState):
|
65 |
+
|
66 |
+
data = self.get_block_state(state)
|
67 |
+
self.check_inputs(data)
|
68 |
+
|
69 |
+
if data.image is None:
|
70 |
+
data.image = load_image(data.image_url).convert("RGB")
|
71 |
+
|
72 |
+
if data.size is not None:
|
73 |
+
data.image = data.image.resize(data.size)
|
74 |
+
|
75 |
+
if data.processor_id is not None:
|
76 |
+
if data.processor_id == "canny":
|
77 |
+
data.image = self.make_canny(data.image)
|
78 |
+
elif data.processor_id == "lineart_anime":
|
79 |
+
data.image = self.make_lineart_anime(data.image)
|
80 |
+
|
81 |
+
self.set_block_state(state, data)
|
82 |
+
|
83 |
+
return pipeline, state
|