#数据集处理 自建数据集生成.h5文件

 根据自身需要修改代码!

批量处理.h5数据

import h5py
import os
import cv2
import math
import numpy as np

target_path = 'D:/../../../../datasets'#.h5文件地址
source_path = 'D:/../../../../Data/'#数据地址
idx = 34 #创建的group数
dataset = h5py.File(os.path.join(target_path, "iris.h5"), 'w')

#计算图片角度信息
def change(file, g):
    angle_v = float(file.split('_')[2])
    angle_h = float(file.split('_')[3])
    side = file.split('_')[-1][0]
    if side == 'L':
        h = math.tan(angle_h * 3.1415926/180.0) * 22-g/2.0
        v = math.tan(angle_v * 3.1415926/180.0) * 22-g/2.0
    else:
        h = math.tan(angle_h * 3.1415926 / 180.0) * 22 + g / 2.0
        v = math.tan(angle_v * 3.1415926 / 180.0) * 22 + g / 2.0
    return math.atan(h/22.0), math.atan(v/22.0)

#获取部分信息
def interal(name):
    file = name.split(".bmp")[0]
    g = float(file.split("_")[-2])
    return change(file, g)

#读取图片
def readimg(i):
    path = source_path + '%05d' % (i+1)
    eye_data = []
    gaze_data = []
    for list in os.listdir(path):
        path_side = path + '/' + list
        for root, dirs, files in os.walk(path_side):
            for file in files:
                path_id = path_side + '/' + file
                img = cv2.imread(path_id, cv2.IMREAD_GRAYSCALE)
                img = cv2.resize(img,(60,36))
                img = np.array(img)
                gaze_v, gaze_h = interal(file)
                gaze = [gaze_v, gaze_h]
                gaze = np.array(gaze)
                gaze_data.append(gaze)
                eye_data.append(img)
    return np.array(eye_data), np.array(gaze_data)

if __name__ == '__main__':

#区分训练集和测试集
    for i in range(idx):
        dataset.create_group("/test/%d" % (i+1))  #test新建group “1”\
    print(dataset['test'].keys())
    for i in range(idx):
        dataset.create_group("train/%d" % (i+1))  #train新建group “1”\
#训练
    for i in range(idx):
        h = dataset['test']['%d' % (i+1)]
        img, gaze = readimg(i)
        # print(img)
        h.create_dataset("eye", data=img)
        h.create_dataset("gaze", data=gaze)
    print(list(h.keys()))
#测试
    for i in range(idx):
        h = dataset['train']['%d' % (i+1)]
        img, gaze = readimg(i)
        # print(img)
        h.create_dataset("eye", data=img)
        h.create_dataset("gaze", data=gaze)
    print(list(h.keys()))

你可能感兴趣的:(python,深度学习,机器学习)