训练深度学习对数据的预处理

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])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值