数据集中图像通道数不一致
前言
我们在网页上爬取图像数据集后会出现图像通道数不一致或者尺寸太小的问题,为了便于统一管理可以选择删除这部分图像
代码如下:
import os
from skimage import io, color, transform
path = "" # 数据集的上一级路径
count = 0
for file_name in os.listdir(path): # file_name为数据集的文件夹名字
file_path = os.path.join(path, file_name) # 获取数据集的路径
for img_name in os.listdir(file_path): # img_name获取图片名字
img = io.imread(file_path+ "\\"+ img_name, plugin='matplotlib') # 读取图像
count += 1
if count % 1000 == 0:
print(count)
if len(img.shape) < 3: # 如果数据只是二维的删除
os.remove(file_path + "\\" + img_name)
elif img.shape[2] != 3: # 如果不是三通道的图像删除
os.remove(file_path+ "\\"+ img_name)
elif img.shape[0] < 32: # 如果图像太小,这里选择是尺寸门槛是32,根据对图像transform来修改
os.remove(file_path + "\\" + img_name)
elif img.shape[1] < 32:
os.remove(file_path + "\\" + img_name)