import tensorflow as tf
import numpy as np
import os
from random import shuffle
import cv2 as cv
name_dict = {"BF":0,"BK":1,"BL":2,"BR":3,"CF":4,"CL":5,'CV':6,'CXK':7,'S':8,'XF':9}
data_root_path = "color_part_data/"
test_file_path = data_root_path +"test.txt" #测试集数据集文件
trainer_file_path = data_root_path +"trainer.txt" #训练集数据集文件
name_data_list = {} #记录每类图片有多少训练图片、测试图片
trainer_list = []
test_list = []
#将图片完整路径存入字典
def save_train_test_file(path,name):
if name not in name_data_list:
img_list =[]
img_list.append(path)
name_data_list[name] = img_list
else:
name_data_list[name].append(path)
#遍历数据集目录,提取出图片路径,分训练集、测试集
dirs = os.listdir(data_root_path)
for d in dirs:
full_path = data_root_path + d
if os.path.isdir(full_path):
imgs = os.listdir(full_path) #列出子目录中所有图片
for img in imgs:
save_train_test_file(full_path+ "/" + img, d)
#返回图像的所有路径
def AllPicPath(data_root_path):
IMG = []
dirs = os.listdir(data_root_path)
for d in dirs:
full_path = data_root_path + d
if os.path.isdir(full_path):
imgs = os.listdir(full_path)
for img in imgs:
IMG.append(full_path+'/'+img)
return IMG
#计算所有图像最大的长和宽
def MAXLenWid(img):
leng , wid = 0 , 0
for imgpath in img:
image = cv.imread(imgpath)
L = image.shape[0]
W = image.shape[1]
if leng <= L :
leng = L
if wid <= W:
wid = W
return leng , wid
maxwid , maxleng = MAXLenWid(AllPicPath(data_root_path))
print(maxwid,maxleng,3)
print("---------------------------------------生成最大长宽图像-------------------------------------")
# image_fill = np.zeros((maxleng,maxwid,3))
#定义创建目录函数
def mkdir(path):
isExists = os.path.exists(path)
if not isExists:
os.mkdir(path)
else:
print("目录已存在")
allpicpath = AllPicPath(data_root_path)
for picpath in allpicpath:
image_fill = np.zeros((maxwid, maxleng, 3))
print(type(picpath))
img = cv.imread(picpath)
print(img.shape)
wid = img.shape[0]
leng = img.shape[1]
offset_w = int((maxwid - wid)/2)
offset_l = int((maxleng - leng)/2)
for i in range(3):
for j in range(wid):
for k in range(leng):
image_fill[j + offset_w][k + offset_l][i] = img[j][k][i]
final_img = image_fill
picpath1 = picpath.split('.')
picpath2 = picpath1[0][16:18]
save_path = 'color_part_data_processing/'+picpath2
mkdir(save_path)
cv.imwrite('color_part_data_processing/'+ picpath1[0][16:]+".jpg", final_img, [cv.IMWRITE_PNG_COMPRESSION, 0])
训练深度学习对数据的预处理
于 2023-02-28 19:58:54 首次发布
该代码段主要涉及图像处理,特别是为深度学习模型准备数据。它首先定义了一个类别字典,然后遍历指定目录下的子目录(每个子目录代表一类图像),将训练集和测试集的图像路径分别存储在不同的列表中。接着,它计算所有图像的最大尺寸,并将所有图像填充到统一大小,以适应模型输入。最后,处理后的图像被保存到新的目录下,准备用于训练。
20万+

被折叠的 条评论
为什么被折叠?



