labelimg打标 最后一个标 不想要了
而yolo格式下 所有标注 如下
0 0.554861 0.434375 0.031944 0.020313
1 0.549306 0.488281 0.020833 0.017188
2 0.502778 0.432031 0.022222 0.017188
3 0.550000 0.405469 0.022222 0.014063
4 0.602778 0.435937 0.019444 0.023438
写脚本 仅保留前4行
使用网上各种程序进行裁缝
先创建个两个文件夹:把所有原txt文档放在labels_old
下,labels_new
为空文件夹
import os, shutil,re
if __name__=='__main__':
work_dir = 'F:\\lianxi\\yolo\\test\\1160_10点\\xya1b1c1\\labels_old\\'
new_dir = 'F:\\lianxi\\yolo\\test\\1160_10点\\xya1b1c1\\labels_new\\'
for parent, dirnames, filenames in os.walk(work_dir, followlinks=True):
for filename in filenames:
file_path = os.path.join(parent, filename)
print('file_path:'+file_path)#file_path为path:原文件加下包含文件名.txt
print('parent:' + parent)#parent为原文件夹path
print('filename:' + filename)#filename为原文件名
print('')
file = open(file_path,"r+",encoding='UTF-8')
newFile = open(new_dir+filename,"w",encoding='UTF-8')
print('new_dir+filename:' + new_dir+filename) # new_dir+filename为新文件路径
flag=-1
for line in file.readlines(): #遍历每行
# print('line:'+line)
flag = flag + 1
if ( flag == 4):
break
print('line:'+line)
newFile.writelines(line)
print (filename)
print('——'*100)
newFile.close()
file.close()
运行结果
file_path:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_old\330bei.txt
parent:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_old
filename:330bei.txt
new_dir+filename:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_new\330bei.txt
line:0 0.554861 0.434375 0.031944 0.020313
line:1 0.549306 0.488281 0.020833 0.017188
line:2 0.502778 0.432031 0.022222 0.017188
line:3 0.550000 0.405469 0.022222 0.014063
330bei.txt
file_path:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_old\331bei.txt
parent:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_old
filename:331bei.txt
new_dir+filename:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_new\331bei.txt
line:0 0.556250 0.441797 0.029167 0.019531
line:1 0.552778 0.490625 0.022222 0.014063
line:2 0.502778 0.437500 0.016667 0.015625
line:3 0.550694 0.412109 0.020833 0.013281
331bei.txt
file_path:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_old\332bei.txt
parent:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_old
filename:332bei.txt
new_dir+filename:F:\lianxi\yolo\test\1160_10点\xya1b1c1\labels_new\332bei.txt
line:0 0.564583 0.448047 0.023611 0.016406
line:1 0.556944 0.500781 0.027778 0.012500
line:2 0.513889 0.446484 0.025000 0.016406
line:3 0.559028 0.420312 0.023611 0.012500
332bei.txt
再贴一个找到的 单独删除某个文件的 第几行 和倒数第几行
import os
def delete_lines(filename, head,tail):
fin = open(filename, 'r')
a = fin.readlines()
fout = open(filename, 'w')
b = ''.join(a[head:-tail])
fout.write(b)
file = r'F:\lianxi\yolo\test\1160_10点\xya1b1\a.txt'
delete_lines(file,6,1) #删除第6行 和倒数第1行
修改后:删除最后一行:
import os
def delete_lines(filename, tail):
fin = open(filename, 'r')
a = fin.readlines()
fout = open(filename, 'w')
b = ''.join(a[:-tail])
fout.write(b)
file = r'F:\lianxi\yolo\test\1160_10点\xya1b1\a.txt' #F:\lianxi\yolo\test\1160_10点\xya1b1\a.txt
delete_lines(file,1)