Instructions to use karimbenharrak/stable-diffusion-inpainting-with-handler with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use karimbenharrak/stable-diffusion-inpainting-with-handler with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import AutoPipelineForInpainting from diffusers.utils import load_image # switch to "mps" for apple devices pipe = AutoPipelineForInpainting.from_pretrained("karimbenharrak/stable-diffusion-inpainting-with-handler", dtype=torch.float16, device_map="cuda") img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" image = load_image(img_url).resize((1024, 1024)) mask_image = load_image(mask_url).resize((1024, 1024)) prompt = "a tiger sitting on a park bench" generator = torch.Generator(device="cuda").manual_seed(0) image = pipe( prompt=prompt, image=image, mask_image=mask_image, guidance_scale=8.0, num_inference_steps=20, # steps between 15 and 30 work well for us strength=0.99, # make sure to use `strength` below 1.0 generator=generator, ).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Update handler.py
Browse files- handler.py +21 -4
handler.py
CHANGED
|
@@ -184,9 +184,26 @@ class EndpointHandler():
|
|
| 184 |
gen_canny_img = np.array(gen_canny_img)
|
| 185 |
|
| 186 |
help_image = cv2.Canny(gen_canny_img, low_threshold, high_threshold)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
input_images = [canny_image.resize((1024, 1024)), image.resize((1024, 1024))]
|
| 192 |
|
|
@@ -222,4 +239,4 @@ class EndpointHandler():
|
|
| 222 |
image[image_mask > 0.5] = -1.0 # set as masked pixel
|
| 223 |
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
|
| 224 |
image = torch.from_numpy(image)
|
| 225 |
-
return image
|
|
|
|
| 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 |
|
|
|
|
| 239 |
image[image_mask > 0.5] = -1.0 # set as masked pixel
|
| 240 |
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
|
| 241 |
image = torch.from_numpy(image)
|
| 242 |
+
return image
|