-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_expansion.py
77 lines (62 loc) · 2.65 KB
/
data_expansion.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import h5py
import os
import time
import numpy as np
class data_expansion():
def __init__(self, input_data_path, output_directory):
self.input_data_path = input_data_path
self.output_directory = output_directory
self.output_directories_list = [ self.output_directory + "/left",
self.output_directory + "/right",
self.output_directory + "/follow",
self.output_directory + "/straight"]
self.input_directories_list = [ self.input_data_path + "/left",
self.input_data_path + "/right",
self.input_data_path + "/follow",
self.input_data_path + "/straight"]
self.folders_counters = [0,0,0,0] # Coubnters while saving
self.create_subdirectories()
def create_subdirectories(self):
'''
Creates subdirectories
'''
for subdir in self.output_directories_list:
os.mkdir(subdir)
def expand(self):
'''
Expands a 4 directories, e.g. Left
'''
for i,m in enumerate(self.input_directories_list):
start = time.time()
print("\nExpanding {} branch...".format(m))
self.expand_folder(i,m)
print("Done expanding {} branch, took {} seconds\n".format(m, time.time()- start))
return
def expand_folder(self, i, folder_path):
'''
Expands a self.input_directories_list instance folder into self.output_directories_list instance
'''
files_list = os.listdir(folder_path)
for file in files_list:
self.expand_file(i, file)
return
def expand_file(self, i, file):
'''
Expands file of 200 image into 200 seperate files
'''
with h5py.File(file, 'r') as hdf:
imgs = hdf.get('rgb')
imgs = np.array(imgs[:,:,:], dtype = np.uint8)
targets = hdf.get('targets')
targets = np.array(targets)
for k in range(imgs.shape[0]):
filename = self.output_directories_list[i] + "/data_" + str(self.folders_counters[i])
self.folders_counters[i] += 1
with h5py.File(filename, 'w') as hdf:
hdf.create_dataset('rgb', data=imgs[k])
hdf.create_dataset('targets', data=targets[k])
return
input_data_path = "/home/mohamed/Desktop/SeqTrain"
output_directory = "/home/mohamed/Desktop/Expanded"
data_expansion = data_expansion(input_data_path, output_directory)
data_expansion.expand()