多文件夹图片预处理:清除空值、重置大小、分割训练集

清理空值 防止出现cannot identify image file

参考Python数据清洗----删除读取失败图片__简单版_python用pil读取图片出错删除掉-CSDN博客

python 复制代码
#%pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
#可能需要重启jupyter
import os
import shutil
import warnings
import cv2
import io
 
from PIL import Image
warnings.filterwarnings("error", category=UserWarning)
PATH = "data" #文件路径
i = 0
 
def is_read_successfully(file):
    try:
        imgFile = Image.open(file)
        return True
    except Exception:
        return False
 
if __name__=="__main__":
    #子文件夹
    for childPATH in os.listdir(PATH):
        #子文件夹路径
        childPATH = PATH + '/'+ str(childPATH)
        for parent, dirs, files in os.walk(PATH):
            for file in files:
                if not is_read_successfully(os.path.join(parent, file)):
                    print(os.path.join(parent, file))
                    i = i + 1
                    os.remove(os.path.join(parent, file)) 
    print(i)
    

重置大小 参考python批量修改图片尺寸(含多个文件夹)_python 修改路径下多个子文件下图片尺寸并重新保存-CSDN博客

python 复制代码
# -*- coding: utf-8 -*-
import cv2
import matplotlib.pyplot as plt
import os
import re
import sys
from PIL import Image
import string
import numpy as np
PATH = "data" #文件路径

i=0
def resizeImage(file,NoResize):
    image = cv2.imread(file,cv2.IMREAD_COLOR)
    #如果type(image) == 'NoneType',会报错,导致程序中断,所以这里先跳过这些图片,
    #并记录下来,结束程序后手动修改(删除)
    if image is None:
        NoResize += [str(file)]
    else:
        resizeImg = cv2.resize(image,(100,100))
        cv2.imwrite(file,resizeImg)
        cv2.waitKey(100)

def resizeAll(root):
    global i
    #待修改文件夹
    fileList = os.listdir(root)
    currentpath = os.getcwd()   
    os.chdir(root)
    NoResize = []  #记录没被修改的图片
    
    for file in fileList:       #遍历文件夹中所有文件
        i+=1
        file = str(file)
        resizeImage(file,NoResize)
         
    print("------------------")
    os.chdir(currentpath)       #改回程序运行前的工作目录
    sys.stdin.flush()       #刷新
    print('没被修改的图片: ',NoResize)

if __name__=="__main__":
    #子文件夹
    for childPATH in os.listdir(PATH):
        #子文件夹路径
        childPATH = PATH + '/'+ str(childPATH)
        print(childPATH)
        resizeAll(childPATH)
    print(f'{i}张图片修改完成')

划分训练集测试集 参考【深度学习】使用python划分数据集为训练集和验证集和测试集并放在不同的文件夹_深度学习中有没有直接划分训练集、验证集和测试集的函数-CSDN博客

python 复制代码
import os
import random
import shutil
from shutil import copy2
 
"""os.listdir会将文件夹下的文件名集合成一个列表并返回"""
def getDir(filepath):
    pathlist=os.listdir(filepath)
    return pathlist
 
"""制作五类图像总的训练集,验证集和测试集所需要的文件夹,例如训练集的文件夹中装有五个文件夹,这些文件夹分别装有一定比例的五类图像"""
def mkTotalDir(data_path):
    os.makedirs(data_path)
    dic=['train','test']
    for i in range(0,2):
        current_path=data_path+dic[i]+'/'
        #这个函数用来判断当前路径是否存在,如果存在则创建失败,如果不存在则可以成功创建
        isExists=os.path.exists(current_path)
        if not isExists:
            os.makedirs(current_path)
            print('successful '+dic[i])
        else:
            print('is existed')
    return
"""传入的参数是n类图像原本的路径,返回的是这个路径下各类图像的名称列表和图像的类别数"""
def getClassesMes(source_path):
    classes_name_list=getDir(source_path)
    classes_num=len(classes_name_list)
    return classes_name_list,classes_num
