noni27 commited on
Commit
f03eb6a
·
verified ·
1 Parent(s): f3dfc26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -49
app.py CHANGED
@@ -1,16 +1,22 @@
1
- import gradio as gr
2
-
3
  import os
4
- home = os.getcwd()
5
- home
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- !git clone https://github.com/IDEA-Research/GroundingDINO
8
- %cd /{home}/GroundingDINO
9
- !pip install -q -e .
10
 
11
- text_prompt = 'basket'
12
- image_path = '/kaggle/input/avataar/wall hanging.jpg'
13
- output_image_path = '/kaggle/working'
14
 
15
 
16
  '''Importing Libraries'''
@@ -27,6 +33,9 @@ from groundingdino.util.utils import clean_state_dict
27
  from huggingface_hub import hf_hub_download
28
  from segment_anything import sam_model_registry
29
  from segment_anything import SamPredictor
 
 
 
30
 
31
  import cv2
32
  import matplotlib.pyplot as plt
@@ -101,8 +110,7 @@ def build_groundingdino():
101
  groundingdino = load_model_hf(ckpt_repo_id, ckpt_filename, ckpt_config_filename)
102
  return groundingdino
103
 
104
- model_sam = build_sam()
105
- model_groundingdino = build_groundingdino()
106
 
107
 
108
  '''Predictions'''
@@ -154,10 +162,6 @@ def draw_image(image_pil, masks, boxes, alpha=0.4):
154
  image = draw_segmentation_masks(image, masks=masks, colors=['red'] * len(masks), alpha=alpha)
155
  return image.numpy().transpose(1, 2, 0)
156
 
157
- image_pil = load_image(image_path)
158
-
159
- masks, boxes, phrases, logits = mask_predict(image_pil, text_prompt=text_prompt, box_threshold=0.23, text_threshold=0.25)
160
- output = draw_image(image_pil, masks, boxes, alpha=0.4)
161
  # torch.save(masks, 'masks.pt')
162
 
163
 
@@ -175,18 +179,25 @@ def visualize_results(img1, img2, task):
175
  for ax in axes:
176
  ax.axis('off')
177
 
178
- visualize_results(image_pil, output, 'segmented')
179
 
 
 
 
 
 
180
 
181
- x_units = 200
182
- y_units = -100
183
- # import torch
184
- # import numpy as np
185
- # masks = torch.load('/kaggle/input/chair-mask/masks.pt')
186
- # print(masks.shape)
187
- # masks
 
 
 
188
 
189
- def main_fun():
190
 
191
  '''Get masked object and background as two separate images'''
192
  mask = np.expand_dims(masks[0], axis=-1)
@@ -200,7 +211,6 @@ def main_fun():
200
  masked_shifted_image = np.where(shifted_image[:, :, 0] != 0, True, False)
201
 
202
  '''Load stable diffuser model at checkpoint finetuned for inpainting task'''
203
- from diffusers import StableDiffusionInpaintPipeline
204
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
205
  # "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16
206
  "stabilityai/stable-diffusion-2-inpainting",torch_dtype=torch.float16)
@@ -208,22 +218,18 @@ def main_fun():
208
 
209
 
210
  # With Dilation
211
-
212
- from scipy.ndimage import binary_dilation
213
  structuring_element = np.ones((15, 15, 1), dtype=bool)
214
  extrapolated_mask = binary_dilation(mask, structure=structuring_element)
215
  mask_as_uint8 = extrapolated_mask.astype(np.uint8) * 255
216
  pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L').resize((1024, 1024))
217
- # pil_mask
218
 
219
  # # Without Dilation
220
  # pil_background = Image.fromarray(background)
221
  # mask_as_uint8 = mask.astype(np.uint8) * 255
222
  # pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L')
223
- # # pil_mask
224
 
225
  '''Do inpainting on masked locations of original image'''
226
- prompt = 'a photo of background'
227
  inpainted_image = pipe(prompt=prompt, image=image_pil, mask_image=pil_mask).images[0]
228
  # inpainted_image
229
 
@@ -237,21 +243,36 @@ def main_fun():
237
  shifted_image = cv2.resize(shifted_image, inpainted_image.size)
238
  output = inpainted_shifted + shifted_image
239
  output = Image.fromarray(output)
