noni27 commited on
Commit
1afd24f
·
verified ·
1 Parent(s): 75ceb3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -67
app.py CHANGED
@@ -201,79 +201,86 @@ def main_fun(image_pil, x_units, y_units, text_prompt):
201
  output = draw_image(image_pil, masks, boxes, alpha=0.4)
202
 
203
 
204
- # '''Get masked object and background as two separate images'''
205
- # mask = np.expand_dims(masks[0], axis=-1)
206
- # masked_object = image_pil * mask
207
- # background = image_pil * ~mask
208
-
209
-
210
- # '''Shifts image by x_units and y_units'''
211
- # M = np.float32([[1, 0, x_units], [0, 1, y_units]])
212
- # shifted_image = cv2.warpAffine(masked_object, M, (masked_object.shape[1] , masked_object.shape[0] ), borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
213
- # masked_shifted_image = np.where(shifted_image[:, :, 0] != 0, True, False)
214
-
215
- # '''Load stable diffuser model at checkpoint finetuned for inpainting task'''
216
- # pipe = StableDiffusionInpaintPipeline.from_pretrained(
217
- # # "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16
218
- # "stabilityai/stable-diffusion-2-inpainting",torch_dtype=torch.float16)
219
- # pipe.to(CFG.device)
220
-
221
-
222
- # # With Dilation
223
- # structuring_element = np.ones((15, 15, 1), dtype=bool)
224
- # extrapolated_mask = binary_dilation(mask, structure=structuring_element)
225
- # mask_as_uint8 = extrapolated_mask.astype(np.uint8) * 255
226
- # pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L').resize((1024, 1024))
227
-
228
- # # # Without Dilation
229
- # # pil_background = Image.fromarray(background)
230
- # # mask_as_uint8 = mask.astype(np.uint8) * 255
231
- # # pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L')
232
-
233
- # '''Do inpainting on masked locations of original image'''
234
- # prompt = 'fill as per background and neighborhood'
235
- # inpainted_image = pipe(prompt=prompt, image=image_pil, mask_image=pil_mask).images[0]
236
- # # inpainted_image
237
-
238
- # '''Get composite of shifted object and background inpainted imaage'''
239
- # pil_shifted_image = Image.fromarray(shifted_image).resize(inpainted_image.size)
240
- # np_shifted_image = np.array(pil_shifted_image)
241
- # masked_shifted_image = np.where(np_shifted_image[:, :, 0] != 0, True, False)
242
- # masked_shifted_image = np.expand_dims(masked_shifted_image, axis=-1)
243
- # inpainted_shifted = np.array(inpainted_image) * ~masked_shifted_image
244
-
245
- # shifted_image = cv2.resize(shifted_image, inpainted_image.size)
246
- # output = inpainted_shifted + shifted_image
247
- # output = Image.fromarray(output)
248
- # # visualize_results(image_pil, output, 'shifted')
249
 
250
  return output
251
 
252
  import gradio as gr
253
 
254
- with gr.Blocks() as demo:
255
- gr.Markdown("Segmentation and shift")
256
- with gr.Tab("Txt2Img"):
257
- with gr.Row():
258
- with gr.Column(scale=1):
259
- # image_pil, x_units, y_units, text_prompt
260
-
261
- text_prompt = gr.Textbox(lines=1, label="Prompt")
262
- image_pil = gr.Image(type="filepath", label='Image')
263
- # negative_prompt = gr.Textbox(lines=1, label="Negative Prompt")
264
- # width = gr.Dropdown(choices=number_choices, value=704, label="Width")
265
- # height = gr.Dropdown(choices=number_choicess, value=408, label="Height")
266
- x_units = gr.Slider(minimum=50, maximum=300, step=1, value=10, label="x_units")
267
- y_units = gr.Slider(minimum=30, maximum=300, step=0.1, value=5, label="y_units")
268
- # seed = gr.Textbox(label="Seed (Leave empty for random seed)")
269
- with gr.Column(scale=2):
270
- output = gr.Image(type="pil", label="Output Image")
271
- # generate = gr.Button("Generate")
272
- # output_seed = gr.Textbox(label="Current Seed")
273
-
274
- # Create the txt2img function
275
 
276
- # generate.click(fn=main_fun, inputs=[image_pil, x_units, y_units, text_prompt], outputs=[output_image])
 
 
 
 
 
 
 
277
 
278
  # Launch the Gradio UI
279
  gr.Interface(fn=main_fun, inputs=[image_pil, x_units, y_units, text_prompt], outputs=[output]).launch(share=True, debug=True)
 
201
  output = draw_image(image_pil, masks, boxes, alpha=0.4)
202
 
203
 
