-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataManager.py
65 lines (55 loc) · 1.51 KB
/
dataManager.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
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def importData(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
X = []
for row in csv_reader:
X.append(np.array(row, dtype=float))
return X, np.max(X[0].shape)
def resize(X, size = None):
if type(X) is list:
if size is None:
size = _calculate_size(X[0].shape)
return [_resize_single(x, size) for x in X]
else:
return _resize_single(X, size)
def _resize_single(X, size = None):
if size is None:
size = _calculate_size(X.shape)
return np.resize(X, size)
def _calculate_size(shape):
return (int(np.sqrt(shape)), int(np.sqrt(shape)))
def show(X, title = ''):
plt.figure(1000)
plt.ion()
if type(X) is list:
for i in range(len(X)):
_show_single(X[i], f'{title} #{i+1}')
else:
_show_single(X, title)
def _show_single(X, title = ''):
plt.figure(1000)
plt.title(title)
plt.imshow(X)
plt.show()
plt.pause(0.01)
def stopAnimation():
print('Click Enter')
input()
plt.figure(1000)
plt.clf()
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
def _readImage(path):
X = mpimg.imread(path)
X = np.sign(rgb2gray(X) - 127).flatten()
return X, np.max(X.shape)
def importImages(paths):
X = []
for path in paths:
x, size = _readImage(path)
X.append(x)
return X, size