python3.9的环境,opencv3.4:
平时在准备图像数据集是,有可能其中有个别图像错误引起在深度学习训练到一半时报错,所有先检查一下数据集中的图像是否有错误图像:
import os标签:__,遍历,img,python,data,opencv,file,path,os From: https://blog.51cto.com/u_8681773/6004644
import cv2
import glob
import pathlib
import random
import numpy as np
from os import listdir, getcwd
from os.path import join
# 检查图片数据是否正确
# 图片路径
data_path = r'E:\\datasets\\CCPD2019-yolo'
# 写入到文件
trainfile = r'E:\\datasets\\err_list.txt'
if __name__ == '__main__':
file_list = os.listdir(data_path)
train_file = open(trainfile, 'a',encoding='utf-8-sig') #带BOM的UTF-8格式
for file_obj in glob.glob(data_path + '/*.jpg', recursive=True):
#for file_obj in file_list:
file_path = os.path.join(data_path, file_obj)
if os.path.exists(file_path):
a=1
#print(file_path)
else:
print('不存在', file_path)
continue
# img=cv2.imread(img_path)
img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1) # 解决读中文名称图片的问题
if img is None:
print('错误:%s', file_path)
d = pathlib.Path(file_path)
train_file.write(file_path + '\t' + 'carplate' + '\n')
continue
train_file.close()