def save_annotation(label, save_dir, filename, add_colormap=True, colormap_type=get_dataset_colormap.get_pascal_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: The directory to which the results will be saved. filename: The image filename. add_colormap: Add color map to the label or not. colormap_type: Colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG')
def testLabelToPASCALColorImage(self): """Test the value of the converted label value.""" label = np.array([[0, 16, 16], [52, 7, 52]]) expected_result = np.array([ [[0, 0, 0], [0, 64, 0], [0, 64, 0]], [[0, 64, 192], [128, 128, 128], [0, 64, 192]] ]) colored_label = get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name()) self.assertTrue(np.array_equal(expected_result, colored_label))
def save_annotation(label, save_dir, filename, add_colormap=True, normalize_to_unit_values=False, scale_values=False, colormap_type=get_dataset_colormap.get_pascal_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: String, the directory to which the results will be saved. filename: String, the image filename. add_colormap: Boolean, add color map to the label or not. normalize_to_unit_values: Boolean, normalize the input values to [0, 1]. scale_values: Boolean, scale the input values to [0, 255] for visualization. colormap_type: String, colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label if normalize_to_unit_values: min_value = np.amin(colored_label) max_value = np.amax(colored_label) range_value = max_value - min_value if range_value != 0: colored_label = (colored_label - min_value) / range_value if scale_values: colored_label = 255. * colored_label pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG')
def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self): """Raise ValueError if input dimension is not correct.""" label = np.array([120]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
def testUnExpectedLabelValueForLabelToPASCALColorImage(self): """Raise ValueError when input value exceeds range.""" label = np.array([[120], [300]]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self): """Raise ValueError if input dimension is not correct.""" label = np.array([120]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
def testUnExpectedLabelValueForLabelToPASCALColorImage(self): """Raise ValueError when input value exceeds range.""" label = np.array([[120], [300]]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
## Helper methods LABEL_NAMES = np.asarray([ 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv' ]) FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1) FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP) TEST_IMAGE_PATHS = [f for f in glob.glob(PATH_TO_TEST_IMAGES_DIR + "/**/*.jpg", recursive=True)] for image_path in TEST_IMAGE_PATHS: image = Image.open(image_path) resized_im, seg_map = model.run(image) seg_image = get_dataset_colormap.label_to_color_image( seg_map, get_dataset_colormap.get_pascal_name()).astype(np.uint8) basename = image_path.replace(PATH_TO_TEST_IMAGES_DIR,"") open_cv_image = np.array(seg_image) open_cv_image = open_cv_image[:, :, ::-1].copy() os.makedirs(OUTPUT_PATH + basename.replace(ntpath.basename(image_path), ""), exist_ok=True) cv2.imwrite(OUTPUT_PATH + basename, open_cv_image)