Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
chinesejunzai12 opened this issue Nov 7, 2024 · 4 comments
Open
1 task done

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

chinesejunzai12 opened this issue Nov 7, 2024 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@chinesejunzai12
Copy link

问题确认 Search before asking

  • 我已经搜索过问题,但是没有找到解答。I have searched the question and found no related answer.

请提出你的问题 Please ask your question

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

@chinesejunzai12 chinesejunzai12 added the question Further information is requested label Nov 7, 2024
@haoyuying
Copy link
Collaborator

记忆中好像分割支持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
Copy link
Author

记忆中好像分割支持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
Copy link
Author

记忆中好像分割支持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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants