场景:做深度学习时,常常标注完某一类别数据后,又需要添加一个类别,比如限速牌标注,但前期没有做红绿灯标注。
方法只有逐个返回图片进行增加标注,这样费时费力。怎么样可以物尽其用,把图片内容需要添加的类别呢?
方法:如上两种方法,例如红绿灯做单独标注,拿到限速和红绿灯两份标注数据,再考虑文件合并。另外方法借用公共数据集(coco)等预训练模型,第九类别即使红绿灯,做单一识别,即可得到红绿灯数据坐标。具体方法就是放入模型做检测,启用--save-txt参数即可。这里不细讲,有需要的话再详细上教程。
得到两个数据集后,考虑合并文件,例如下所示
文件一,假设目标物a
文件二类型b
同名文件,想要得到合并操作如下,
注意细节,自己不想写代码的可以问chat-gpt
import os
# 指定两个文件夹路径
folder_path1 = 'D:/weixin/tk/tk1'
folder_path2 = 'D:/weixin/tk/tk2'
folder_path3 = 'D:/weixin/tk/tk3' #合并的文件夹
# 获取文件夹1下所有txt文件名
txt_files1 = [f for f in os.listdir(folder_path1) if os.path.isfile(os.path.join(folder_path1, f)) and f.endswith('.txt')]
# 获取文件夹2下所有txt文件名
txt_files2 = [f for f in os.listdir(folder_path2) if os.path.isfile(os.path.join(folder_path2, f)) and f.endswith('.txt')]
# 合并同名txt文件
for txt_file1 in set([f.split('.')[0] for f in txt_files1]):
# 检查文件夹1中同名txt文件的内容是否与文件夹2中相同
file1_path = os.path.join(folder_path1, txt_file1 + '.txt')
file2_path = os.path.join(folder_path2, txt_file1 + '.txt')
if os.path.exists(file2_path):
with open(file1_path) as file1_obj, open(file2_path) as file2_obj:
if file1_obj.read() != file2_obj.read():
# 文件内容不同,合并两个txt文件
merged_file_name = os.path.join(folder_path3, txt_file1 + '.txt') #_merged
with open(merged_file_name, 'w') as outfile:
# 写入文件夹1中同名txt文件的内容
with open(file1_path) as infile_obj:
outfile.write(infile_obj.read())
# 写入文件夹2中同名txt文件的内容
with open(file2_path) as infile_obj:
outfile.write(infile_obj.read())
else:
# 文件夹2中不存在同名txt文件,直接将文件夹1中的文件复制到合并后的文件中
merged_file_name = os.path.join(folder_path3, txt_file1 + '_merged.txt')
with open(merged_file_name, 'w') as outfile:
with open(file1_path) as infile_obj:
outfile.write(infile_obj.read())
这样可以批量处理不同文件,省时省力。代码是科技所写,有多余判断,但完全能用