karimbenharrak commited on
Commit
ec0b321
·
verified ·
1 Parent(s): 2ce18a9

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +21 -4
handler.py CHANGED
@@ -184,9 +184,26 @@ class EndpointHandler():
184
  gen_canny_img = np.array(gen_canny_img)
185
 
186
  help_image = cv2.Canny(gen_canny_img, low_threshold, high_threshold)
187
- help_image = help_image[:, :, None]
188
- help_image = np.concatenate([help_image, help_image, help_image], axis=2)
189
- canny_image = Image.fromarray(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
  input_images = [canny_image.resize((1024, 1024)), image.resize((1024, 1024))]
192
 
@@ -222,4 +239,4 @@ class EndpointHandler():
222
  image[image_mask > 0.5] = -1.0 # set as masked pixel
223
  image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
224
  image = torch.from_numpy(image)
225
- return image
 
184
  gen_canny_img = np.array(gen_canny_img)
185
 
186
  help_image = cv2.Canny(gen_canny_img, low_threshold, high_threshold)
187
+
188
+ # get bounding box from selected area in mask image
189
+ # make help_image fit exactly into the bounding box
190
+ # create black image with canny edges only in the selected area
191
+
192
+ # get bounding box from selected area in mask image
193
+ mask_image = np.array(mask_image)
194
+ mask_image = cv2.cvtColor(mask_image, cv2.COLOR_RGB2GRAY)
195
+ mask_image = cv2.bitwise_not(mask_image)
196
+ contours, _ = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
197
+ x, y, w, h = cv2.boundingRect(contours[0])
198
+
199
+ # create a completely black image with the same size as the mask image
200
+ black_image = np.zeros_like(mask_image)
201
+
202
+ # put the canny edges into the black image but resize it to the bounding box
203
+ help_image = cv2.resize(help_image, (w, h))
204
+ black_image[y:y+h, x:x+w] = help_image
205
+
206
+ canny_image = Image.fromarray(black_image)
207
 
208
  input_images = [canny_image.resize((1024, 1024)), image.resize((1024, 1024))]
209
 
 
239
  image[image_mask > 0.5] = -1.0 # set as masked pixel
240
  image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
241
  image = torch.from_numpy(image)
242
+ return image