本文主要是介绍Python——目标检测标签中的英文名转化为对应的类别编号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Yolov5进行目标检测流程在下文中已做说明:
Python——一文详解使用yolov5进行目标检测全流程(无需gpu)_yolo不用gpu-CSDN博客
在Yolov5使用中中,标签数据里类别的中文名,需要转换成类别编号。相关代码如下:
## 将标签中的英文名转化为对应的类别编号
import ospath = r'./datasets/labels/test1/' # 转换前txt保存的文件夹
save = r'./datasets/labels/test/' # 转换后txt保存的文件夹for item in os.listdir(path): # 遍历文件夹里的文件path_item = os.path.join(path, item)path_item2 = os.path.join(save, item) # 保存的路径a = []with open(path_item, 'r') as f1, open(path_item2, "a") as f2:for line in f1:a.append(line)print(a)for i in a:if ' ' in str(i): # 遍历每一行b = str(i) # 提取第i行m = b.split(' ') # 以空格为分隔符,进行切分dic = {'airplane': "0", # 字典对类型进行转换'airport': "1",'baseballfield': "2",'basketballcourt': "3",'bridge': "4",'chimney': "5",'dam': "6",'Expressway-Service-area': "7",'Expressway-toll-station': "8",'golffield': "9",'groundtrackfield': "10",'harbor': "11",'overpass': "12",'ship': "13",'stadium': "14",'storagetank': "15",'tenniscourt': "16",'trainstation': "17",'vehicle': "18",'windmill': "19",}m[0] = dic.get(m[0]) # 对每行的第一个变量(即类别),将其替换成相应的值b = ' '.join(m) # 将列表m重新拼接成一个字符串print(b)f2.write("%s" % (b))print('替换完成')
这篇关于Python——目标检测标签中的英文名转化为对应的类别编号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!