204
+ '''Get masked object and background as two separate images'''
205
+ mask = np.expand_dims(masks[0], axis=-1)
206
+ masked_object = image_pil * mask
207
+ background = image_pil * ~mask
208
+
209
+
210
+ '''Shifts image by x_units and y_units'''
211
+ M = np.float32([[1, 0, x_units], [0, 1, y_units]])
212
+ shifted_image = cv2.warpAffine(masked_object, M, (masked_object.shape[1] , masked_object.shape[0] ), borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0))
213
+ masked_shifted_image = np.where(shifted_image[:, :, 0] != 0, True, False)
214
+
215
+ '''Load stable diffuser model at checkpoint finetuned for inpainting task'''
216
+ pipe = StableDiffusionInpaintPipeline.from_pretrained(
217
+ # "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16
218
+ "stabilityai/stable-diffusion-2-inpainting",torch_dtype=torch.float16)
219
+ pipe.to(CFG.device)
220
+
221
+
222
+ # With Dilation
223
+ structuring_element = np.ones((15, 15, 1), dtype=bool)
224
+ extrapolated_mask = binary_dilation(mask, structure=structuring_element)
225
+ mask_as_uint8 = extrapolated_mask.astype(np.uint8) * 255
226
+ pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L').resize((1024, 1024))
227
+
228
+ # # Without Dilation
229
+ # pil_background = Image.fromarray(background)
230
+ # mask_as_uint8 = mask.astype(np.uint8) * 255
231
+ # pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L')
232
+
233
+ '''Do inpainting on masked locations of original image'''
234
+ prompt = 'fill as per background and neighborhood'
235
+ inpainted_image = pipe(prompt=prompt, image=image_pil, mask_image=pil_mask).images[0]
236
+ # inpainted_image
237
+
238
+ '''Get composite of shifted object and background inpainted imaage'''
239
+ pil_shifted_image = Image.fromarray(shifted_image).resize(inpainted_image.size)
240
+ np_shifted_image = np.array(pil_shifted_image)
241
+ masked_shifted_image = np.where(np_shifted_image[:, :, 0] != 0, True, False)
242
+ masked_shifted_image = np.expand_dims(masked_shifted_image, axis=-1)
243
+ inpainted_shifted = np.array(inpainted_image) * ~masked_shifted_image
244
+
245
+ shifted_image = cv2.resize(shifted_image, inpainted_image.size)
246
+ output = inpainted_shifted + shifted_image
247
+ output = Image.fromarray(output)
248
+ # visualize_results(image_pil, output, 'shifted')
249
 
250
  return output
251
 
252
  import gradio as gr
253
 
254
+ # with gr.Blocks() as demo:
255
+ # gr.Markdown("Segmentation and shift")
256
+ # with gr.Tab("Txt2Img"):
257
+ # with gr.Row():
258
+ # with gr.Column(scale=1):
259
+ # # image_pil, x_units, y_units, text_prompt
260
+
261
+ # text_prompt = gr.Textbox(lines=1, label="Prompt")
262
+ # image_pil = gr.Image(type="filepath", label='Image')
263
+ # # negative_prompt = gr.Textbox(lines=1, label="Negative Prompt")
264
+ # # width = gr.Dropdown(choices=number_choices, value=704, label="Width")
265
+ # # height = gr.Dropdown(choices=number_choicess, value=408, label="Height")
266
+ # x_units = gr.Slider(minimum=50, maximum=300, step=1, value=10, label="x_units")
267
+ # y_units = gr.Slider(minimum=30, maximum=300, step=0.1, value=5, label="y_units")
268
+ # # seed = gr.Textbox(label="Seed (Leave empty for random seed)")
269
+ # with gr.Column(scale=2):
270
+ # output = gr.Image(type="pil", label="Output Image")
271
+ # # generate = gr.Button("Generate")
272
+ # # output_seed = gr.Textbox(label="Current Seed")
273
+
274
+ # # Create the txt2img function
275
 
276
+ # # generate.click(fn=main_fun, inputs=[image_pil, x_units, y_units, text_prompt], outputs=[output_image])
277
+
278
+ x_units = gr.Slider(minimum=50, maximum=300, step=1, value=10, label="x_units")
279
+ y_units = gr.Slider(minimum=30, maximum=300, step=0.1, value=5, label="y_units")
280
+ text_prompt = gr.Textbox(lines=1, label="Prompt")
281
+ image_pil = gr.components.Image(type="filepath", label="Input Image")
282
+ output = gr.components.Image(type="numpy", label="Output Image")
283
+ ]
284
 
285
  # Launch the Gradio UI
286
  gr.Interface(fn=main_fun, inputs=[image_pil, x_units, y_units, text_prompt], outputs=[output]).launch(share=True, debug=True)