Akjava commited on
Commit
1213d9c
·
1 Parent(s): 819f556

add dilate

Browse files
Files changed (2) hide show
  1. app.py +8 -7
  2. opencvinpaint.py +13 -4
app.py CHANGED
@@ -13,7 +13,7 @@ def sanitize_prompt(prompt):
13
  return sanitized_prompt
14
 
15
  #@spaces.GPU(duration=120)
16
- def process_images(image, image2=None,inpaint_radius=3,blur_radius=25,edge_expand=8,inpaint_mode="Telea",progress=gr.Progress(track_tqdm=True)):
17
  progress(0, desc="Start Inpainting")
18
  #print("process_images")
19
  # I'm not sure when this happen
@@ -40,9 +40,9 @@ def process_images(image, image2=None,inpaint_radius=3,blur_radius=25,edge_expan
40
  pass
41
  #print("size ok")
42
 
43
- output = opencvinpaint.process_cvinpaint(image["background"],mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode)
44
 
45
- return output,mask
46
 
47
 
48
  # code from https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/blob/main/app.py
@@ -96,9 +96,10 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
96
  image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask_Upload",height=400, value=None)
97
  with gr.Accordion(label="Advanced Settings", open=False):
98
  with gr.Row( equal_height=True):
99
- inpaint_radius = gr.Number(value=3, minimum=1.0, maximum=20.0, step=1, label="Inpaint Radius")
100
- blur_radius = gr.Number(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius")
101
- edge_expand = gr.Number(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand")
 
102
  with gr.Row(equal_height=True):
103
  modes = ["Telea", "Navier-Stokes"]
104
  inpaint_mode = gr.Dropdown(label="modes", choices=modes, value="Telea")
@@ -108,7 +109,7 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
108
 
109
 
110
 
111
- btn.click(fn=process_images, inputs=[image, image_mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode], outputs =[image_out,mask_out], api_name='infer')
112
  gr.Examples(
113
  examples=[["examples/00207245.jpg", "examples/00207245_mask.jpg","examples/00207245.webp"],
114
  ["examples/00047689.jpg", "examples/00047689_mask.jpg","examples/00047689.webp"]]
 
13
  return sanitized_prompt
14
 
15
  #@spaces.GPU(duration=120)
16
+ def process_images(image, image2=None,inpaint_radius=3,blur_radius=25,edge_expand=8,inpaint_mode="Telea",dilate=0,progress=gr.Progress(track_tqdm=True)):
17
  progress(0, desc="Start Inpainting")
18
  #print("process_images")
19
  # I'm not sure when this happen
 
40
  pass
41
  #print("size ok")
42
 
43
+ output,cvmask = opencvinpaint.process_cvinpaint(image["background"],mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode,dilate)
44
 
45
+ return output,cvmask
46
 
47
 
48
  # code from https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/blob/main/app.py
 
96
  image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask_Upload",height=400, value=None)
97
  with gr.Accordion(label="Advanced Settings", open=False):
98
  with gr.Row( equal_height=True):
99
+ inpaint_radius = gr.Slider(value=3, minimum=1.0, maximum=20.0, step=1, label="Inpaint Radius",info="increse become slow but smooth")
100
+ blur_radius = gr.Slider(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius",info="not bluar mask,inner blur")
101
+ edge_expand = gr.Slider(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand",info="not mask extend,for smooth mix")
102
+ dilate = gr.Slider(value=0, minimum=0.0, maximum=40.0, step=1, label="Dilate",info="extend mask,but make slow")
103
  with gr.Row(equal_height=True):
104
  modes = ["Telea", "Navier-Stokes"]
105
  inpaint_mode = gr.Dropdown(label="modes", choices=modes, value="Telea")
 
109
 
110
 
111
 
112
+ btn.click(fn=process_images, inputs=[image, image_mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode,dilate], outputs =[image_out,mask_out], api_name='infer')
113
  gr.Examples(
114
  examples=[["examples/00207245.jpg", "examples/00207245_mask.jpg","examples/00207245.webp"],
115
  ["examples/00047689.jpg", "examples/00047689_mask.jpg","examples/00047689.webp"]]
opencvinpaint.py CHANGED
@@ -43,17 +43,26 @@ def blend_rgb_images(image1: np.ndarray, image2: np.ndarray, mask: np.ndarray) -
43
 
44
  return blended.astype(np.uint8)
45
 
46
- def process_cvinpaint(image,mask_image,inpaint_radius,blur_radius,edge_expand,inpaint_mode):
47
  #print("process cvinpaint")
48
  #print(blur_radius,",",edge_expand)
49
  cv_image = pil_to_cv(image)
50
 
51
  cv_mask = pil_to_cv(mask_image)
 
 
 
 
52
  cv_gray = cv2.cvtColor(cv_mask,cv2.COLOR_BGR2GRAY)
 
53
 
54
  mask = gray3d_to_2d(cv_gray)
55
- cv2.imwrite("_mask.jpg",mask)
56
- cv2.imwrite("_image.jpg",cv_image)
 
 
 
 
57
  mode = cv2.INPAINT_TELEA if inpaint_mode == "Telea" else cv2.INPAINT_NS
58
  img_inpainted = cv2.inpaint(cv_image, mask,inpaint_radius, mode)
59
  if debug:
@@ -88,7 +97,7 @@ def process_cvinpaint(image,mask_image,inpaint_radius,blur_radius,edge_expand,in
88
  if output_image.shape[2] == 3: # カラー
89
  output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
90
 
91
- return Image.fromarray(output_image)
92
 
93
  if __name__ == "__main__":
94
  image = Image.open(sys.argv[1])
 
43
 
44
  return blended.astype(np.uint8)
45
 
46
+ def process_cvinpaint(image,mask_image,inpaint_radius,blur_radius,edge_expand,inpaint_mode,dilate=0):
47
  #print("process cvinpaint")
48
  #print(blur_radius,",",edge_expand)
49
  cv_image = pil_to_cv(image)
50
 
51
  cv_mask = pil_to_cv(mask_image)
52
+
53
+
54
+
55
+
56
  cv_gray = cv2.cvtColor(cv_mask,cv2.COLOR_BGR2GRAY)
57
+
58
 
59
  mask = gray3d_to_2d(cv_gray)
60
+ if dilate>0:
61
+ kernel = np.ones((dilate, dilate), np.uint8)
62
+ mask = cv2.dilate(mask, kernel, iterations=1)
63
+
64
+ #cv2.imwrite("_mask.jpg",mask)
65
+ #cv2.imwrite("_image.jpg",cv_image)
66
  mode = cv2.INPAINT_TELEA if inpaint_mode == "Telea" else cv2.INPAINT_NS
67
  img_inpainted = cv2.inpaint(cv_image, mask,inpaint_radius, mode)
68
  if debug:
 
97
  if output_image.shape[2] == 3: # カラー
98
  output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
99
 
100
+ return Image.fromarray(output_image),Image.fromarray(mask)
101
 
102
  if __name__ == "__main__":
103
  image = Image.open(sys.argv[1])