有时候在用caffe训练模型的时候,会因为数据集的某些图片损坏而出现一些莫名其妙的错误,因此写了这样一个判断图片是否可用的代码,用来清洗数据
def get_image_files():
path = 'images_path'
files = os.listdir(path)
s=[]
for file in files:
s.append(file)
return s
def remove_bad_images():
s = get_image_files()
for image in s:
try:
image_file = 'images_path' +image
img = Image.open(image_file)
img.verify()
except IOError:
print(image_file)
shutil.move(image_file,'/home/data/errimg')