def _aug(self, image, mask): mask = SegmentationMapsOnImage(mask, image.shape[:2]) seq = iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Rot90([0, 3]), iaa.SomeOf( 1, [ iaa.Affine(scale={ "x": (0.7, 1.5), "y": (1.6, 1.5) }), iaa.Affine(rotate=(-30, 30)), # iaa.Add((-110, 111)), # iaa.GaussianBlur(sigma=1.8 * np.random.rand()), # iaa.Emboss(alpha=(0.0, 1.0), strength=(0.5, 1.5)), # iaa.AdditiveGaussianNoise(scale=0.05*255), # iaa.Multiply((0.5, 1.5)), # iaa.Affine(shear=(-20, 20)), # iaa.PiecewiseAffine(scale=(0.01, 0.02)), iaa.PerspectiveTransform(scale=(0.01, 0.1)), ], ), ], random_order=True, ) image, mask = seq(image=image, segmentation_maps=mask) mask = mask.get_arr_int().astype(np.uint8) return image, mask
def load_batch_pair_in_pair(self, img_paths, label_paths): images = [] segs = [] preprocessors = [self.resize_img, self.mean_substraction] for i in range(len(img_paths)): image = self.load_image(img_paths[i]) # image = self.preprocessing(image, preprocessors) seg = self.load_image(label_paths[i]) # seg = self.preprocessing(seg, [self.resize_img]) # print(seg.dtype) # seg = seg.astype(np.uint8) # seg = self.parse_label(seg) # seg = np.argmax(seg, axis=-1).astype(np.int32) segmap = SegmentationMapsOnImage(seg[:, :, 0], shape=image.shape, nb_classes=np.max(seg[:, :, 0]) + 1) if self.augmentation: image, segmap = self.augment_func(image=image, segmentation_maps=segmap) seg[:, :, 0] = segmap.get_arr_int() image = self.preprocessing(image, preprocessors) # seg = self.preprocessing(seg, [self.resize_img]) seg = self.resize_img(seg, interpolation=cv2.INTER_NEAREST) seg = seg / 255 seg[seg > 0.5] = 1 seg[seg <= 0.5] = 0 seg = self.parse_label(seg) images.append(image) segs.append(seg) return np.array(images), np.array(segs)
def make_json(masks_dir, masks_list, img_dir, class_name, dataset_dir): with open("template.json") as json_file: template = json.load(json_file) for mask in masks_list: mask_path = os.path.join(masks_dir, '0' + mask[0], mask) image_name = mask.split('_')[0] new_shape = {} new_shape['label'] = class_name new_shape['line_color'] = None new_shape['fill_color'] = None new_shape['shape_type'] = "polygon" new_shape['flags'] = {} real_image = Image.open( os.path.join(dataset_dir, img_dir, class_name, image_name + '.jpg')) real_width, real_height = real_image.size mask_image = Image.open(mask_path) width, height = mask_image.size mask_array = np.array(mask_image) mask_array = SegmentationMapsOnImage(mask_array, (height, width)).resize( (real_height, real_width)) segmap_aug_arr = np.array( mask_array.get_arr_int(background_threshold=0 + .1)) segmap_single = np.ma.masked_where( True, segmap_aug_arr).mask * segmap_aug_arr polygons = Mask(segmap_single).polygons() # Image.fromarray(mask_array.draw_on_image(np.array(real_image))[0]).show() if len(polygons.points) > 1: selected_points = polygons.points[0] for points in polygons.points: if selected_points.size < points.size: selected_points = points new_shape['points'] = np.array(selected_points).tolist() else: try: new_shape['points'] = np.array(polygons.points).tolist()[0] except: new_shape['points'] = np.array(polygons.points).tolist() json_file_path = os.path.join(dataset_dir, img_dir, class_name, image_name + '.json') if os.path.exists(json_file_path): existing_json = None with open(json_file_path) as preexisting_json: existing_json = json.load(preexisting_json) for old_shapes in existing_json['shapes']: template['shapes'].append(old_shapes) template['flags']["multiple targets"] = True template['imagePath'] = image_name + '.jpg' template['imageHeight'] = existing_json['imageHeight'] template['imageWidth'] = existing_json['imageWidth'] template['shapes'].append(new_shape) template['imagePath'] = image_name + '.jpg' template['imageHeight'] = real_height template['imageWidth'] = real_width template['imageData'] = None with open(json_file_path, 'w+') as outfile: json.dump(template, outfile, indent=2) new_mask_path = os.path.join(dataset_dir, img_dir, class_name, mask) shutil.copy(mask_path, new_mask_path) template['shapes'] = []