def draw_predictions_dual( input: dict, output: dict, image_id_key="image_id", mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), class_colors=[ (0, 0, 0), # 0=background (0, 255, 0), # no damage (or just 'building' for localization) (green) (255, 255, 0), # minor damage (yellow) (255, 128, 0), # major damage (red) (255, 0, 0), # destroyed (red) ], ): images = [] num_images = len(input[image_id_key]) for i, image_id in enumerate(range(num_images)): image_pre = rgb_image_from_tensor(input[INPUT_IMAGE_PRE_KEY][i], mean, std) image_pre = cv2.cvtColor(image_pre, cv2.COLOR_RGB2BGR) image_post = rgb_image_from_tensor(input[INPUT_IMAGE_POST_KEY][i], mean, std) image_post = cv2.cvtColor(image_post, cv2.COLOR_RGB2BGR) image_pre_gt = image_pre.copy() image_post_gt = image_post.copy() localization_target = to_numpy(input[INPUT_MASK_PRE_KEY][i].squeeze(0)) damage_target = to_numpy(input[INPUT_MASK_POST_KEY][i]) image_pre_gt = overlay_image_and_mask(image_pre_gt, localization_target, class_colors) image_post_gt = overlay_image_and_mask(image_post_gt, damage_target, class_colors) localization_predictions = to_numpy( output[OUTPUT_MASK_PRE_KEY][i].squeeze(0).sigmoid() > 0.5).astype( np.uint8) damage_predictions = to_numpy( output[OUTPUT_MASK_POST_KEY][i]).argmax(axis=0) image_pre = overlay_image_and_mask(image_pre, localization_predictions, class_colors) image_post = overlay_image_and_mask(image_post, damage_predictions, class_colors) overlay_gt = np.column_stack([image_pre_gt, image_post_gt]) overlay = np.column_stack([image_pre, image_post]) overlay = np.row_stack([overlay_gt, overlay]) overlay = longest_max_size(overlay, 1024, cv2.INTER_LINEAR) cv2.putText(overlay, str(image_id), (10, 15), cv2.FONT_HERSHEY_PLAIN, 1, (250, 250, 250)) images.append(overlay) return images
def test_longest_max_size(target): img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], dtype=np.uint8) expected = np.array([[2, 3], [6, 7], [10, 11]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) scaled = F.longest_max_size(img, max_size=3, interpolation=cv2.INTER_LINEAR) assert np.array_equal(scaled, expected)
def draw_predictions( input: dict, output: dict, image_id_key="image_id", mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), class_colors=[ (0, 0, 0), # 0=background (0, 255, 0), # no damage (or just 'building' for localization) (green) (255, 255, 0), # minor damage (yellow) (255, 128, 0), # major damage (red) (255, 0, 0), # destroyed (red) (127, 127, 127) ], max_images=32): images = [] num_images = len(input[image_id_key]) for i in range(num_images): image_id = input[INPUT_IMAGE_ID_KEY][i] image_pre = rgb_image_from_tensor(input[INPUT_IMAGE_KEY][i, 0:3, ...], mean, std) image_pre = cv2.cvtColor(image_pre, cv2.COLOR_RGB2BGR) image_post = rgb_image_from_tensor(input[INPUT_IMAGE_KEY][i, 3:6, ...], mean, std) image_post = cv2.cvtColor(image_post, cv2.COLOR_RGB2BGR) image_pre_gt = image_pre.copy() image_post_gt = image_post.copy() damage_target = to_numpy(input[INPUT_MASK_KEY][i]) image_pre_gt = overlay_image_and_mask(image_pre_gt, damage_target, class_colors) image_post_gt = overlay_image_and_mask(image_post_gt, damage_target, class_colors) damage_predictions = to_numpy(output[INPUT_MASK_KEY][i]).argmax(axis=0) image_pre = overlay_image_and_mask(image_pre, damage_predictions, class_colors) image_post = overlay_image_and_mask(image_post, damage_predictions, class_colors) overlay_gt = np.column_stack([image_pre_gt, image_post_gt]) overlay = np.column_stack([image_pre, image_post]) overlay = np.row_stack([overlay_gt, overlay]) overlay = longest_max_size(overlay, 1024, cv2.INTER_LINEAR) cv2.putText(overlay, str(image_id), (10, 15), cv2.FONT_HERSHEY_PLAIN, 1, (250, 250, 250)) images.append(overlay) if len(images) >= max_images: break return images
def preprocess(image_fname, output_dir, image_size=768): image = cv2.imread(image_fname) image = crop_black(image, tolerance=5) image = longest_max_size(image, max_size=image_size, interpolation=cv2.INTER_CUBIC) image_id = fs.id_from_fname(image_fname) dst_fname = os.path.join(output_dir, image_id + '.png') cv2.imwrite(dst_fname, image) return
def create_text_image(self, index, etc_text_params=None): if random.random() <= self.same_text_in_batch_prob: cur_unicode_list = self.cur_unicode_list else: cur_unicode_list = self._create_random_text() self.cur_text_ing_before = "".join( [chr(c) for c in cur_unicode_list]) if random.random() <= self.same_font_size_in_batch_prob: font_size = self.cur_font_size else: font_size = random.choice(self.font_size_range) text = "".join([chr(c) for c in cur_unicode_list]) self.cur_text_ing = text if random.random() <= self.same_text_params_in_batch_prob: char_params = self.cur_text_params else: char_params = self._get_text_params() char_params['font_size'] = font_size self.cur_params = char_params self.cur_font = self.font_list[index] # import uuid # char_params['output_path'] = "../testimage/{}_{}_{}.jpg".format(self.cur_unicode_list[0], index, # str(uuid.uuid4())) # text_image_maker.create_text_image(text, self.font_list[index], **char_params) char_params['output_path'] = None char_params['auto_chance_color_when_same'] = True char_params['raise_exception'] = False char_params['return_mask'] = self.return_mask # import json # print(json.dumps(char_params)) # char_params = json.loads('{"color_mode": "RGB", "paddings": {"right": 0.1050272078196586, "top": 0.26090272932922254, "bottom": 0.17015320517900254, "left": 0.25837411687366774}, "text_border": null, "fg_color": [209, 235, 98, 166], "text_italic": false, "font_size": 15, "bg_img_path": "/home/irelin/resource/font_recognition/bgs/294.winterwax-500x500.jpg", "text_shadow": null, "use_img_persp_trans": true, "pos_ratio": [0.3, 0.2], "text_persp_trans_params": [-0.009976076882772873, 0.036253351174196216], "text_rotate": 14, "bg_img_width_ratio": 1.2, "text_blur": 0, "bg_img_height_ratio": 1.1, "use_bg_color": false, "bg_img_scale": 0.5601430892743575, "use_text_persp_trans": true, "use_binarize": false, "img_persp_trans_params": [-0.02296148027430462, 0.004213654695530833], "text_width_ratio": 1.0, "text_gradient": null, "output_path": null, "auto_chance_color_when_same": true, "text_height_ratio": 1.0}') # if self.use_debug: # print(os.path.basename(self.font_list[index]), text) if etc_text_params: char_params.update(etc_text_params) img = text_image_maker.create_text_image(text, self.font_list[index], **char_params) if self.return_mask: img, mask = img if self.use_same_random_crop_in_batch: height, width = img.shape[:2] if width >= height: img = F.smallest_max_size(img, max_size=self.input_size, interpolation=cv2.INTER_LINEAR) else: img = F.longest_max_size(img, max_size=self.input_size, interpolation=cv2.INTER_LINEAR) pad_width = self.input_size - img.shape[:2][1] left = pad_width // 2 right = pad_width - left img = F.pad_with_params(img, 0, 0, left, right, border_mode=cv2.BORDER_CONSTANT, value=0) height, width = img.shape[:2] if width > self.input_size: last_index = width - self.input_size start_index = int(self.crop_start_ratio * last_index) img = img[:, start_index:start_index + self.input_size, :] if self.transform is not None: img = self.transform(image=img)['image'] results = [img] if self.return_mask: results.append(mask) if self.return_text: results.append(text) if len(results) > 1: return results else: return img
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): if max(img.shape[:2]) < self.max_size: return img return F.longest_max_size(img, max_size=self.max_size, interpolation=interpolation)
def longest_max_size(img, interpolation=cv2.INTER_LINEAR, **params): img = F.longest_max_size(img, max_size=input_size[1], interpolation=interpolation) return img