训练深度学习网络时,往往要给自己的数据集按照一定的格式批量重命名,这里提供一个好用的Python脚本
# -*- coding:utf8 -*-
import os
class BatchRename():
'''
批量重命名文件夹中的图片文件
'''
def __init__(self):
self.path = '/home/luke/darknet/data/person' #存放图片的文件夹路径
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 1
for item in filelist:
if item.endswith('.jpg'): #图片格式为jpg
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), 'person_' + str(i) + '.jpg') #设置新的图片名称
try:
os.rename(src, dst)
print ("converting %s to %s ..." % (src, dst))
i = i + 1
except:
continue
print ("total %d to rename & converted %d jpgs" % (total_num, i))
if __name__ == '__main__':
demo = BatchRename()
demo.rename()
脚本运行结果:
参考链接:
https://blog.csdn.net/lwplwf/article/details/78368229
https://blog.csdn.net/liuyan20062010/article/details/78891370
来源:CSDN
作者:Louiseluke
链接:https://blog.csdn.net/u014797226/article/details/80028459