制作JPEGImages出现的bug

我用的是下面这个脚本进行改名字:

import os
import sys
 
path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages"

count = 0

for (path,dirs,files) in os.walk(path):
    for filename in files:
        count += 1
        new_name = "%06d"%(count)+'.jpg'
        print new_name
        os.rename(path+"/"+filename,path+"/"+new_name)
print count

但制作出来的图片顺序和原顺序不一样:

原因在于os.walk读取目录的文件不是按顺序的,或者说按某种顺序但我不知道,至少没有按照文件夹本来的文件顺序返回

 

利用all_image_index这个文件来实现不改变顺序的同时修改了文件的名字

import os
import sys
 
path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages"


count = 0

with open('/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2007/all_image_index.txt','r') as file:
    for line in file:
        count += 1
        old_name = line.strip()
        # print new_name
        new_name = "%06d"%(count)+'.jpg'
        os.rename(path+"/"+old_name,path+"/"+new_name)

 

posted @ 2017-09-21 22:13  有梦就要去实现他  阅读(301)  评论(0编辑  收藏  举报