在深度学习处理数据时经常会用到的一步操作,路径名因人而异,可以根据需求自行更改。
import os
# get .txt document
rootdir=os.path.join('要读取的txt文件的绝对路径')
# read
write_path=open('要写入的txt文件的绝对路径/write.txt','w')
for (dirpath,dirnames,filenames) in os.walk(rootdir):
for filename in filenames:
if os.path.splitext(filename)[1]=='.txt':
write_path.write('要写的绝对路径'+filename+'\n')
write_path.close()
import os
import cv2
from tqdm import tqdm
def main(source_root):
cwd = os.getcwd() # delete '.DS_Store' existed in the source_root
os.chdir(source_root)
os.system("find . -name '*.DS_Store' -type f -delete")
os.chdir(cwd)
write_path=open('./data/refined_casia.txt','w')
for subfolder in tqdm(os.listdir(source_root)):
for image_name in os.listdir(os.path.join(source_root, subfolder)):
print("Processing\t{}".format(os.path.join(source_root, subfolder, image_name)))
img = cv2.imread(os.path.join(source_root, subfolder, image_name))
if type(img) == type(None):
print("damaged image %s, del it" % (img))
os.remove(img)
continue
write_path.write(os.path.join(source_root, subfolder, image_name.split('.')[0] + '.jpg' + '\n'))
write_path.close()
if __name__ == "__main__":
main(source_root = "./data/imgs")
来源:oschina
链接:https://my.oschina.net/timebear/blog/4330162