"""change_path其实就是制作好的n类图像总的训练集,验证集和测试集的路径,sourcepath和上面一个函数相同
这个函数是用来建训练集,测试集,验证集下五类图像的文件夹,就是建15个文件夹,当然也可以建很多类
"""
def mkClassDir(source_path,change_path):
    classes_name_list,classes_num=getClassesMes(source_path)
    for i in range(0,classes_num):
        current_class_path=os.path.join(change_path,classes_name_list[i])
        isExists=os.path.exists(current_class_path)
        if not isExists:
            os.makedirs(current_class_path)
            print('successful '+classes_name_list[i])
        else:
            print('is existed')
 
#source_path:原始多类图像的存放路径
#train_path:训练集图像的存放路径
#validation_path:验证集图像的存放路径D:\RSdata_dir\NWPU-RESISC45\\
#test_path:测试集图像的存放路径
 
 
def divideTrainValidationTest(source_path,train_path,test_path):
    """先获取五类图像的名称列表和类别数目"""
    classes_name_list,classes_num=getClassesMes(source_path)
    """调用上面的函数,在训练集验证集和测试集文件夹下建立五类图像的文件夹"""
    mkClassDir(source_path,train_path)
    mkClassDir(source_path,test_path)
    """
    先将一类图像的路径拿出来,将这个路径下所有这类的图片,就是800张图片的文件名做成一个列表,使用os.listdir函数,
    然后再将列表里面的所有图像名进行shuffle就是随机打乱,然后从打乱后的图像中抽7成放入训练集,3成放入测试集的图像名称列表
    """
    for i in range(0,classes_num):
        source_image_dir=os.listdir(source_path+classes_name_list[i]+'/')
        random.shuffle(source_image_dir)
        train_image_list=source_image_dir[0:int(0.7*len(source_image_dir))]
        test_image_list=source_image_dir[int(0.7*len(source_image_dir)):]
        """
        找到每一个集合列表中每一张图像的原始图像位置,然后将这张图像复制到目标的路径下,一共是五类图像
        每类图像随机被分成三个去向,使用shutil库中的copy2函数进行复制,当然也可以使用move函数,但是move
        相当于移动图像,当操作结束后,原始文件夹中的图像会都跑到目标文件夹中,如果划分不正确你想重新划分
        就需要备份,不然的话很麻烦
        """
        for train_image in train_image_list:
            origins_train_image_path=source_path+classes_name_list[i]+'/'+train_image
            new_train_image_path=train_path+classes_name_list[i]+'/'
            copy2(origins_train_image_path,new_train_image_path)
        for test_image in test_image_list:
            origins_test_image_path=source_path+classes_name_list[i]+'/'+test_image
            new_test_image_path=test_path+classes_name_list[i]+'/'
            copy2(origins_test_image_path,new_test_image_path)
 
if __name__=='__main__':
    source_path = './data/'
    data_path = './datadev/'        #脚本新建的文件夹
    train_path = './datadev/train/'
    test_path = './datadev/test/'
    mkTotalDir(data_path)
    divideTrainValidationTest(source_path, train_path, test_path)
 
相关推荐
查理零世11 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
紫雾凌寒20 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
sun lover32 分钟前
conda简单命令
python·conda
Mike_188702783511 小时前
1688代采下单API接口使用指南:实现商品采集与自动化下单
前端·python·自动化
青铜念诗1 小时前
python脚本文件设置进程优先级(在.py文件中实现)
开发语言·python
Dyan_csdn2 小时前
【Python项目】文本相似度计算系统
开发语言·python
pianmian12 小时前
python绘图之回归拟合图
开发语言·python·回归
MiyamiKK572 小时前
leetcode_位运算 190.颠倒二进制位
python·算法·leetcode
伊一线天2 小时前
体验用ai做了个python小游戏
人工智能·python·pygame
高力士等十万人2 小时前
OpenCV二值化处理
python·opencv·计算机视觉