这个代码是参考了两个博客 我是感觉第一篇博客可能有问题,然后自己做了改进,如果我是错误的或者正确的,请各位评论区说一下,感谢
Json格式的数据集标签转化为有效的txt格式(data_coco)_train.json-CSDN博客
COCO(.json)格式 转换为 YOLO(.txt)格式训练(详细介绍,避坑贴)_json to yolo-CSDN博客
# 处理同一个数据集下多个json文件时,仅运行一次class_txt即可
import json
import os
'''
def convert2(size, box):dw = 1.0 / size[0]dh = 1.0 / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)'''
def convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = box[0] + box[2] / 2.0y = box[1] + box[3] / 2.0w = box[2] - box[0]h = box[3] - box[1]# round函数确定(xmin, ymin, xmax, ymax)的小数位数x = x * dwy = y * dhw = w * dwh = h * dhreturn (x, y, w, h)
"存储标签与预测框到txt文件中"
def json_txt(json_path, txt_path):"json_path: 需要处理的json文件的路径""txt_path: 将json文件处理后txt文件存放的文件夹名"# 生成存放json文件的路径if not os.path.exists(txt_path):os.mkdir(txt_path)# 读取json文件with open(json_path, 'r') as f:dict = json.load(f)# 得到images和annotations信息images_value = dict.get("images") # 得到某个键下对应的值annotations_value = dict.get("annotations") # 得到某个键下对应的值# 使用images下的图像名的id创建txt文件'''list=[] # 将文件名存储在list中for i in images_value:open(txt_path + str(i.get("file_name")) + '.txt', 'w')list.append(i.get("id"))for i in images_value:for j in annotations_value:if j.get("image_id") == i.get("id"):# bbox标签归一化处理num = sum(j.get('bbox'))new_list = [round(m / num, 6) for m in j.get('bbox')] # 保留六位小数with open(txt_path + str(i.get("file_name")) + '.txt', 'a') as file1: # 写入txt文件中print(j.get("category_id"), new_list[0], new_list[1], new_list[2], new_list[3], file=file1)print("结束")
'''# 将id对应图片的bbox写入txt文件中for i in images_value:for j in annotations_value:if j.get("image_id") == i.get("id"):# bbox标签归一化处理w = i.get("width")h = i.get("height")bbox = j.get('bbox') # 保留六位小数xmin = bbox[0]xmax = bbox[1]ymin = bbox[2]ymax = bbox[3]b = (float(xmin), float(xmax), float(ymin), float(ymax))new_list = convert((w, h), b)with open(txt_path + str(i.get("file_name"))[:-3] + 'txt', 'a') as file1: # 写入txt文件中print(j.get("category_id"), str(new_list[0]), str(new_list[1]), str(new_list[2]), str(new_list[3]), file=file1)print("结束")"将id对应的标签存储在class.txt中"
def class_txt(json_path, class_txt_path):"json_path: 需要处理的json文件的路径""txt_path: 将json文件处理后存放所需的txt文件名"# 生成存放json文件的路径with open(json_path, 'r') as f:dict = json.load(f)# 得到categories下对应的信息categories_value = dict.get("categories") # 得到某个键下对应的值# 将每个类别id与类别写入txt文件中with open(class_txt_path, 'a') as file0:for i in categories_value:print(i.get("id"), i.get('name'), file=file0)print("结束")json_txt("./annotations/val.json", "val_labels/")
#class_txt("./annotations/train.json", "id_categories.txt")