예제 #1
0
def main(args: List[str]):
    if len(args) > 1:
        data_folder_str = args[1]
    else:
        data_folder_str = folder.data_folder()
    voc_label_path = Path(__file__).parent.parent / data_folder_str / folder.annotation_folder()  # VOC格式标记的数据
    image_path = Path(__file__).parent.parent / data_folder_str / folder.img_folder()  # 图片数据
    image_sets_path = Path(__file__).parent.parent / data_folder_str / folder.split_folder()  # 训练集测试集分类数据
    coco_labels_path = Path(__file__).parent.parent / data_folder_str / folder.coco_labels_folder()  # COCO格式标记的数据
    if not coco_labels_path.exists():
        coco_labels_path.mkdir()
    for name in ('train', 'test'):
        image_ids = \
            set((image_sets_path / f'{name}.txt').open('r').read().strip().split())  # [0001 0002 ...]
        xmls = []
        # 给coco查找图片用的
        # print(image_path.exists())
        # print(image_path)
        for image_xml in voc_label_path.glob("*.xml"):
            # print(image)
            # 图片是后缀名且文件名在对应标签里
            if image_xml.stem in image_ids:
                xmls.append(image_xml)
        # print(xmls)
        convert(xmls, coco_labels_path / f'{name}.json')
예제 #2
0
def main(args: list):
    if len(args) > 1:
        data_folder_str = args[1]
    else:
        data_folder_str = folder.data_folder()
    image_path = Path(
        __file__).parent.parent / data_folder_str / folder.img_folder()  # 图片数据
    mean, standard = compute(image_path)
    print("mean", str(mean).replace(' ', ','))
    print("std", str(standard).replace(' ', ','))
예제 #3
0
def main(args: list):
    if len(args) > 1:
        data_folder = Path(args[1])
    else:
        data_folder = Path(folder.data_folder())
    label_folder_path = Path(
        __file__).parent.parent / data_folder / folder.annotation_folder(
        )  # 标记的数据
    image_folder_path = Path(
        __file__).parent.parent / data_folder / folder.img_folder()  # 图片数据
    # print(label_folder_path.exists())
    annotation_set = set(f.stem for f in label_folder_path.glob("*.xml"))
    # print(annotation_set)
    for img_path in image_folder_path.rglob(r".*\.png|.*\.jpg|.*\.jpeg"):
        if img_path.stem not in annotation_set:
            xml = create_xml(img_path)
            # print(xml)
            filename = f'{img_path.stem}.xml'
            # print(filename)
            with (label_folder_path / filename).open('w', newline='\n') as f:
                f.write(xml)
예제 #4
0
def main(args: list):
    if len(args) > 1:
        data_folder_str = args[1]
    else:
        data_folder_str = folder.data_folder()
    voc_label_path = Path(
        __file__).parent.parent / data_folder_str / folder.annotation_folder(
        )  # VOC格式标记的数据
    image_path = Path(
        __file__).parent.parent / data_folder_str / folder.img_folder()  # 图片数据
    image_sets_path = Path(
        __file__).parent.parent / data_folder_str / folder.split_folder(
        )  # 训练集测试集分类数据
    yolo_labels_path = Path(
        __file__).parent.parent / data_folder_str / folder.yolo_labels_folder(
        )  # YOLO格式标记的数据

    if not yolo_labels_path.exists():
        yolo_labels_path.mkdir()
    for name in ('train', 'test'):
        image_ids = \
            set((image_sets_path / f'{name}.txt').open('r').read().strip().split())  # [0001 0002 ...]
        list_file = (Path(__file__).parent.parent /
                     f'darknet_{name}.txt').open('wb')
        # 给darknet查找图片用的,保存在当前目录
        # print(image_path.exists())
        # print(image_path)
        for image in image_path.iterdir():
            # print(image)
            # 图片是后缀名且文件名在对应标签里
            if image.suffix in ('.png', '.jpeg', '.jpg', '.gif',
                                '.bmp') and image.stem in image_ids:
                list_file.write(bytes(image.absolute()))
                list_file.write(bytes('\n', 'utf-8'))

            convert_annotation(voc_label_path / f'{image.stem}.xml',
                               yolo_labels_path / f'{image.stem}.txt')
        list_file.close()
예제 #5
0
def main(args: list):
    if len(args) > 1:
        ratio = float(args[1])
    else:
        ratio = 0.2
    if len(args) > 2:
        data_folder = Path(args[2])
    else:
        data_folder = Path(folder.data_folder())
    label_path = Path(
        __file__).parent.parent / data_folder / folder.annotation_folder(
        )  # 标记的数据
    name_path = Path(
        __file__).parent.parent / data_folder / folder.split_folder(
        )  # 用于训练和测试样本的名字文件的路径
    list_path = list(f'{f.stem}\n' for f in label_path.glob("*.xml"))
    train_list, test_list = split(list_path, True, ratio)
    print(train_list)
    print(test_list)
    for name in ('train', 'test'):
        pth = name_path / f"{name}.txt"
        with pth.open('w', newline='\n') as f:
            f.writelines(eval(f"{name}_list"))