(亲测好用)YOLO格式txt数据集转COCO格式json

1、数据集结构形式

YOLO格式数据集:

b文件夹下有images和labels两个文件夹,分别存放图片和标签格式的数据。

两个文件夹下分别有train、val、test三个文件夹,里面存放对应的数据。

COCO数据集格式:

COCO格式数据文件夹下有三个文件夹,annotations里面存放三个json标注文件(代码生成),其他三个文件夹下分别存放对应的图片文件(手动移动)。

2、代码实现

只需要替换最后的两行文件夹路径。以及前面categories 里自己数据集的标签类别和id。

python 复制代码
import json
import os
import shutil
import cv2
 
# info ,license,categories 结构初始化;
# 在train.json,val.json,test.json里面信息是一致的;
 
# info,license暂时用不到
info = {
    "year": 2024,
    "version": '1.0',
    "date_created": 2024 - 12 - 7
}
 
licenses = {
    "id": 1,
    "name": "null",
    "url": "null",
}
 
#自己的标签类别,跟yolo的数据集类别要对应好;
#id就是0,1,2,3 ...依次递增。 name就是标签名称,比如car、person。。。
categories = [
    {
        "id": 0,
        "name": 'car',
        "supercategory": 'lines',
    },
    {
        "id": 1,
        "name": 'person',
        "supercategory": 'lines',
    },
{
        "id": 2,
        "name": 'hat',
        "supercategory": 'lines',
    }
 
]
 
#初始化train,test、valid 数据字典
# info licenses categories 在train和test里面都是一致的;
train_data = {'info': info, 'licenses': licenses, 'categories': categories, 'images': [], 'annotations': []}
test_data = {'info': info, 'licenses': licenses, 'categories': categories, 'images': [], 'annotations': []}
valid_data = {'info': info, 'licenses': licenses, 'categories': categories, 'images': [], 'annotations': []}
 
# image_path 对应yolov8的图像路径,比如images/train;
# label_path 对应yolov8的label路径,比如labels/train 跟images要对应;
def yolo_covert_coco_format(image_path, label_path):
    images = []
    annotations = []
    for index, img_file in enumerate(os.listdir(image_path)):
        if img_file.endswith('.jpg'):
            image_info = {}
            img = cv2.imread(os.path.join(image_path, img_file))
            height, width, channel = img.shape
            image_info['id'] = index
            image_info['file_name'] = img_file
            image_info['width'], image_info['height'] = width, height
        else:
            continue
        if image_info != {}:
            images.append(image_info)
        # 处理label信息-------
        label_file = os.path.join(label_path, img_file.replace('.jpg', '.txt'))
        with open(label_file, 'r') as f:
            for idx, line in enumerate(f.readlines()):
                info_annotation = {}
                class_num, xs, ys, ws, hs = line.strip().split(' ')
                class_id, xc, yc, w, h = int(class_num), float(xs), float(ys), float(ws), float(hs)
                xmin = (xc - w / 2) * width
                ymin = (yc - h / 2) * height
                xmax = (xc + w / 2) * width
                ymax = (yc + h / 2) * height
                bbox_w = int(width * w)
                bbox_h = int(height * h)
                img_copy = img[int(ymin):int(ymax),int(xmin):int(xmax)].copy()
 
                info_annotation["category_id"] = class_id  # 类别的id
                info_annotation['bbox'] = [xmin, ymin, bbox_w, bbox_h]  ## bbox的坐标
                info_annotation['area'] = bbox_h * bbox_w ###area
                info_annotation['image_id'] = index # bbox的id
                info_annotation['id'] = index * 100 + idx  # bbox的id
                # cv2.imwrite(f"./temp/{info_annotation['id']}.jpg", img_copy)
                info_annotation['segmentation'] = [[xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax]]  # 四个点的坐标
                info_annotation['iscrowd'] = 0  # 单例
                annotations.append(info_annotation)
    return images, annotations
 
# key == train,test,val
# 对应要生成的json文件,比如instances_train.json,instances_test.json,instances_val.json
# 只是为了不重复写代码。。。。。
def gen_json_file(yolov8_data_path, coco_format_path, key):
    print('a1')
    # json path
    json_path = os.path.join(coco_format_path, f'annotations/instances_{key}.json')
    dst_path = os.path.join(coco_format_path, f'{key}')
    if not os.path.exists(os.path.dirname(json_path)):
        os.makedirs(os.path.dirname(json_path), exist_ok=True)
    data_path = os.path.join(yolov8_data_path, f'images/{key}')
    label_path = os.path.join(yolov8_data_path, f'labels/{key}')
    images, anns = yolo_covert_coco_format(data_path, label_path)
    print('a2')
    if key == 'train':
        train_data['images'] = images
        train_data['annotations'] = anns
        with open(json_path, 'w') as f:
            json.dump(train_data, f, indent=2)
        # shutil.copy(data_path,'')
        print('a3')
    elif key == 'test':
        test_data['images'] = images
        test_data['annotations'] = anns
        with open(json_path, 'w') as f:
            json.dump(test_data, f, indent=2)
    elif key == 'val':
        valid_data['images'] = images
        valid_data['annotations'] = anns
        with open(json_path, 'w') as f:
            json.dump(valid_data, f, indent=2)
    else:
        print(f'key is {key}')
    print(f'generate {key} json success!')
    print('a4')
    return


if __name__ == '__main__':

    # 将下列两行代码路径替换为自己的数据集路径
    yolov8_data_path = 'C:/Users/37449/Desktop/b'   #该路径为YOLO格式数据集根目录路径
    coco_format_path = 'C:/Users/37449/Desktop/coco'    #该路径为存放coco格式数据集的根目录路径
    gen_json_file(yolov8_data_path, coco_format_path,key='train')
    gen_json_file(yolov8_data_path, coco_format_path,key='val')
    gen_json_file(yolov8_data_path, coco_format_path, key='test')
相关推荐
AI街潜水的八角4 小时前
工业缺陷检测实战——基于深度学习YOLOv10神经网络PCB缺陷检测系统
pytorch·深度学习·yolo
mit6.8249 小时前
What is Json?
c++·学习·json
金色旭光9 小时前
目标检测高频评价指标的计算过程
算法·yolo
AI街潜水的八角18 小时前
PyTorch框架——基于深度学习YOLOv8神经网络学生课堂行为检测识别系统
pytorch·深度学习·yolo
Hugh&1 天前
(开源)基于Django+Yolov8+Tensorflow的智能鸟类识别平台
python·yolo·django·tensorflow
小安同学iter1 天前
Web开发 -前端部分-HTML5新特性
javascript·css·正则表达式·json·css3·html5
我真不会起名字啊1 天前
“深入浅出”系列之C++:(10)nlohmann Json库
json
天天代码码天天1 天前
C# OpenCvSharp 部署读光-票证检测矫正模型(cv_resnet18_card_correction)
人工智能·深度学习·yolo·目标检测·计算机视觉·c#·票证检测矫正
前网易架构师-高司机2 天前
行人识别检测数据集,yolo格式,PASICAL VOC XML,COCO JSON,darknet等格式的标注都支持,准确识别率可达99.5%
xml·yolo·行人检测数据集
Watermelo6172 天前
使用JSONObject.getString()时报错:Cannot resolve method ‘getString‘ in ‘JSONObject‘,详解JSONObject三种库的用法
java·开发语言·spring boot·后端·java-ee·json·springboot