python3.9环境,opencv3.4:
平时在准备深度学习数据集时,会有图像和对应的图像列表文件,可以使用opencv通过列表文件来读取图像,看是否存在错误数据:
使用python脚本来检查数据:
import os标签:str,img,python,opencv,train,file,图像,path,txt From: https://blog.51cto.com/u_8681773/6004645
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'D:\datasets\gen_number_str\train_enhance_resize'
txt_path = r'D:\datasets\gen_number_str\train.txt'
# 写入到文件
trainfile = r'D:\datasets\gen_number_str\err_list.txt'
train_file = open(trainfile, 'a', encoding='utf-8-sig') # 带BOM的UTF-8格式
with open(txt_path, "r", encoding='utf8') as f:
for line in f.readlines():
line = line.strip('\n') # 去掉列表中每一行元素的换行符
lineData = line.split('\t')
image = lineData[0]
img = pathlib.Path(image)
imagename = str(img.stem)
imagesuffix = str(img.suffix)
imgnamewithsuffix = imagename + imagesuffix
images_file = data_path + '/' + imgnamewithsuffix
img_data = cv2.imread(images_file, cv2.IMREAD_UNCHANGED)
if img_data is None:
print(img)
train_file.write(images_file + '\n')
continue
train_file.close()