def overwrite_label_img_masks(img_fpath: str, label_img_fpath: str,
                              parent_fname_to_updatelist_dict, mld: Any,
                              classname_to_id_map: Mapping[str, int],
                              require_strict_boundaries: bool,
                              split: str) -> None:
    """
	Swap the pixel values inside a label map's mask to a new value. This
	effectively changes the mask's category on disk.

	Get fname stem from rgb image file path
	Get sequence ID/parent from label image path file system parent.

		Args:
		-	img_fpath:
		-	label_img_fpath:
		-	parent_fname_to_updatelist_dict:
		-	mld:
		-	classname_to_id_map:
		-	require_strict_boundaries:
		-	split:

		Returns:
		-	None
	"""
    fname_stem = Path(img_fpath).stem
    parent = Path(label_img_fpath).parts[
        -2]  # get parent name, functions as sequence ID

    if fname_stem not in parent_fname_to_updatelist_dict[parent]:
        # we loop through the entire dataset, and many images won't have any
        # updated masks. they'll pass through here.
        return

    # load up each label_img
    # label image will already by in universal taxonomy
    label_img = imageio.imread(label_img_fpath)

    update_records = parent_fname_to_updatelist_dict[parent][fname_stem]
    for rec in update_records:
        # if it is, perform each update as described in the object
        segment_mask = mld.get_segment_mask(parent, rec.segmentid, fname_stem,
                                            split)

        if segment_mask is None:
            print('No such mask found, exiting...')
            exit()

        # update the label image each time
        orig_class_idx = classname_to_id_map[rec.orig_class]
        new_class_idx = classname_to_id_map[rec.relabeled_class]
        label_img = swap_px_inside_mask(label_img, segment_mask,
                                        orig_class_idx, new_class_idx,
                                        require_strict_boundaries)

        # save it to disk, at new data root, using same abs path as before
    overwrite = True  # False #
    if overwrite:
        imageio.imwrite(label_img_fpath, label_img)
示例#2
0
def test_swap_px_inside_mask():
    """ """
    segment_mask = np.array([[0, 0, 1, 0], [0, 1, 1, 0], [1, 1, 1, 1]])
    semantic_img = np.zeros((3, 4), dtype=np.uint8)
    new_img = swap_px_inside_mask(semantic_img,
                                  segment_mask,
                                  old_val=0,
                                  new_val=8,
                                  require_strict_boundaries=True)
    gt_new_img = np.array([[0, 0, 8, 0], [0, 8, 8, 0], [8, 8, 8, 8]])
    assert np.allclose(gt_new_img, new_img)
示例#3
0
def test_swap_px_inside_mask():

    label_img = np.array([[7, 8, 9, 0], [0, 7, 0, 7], [3, 4, 7, 1]],
                         dtype=np.uint8)

    segment_mask = np.array([[1, 0, 0, 0], [0, 1, 0, 1], [0, 0, 1, 0]],
                            dtype=np.uint8)

    old_val = 7
    new_val = 255
    new_label_img = swap_px_inside_mask(label_img, segment_mask, old_val,
                                        new_val)
    gt_new_label_img = np.array(
        [[255, 8, 9, 0], [0, 255, 0, 255], [3, 4, 255, 1]], dtype=np.uint8)

    assert np.allclose(new_label_img, gt_new_label_img)
示例#4
0
def write_semantic_from_panoptic(
	cse: COCOSemanticExtractor,
	split: str,
	instance_img_fpath: str,
	ignore_idx: int = 255
) -> None:
	"""
		Args:
		-	cse
		-	split
		-	instance_img_fpath
		-	ignore_idx

		Returns:
		-	None
	"""
	fname_stem = Path(instance_img_fpath).stem
	instance_id_img = cse.instance_api.get_instance_id_img(split, fname_stem)
	img_annot = cse.semantic_api.get_img_annotation(split, fname_stem)
	
	# default pixel value is unlabeled
	semantic_img = np.ones_like(instance_id_img, dtype=np.uint8) * ignore_idx
	for segment in img_annot['segments_info']:
		segmentid = segment['id']
		categoryid = segment['category_id']
		segment_mask = (instance_id_img == segmentid).astype(np.uint8)

		semantic_img = swap_px_inside_mask(
			semantic_img, 
			segment_mask,
			old_val=ignore_idx,
			new_val=categoryid,
			require_strict_boundaries=True
		)
	semantic_fpath = instance_img_fpath.replace(
		f'annotations/panoptic_{split}2017',
		f'semantic_annotations201/{split}2017' # in 201-class taxonomy
		)
	check_mkdir(Path(semantic_fpath).parent)
	imageio.imwrite(semantic_fpath, semantic_img)