240
- visualize_results(image_pil, output, 'shifted')
241
-
242
-
243
-
244
- inputs_image = [
245
- gr.components.Image(type="filepath", label="Input Image"),
246
- ]
247
- outputs_image = [
248
- gr.components.Image(type="numpy", label="Output Image"),
249
- ]
250
- interface_image = gr.Interface(
251
- fn=main_fun,
252
- inputs=inputs_image,
253
- outputs=outputs_image,
254
- title="Pothole detector",
255
- # examples=path,
256
- cache_examples=False,
257
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import warnings
3
+ warnings.filterwarnings('ignore')
4
+
5
+ import subprocess, io, os, sys, time
6
+
7
+ # os.system("pip install -q gradio==3.48.0")
8
+ os.system("pip install -q gradio")
9
+ os.system("pip install -q diffusers")
10
+
11
+ result = subprocess.run(['pip', 'install', '-e', 'GroundingDINO'], check=True)
12
+ print(f'pip install GroundingDINO = {result}')
13
+ sys.path.insert(0, './GroundingDINO')
14
+
15
+ # text_prompt = 'basket'
16
+ # image_path = '/kaggle/input/avataar/wall hanging.jpg'
17
+ # output_image_path = '/kaggle/working'
18
 
 
 
 
19
 
 
 
 
20
 
21
 
22
  '''Importing Libraries'''
 
33
  from huggingface_hub import hf_hub_download
34
  from segment_anything import sam_model_registry
35
  from segment_anything import SamPredictor
36
+ from diffusers import StableDiffusionInpaintPipeline
37
+
38
+ from scipy.ndimage import binary_dilation
39
 
40
  import cv2
41
  import matplotlib.pyplot as plt
 
110
  groundingdino = load_model_hf(ckpt_repo_id, ckpt_filename, ckpt_config_filename)
111
  return groundingdino
112
 
113
+
 
114
 
115
 
116
  '''Predictions'''
 
162
  image = draw_segmentation_masks(image, masks=masks, colors=['red'] * len(masks), alpha=alpha)
163
  return image.numpy().transpose(1, 2, 0)
164
 
 
 
 
 
165
  # torch.save(masks, 'masks.pt')
166
 
167
 
 
179
  for ax in axes:
180
  ax.axis('off')
181
 
182
+ # visualize_results(image_pil, output, 'segmented')
183
 
184
+ # x_units = 200
185
+ # y_units = -100
186
+ # text_prompt = 'wooden stool'
187
+ # image_path = '/kaggle/input/avataar/stool.jpeg'
188
+ # output_image_path = '/kaggle/working'
189
 
190
+ def main_fun(image_pil, x_units, y_units, text_prompt):
191
+ # x_units = 200
192
+ # y_units = -100
193
+ # text_prompt = 'wooden stool'
194
+ model_sam = build_sam()
195
+ model_groundingdino = build_groundingdino()
196
+
197
+ # image_pil = load_image(image_path)
198
+ masks, boxes, phrases, logits = mask_predict(image_pil, text_prompt=text_prompt, box_threshold=0.23, text_threshold=0.25)
199
+ output = draw_image(image_pil, masks, boxes, alpha=0.4)
200
 
 
201
 
202
  '''Get masked object and background as two separate images'''
203
  mask = np.expand_dims(masks[0], axis=-1)
 
211
  masked_shifted_image = np.where(shifted_image[:, :, 0] != 0, True, False)
212
 
213
  '''Load stable diffuser model at checkpoint finetuned for inpainting task'''
 
214
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
215
  # "runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16
216
  "stabilityai/stable-diffusion-2-inpainting",torch_dtype=torch.float16)
 
218
 
219
 
220
  # With Dilation
 
 
221
  structuring_element = np.ones((15, 15, 1), dtype=bool)
222
  extrapolated_mask = binary_dilation(mask, structure=structuring_element)
223
  mask_as_uint8 = extrapolated_mask.astype(np.uint8) * 255
224
  pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L').resize((1024, 1024))
 
225
 
226
  # # Without Dilation
227
  # pil_background = Image.fromarray(background)
228
  # mask_as_uint8 = mask.astype(np.uint8) * 255
229
  # pil_mask = Image.fromarray(mask_as_uint8.squeeze(), mode='L')
 
230
 
231
  '''Do inpainting on masked locations of original image'''
232
+ prompt = 'fill as per background and neighborhood'
233
  inpainted_image = pipe(prompt=prompt, image=image_pil, mask_image=pil_mask).images[0]
234
  # inpainted_image
235
 
 
243
  shifted_image = cv2.resize(shifted_image, inpainted_image.size)
244
  output = inpainted_shifted + shifted_image
245
  output = Image.fromarray(output)
246
+ # visualize_results(image_pil, output, 'shifted')
247
+
248
+ return output
249
+
250
+ import gradio as gr
251
+
252
+ with gr.Blocks() as demo:
253
+ gr.Markdown("Segmentation and shift")
254
+ with gr.Tab("Txt2Img"):
255
+ with gr.Row():
256
+ with gr.Column(scale=1):
257
+ # image_pil, x_units, y_units, text_prompt
258
+
259
+ text_prompt = gr.Textbox(lines=1, label="Prompt")
260
+ image_pil = gr.Image(label='Image Input', type='pil', tool='sketch', source="upload")
261
+ # negative_prompt = gr.Textbox(lines=1, label="Negative Prompt")
262
+ # width = gr.Dropdown(choices=number_choices, value=704, label="Width")
263
+ # height = gr.Dropdown(choices=number_choicess, value=408, label="Height")
264
+ x_units = gr.Slider(minimum=50, maximum=300, step=1, value=10, label="x_units")
265
+ y_units = gr.Slider(minimum=30, maximum=300, step=0.1, value=5, label="y_units")
266
+ # seed = gr.Textbox(label="Seed (Leave empty for random seed)")
267
+ with gr.Column(scale=2):
268
+ output_image = gr.Image(container=True, height=500, width=500)
269
+ # generate = gr.Button("Generate")
270
+ # output_seed = gr.Textbox(label="Current Seed")
271
+
272
+ # Create the txt2img function
273
+
274
+ # generate.click(fn=main_fun, inputs=[image_pil, x_units, y_units, text_prompt], outputs=[output_image])
275
+
276
+ # Launch the Gradio UI
277
+ gr.Interface(fn=main_fun, inputs=[image_pil, x_units, y_units, text_prompt], outputs=[output_image]).launch(share=True, debug=True)
278
+ # demo