sachinkidzure AdityA36912355 commited on
Commit
c7e56cd
·
verified ·
1 Parent(s): 60ca30a

Update app.py (#1)

Browse files

- Update app.py (be111dd97bcb3fba3def0a8384de64a5f118b7cd)


Co-authored-by: Aditya Deshmukh <[email protected]>

Files changed (1) hide show
  1. app.py +17 -108
app.py CHANGED
@@ -95,17 +95,14 @@ def get_masked_img(img, w, h, features, orig_h, orig_w, input_h, input_w, dilate
95
  return *figs, *masks
96
 
97
 
98
- def get_inpainted_img(img, mask0, mask1, mask2):
99
  lama_config = args.lama_config
100
  device = "cuda" if torch.cuda.is_available() else "cpu"
101
- out = []
102
- for mask in [mask0, mask1, mask2]:
103
- if len(mask.shape)==3:
104
- mask = mask[:,:,0]
105
- img_inpainted = inpaint_img_with_builded_lama(
106
- model['lama'], img, mask, lama_config, device=device)
107
- out.append(img_inpainted)
108
- return out
109
 
110
 
111
  # get args
@@ -128,104 +125,16 @@ lama_ckpt = args.lama_ckpt
128
  device = "cuda" if torch.cuda.is_available() else "cpu"
129
  model['lama'] = build_lama_model(lama_config, lama_ckpt, device=device)
130
 
131
- button_size = (100,50)
132
- with gr.Blocks() as demo:
133
- features = gr.State(None)
134
- orig_h = gr.State(None)
135
- orig_w = gr.State(None)
136
- input_h = gr.State(None)
137
- input_w = gr.State(None)
138
-
139
- with gr.Row().style(mobile_collapse=False, equal_height=True):
140
- with gr.Column(variant="panel"):
141
- with gr.Row():
142
- gr.Markdown("## Input Image")
143
- with gr.Row():
144
- img = gr.Image(label="Input Image").style(height="200px")
145
- with gr.Column(variant="panel"):
146
- with gr.Row():
147
- gr.Markdown("## Pointed Image")
148
- with gr.Row():
149
- img_pointed = gr.Plot(label='Pointed Image')
150
- with gr.Column(variant="panel"):
151
- with gr.Row():
152
- gr.Markdown("## Control Panel")
153
- with gr.Row():
154
- w = gr.Number(label="Point Coordinate W")
155
- h = gr.Number(label="Point Coordinate H")
156
- dilate_kernel_size = gr.Slider(label="Dilate Kernel Size", minimum=0, maximum=100, step=1, value=15)
157
- sam_mask = gr.Button("Predict Mask", variant="primary").style(full_width=True, size="sm")
158
- lama = gr.Button("Inpaint Image", variant="primary").style(full_width=True, size="sm")
159
- clear_button_image = gr.Button(value="Reset", label="Reset", variant="secondary").style(full_width=True, size="sm")
160
-
161
- # todo: maybe we can delete this row, for it's unnecessary to show the original mask for customers
162
- with gr.Row(variant="panel"):
163
- with gr.Column():
164
- with gr.Row():
165
- gr.Markdown("## Segmentation Mask")
166
- with gr.Row():
167
- mask_0 = gr.outputs.Image(type="numpy", label="Segmentation Mask 0").style(height="200px")
168
- mask_1 = gr.outputs.Image(type="numpy", label="Segmentation Mask 1").style(height="200px")
169
- mask_2 = gr.outputs.Image(type="numpy", label="Segmentation Mask 2").style(height="200px")
170
-
171
- with gr.Row(variant="panel"):
172
- with gr.Column():
173
- with gr.Row():
174
- gr.Markdown("## Image with Mask")
175
- with gr.Row():
176
- img_with_mask_0 = gr.Plot(label="Image with Segmentation Mask 0")
177
- img_with_mask_1 = gr.Plot(label="Image with Segmentation Mask 1")
178
- img_with_mask_2 = gr.Plot(label="Image with Segmentation Mask 2")
179
-
180
- with gr.Row(variant="panel"):
181
- with gr.Column():
182
- with gr.Row():
183
- gr.Markdown("## Image Removed with Mask")
184
- with gr.Row():
185
- img_rm_with_mask_0 = gr.outputs.Image(
186
- type="numpy", label="Image Removed with Segmentation Mask 0").style(height="200px")
187
- img_rm_with_mask_1 = gr.outputs.Image(
188
- type="numpy", label="Image Removed with Segmentation Mask 1").style(height="200px")
189
- img_rm_with_mask_2 = gr.outputs.Image(
190
- type="numpy", label="Image Removed with Segmentation Mask 2").style(height="200px")
191
-
192
-
193
- def get_select_coords(img, evt: gr.SelectData):
194
- dpi = plt.rcParams['figure.dpi']
195
- height, width = img.shape[:2]
196
- fig = plt.figure(figsize=(width/dpi/0.77, height/dpi/0.77))
197
- plt.imshow(img)
198
- plt.axis('off')
199
- plt.tight_layout()
200
- show_points(plt.gca(), [[evt.index[0], evt.index[1]]], [1],
201
- size=(width*0.04)**2)
202
- return evt.index[0], evt.index[1], fig
203
-
204
- img.select(get_select_coords, [img], [w, h, img_pointed])
205
- img.upload(get_sam_feat, [img], [features, orig_h, orig_w, input_h, input_w])
206
-
207
- sam_mask.click(
208
- get_masked_img,
209
- [img, w, h, features, orig_h, orig_w, input_h, input_w, dilate_kernel_size],
210
- [img_with_mask_0, img_with_mask_1, img_with_mask_2, mask_0, mask_1, mask_2]
211
- )
212
-
213
- lama.click(
214
- get_inpainted_img,
215
- [img, mask_0, mask_1, mask_2],
216
- [img_rm_with_mask_0, img_rm_with_mask_1, img_rm_with_mask_2]
217
- )
218
-
219
-
220
- def reset(*args):
221
- return [None for _ in args]
222
-
223
- clear_button_image.click(
224
- reset,
225
- [img, features, img_pointed, w, h, mask_0, mask_1, mask_2, img_with_mask_0, img_with_mask_1, img_with_mask_2, img_rm_with_mask_0, img_rm_with_mask_1, img_rm_with_mask_2],
226
- [img, features, img_pointed, w, h, mask_0, mask_1, mask_2, img_with_mask_0, img_with_mask_1, img_with_mask_2, img_rm_with_mask_0, img_rm_with_mask_1, img_rm_with_mask_2]
227
- )
228
 
229
  if __name__ == "__main__":
230
- demo.launch()
231
-
 
95
  return *figs, *masks
96
 
97
 
98
+ def get_inpainted_img(img,mask):
99
  lama_config = args.lama_config
100
  device = "cuda" if torch.cuda.is_available() else "cpu"
101
+ if len(mask.shape)==3:
102
+ mask = mask[:,:,0]
103
+ img_inpainted = inpaint_img_with_builded_lama(
104
+ model['lama'], img, mask, lama_config, device=device)
105
+ return img_inpainted
 
 
 
106
 
107
 
108
  # get args
 
125
  device = "cuda" if torch.cuda.is_available() else "cpu"
126
  model['lama'] = build_lama_model(lama_config, lama_ckpt, device=device)
127
 
128
+ image_input = gr.Image(label="Input Image")
129
+ mask_input = gr.Image(label="Mask Image")
130
+ demo = gr.Interface(
131
+ fn=get_inpainted_img,
132
+ inputs=[image_input, mask_input],
133
+ outputs=gr.Image(type="numpy", label="Output Image"),
134
+ title="Image and Mask Processor",
135
+ description="Upload an image and a mask to process the image. The mask highlights the areas to be processed.",
136
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  if __name__ == "__main__":
139
+ demo.queue(api_open=True, concurrency_count=2, max_size=10)
140
+ demo.launch(show_api=True)