Esempio n. 1
0
def bdc2dsmd_det_2d(annot_dir,
                    image_dir=None,
                    class2label=None,
                    ignore_label_name=True,
                    replace_ext=lambda x: x):
    # N.B. annotation file name and image file name should be the same
    num_classes = len(class2label) if class2label is not None else 1

    filenames = mv.listdir(annot_dir)
    empty_bboxes = np.zeros((0, 4), dtype=np.float32)
    dsmd = {
        replace_ext(filename): [empty_bboxes] * num_classes
        for filename in filenames
    }
    for filename in filenames:
        annot_filepath = mv.joinpath(annot_dir, filename)
        bboxes = load_bdc_dr_bbox(
            annot_filepath, lambda x: 0 if ignore_label_name else class2label)
        for label, bbox in bboxes:
            bbox = np.array(bbox, dtype=np.float32).reshape(-1, 4)
            if dsmd[replace_ext(filename)][label].shape[0] == 0:
                dsmd[replace_ext(filename)][label] = bbox
            else:
                dsmd[replace_ext(filename)][label] = np.append(
                    dsmd[replace_ext(filename)][label], bbox, axis=0)

    return mv.make_dsmd(dsmd)
Esempio n. 2
0
def save_cls_dsmd(dsmd_path, data, auto_mkdirs=True):
    if auto_mkdirs:
        mv.mkdirs(mv.parentdir(dsmd_path))

    dsmd = mv.make_dsmd(data)
    with open(dsmd_path, 'w') as fd:
        for key, value in dsmd.items():
            if mv.isarrayinstance(value):  # handle multi-label case
                value = ','.join([str(entry) for entry in value])
            line = '%s,%s\n' % (str(key), str(value))
            fd.write(line)
Esempio n. 3
0
def load_cls_dsmd(dsmd_path):
    data = {}
    with open(dsmd_path, 'r') as fd:
        for line in fd:
            key, value = line.strip().split(',', 1)
            try:  # try to interpret annotation as int or list[int].
                value = ast.literal_eval(value.strip())
            except (SyntaxError, ValueError):
                pass
            data[key] = value

    return mv.make_dsmd(data)
Esempio n. 4
0
def save_det_dsmd(dsmd_path, data, class2label, auto_mkdirs=True):
    """ Save dataset metadata to specified file.

    Args:
        dsmd_path (str): file path to save dataset metadata.
        data (dict): dataset metadata, refer to 'load_dsmd'.
        class2label (str or dict): class-to-label file or class2label dict.
        auto_mkdirs (bool): If the parent folder of `file_path` does not
            exist, whether to create it automatically.
    """
    if auto_mkdirs:
        mv.mkdirs(mv.parentdir(dsmd_path))

    # get label->class mapping
    if isinstance(class2label, str):
        class2label = mv.load_c2l(class2label)
    label2class = {value: key for key, value in class2label.items()}

    # write dataset metadata loop
    dsmd = mv.make_dsmd(data)
    with open(dsmd_path, 'w') as fd:
        for key, value in dsmd.items():
            _write_record(fd, key, value, label2class)
Esempio n. 5
0
def load_det_dsmd(dsmd_path, class2label):
    """ load detection dataset metadata.

    Args:
        dsmd_path (str): dataset metadata file path.
        class2label (str or dict): class-to-label file.

    Return:
        (OrderedDict): Loaded dsmd is a OrderedDict looks like
        {
            data/1.png: [
                [bboxes of category 'cat' in ndarray of shape (n, 4)],
                [bboxes of category 'dog' in ndarray of shape (n, 4)],
                ...
            ]
            data/2.png: [
                ...
            ]
            ...
        }
    """
    data = {}
    if isinstance(class2label, str):
        class2label = mv.load_c2l(class2label)

    # label should start from 0
    assert min(class2label.values()) == 0

    with open(dsmd_path, 'r') as fd:
        for line in fd:
            _update_data(data, line, class2label)

    # convert bboxes from list of lists to ndarray
    data = _convert_bboxes_format(data)

    return mv.make_dsmd(data)