将YOLO数据集转成COCO格式,单个文件夹转为单个json文件,例如.../images/train转为instance_train.json

写在前面

datasets
├─images
│  ├─test
│  ├─train
│  └─val
├─annotations
   ├─test
   ├─train
   └─val

代码

  • 指定好四个参数即可
    • --root_dir:待转换的图像的路径,例如我传入的是训练集的路径...\images\train
    • --save_dir:保存转换后的json文件的路径,通常都是存放在数据集的annotations子文件下的
    • --classtxt_path:存放类别的文件路径
    • --save_name:转换后的json文件名
import os
import cv2
import json
from tqdm import tqdm
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--root_dir', default=r'F:\A_Publicdatasets\RDD2020-1202\train_valid\RDD2020_together\images\test', type=str, help="root path of images and labels, include ./images and ./labels and classes.txt")
parser.add_argument('--save_dir', type=str, default=r'F:\A_Publicdatasets\RDD2020-1202\train_valid\RDD2020_together\annotations', help="if not split the dataset, give a path to a json file")
parser.add_argument('--classtxt_path', type=str, default=r'G:\pycharmprojects\autodl-yolov7\yolov7-main-biyebase\TXTOCOCO\classes.txt', help="class filepath")
parser.add_argument('--save_name', type=str, default='instances_test.json', help="建议只修改后面的train为val、test等,否则自行改代码")

arg = parser.parse_args()

def yolo2coco(arg):

    with open(arg.classtxt_path, 'r') as f: # 获取类别名
        classes = list(map(lambda x: x.strip(), f.readlines()))

    indexes = []
    imagesdir = arg.root_dir
    for file in os.listdir(imagesdir):
        indexes.append(f'{imagesdir}/{file}')   # 绝对路径

    '''
    下面这段代码是对我自己有用的,看官可将其删除
    正常的文件排布应该为:
    -- images
       --- train
       --- val
       --- test
    而我的是:
    -- images
       --- Czech
            ---- train
            ---- val
            ---- test
       --- India ...
       --- Japan ...
    '''
    # --------------- lwd ---------------- #
    # cities = ['Czech', 'India', 'Japan']
    # setdir = arg.save_path.split('_')[-1].split('.')[0] # 'train' 'val' 'test'
    # indexes = []
    # for city in cities:
    #     city_imagedir = f'F:/A_Publicdatasets/RDD2020-1202/train_valid/{city}/images/{setdir}'
    #     for file in os.listdir(city_imagedir):
    #         indexes.append(f'{city_imagedir}/{file}')
    # --------------- lwd ---------------- #


    dataset = {'categories': [], 'annotations': [], 'images': []}
    for i, cls in enumerate(classes, 0):
        dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'mark'})

    # 标注的id
    ann_id_cnt = 0
    for k, index in enumerate(tqdm(indexes)):
        # 支持 png jpg 格式的图片。
        txtPath = index.replace('images', 'labels').replace('.jpg', '.txt')
        # 读取图像的宽和高
        im = cv2.imread(index)
        imageFile = index.split('/')[-1]    # img.jpg

        height, width, _ = im.shape
        # 添加图像的信息
        if not os.path.exists(txtPath):
            # 如没标签,跳过,只保留图片信息。
            continue
        dataset['images'].append({'file_name': imageFile,
                                  'id': k,
                                  'width': width,
                                  'height': height})
        with open(txtPath, 'r') as fr:
            labelList = fr.readlines()
            for label in labelList:
                label = label.strip().split()
                x = float(label[1])
                y = float(label[2])
                w = float(label[3])
                h = float(label[4])

                # convert x,y,w,h to x1,y1,x2,y2
                H, W, _ = im.shape
                x1 = (x - w / 2) * W
                y1 = (y - h / 2) * H
                x2 = (x + w / 2) * W
                y2 = (y + h / 2) * H
                # 标签序号从0开始计算, coco2017数据集标号混乱,不管它了。
                cls_id = int(label[0])
                width = max(0, x2 - x1)
                height = max(0, y2 - y1)
                dataset['annotations'].append({
                    'area': width * height,
                    'bbox': [x1, y1, width, height],
                    'category_id': cls_id,
                    'id': ann_id_cnt,
                    'image_id': k,
                    'iscrowd': 0,
                    # mask, 矩形是从左上角点按顺时针的四个顶点
                    'segmentation': [[x1, y1, x2, y1, x2, y2, x1, y2]]
                })
                ann_id_cnt += 1

    # 保存结果
    save_path = os.path.join(arg.save_dir, arg.save_name)
    with open(save_path, 'w') as f:
        # json.dump(dataset, f)
        json_str = json.dumps(dataset)
        f.write(json_str)
        print('Save annotation to {}'.format(save_path))


if __name__ == "__main__":
    yolo2coco(arg)
  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
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 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

孟孟单单

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

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

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

打赏作者

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

抵扣说明:

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

余额充值