写在前面
datasets
├─images
│ ├─test
│ ├─train
│ └─val
├─annotations
├─test
├─train
└─val
代码
- 指定好四个参数即可
--root_dir
:待转换的图像的路径,例如我传入的是训练集的路径...\images\train
--save_dir
:保存转换后的json文件的路径--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\train', 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', 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_train.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 ...
'''
dataset = {'categories': [], 'annotations': [], 'images': []}
for i, cls in enumerate(classes, 0):
dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'mark'})
ann_id_cnt = 0
for k, index in enumerate(tqdm(indexes)):
txtPath = index.replace('images', 'labels').replace('.jpg', '.txt')
im = cv2.imread(index)
imageFile = index.split('/')[-1]
height, width, _ = im.shape
if not os.path.exists(txtPath):
continue
dataset['images'].append({'file_name': imageFile,
'id': int(imageFile[:-4]) if imageFile[:-4].isnumeric() else imageFile[:-4],
'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])
H, W, _ = im.shape
x1 = (x - w / 2) * W
y1 = (y - h / 2) * H
x2 = (x + w / 2) * W
y2 = (y + h / 2) * H
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': int(imageFile[:-4]) if imageFile[:-4].isnumeric() else imageFile[:-4],
'iscrowd': 0,
'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)
print('Save annotation to {}'.format(save_path))
if __name__ == "__main__":
yolo2coco(arg)