问题
百度网盘下载的kitti数据集图片存在损坏或者丢失,为了后续处理需要对这些文件进行增减等处理。所以写了一个python脚本检查这些错误或者丢失的文件。
链接: https://pan.baidu.com/s/1l7oKQMeTDnaYa6rqZAjXpw 提取码: 8yad
–来自百度网盘超级会员v5的分享
对于截断的图片,没有找到合适的修复方法,就做了一个简单的裁剪。如果你有其他方法修复,欢迎交流
code
# -*- coding: utf-8 -*-
import io
import os
from PIL import Image
NO_png=[]
Bad_png=[]
imageSets_path='/home/zgq/Documents/dataSet/kitti/ImageSets'
path ='/home/zgq/Documents/dataSet/kitti/training'
txtfile=(os.path.join(imageSets_path,'trainval.txt'))
f= os.listdir(path+'/image_2')
def checkdataflow(file):
try:
with open(file,'rb') as image_file:
image_byte=image_file.read()
image_file=io.BytesIO(image_byte)
image=Image.open(image_file)
image.verify()
image_file.close()
image.close()
except:
Bad_png.append(file)
try:
image_file.close()
except:
pass
try:
image.close()
except:
pass
# raise
else:
pass
# print('Image ok, format is %s.' % image.format)
def write_result(file,result):
with open(file,'w') as f:
f.write(result)
listofLines=list()
with open(txtfile,'r') as file:
for line in file:
if line[-1]!='\n':
line=line+'\n'
p=os.path.join(path,'image_2/'+(line[:-1]+'.png'))
# if p=='/home/zgq/Documents/dataSet/kitti/training/image_2/00748.png':
# print(p)
if not os.path.exists(p):
NO_png.append(p)
else:
checkdataflow(p)
print('absent one :')
print(NO_png)
print('Bad one :')
print(Bad_png)