-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_frames.py
45 lines (37 loc) · 1.54 KB
/
process_frames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
from natsort import natsorted
import h5py
from PIL import Image
import numpy as np
path_to_train = 'path_to_train_frames'
path_to_test = 'path_to_test_frames'
h5_file_name = 'path_where_h5_shall_be_created/ds.h5'
def save_dict_h5py_image_sequence(path_to_train, path_to_test, h5_fname):
"""
h5 containing groups/datasets which works with key/values
:param array_dict:
:param fname: path to the .h5 file which we want to create
"""
# Ensure directory exists
directory = os.path.dirname(h5_fname)
if not os.path.exists(directory):
os.makedirs(directory)
with h5py.File(h5_fname, 'w') as hf:
train_imgs = os.listdir(path_to_train)
train_imgs = natsorted(train_imgs)
grp = hf.create_group('train') # each group is an episode
for i, p in enumerate(train_imgs):
img_path = os.path.join(path_to_train, p)
image = Image.open(img_path)
image = np.asarray(image)
grp.create_dataset(str(i), data=image, compression="gzip")
test_imgs = os.listdir(path_to_test)
test_imgs = natsorted(test_imgs)
grp = hf.create_group('test') # each group is an episode
for i, p in enumerate(test_imgs):
img_path = os.path.join(path_to_test, p)
image = Image.open(img_path)
image = np.asarray(image)
grp.create_dataset(str(i), data=image, compression="gzip")
print(f'images have been saved to {h5_fname}')
save_dict_h5py_image_sequence(path_to_train, path_to_test, h5_file_name)