praeclarumjj3 commited on
Commit
9e43b47
1 Parent(s): 4530503

Add mask drawing

Browse files
app.py CHANGED
@@ -76,12 +76,20 @@ def inpaint(input_img, mask, option):
76
  mask = Image.fromarray(result[0]['mask'])
77
  else:
78
  mask = mask.resize((width,height))
79
-
80
- if width is not 512 or height is not 512:
 
81
  input_img = input_img.resize((512, 512))
82
  mask = mask.resize((512, 512))
 
 
 
 
 
 
 
 
83
 
84
- mask = mask.convert('L')
85
  mask = np.array(mask) / 255.
86
  mask_tensor = torch.from_numpy(mask).to(torch.float32)
87
  mask_tensor = mask_tensor.unsqueeze(0)
@@ -106,30 +114,34 @@ def inpaint(input_img, mask, option):
106
  gradio_inputs = [gr.inputs.Image(type='pil',
107
  tool=None,
108
  label="Input Image"),
109
- gr.inputs.Image(type='pil',source="canvas", label="Mask", invert_colors=True),
 
 
 
110
  gr.inputs.Radio(choices=["Automatic", "Manual"], type="value", default="Manual", label="Masking Choice")
111
  ]
112
 
113
  gradio_outputs = [gr.outputs.Image(label='Image with Hole'),
114
  gr.outputs.Image(label='Inpainted Image')]
115
 
116
- examples = [['test_512/person512.png', 'test_512/mask_auto.png', 'Automatic'],
117
- ['test_512/a_org.png', 'test_512/a_mask.png', 'Manual'],
118
- ['test_512/c_org.png', 'test_512/b_mask.png', 'Manual'],
119
- ['test_512/b_org.png', 'test_512/c_mask.png', 'Manual'],
120
- ['test_512/d_org.png', 'test_512/d_mask.png', 'Manual'],
121
- ['test_512/e_org.png', 'test_512/e_mask.png', 'Manual'],
122
- ['test_512/f_org.png', 'test_512/f_mask.png', 'Manual'],
123
- ['test_512/g_org.png', 'test_512/g_mask.png', 'Manual'],
124
- ['test_512/h_org.png', 'test_512/h_mask.png', 'Manual'],
125
- ['test_512/i_org.png', 'test_512/i_mask.png', 'Manual']]
126
 
127
  title = "FcF-Inpainting"
128
  description = "[Note: Queue time may take upto 20 seconds! The image and mask are resized to 512x512 before inpainting.] To use FcF-Inpainting: \n \
129
  (1) Upload an Image; \n \
130
- (2) Draw (Manual) a Mask on the White Canvas or Generate a mask using U2Net by selecting the Automatic option; \n \
 
131
  (3) Click on Submit and witness the MAGIC! 🪄 ✨ ✨"
132
- article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2112.10741' target='_blank'> Keys to Better Image Inpainting: Structure and Texture Go Hand in Hand</a> | <a href='https://github.com/SHI-Labs/FcF-Inpainting' target='_blank'>Github Repo</a></p>"
133
 
134
  css = ".image-preview {height: 32rem; width: auto;} .output-image {height: 32rem; width: auto;} .panel-buttons { display: flex; flex-direction: row;}"
135
 
 
76
  mask = Image.fromarray(result[0]['mask'])
77
  else:
78
  mask = mask.resize((width,height))
79
+ mask = mask.convert('L')
80
+
81
+ if width != 512 or height != 512:
82
  input_img = input_img.resize((512, 512))
83
  mask = mask.resize((512, 512))
84
+
85
+
86
+ if option == 'Manual':
87
+ mask = (np.array(mask) - np.array(input_img.convert('L'))) > 0.
88
+ mask = mask * 1.
89
+ kernel = np.ones((5, 5), np.uint8)
90
+ mask = cv2.dilate(mask, kernel)
91
+ mask = mask * 255.
92
 
 
93
  mask = np.array(mask) / 255.
94
  mask_tensor = torch.from_numpy(mask).to(torch.float32)
95
  mask_tensor = mask_tensor.unsqueeze(0)
 
114
  gradio_inputs = [gr.inputs.Image(type='pil',
115
  tool=None,
116
  label="Input Image"),
117
+ # gr.inputs.Image(type='pil',source="canvas", label="Mask", invert_colors=True),
118
+ gr.inputs.Image(type='pil',
119
+ tool="editor",
120
+ label="Mask"),
121
  gr.inputs.Radio(choices=["Automatic", "Manual"], type="value", default="Manual", label="Masking Choice")
122
  ]
123
 
124
  gradio_outputs = [gr.outputs.Image(label='Image with Hole'),
125
  gr.outputs.Image(label='Inpainted Image')]
126
 
127
+ examples = [['test_512/person512.png', 'test_512/person512.png', 'Automatic'],
128
+ ['test_512/a_org.png', 'test_512/a_overlay.png', 'Manual'],
129
+ ['test_512/b_org.png', 'test_512/b_overlay.png', 'Manual'],
130
+ ['test_512/c_org.png', 'test_512/c_overlay.png', 'Manual'],
131
+ ['test_512/d_org.png', 'test_512/d_overlay.png', 'Manual'],
132
+ ['test_512/e_org.png', 'test_512/e_overlay.png', 'Manual'],
133
+ ['test_512/f_org.png', 'test_512/f_overlay.png', 'Manual'],
134
+ ['test_512/g_org.png', 'test_512/g_overlay.png', 'Manual'],
135
+ ['test_512/h_org.png', 'test_512/h_overlay.png', 'Manual'],
136
+ ['test_512/i_org.png', 'test_512/i_overlay.png', 'Manual']]
137
 
138
  title = "FcF-Inpainting"
139
  description = "[Note: Queue time may take upto 20 seconds! The image and mask are resized to 512x512 before inpainting.] To use FcF-Inpainting: \n \
140
  (1) Upload an Image; \n \
141
+ (2a) Draw (Manual) a Mask using the brush (click on the edit option in the top right of the Mask View) \n \
142
+ (2b) You can also generate a mask using U2Net by selecting the Automatic option; \n \
143
  (3) Click on Submit and witness the MAGIC! 🪄 ✨ ✨"
144
+ article = "<p style='text-align: center'><a href='https://github.com/SHI-Labs/FcF-Inpainting' target='_blank'> Keys to Better Image Inpainting: Structure and Texture Go Hand in Hand</a> | <a href='https://github.com/SHI-Labs/FcF-Inpainting' target='_blank'>Github Repo</a></p>"
145
 
146
  css = ".image-preview {height: 32rem; width: auto;} .output-image {height: 32rem; width: auto;} .panel-buttons { display: flex; flex-direction: row;}"
147
 
test_512/a_overlay.png ADDED
test_512/b_overlay.png ADDED
test_512/c_overlay.png ADDED
test_512/d_overlay.png ADDED
test_512/e_overlay.png ADDED
test_512/f_overlay.png ADDED
test_512/g_overlay.png ADDED
test_512/h_overlay.png ADDED
test_512/i_overlay.png ADDED