資料集檔案處理-重新命名檔案
阿新 • • 發佈:2019-01-11
- (1)很多資料集合的照片是取自視訊流,名稱相對十分不規律,類似如下62fd8bf4d53a1b94fbac16738406f10b.jpg 1 0bdec5cccbcade6b6e94087cb5509d98.jpg 1。為了方便管理與處理,要對幾萬張圖片重新命名。圖片全儲存在一個資料夾裡面,而另外的label標籤種類資料全在一個txt文字檔案裡面(如圖)。
- (2)在此,我需要先將圖片與label標籤資料的名稱進行匹配,然後統一重新命名。最後效果如下圖。
接下來直接上程式碼:
# -*- coding:utf-8 -*- import os class ImageRename(): def __init__(self): self.path = 'F:\\18\\eclipse-workspace\\img\\檢測\\images' def rename(self): txt_file = open('F:\\18\\eclipse-workspace\\img\\檢測\\trainf.txt', 'r') # 原始labels.txt的地址 filelist = os.listdir(self.path) total_num = len(filelist) piclist = [] for parent, dirnames, filenames in os.walk(self.path): for pic_name in filenames: pic_name = pic_name[:-4] piclist.append(pic_name) pic_set = set(piclist) i = 0 for eachline in txt_file: data = eachline.strip().split(',') filename = data[0] filename = filename[:-4] for each_pic in pic_set: if each_pic == filename: for item in filelist: item_name=item[:-4] if item_name==filename: if item.endswith('.JPG'): src = os.path.join(os.path.abspath(self.path), item) dst = os.path.join(os.path.abspath(self.path), '0000' + format(str(i), '0>3s') + '.jpeg') os.rename(src, dst) # print('converting %s to %s ...' % (src, dst)) txt_path = 'F:\\18\\eclipse-workspace\\img\\檢測\\train_f.txt' # 生成的txt標註檔案地址 txt = open(txt_path, 'a') new_line = '0000' + format(str(i), '0>3s') + '.JPG' + ' ' + data[1] txt.writelines(new_line) txt.write('\n') txt.close() i = i + 1 elif item.endswith('.jpg'): src = os.path.join(os.path.abspath(self.path), item) dst = os.path.join(os.path.abspath(self.path), '0000' + format(str(i), '0>3s') + '.jpg') os.rename(src, dst) # print('converting %s to %s ...' % (src, dst)) txt_path = 'F:\\18\\eclipse-workspace\\img\\檢測\\'+'train_f'+'.txt' # 生成的txt標註檔案地址 txt = open(txt_path, 'a') new_line = '0000' + format(str(i), '0>3s') + '.jpg' + ' ' + data[1] txt.writelines(new_line) txt.write('\n') txt.close() i = i + 1 txt_file.close() print( 'total %d to rename & converted %d jpgs' % (total_num, i)) if __name__ == '__main__': newname = ImageRename() newname.rename()