python txt預處理,處理成 需要的格式

處理之前的txt文件內容:bounding_boxes_17889.txt

處理之後的txt文件內容:test.txt

代碼如下:

#encoding:utf-8

txt_path='./bounding_boxes_17889.txt'
after_handling_txt='./Annotations/test.txt'


f=open(txt_path,'r')
f_lines=f.readlines()
f_txt=open(after_handling_txt,'w')

cls_name='face'

for line in f_lines:
    lines=str(line).split(' ')
    bbx_num=lines[1:]
    # 如果只有路徑,沒有標註的座標點 ,則跳過,進行下一步
    if len(lines)==1:
        continue
    file_name=lines[0].split("/")[-1].split(".")[0]
    f_txt.write(file_name+' '+cls_name+' '+bbx_num[0]+' '+bbx_num[1]+' '+bbx_num[2]+' '+bbx_num[3])
    # f_txt.write(''.join(file_name,cls_name,bbx_num[0],bbx_num[1],bbx_num[2],bbx_num[3]))
    

ps:代碼並不亂,只因是小白,記錄一下的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章