Commit
•
1a72c84
1
Parent(s):
f556391
Update handler.py
Browse files- handler.py +8 -56
handler.py
CHANGED
@@ -25,29 +25,14 @@ class EndpointHandler():
|
|
25 |
# )
|
26 |
# self.smooth_pipe.to("cuda")
|
27 |
|
28 |
-
|
29 |
-
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
|
30 |
-
)
|
31 |
-
self.canny_pipe = self.canny_pipe.to("cuda")
|
32 |
-
self.canny_pipe.enable_model_cpu_offload()
|
33 |
-
self.canny_pipe.enable_xformers_memory_efficient_attention()
|
34 |
-
|
35 |
-
self.controlnets = [
|
36 |
-
ControlNetModel.from_pretrained(
|
37 |
-
"diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16, use_safetensors=True
|
38 |
-
),
|
39 |
-
ControlNetModel.from_pretrained(
|
40 |
-
"lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16
|
41 |
-
)
|
42 |
-
]
|
43 |
-
"""
|
44 |
self.controlnet = ControlNetModel.from_pretrained(
|
45 |
"lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16
|
46 |
)
|
47 |
-
|
48 |
|
49 |
self.pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
50 |
-
"
|
51 |
)
|
52 |
|
53 |
self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)
|
@@ -172,53 +157,20 @@ class EndpointHandler():
|
|
172 |
return image
|
173 |
"""
|
174 |
|
175 |
-
|
176 |
control_image = self.make_inpaint_condition(image, mask_image)
|
177 |
|
178 |
-
low_threshold = 100
|
179 |
-
high_threshold = 200
|
180 |
-
|
181 |
-
# generate a first version of the prompt for the canny image
|
182 |
-
gen_canny_img = self.canny_pipe(prompt).images[0]
|
183 |
-
|
184 |
-
gen_canny_img = np.array(gen_canny_img)
|
185 |
-
|
186 |
-
help_image = cv2.Canny(gen_canny_img, low_threshold, high_threshold)
|
187 |
-
|
188 |
-
# get bounding box from selected area in mask image
|
189 |
-
# make help_image fit exactly into the bounding box
|
190 |
-
# create black image with canny edges only in the selected area
|
191 |
-
|
192 |
-
# get bounding box from selected area in mask image
|
193 |
-
mask_image = np.array(mask_image)
|
194 |
-
mask_image = cv2.cvtColor(mask_image, cv2.COLOR_RGB2GRAY)
|
195 |
-
mask_image = cv2.bitwise_not(mask_image)
|
196 |
-
contours, _ = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
197 |
-
x, y, w, h = cv2.boundingRect(contours[0])
|
198 |
-
|
199 |
-
# create a completely black image with the same size as the mask image
|
200 |
-
black_image = np.zeros_like(mask_image)
|
201 |
-
|
202 |
-
# put the canny edges into the black image but resize it to the bounding box
|
203 |
-
help_image = cv2.resize(help_image, (w, h))
|
204 |
-
black_image[y:y+h, x:x+w] = help_image
|
205 |
-
|
206 |
-
canny_image = Image.fromarray(black_image)
|
207 |
-
|
208 |
-
input_images = [canny_image.resize((1024, 1024)), image.resize((1024, 1024))]
|
209 |
|
210 |
# generate image
|
211 |
image = self.pipe(
|
212 |
-
prompt,
|
213 |
-
|
214 |
negative_prompt=negative_prompt,
|
215 |
num_inference_steps=num_inference_steps,
|
216 |
eta=1.0,
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
strength=strength,
|
221 |
-
controlnet_conditioning_scale=[0.8, 1.0]
|
222 |
).images[0]
|
223 |
|
224 |
return image
|
|
|
25 |
# )
|
26 |
# self.smooth_pipe.to("cuda")
|
27 |
|
28 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
self.controlnet = ControlNetModel.from_pretrained(
|
30 |
"lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16
|
31 |
)
|
32 |
+
|
33 |
|
34 |
self.pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
35 |
+
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1", controlnet=self.controlnets, torch_dtype=torch.float16
|
36 |
)
|
37 |
|
38 |
self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)
|
|
|
157 |
return image
|
158 |
"""
|
159 |
|
|
|
160 |
control_image = self.make_inpaint_condition(image, mask_image)
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
# generate image
|
164 |
image = self.pipe(
|
165 |
+
prompt=prompt,
|
166 |
+
image=image,
|
167 |
negative_prompt=negative_prompt,
|
168 |
num_inference_steps=num_inference_steps,
|
169 |
eta=1.0,
|
170 |
+
mask_image=mask_image,
|
171 |
+
control_image=control_image,
|
172 |
+
guidance_scale=guidance_scale,
|
173 |
strength=strength,
|
|
|
174 |
).images[0]
|
175 |
|
176 |
return image
|