File size: 4,510 Bytes
f24d19f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c945b6
 
df01e43
7c0ca8f
316424f
7c0ca8f
 
316424f
7c0ca8f
316424f
7c0ca8f
 
 
 
316424f
7c0ca8f
 
316424f
7c0ca8f
8a56dc6
 
 
ba18265
c3ff188
 
04382be
7c0ca8f
3272647
f24d19f
1bb37af
f24d19f
 
 
 
 
 
 
 
 
 
 
7c0ca8f
 
 
 
 
 
 
 
 
df01e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c0ca8f
 
f24d19f
3272647
 
7c0ca8f
 
b890ee4
f24d19f
 
8a56dc6
7c0ca8f
f24d19f
 
7c0ca8f
f24d19f
 
 
 
 
7c0ca8f
 
 
f24d19f
04382be
 
7c0ca8f
f24d19f
 
fbb8f18
f24d19f
04382be
 
f24d19f
 
04382be
f24d19f
 
df01e43
f24d19f
b890ee4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import spaces
import gradio as gr
import subprocess
import re
from PIL import Image

import opencvinpaint

def sanitize_prompt(prompt):
  # Allow only alphanumeric characters, spaces, and basic punctuation
  allowed_chars = re.compile(r"[^a-zA-Z0-9\s.,!?-]")
  sanitized_prompt = allowed_chars.sub("", prompt)
  return sanitized_prompt

#@spaces.GPU(duration=120)
def process_images(image, image2=None,inpaint_radius=3,blur_radius=25,edge_expand=8,inpaint_mode="Telea",progress=gr.Progress(track_tqdm=True)):
    progress(0, desc="Start Inpainting")
    #print("process_images")
    # I'm not sure when this happen
    if not isinstance(image, dict):
        if image2 == None:
             print("empty mask")
             return image
        else:
            image = dict({'background': image, 'layers': [image2]})

    if image2!=None:
         mask = image2
    else:
         if len(image['layers']) == 0:
              print("empty mask")
              return image
         mask = image['layers'][0]

    img_width,img_height = image["background"].size
    mask_width, mask_height = mask.size
    if img_width!=mask_width or img_height!=mask_height:
         raise gr.Error(f"image size({img_width},{img_height}) must be same as mask size({mask_width},{mask_height})")
    else:
         pass
         #print("size ok")

    output = opencvinpaint.process_cvinpaint(image["background"],mask,inpaint_radius,blur_radius,edge_expand,inpaint_mode)
        
    return output,mask
    

# code from https://huggingface.co/spaces/diffusers/stable-diffusion-xl-inpainting/blob/main/app.py
def read_file(file_path: str) -> str:
    """read the text of target file
    """
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    return content

css="""
#col-left {
    margin: 0 auto;
    max-width: 640px;
}
#col-right {
    margin: 0 auto;
    max-width: 640px;
}
.grid-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap:10px
}

.image {
  width: 128px; 
  height: 128px; 
  object-fit: cover; 
}

.text {
  font-size: 16px;
}
"""

#css=css,

with gr.Blocks(css=css, elem_id="demo-container") as demo:
    with gr.Column():
        gr.HTML(read_file("demo_header.html"))
        gr.HTML(read_file("demo_tools.html"))
    with gr.Row():
                with gr.Column():
                    image = gr.ImageEditor(height=800,sources=['upload','clipboard'],transforms=[],image_mode='RGB', layers=False,  elem_id="image_upload", type="pil", label="Upload",brush=gr.Brush(colors=["#fff"], color_mode="fixed"))
                    with gr.Row(elem_id="prompt-container",  equal_height=False):
                        with gr.Row():
                            btn = gr.Button("Inpaint!", elem_id="run_button")
                    image_mask = gr.Image(sources=['upload','clipboard'],  elem_id="mask_upload", type="pil", label="Mask_Upload",height=400, value=None)
                    with gr.Accordion(label="Advanced Settings", open=False):
                        with gr.Row( equal_height=True):
                            inpaint_radius = gr.Number(value=3, minimum=1.0, maximum=20.0, step=1, label="Inpaint Radius")
                            blur_radius = gr.Number(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius")
                            edge_expand = gr.Number(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand")
                    with gr.Row(equal_height=True):
                            modes = ["Telea", "Navier-Stokes"]
                            inpaint_mode = gr.Dropdown(label="modes", choices=modes, value="Telea") 
                with gr.Column():
                    image_out = gr.Image(sources=[],label="Output", elem_id="output-img")
                    mask_out = gr.Image(sources=[],label="Mask", elem_id="mask-img",format="jpeg")
                    
            

    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')
    gr.Examples(
               examples=[["examples/00207245.jpg", "examples/00207245_mask.jpg","examples/00207245.webp"],
                         ["examples/00047689.jpg", "examples/00047689_mask.jpg","examples/00047689.webp"]]
,
                #fn=predict,
                inputs=[image,image_mask,image_out],
                cache_examples=False,
    )
    gr.HTML(read_file("demo_footer.html"))

    if __name__ == "__main__":
        demo.launch()