python创建h5文件——图片文件夹批量制作数据集

话不多说,代码全套完整,亲测实用NB666

只说真话,不打诳语。

import numpy as np
import matplotlib.pyplot as plt
import h5py
import os

def createData(path):
    pics = os.listdir(path)
    all_data = []
    for item in pics:
        try:
            all_data.append(plt.imread(path+item).tolist())
            # all_data.append(plt.imread(path+'/'+item).tolist())
        except Exception as pic_wrong:
            print(item+" pic wrong")
    return all_data

def createSet(hf, name, tip, data):
    hf.create_dataset(name, data=data)
    t = [[tip]*len(data)]
    hf.create_dataset(name + '_tip', data=t)

if __name__ == '__main__':
    file_train_name="D:/Pictures/zhenhua/temp001/data_train.h5"
    hf = h5py.File(file_train_name, 'w')
    all_data = createData('D:/Pictures/zhenhua/temp2/')
    createSet(hf, 'train_set_1', 1, all_dat

你可能感兴趣的:(python2021,python)