Spaces:
Running
Running
israelweiss
commited on
Commit
•
46c6241
1
Parent(s):
601d127
redundant code, max width/height
Browse files
app.py
CHANGED
@@ -37,62 +37,6 @@ def eraser_api_call(image_base64_file, mask_base64_file, mask_type):
|
|
37 |
|
38 |
return res_image
|
39 |
|
40 |
-
ratios_map = {
|
41 |
-
0.5:{"width":704,"height":1408},
|
42 |
-
0.57:{"width":768,"height":1344},
|
43 |
-
0.68:{"width":832,"height":1216},
|
44 |
-
0.72:{"width":832,"height":1152},
|
45 |
-
0.78:{"width":896,"height":1152},
|
46 |
-
0.82:{"width":896,"height":1088},
|
47 |
-
0.88:{"width":960,"height":1088},
|
48 |
-
0.94:{"width":960,"height":1024},
|
49 |
-
1.00:{"width":1024,"height":1024},
|
50 |
-
1.13:{"width":1088,"height":960},
|
51 |
-
1.21:{"width":1088,"height":896},
|
52 |
-
1.29:{"width":1152,"height":896},
|
53 |
-
1.38:{"width":1152,"height":832},
|
54 |
-
1.46:{"width":1216,"height":832},
|
55 |
-
1.67:{"width":1280,"height":768},
|
56 |
-
1.75:{"width":1344,"height":768},
|
57 |
-
2.00:{"width":1408,"height":704}
|
58 |
-
}
|
59 |
-
ratios = np.array(list(ratios_map.keys()))
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
def get_masked_image(image, image_mask, width, height):
|
64 |
-
image_mask = image_mask # inpaint area is white
|
65 |
-
image_mask = image_mask.resize((width, height)) # object to remove is white (1)
|
66 |
-
image_mask_pil = image_mask
|
67 |
-
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
|
68 |
-
image_mask = np.array(image_mask_pil.convert("L")).astype(np.float32) / 255.0
|
69 |
-
assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
|
70 |
-
masked_image_to_present = image.copy()
|
71 |
-
masked_image_to_present[image_mask > 0.5] = (0.5,0.5,0.5) # set as masked pixel
|
72 |
-
image[image_mask > 0.5] = 0.5 # set as masked pixel - s.t. will be grey
|
73 |
-
image = Image.fromarray((image * 255.0).astype(np.uint8))
|
74 |
-
masked_image_to_present = Image.fromarray((masked_image_to_present * 255.0).astype(np.uint8))
|
75 |
-
return image, image_mask_pil, masked_image_to_present
|
76 |
-
|
77 |
-
|
78 |
-
def get_size(init_image):
|
79 |
-
w,h=init_image.size
|
80 |
-
curr_ratio = w/h
|
81 |
-
ind = np.argmin(np.abs(curr_ratio-ratios))
|
82 |
-
ratio = ratios[ind]
|
83 |
-
chosen_ratio = ratios_map[ratio]
|
84 |
-
w,h = chosen_ratio['width'], chosen_ratio['height']
|
85 |
-
|
86 |
-
return w,h
|
87 |
-
|
88 |
-
|
89 |
-
def read_content(file_path: str) -> str:
|
90 |
-
"""read the content of target file
|
91 |
-
"""
|
92 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
93 |
-
content = f.read()
|
94 |
-
|
95 |
-
return content
|
96 |
|
97 |
def predict(dict):
|
98 |
|
@@ -162,13 +106,16 @@ with image_blocks as demo:
|
|
162 |
''')
|
163 |
with gr.Row():
|
164 |
with gr.Column():
|
165 |
-
image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
|
|
|
|
|
|
|
166 |
with gr.Row(elem_id="prompt-container", equal_height=True):
|
167 |
with gr.Column(): # Wrap the button inside a Column
|
168 |
btn = gr.Button("Erase!", elem_id="run_button")
|
169 |
|
170 |
with gr.Column():
|
171 |
-
image_out = gr.Image(label="Output", elem_id="output-img",
|
172 |
|
173 |
# Button click will trigger the inpainting function (no prompt required)
|
174 |
btn.click(fn=predict, inputs=[image], outputs=[image_out], api_name='run')
|
|
|
37 |
|
38 |
return res_image
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
def predict(dict):
|
42 |
|
|
|
106 |
''')
|
107 |
with gr.Row():
|
108 |
with gr.Column():
|
109 |
+
image = gr.ImageEditor(sources=["upload"], layers=False, transforms=[],
|
110 |
+
brush=gr.Brush(colors=["#000000"], color_mode="fixed"),
|
111 |
+
max_width=512, max_height=512
|
112 |
+
)
|
113 |
with gr.Row(elem_id="prompt-container", equal_height=True):
|
114 |
with gr.Column(): # Wrap the button inside a Column
|
115 |
btn = gr.Button("Erase!", elem_id="run_button")
|
116 |
|
117 |
with gr.Column():
|
118 |
+
image_out = gr.Image(label="Output", elem_id="output-img", max_width=512, max_height=512)
|
119 |
|
120 |
# Button click will trigger the inpainting function (no prompt required)
|
121 |
btn.click(fn=predict, inputs=[image], outputs=[image_out], api_name='run')
|