ghoskno commited on
Commit
7db9719
1 Parent(s): 1f7767c

update: add color inpainting feature

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -66,16 +66,10 @@ def resize_image(input_image, resolution, max_edge=False, edge_limit=False):
66
  pimg[oH:oH+H, oW:oW+W] = img
67
  return pimg
68
 
69
- def get_canny_filter(image, format='pil', low_threshold=100, high_threshold=200):
70
-
71
- if not isinstance(image, np.ndarray):
72
- image = np.array(image)
73
-
74
  image = cv2.Canny(image, low_threshold, high_threshold)
75
  image = image[:, :, None]
76
  image = np.concatenate([image, image, image], axis=2)
77
- if format == 'pil':
78
- image = Image.fromarray(image)
79
  return image
80
 
81
  def get_color_filter(cond_image, mask_size=64):
@@ -85,16 +79,11 @@ def get_color_filter(cond_image, mask_size=64):
85
  return color
86
 
87
  def get_colorcanny(image, mask_size):
88
-
89
- if not isinstance(image, np.ndarray):
90
- image = np.array(image)
91
-
92
- canny_img = get_canny_filter(image, format='np')
93
 
94
  color_img = get_color_filter(image, int(mask_size))
95
 
96
  color_img[np.where(canny_img > 128)] = 255
97
- color_img = Image.fromarray(color_img)
98
  return color_img
99
 
100
  def process(input_image, prompt, n_prompt, strength=1.0, color_mask_size=96, size=512, scale=6.0, ddim_steps=20):
@@ -102,6 +91,35 @@ def process(input_image, prompt, n_prompt, strength=1.0, color_mask_size=96, siz
102
  input_image = resize_image(input_image, size, max_edge=True, edge_limit=True)
103
 
104
  cond_img = get_colorcanny(input_image, color_mask_size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  output = pipe(
106
  prompt_embeds=prompt_embeds,
107
  negative_prompt_embeds=negative_prompt_embeds,
@@ -130,9 +148,12 @@ with block:
130
  with gr.Row():
131
  with gr.Column():
132
  input_image = gr.Image(source='upload', type="numpy")
 
133
  prompt = gr.Textbox(label="Prompt", value='')
134
  n_prompt = gr.Textbox(label="Negative Prompt", value='')
135
- run_button = gr.Button(label="Run")
 
 
136
  with gr.Accordion('Advanced', open=False):
137
  strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
138
  color_mask_size = gr.Slider(label="Color Mask Size", minimum=32, maximum=256, value=96, step=16)
@@ -144,6 +165,8 @@ with block:
144
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
145
  ips = [input_image, prompt, n_prompt, strength, color_mask_size, size, scale, ddim_steps]
146
  run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
 
 
147
 
148
  gr.Examples(
149
  examples=[
 
66
  pimg[oH:oH+H, oW:oW+W] = img
67
  return pimg
68
 
69
+ def get_canny_filter(image, low_threshold=100, high_threshold=200):
 
 
 
 
70
  image = cv2.Canny(image, low_threshold, high_threshold)
71
  image = image[:, :, None]
72
  image = np.concatenate([image, image, image], axis=2)
 
 
73
  return image
74
 
75
  def get_color_filter(cond_image, mask_size=64):
 
79
  return color
80
 
81
  def get_colorcanny(image, mask_size):
82
+ canny_img = get_canny_filter(image)
 
 
 
 
83
 
84
  color_img = get_color_filter(image, int(mask_size))
85
 
86
  color_img[np.where(canny_img > 128)] = 255
 
87
  return color_img
88
 
89
  def process(input_image, prompt, n_prompt, strength=1.0, color_mask_size=96, size=512, scale=6.0, ddim_steps=20):
 
91
  input_image = resize_image(input_image, size, max_edge=True, edge_limit=True)
92
 
93
  cond_img = get_colorcanny(input_image, color_mask_size)
94
+ cond_img = Image.fromarray(cond_img)
95
+ output = pipe(
96
+ prompt_embeds=prompt_embeds,
97
+ negative_prompt_embeds=negative_prompt_embeds,
98
+ image=cond_img,
99
+ generator=generator,
100
+ num_images_per_prompt=1,
101
+ num_inference_steps=ddim_steps,
102
+ guidance_scale=scale,
103
+ controlnet_conditioning_scale=float(strength)
104
+ )
105
+ return [output.images[0], cond_img]
106
+
107
+
108
+ def inpaint_process(inpaint_image, input_image, prompt, n_prompt, strength=1.0, color_mask_size=96, size=512, scale=6.0, ddim_steps=20):
109
+ if inpaint_image is None:
110
+ return process(input_image, prompt, n_prompt, strength, color_mask_size, size, scale, ddim_steps)
111
+
112
+ prompt_embeds, negative_prompt_embeds = _encode_prompt(pipe, prompt, pipe.device, 1, True, n_prompt, 3)
113
+ input_image = resize_image(input_image, size, max_edge=True, edge_limit=True)
114
+ inpaint_image = resize_image(inpaint_image, size, max_edge=True, edge_limit=True)
115
+
116
+ canny_img = get_canny_filter(input_image)
117
+
118
+ color_img = get_color_filter(inpaint_image, int(color_mask_size))
119
+
120
+ color_img[np.where(canny_img > 128)] = 255
121
+ cond_img = Image.fromarray(color_img)
122
+
123
  output = pipe(
124
  prompt_embeds=prompt_embeds,
125
  negative_prompt_embeds=negative_prompt_embeds,
 
148
  with gr.Row():
149
  with gr.Column():
150
  input_image = gr.Image(source='upload', type="numpy")
151
+ color_image = gr.ImagePaint(type="numpy")
152
  prompt = gr.Textbox(label="Prompt", value='')
153
  n_prompt = gr.Textbox(label="Negative Prompt", value='')
154
+ with gr.Row():
155
+ run_button = gr.Button(label="Run")
156
+ run_edit_button = gr.Button(value='Run with inpaint color', label="Run with inpaint color")
157
  with gr.Accordion('Advanced', open=False):
158
  strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
159
  color_mask_size = gr.Slider(label="Color Mask Size", minimum=32, maximum=256, value=96, step=16)
 
165
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
166
  ips = [input_image, prompt, n_prompt, strength, color_mask_size, size, scale, ddim_steps]
167
  run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
168
+ run_edit_button.click(fn=inpaint_process, inputs=[color_image] + ips, outputs=[result_gallery])
169
+
170
 
171
  gr.Examples(
172
  examples=[