Yolov5进行目标检测流程在下文中已做说明:
Python------一文详解使用yolov5进行目标检测全流程(无需gpu)_yolo不用gpu-CSDN博客
在Yolov5使用中中,标签数据里类别的中文名,需要转换成类别编号。相关代码如下:
python
## 将标签中的英文名转化为对应的类别编号
import os
path = 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('替换完成')