import sys
import glob
import h5py
import numpy as np
from PIL import Image
IMG_WIDTH = 500
IMG_HEIGHT = 500
h5file = 'test1_catvnoncat.h5'
nfiles = len(glob.glob('images/TestData/*.jpg'))
print(f'count of image files nfiles={nfiles}')
with h5py.File(h5file,'w') as h5f:
dt = h5py.string_dtype(encoding='ascii',length=7)
classes = h5f.create_dataset('list_classes',shape=(2), dtype=dt)
classes[0] = "non-cat"
classes[1] = "cat"
binary_output = np.zeros(nfiles).reshape(nfiles,1)
img_ds = h5f.create_dataset('data_set_x',shape=(nfiles, IMG_WIDTH, IMG_HEIGHT,3), dtype=int)
for cnt, ifile in enumerate(glob.iglob('images/TestData/*.jpg')) :
image_resized = np.array(Image.open(ifile).resize((IMG_WIDTH, IMG_HEIGHT)))
print(ifile)
img_ds[cnt:cnt+1:,:,:] = image_resized
print(cnt)
if ifile[16] == 'c':
binary_output[cnt] = 1
bin_ds = h5f.create_dataset('data_set_y',shape=(nfiles, 1), dtype=int)
bin_ds[:] = binary_output