PaddlePaddle / PaddleSeg

Easy-to-use image segmentation library with awesome pre-trained model zoo, supporting wide-range of practical tasks in Semantic Segmentation, Interactive Segmentation, Panoptic Segmentation, Image Matting, 3D Segmentation, etc.
https://arxiv.org/abs/2101.06175
Apache License 2.0
8.73k stars 1.68k forks source link

分割图像标注转化yolo格式 #3843

Open chinesejunzai12 opened 3 weeks ago

chinesejunzai12 commented 3 weeks ago

问题确认 Search before asking

请提出你的问题 Please ask your question

在使用EIseg标注图像分割完成之后, 有对应的脚本转化为yolo格式么, 如果有的话支持那种, 是coco转yolo, 还是json转yolo

haoyuying commented 2 weeks ago

记忆中好像分割支持yolo,voc,coco和labelme的json,你可以翻下保存格式瞅瞅,如果没有,copilot老师写了一个coco转yolo的脚本,参考改改: import json import os from pycocotools.coco import COCO

def convert_coco_to_yolo(coco_annotation_file, output_dir):

创建输出目录

if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 加载 COCO 标注文件
coco = COCO(coco_annotation_file)

# 获取图像信息
images = coco.loadImgs(coco.getImgIds())

for img in images:
    img_id = img['id']
    img_width = img['width']
    img_height = img['height']
    img_filename = img['file_name']

    # 获取该图像的所有标注
    ann_ids = coco.getAnnIds(imgIds=img_id)
    anns = coco.loadAnns(ann_ids)

    yolo_annotations = []

    for ann in anns:
        if 'segmentation' in ann:
            segmentation = ann['segmentation']
            category_id = ann['category_id']

            for seg in segmentation:
                # 将分割坐标转换为 YOLO 格式
                x_coords = seg[0::2]
                y_coords = seg[1::2]
                x_min = min(x_coords)
                x_max = max(x_coords)
                y_min = min(y_coords)
                y_max = max(y_coords)

                x_center = (x_min + x_max) / 2 / img_width
                y_center = (y_min + y_max) / 2 / img_height
                width = (x_max - x_min) / img_width
                height = (y_max - y_min) / img_height

                yolo_annotations.append((category_id, x_center, y_center, width, height))

    # 保存 YOLO 格式标注
    yolo_filename = os.path.splitext(img_filename)[0] + '.txt'
    yolo_filepath = os.path.join(output_dir, yolo_filename)

    with open(yolo_filepath, 'w') as f:
        for annotation in yolo_annotations:
            category_id, x_center, y_center, width, height = annotation
            f.write(f"{category_id} {x_center} {y_center} {width} {height}\n")

示例用法

coco_annotation_file = 'path/to/your/coco_annotations.json' output_dir = 'path/to/save/yolo_annotations' convert_coco_to_yolo(coco_annotation_file, output_dir)

chinesejunzai12 commented 2 weeks ago

记忆中好像分割支持yolo,voc,coco和labelme的json,你可以翻下保存格式瞅瞅,如果没有,copilot老师写了一个coco转yolo的脚本,参考改改: import json import os from pycocotools.coco import COCO

def convert_coco_to_yolo(coco_annotation_file, output_dir): # 创建输出目录 if not os.path.exists(output_dir): os.makedirs(output_dir)

# 加载 COCO 标注文件
coco = COCO(coco_annotation_file)

# 获取图像信息
images = coco.loadImgs(coco.getImgIds())

for img in images:
    img_id = img['id']
    img_width = img['width']
    img_height = img['height']
    img_filename = img['file_name']

    # 获取该图像的所有标注
    ann_ids = coco.getAnnIds(imgIds=img_id)
    anns = coco.loadAnns(ann_ids)

    yolo_annotations = []

    for ann in anns:
        if 'segmentation' in ann:
            segmentation = ann['segmentation']
            category_id = ann['category_id']

            for seg in segmentation:
                # 将分割坐标转换为 YOLO 格式
                x_coords = seg[0::2]
                y_coords = seg[1::2]
                x_min = min(x_coords)
                x_max = max(x_coords)
                y_min = min(y_coords)
                y_max = max(y_coords)

                x_center = (x_min + x_max) / 2 / img_width
                y_center = (y_min + y_max) / 2 / img_height
                width = (x_max - x_min) / img_width
                height = (y_max - y_min) / img_height

                yolo_annotations.append((category_id, x_center, y_center, width, height))

    # 保存 YOLO 格式标注
    yolo_filename = os.path.splitext(img_filename)[0] + '.txt'
    yolo_filepath = os.path.join(output_dir, yolo_filename)

    with open(yolo_filepath, 'w') as f:
        for annotation in yolo_annotations:
            category_id, x_center, y_center, width, height = annotation
            f.write(f"{category_id} {x_center} {y_center} {width} {height}\n")

示例用法

coco_annotation_file = 'path/to/your/coco_annotations.json' output_dir = 'path/to/save/yolo_annotations' convert_coco_to_yolo(coco_annotation_file, output_dir)

非常感谢, 我试试

chinesejunzai12 commented 2 weeks ago

记忆中好像分割支持yolo,voc,coco和labelme的json,你可以翻下保存格式瞅瞅,如果没有,copilot老师写了一个coco转yolo的脚本,参考改改: import json import os from pycocotools.coco import COCO

def convert_coco_to_yolo(coco_annotation_file, output_dir): # 创建输出目录 if not os.path.exists(output_dir): os.makedirs(output_dir)

# 加载 COCO 标注文件
coco = COCO(coco_annotation_file)

# 获取图像信息
images = coco.loadImgs(coco.getImgIds())

for img in images:
    img_id = img['id']
    img_width = img['width']
    img_height = img['height']
    img_filename = img['file_name']

    # 获取该图像的所有标注
    ann_ids = coco.getAnnIds(imgIds=img_id)
    anns = coco.loadAnns(ann_ids)

    yolo_annotations = []

    for ann in anns:
        if 'segmentation' in ann:
            segmentation = ann['segmentation']
            category_id = ann['category_id']

            for seg in segmentation:
                # 将分割坐标转换为 YOLO 格式
                x_coords = seg[0::2]
                y_coords = seg[1::2]
                x_min = min(x_coords)
                x_max = max(x_coords)
                y_min = min(y_coords)
                y_max = max(y_coords)

                x_center = (x_min + x_max) / 2 / img_width
                y_center = (y_min + y_max) / 2 / img_height
                width = (x_max - x_min) / img_width
                height = (y_max - y_min) / img_height

                yolo_annotations.append((category_id, x_center, y_center, width, height))

    # 保存 YOLO 格式标注
    yolo_filename = os.path.splitext(img_filename)[0] + '.txt'
    yolo_filepath = os.path.join(output_dir, yolo_filename)

    with open(yolo_filepath, 'w') as f:
        for annotation in yolo_annotations:
            category_id, x_center, y_center, width, height = annotation
            f.write(f"{category_id} {x_center} {y_center} {width} {height}\n")

示例用法

coco_annotation_file = 'path/to/your/coco_annotations.json' output_dir = 'path/to/save/yolo_annotations' convert_coco_to_yolo(coco_annotation_file, output_dir)

你好这个好像只是标注矩形框的, 实例分割需要多边形的, 而且转换精度也是一个问题, 所以这个还没有很好的转换

cuicheng01 commented 2 weeks ago

如果没有的话,可以试试用copilot或者文心快码写一个转换脚本呢