xinsir commited on
Commit
836a26a
1 Parent(s): 06d148d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +16 -26
README.md CHANGED
@@ -81,30 +81,19 @@ prompt: 1girl, solo, ball, swimsuit, bikini, mole, beachball, white bikini, brea
81
  Use the code below to get started with the model.
82
 
83
  ```python
 
84
  from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
85
  from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
 
 
 
 
86
  from PIL import Image
87
  import torch
88
  import numpy as np
89
  import cv2
 
90
 
91
- def HWC3(x):
92
- assert x.dtype == np.uint8
93
- if x.ndim == 2:
94
- x = x[:, :, None]
95
- assert x.ndim == 3
96
- H, W, C = x.shape
97
- assert C == 1 or C == 3 or C == 4
98
- if C == 3:
99
- return x
100
- if C == 1:
101
- return np.concatenate([x, x, x], axis=2)
102
- if C == 4:
103
- color = x[:, :, 0:3].astype(np.float32)
104
- alpha = x[:, :, 3:4].astype(np.float32) / 255.0
105
- y = color * alpha + 255.0 * (1.0 - alpha)
106
- y = y.clip(0, 255).astype(np.uint8)
107
- return y
108
 
109
  def nms(x, t, s):
110
  x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
@@ -150,22 +139,23 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
150
  )
151
 
152
  # you can use either hed to generate a fake scribble given an image or a sketch image totally draw by yourself
153
-
154
  if random.random() > 0.5:
155
  # Method 1
156
  # if you use hed, you should provide an image, the image can be real or anime, you extract its hed lines and use it as the scribbles
157
  # The detail about hed detect you can refer to https://github.com/lllyasviel/ControlNet/blob/main/gradio_fake_scribble2image.py
158
- # I provide a pseudo-code here
159
- # img = cv2.imread(img_path)
160
- # hed_img = apply_hed(img)
161
- # cv2.imwrite("a hed detect path for an image", hed_img)
162
-
163
- controlnet_img = Image.open("a hed detect path for an image")
 
 
164
  controlnet_img = np.array(controlnet_img)
165
  controlnet_img = nms(controlnet_img, 127, 3)
166
  controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
167
-
168
- # different threshold for different lines
169
  random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
170
  controlnet_img[controlnet_img > random_val] = 255
171
  controlnet_img[controlnet_img < 255] = 0
 
81
  Use the code below to get started with the model.
82
 
83
  ```python
84
+
85
  from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
86
  from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
87
+ from controlnet_aux import PidiNetDetector, HEDdetector
88
+ from diffusers.utils import load_image
89
+ from huggingface_hub import HfApi
90
+ from pathlib import Path
91
  from PIL import Image
92
  import torch
93
  import numpy as np
94
  import cv2
95
+ import os
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  def nms(x, t, s):
99
  x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
 
139
  )
140
 
141
  # you can use either hed to generate a fake scribble given an image or a sketch image totally draw by yourself
 
142
  if random.random() > 0.5:
143
  # Method 1
144
  # if you use hed, you should provide an image, the image can be real or anime, you extract its hed lines and use it as the scribbles
145
  # The detail about hed detect you can refer to https://github.com/lllyasviel/ControlNet/blob/main/gradio_fake_scribble2image.py
146
+ # Below is a example using diffusers HED detector
147
+
148
+ image_path = Image.open("your image path, the image can be real or anime, HED detector will extract its edge boundery")
149
+ processor = HEDdetector.from_pretrained('lllyasviel/Annotators')
150
+ controlnet_img = processor(image_path, scribble=False)
151
+ controlnet_img.save("a hed detect path for an image")
152
+
153
+ # following is some processing to simulate human sketch draw, different threshold can generate different width of lines
154
  controlnet_img = np.array(controlnet_img)
155
  controlnet_img = nms(controlnet_img, 127, 3)
156
  controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
157
+
158
+ # higher threshold, thiner line
159
  random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
160
  controlnet_img[controlnet_img > random_val] = 255
161
  controlnet_img[controlnet_img < 255] = 0