-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_gen.py
71 lines (57 loc) · 2.22 KB
/
data_gen.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
import matplotlib.image as mpimg
import numpy as np
import os
def get_next_batch(data_list, iter, batch_size):
X_data = []
index = (iter * batch_size) % len(data_list)
print(index)
for i in range(batch_size):
single_batch = read_one_batch_data(data_list[index + i])
X_data.append(single_batch)
return np.array(X_data, dtype=np.float32)
def read_one_batch_data(file_name_list):
ret = []
for name in np.nditer(file_name_list): # loop through step num:
try:
img = mpimg.imread(str(name)).reshape(-1)
ret.append(img)
except Exception as ex:
print("Exception while reading ", img, ". Skipping it")
return ret
def read_video(video_path):
ret = []
for img in os.listdir(video_path):
try:
img = mpimg.imread(os.path.join(video_path, img)).reshape(-1)
ret.append(img)
except Exception as ex:
print("Exception while reading ", img, ". Skipping it")
return ret
def get_train_data(training_dir, step_num):
X_data = []
for r, dirs, files in os.walk(training_dir):
for dir in dirs:
for file in sorted(os.listdir(os.path.join(r, dir))):
file_path = os.path.join(r, dir, file)
X_data.append(file_path)
return np.array(X_data).reshape(int(len(X_data)/step_num), step_num)
def get_data_full(training_dir, step_num):
X_data = []
for r, dirs, files in os.walk(training_dir):
for dir in dirs:
for file in sorted(os.listdir(os.path.join(r, dir))):
file_path = os.path.join(r, dir, file)
try:
img = mpimg.imread(str(file_path)).reshape(-1)
X_data.append(img)
except Exception as ex:
print("Exception while reading ", file_path, ". Skipping it")
return np.array(X_data).reshape(int(len(X_data) / step_num), step_num, len(img))
def get_test_data():
return [], []
def save_images(arr, name):
folder = os.path.join("data/lstmAE/test", name)
if not os.path.isdir(folder):
os.mkdir(folder)
for idx, img in enumerate(arr):
mpimg.imsave(folder + "/" + str(idx)+".tif", img.reshape(76, 115))