Akjava commited on
Commit
7c0ca8f
1 Parent(s): 4d133c8
Files changed (4) hide show
  1. _image.jpg +0 -0
  2. _mask.jpg +0 -0
  3. app.py +43 -23
  4. opencvinpaint.py +3 -2
_image.jpg ADDED
_mask.jpg ADDED
app.py CHANGED
@@ -13,21 +13,28 @@ def sanitize_prompt(prompt):
13
  return sanitized_prompt
14
 
15
  #@spaces.GPU(duration=120)
16
- def process_images(image1, image2,inpaint_radius,blur_radius,edge_expand):
17
- if image1!=None:
18
- pass
19
- #image1.save("input.jpg","JPEG")
20
- if image2!= None:
21
-
22
- pass
23
- #image2.save("mask.jpg","JPEG")
24
- if image1!=None and image2!=None:
25
-
26
- output = opencvinpaint.process_image(image1,image2,inpaint_radius,blur_radius,edge_expand)
 
 
 
 
 
 
 
 
27
 
28
- return output
29
 
30
- return None
31
 
32
  # code from https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/blob/main/app.py
33
  def read_file(file_path: str) -> str:
@@ -38,30 +45,43 @@ def read_file(file_path: str) -> str:
38
 
39
  return content
40
 
 
 
 
 
 
 
 
 
 
 
 
41
  #css=css,
42
- demo_blocks = gr.Blocks( elem_id="demo-container")
43
  with demo_blocks as demo:
44
- gr.HTML(read_file("demo_header.html"))
 
45
  with gr.Row():
46
  with gr.Column():
47
- image = gr.Image(sources=['upload','clipboard'], elem_id="image_upload", type="pil", label="Upload",height=400)
48
- with gr.Row(elem_id="prompt-container", equal_height=True):
49
  with gr.Row():
50
  btn = gr.Button("Inpaint!", elem_id="run_button")
51
- image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask_Upload",height=400)
52
  with gr.Accordion(label="Advanced Settings", open=False):
53
  with gr.Row( equal_height=True):
54
  inpaint_radius = gr.Number(value=3, minimum=1.0, maximum=20.0, step=1, label="Inpaint Radius")
55
  blur_radius = gr.Number(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius")
56
  edge_expand = gr.Number(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand")
57
-
 
 
58
  with gr.Column():
59
- image_out = gr.Image(sources=[],label="Output", elem_id="output-img", height=400)
60
- with gr.Group(elem_id="share-btn-container", visible=False) as share_btn_container:
61
- pass
62
 
63
 
64
- btn.click(fn=process_images, inputs=[image, image_mask,inpaint_radius,blur_radius,edge_expand], outputs =image_out, api_name='run')
65
  gr.Examples(
66
  examples=[["examples/street.jpg", "examples/street_mask.jpg"]]
67
  ,
 
13
  return sanitized_prompt
14
 
15
  #@spaces.GPU(duration=120)
16
+ def process_images(image1, image2=None,inpaint_radius=3,blur_radius=25,edge_expand=8,inpaint_mode="Telea"):
17
+ # I'm not sure when this happen
18
+ if not isinstance(image1, dict):
19
+ if image2 == None:
20
+ print("empty mask")
21
+ return image1
22
+ else:
23
+ image1 = dict({'background': image1, 'layers': [image2]})
24
+
25
+ if image2!=None:
26
+ mask = image2
27
+ else:
28
+ if len(image1['layers']) == 0:
29
+ print("empty mask")
30
+ return image
31
+ mask = image1['layers'][0]
32
+
33
+
34
+ output = opencvinpaint.process_image(image1["background"],mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode)
35
 
36
+ return output
37
 
 
38
 
39
  # code from https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/blob/main/app.py
40
  def read_file(file_path: str) -> str:
 
45
 
46
  return content
47
 
48
+ css="""
49
+ #col-left {
50
+ margin: 0 auto;
51
+ max-width: 640px;
52
+ }
53
+ #col-right {
54
+ margin: 0 auto;
55
+ max-width: 640px;
56
+ }
57
+ """
58
+
59
  #css=css,
60
+ demo_blocks = gr.Blocks(css=css, elem_id="demo-container")
61
  with demo_blocks as demo:
62
+ with gr.Column():
63
+ gr.HTML(read_file("demo_header.html"))
64
  with gr.Row():
65
  with gr.Column():
66
+ image = gr.ImageEditor(sources=['upload','clipboard'],transforms=[],image_mode='RGB', layers=False, elem_id="image_upload", type="pil", label="Upload",brush=gr.Brush(colors=["#333"], color_mode="fixed"))
67
+ with gr.Row(elem_id="prompt-container", equal_height=False):
68
  with gr.Row():
69
  btn = gr.Button("Inpaint!", elem_id="run_button")
70
+ image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask_Upload",height=400, value=None)
71
  with gr.Accordion(label="Advanced Settings", open=False):
72
  with gr.Row( equal_height=True):
73
  inpaint_radius = gr.Number(value=3, minimum=1.0, maximum=20.0, step=1, label="Inpaint Radius")
74
  blur_radius = gr.Number(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius")
75
  edge_expand = gr.Number(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand")
76
+ with gr.Row(equal_height=True):
77
+ modes = ["Telea", "Navier-Stokes"]
78
+ inpaint_mode = gr.Dropdown(label="modes", choices=modes, value="Telea")
79
  with gr.Column():
80
+ image_out = gr.Image(sources=[],label="Output", elem_id="output-img")
81
+
 
82
 
83
 
84
+ btn.click(fn=process_images, inputs=[image, image_mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode], outputs =image_out, api_name='run')
85
  gr.Examples(
86
  examples=[["examples/street.jpg", "examples/street_mask.jpg"]]
87
  ,
opencvinpaint.py CHANGED
@@ -43,7 +43,7 @@ def blend_rgb_images(image1: np.ndarray, image2: np.ndarray, mask: np.ndarray) -
43
 
44
  return blended.astype(np.uint8)
45
 
46
- def process_image(image,mask_image,inpaint_radius,blur_radius,edge_expand):
47
  #print(blur_radius,",",edge_expand)
48
  cv_image = pil_to_cv(image)
49
 
@@ -53,7 +53,8 @@ def process_image(image,mask_image,inpaint_radius,blur_radius,edge_expand):
53
  mask = gray3d_to_2d(cv_gray)
54
  cv2.imwrite("_mask.jpg",mask)
55
  cv2.imwrite("_image.jpg",cv_image)
56
- img_inpainted = cv2.inpaint(cv_image, mask,inpaint_radius, cv2.INPAINT_TELEA)
 
57
  if debug:
58
  cv2.imwrite("close_eye_inpaint.jpg",img_inpainted)
59
 
 
43
 
44
  return blended.astype(np.uint8)
45
 
46
+ def process_image(image,mask_image,inpaint_radius,blur_radius,edge_expand,inpaint_mode):
47
  #print(blur_radius,",",edge_expand)
48
  cv_image = pil_to_cv(image)
49
 
 
53
  mask = gray3d_to_2d(cv_gray)
54
  cv2.imwrite("_mask.jpg",mask)
55
  cv2.imwrite("_image.jpg",cv_image)
56
+ mode = cv2.INPAINT_TELEA if inpaint_mode == "Telea" else cv2.INPAINT_NS
57
+ img_inpainted = cv2.inpaint(cv_image, mask,inpaint_radius, mode)
58
  if debug:
59
  cv2.imwrite("close_eye_inpaint.jpg",img_inpainted)
60