Yolo数据集格式转换

xml转txt格式,只需要修改3处即可,需要修改哪里已经标注出来了

import xml.etree.ElementTree as ET
import os

# 定义类别和其对应的标签(按照自己的类别进行修改)
classes = {
    'aeroplane': 0,
    'bicycle': 1,
    'bird': 2,
    'boat': 3,
    'bottle': 4,
    'bus': 5,
    'car': 6,
    'cat': 7,
    'chair': 8,
    # ...
}

# 定义输入输出路径(按照自己路径修改)
xml_dir = 'path/to/xml/dir'
output_dir = 'path/to/output/dir'

# 获取所有XML文件路径
xml_files = os.listdir(xml_dir)
xml_files = [f for f in xml_files if f.endswith('.xml')]

# 遍历XML文件并进行转换
for xml_file in xml_files:
    # 读取XML文件
    tree = ET.parse(os.path.join(xml_dir, xml_file))
    root = tree.getroot()

    # 获取图像文件名
    img_name = root.find('filename').text

    # 获取图像的宽度和高度
    width = int(root.find('size').find('width').text)
    height = int(root.find('size').find('height').text)

    # 创建输出文件并写入标注信息(看看自己的数据集是jpg还是png格式,对照修改,保持一致)
    with open(os.path.join(output_dir, img_name.replace('jpg', 'txt')), 'w') as f:
        for obj in root.findall('object'):
            # 获取类别名称
            cls_name = obj.find('name').text
            if cls_name not in classes:
                continue

            # 获取类别标签
            cls_id = classes[cls_name]

            # 获取标注框的坐标
            bbox = obj.find('bndbox')
            xmin = int(bbox.find('xmin').text)
            ymin = int(bbox.find('ymin').text)
            xmax = int(bbox.find('xmax').text)
            ymax = int(bbox.find('ymax').text)

            # 转换为相对坐标
            x = (xmin + xmax) / 2 / width
            y = (ymin + ymax) / 2 / height
            w = (xmax - xmin) / width
            h = (ymax - ymin) / height

            # 将标注信息写入输出文件
            f.write(f'{cls_id} {x:.6f} {y:.6f} {w:.6f} {h:.6f}\n')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
YOLO数据集转换为COCO格式需要进行以下步骤: 1. 将YOLO标注文件(.txt)转换为COCO标注文件(.json) 2. 将YOLO图像文件(.jpg)复制到COCO数据集目录下的images文件夹中 3. 在COCO标注文件中添加图像信息 具体实现可以参考以下步骤: 1. 安装cocoapi ``` pip install pycocotools ``` 2. 创建COCO数据集目录结构 ``` coco_dataset/ annotations/ instances_train.json instances_val.json images/ train/ val/ ``` 3. 编写转换脚本 ```python import json import os from PIL import Image # YOLO标注文件路径 yolo_annotation_path = 'yolo_dataset/annotations/train.txt' # COCO数据集路径 coco_dataset_path = 'coco_dataset' # COCO标注文件路径 coco_annotation_path = os.path.join(coco_dataset_path, 'annotations', 'instances_train.json') # 图像目录路径 image_dir = os.path.join(coco_dataset_path, 'images', 'train') # 类别名称映射表 class_name_map = { '0': 'person', '1': 'car', # ... } # COCO标注文件格式 coco_annotation = { "info": {}, "licenses": [], "images": [], "annotations": [], "categories": [] } # 添加类别信息 for class_id, class_name in class_name_map.items(): coco_annotation['categories'].append({ 'id': int(class_id), 'name': class_name, 'supercategory': '' }) # 读取YOLO标注文件 with open(yolo_annotation_path, 'r') as f: lines = f.readlines() # 处理每个标注文件 for line in lines: line = line.strip() image_path, *bboxes = line.split(' ') # 添加图像信息 image_id = len(coco_annotation['images']) + 1 image_name = os.path.basename(image_path) image = Image.open(image_path) width, height = image.size coco_annotation['images'].append({ 'id': image_id, 'file_name': image_name, 'width': width, 'height': height }) # 处理每个bbox for bbox in bboxes: class_id, x_center, y_center, w, h = bbox.split(',') x_min = int(float(x_center) - float(w) / 2 * width) y_min = int(float(y_center) - float(h) / 2 * height) x_max = int(float(x_center) + float(w) / 2 * width) y_max = int(float(y_center) + float(h) / 2 * height) # 添加标注信息 annotation_id = len(coco_annotation['annotations']) + 1 coco_annotation['annotations'].append({ 'id': annotation_id, 'image_id': image_id, 'category_id': int(class_id), 'bbox': [x_min, y_min, x_max - x_min, y_max - y_min], 'area': (x_max - x_min) * (y_max - y_min), 'iscrowd': 0 }) # 保存COCO标注文件 with open(coco_annotation_path, 'w') as f: json.dump(coco_annotation, f) ``` 4. 运行转换脚本 ``` python yolo2coco.py ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科研分母

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值