YOLOV5 V8数据集中文标签踩坑笔记
labelme2yolov2Seg(jsonfilePath=jsonfilePath, resultDirPath=resultDirPath, classList=["钢渣粗骨料","天然粗骨料","孔隙"])jsonfilePath = r"D:\Code\PycharmProjects\YOLOV8\ultralytics-main\my_seg\json"# 要转换的json文件所在目录
·
标注使用了中文报错,在转yolo txt的时候就会出现问题
即使添加
# coding=utf-8 # -*- coding:utf-8 -*-
也不能解决问题
当出现报错时候不管是utf-8 还是utf-16 都会报错gbk ,还需检查下设置是否为utf-8,必要条件
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
就算


我的方案是把标签转为英文,网站上的代码如果直接抄还是会中文报错。
json批量替换来自:http://t.csdn.cn/eZ8t0
附上他的代码,在open添加了解码
# coding=utf-8
# -*- coding:utf-8 -*-
import json
import os
path = 'D:\\Code\\PycharmProjects\\YOLOV8\\ultralytics-main\\my_seg\\json'
dirs = os.listdir(path) # 从路径获取文件列表
num_flag = 0
for file in dirs: # 循环读取路径下的文件并筛选输出
if os.path.splitext(file)[1] == ".json": # 筛选csv文件
num_flag = num_flag +1
print("path ===== ",file)
# print(os.path.join(path,file))
with open(os.path.join(path,file),'r', encoding='utf-8')as load_f:
load_dict = json.load(load_f)
n=len(load_dict['shapes'])
for i in range (0,n):
if load_dict['shapes'][i]['label'] == '钢渣粗骨料':
# 天然粗骨料 钢渣粗骨料 孔隙
load_dict['shapes'][i]['label'] = 'Steel Slag aggregate'
# natural aggregate Steel Slag aggregate Voids
# if load_dict['shapes'][i]['label'] == 'interstice':
# load_dict['shapes'][i]['label'] = 'cleft'
with open(os.path.join(path,file),'w') as dump_f:
json.dump(load_dict, dump_f)
if(num_flag == 0):
print('所选文件夹不存在json文件,请重新确认要选择的文件夹')
else:
print('共{}个json文件'.format(num_flag))
成功替换后,再json转yolo txt 文件
# -*- coding:utf-8 -*-
# coding=utf-8
import json
import os
import glob
import os.path as osp
def labelme2yolov2Seg(jsonfilePath="", resultDirPath="", classList=["buds"]):
"""
此函数用来将labelme软件标注好的数据集转换为yolov5_7.0sege中使用的数据集
:param jsonfilePath: labelme标注好的*.json文件所在文件夹
:param resultDirPath: 转换好后的*.txt保存文件夹
:param classList: 数据集中的类别标签
:return:
"""
# 0.创建保存转换结果的文件夹
if (not os.path.exists(resultDirPath)):
os.mkdir(resultDirPath)
# 1.获取目录下所有的labelme标注好的Json文件,存入列表中
jsonfileList = glob.glob(osp.join(jsonfilePath, "*.json"))
print(jsonfileList) # 打印文件夹下的文件名称
# 2.遍历json文件,进行转换
for jsonfile in jsonfileList:
# 3. 打开json文件
with open(jsonfile, "r", encoding='utf-8') as f:
file_in = json.load(f)
# 4. 读取文件中记录的所有标注目标
shapes = file_in["shapes"]
# 5. 使用图像名称创建一个txt文件,用来保存数据
with open(resultDirPath + "\\" + jsonfile.split("\\")[-1].replace(".json", ".txt"), "w") as file_handle:
# 6. 遍历shapes中的每个目标的轮廓
for shape in shapes:
# 7.根据json中目标的类别标签,从classList中寻找类别的ID,然后写入txt文件中
file_handle.writelines(str(classList.index(shape["label"])) + " ")
# 8. 遍历shape轮廓中的每个点,每个点要进行图像尺寸的缩放,即x/width, y/height
for point in shape["points"]:
x = point[0] / file_in["imageWidth"] # mask轮廓中一点的X坐标
y = point[1] / file_in["imageHeight"] # mask轮廓中一点的Y坐标
file_handle.writelines(str(x) + " " + str(y) + " ") # 写入mask轮廓点
# 9.每个物体一行数据,一个物体遍历完成后需要换行
file_handle.writelines("\n")
# 10.所有物体都遍历完,需要关闭文件
file_handle.close()
# 10.所有物体都遍历完,需要关闭文件
f.close()
if __name__ == "__main__":
jsonfilePath = r"D:\Code\PycharmProjects\YOLOV8\ultralytics-main\my_seg\json" # 要转换的json文件所在目录
resultDirPath = r"D:\Code\PycharmProjects\YOLOV8\ultralytics-main\my_seg\txt" # 要生成的txt文件夹
labelme2yolov2Seg(jsonfilePath=jsonfilePath, resultDirPath=resultDirPath, classList=["钢渣粗骨料","天然粗骨料","孔隙"])
# 更改为自己的类别名
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)