-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (123 loc) · 4.08 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.animation as animation
from glob import glob
from Frame import Frame
from Model import Model
# animates motion of given directory of frames
def plot(frames):
fs = []
fig = plt.figure()
for i in range(0, max(len(frames), 100), 10):
fs.append([plt.imshow(frames[i], cmap=cm.Greys_r, animated=True)])
ani = animation.ArtistAnimation(fig, fs, interval=50, blit=True, repeat_delay=1000)
# writergif = animation.PillowWriter(fps=10)
# ani.save('blob_unfiltered.gif', writer=writergif)
plt.show()
def load_frames(path):
frames = []
for fname in glob(path + "/*"):
frames.append(np.load(fname, allow_pickle=True))
return frames
def load_data(dir):
frames = load_frames(dir)
# plot(frames)
areas1 = []
areas2 = []
relCoords = []
for f in frames:
frame = Frame(f)
areas = frame.get_areas()
if len(areas) > 2:
print("more than 2 blobs detected")
elif len(areas) == 2:
# update area info
areas1.append(areas[0])
areas2.append(areas[1])
# update relative coordinate info
coords = frame.get_centers()
x = coords[0][0] - coords[1][0]
y = coords[0][1] - coords[1][1]
relCoords.append((x, y))
# frame.show()
# frame.show_contour()
# take area to be average of larges 20 frames
areas1.sort()
areas1 = areas1[-20:]
areas2.sort()
areas2 = areas2[-20:]
a1 = sum(areas1) / len(areas1)
a2 = sum(areas2) / len(areas2)
# get relative movement from relative coordinates
relMovement = []
# relEuclideanMovement = [0] * len(relCoords)
for i in range(1, len(relCoords)):
prev = relCoords[i - 1]
cur = relCoords[i]
relMovement += [cur[0] - prev[0], cur[1] - prev[1]]
# relEuclideanMovement[i] = math.sqrt((cur[0] - prev[0]) ** 2 + (cur[1] - prev[1]) ** 2)
# print(a1)
# print(a2)
# print(relMovement)
# print(relEuclideanMovement)
# x_mov = 0
# y_mov = 0
# sample_num = min(100, len(relMovement))
# for i in range(sample_num):
# x_mov += relMovement[i][0]
# y_mov += relMovement[i][1]
# x_mov /= sample_num
# y_mov /= sample_num
x = relCoords[0][0]
y = relCoords[0][1]
return [a1, a2] + [x, y] + relMovement[:100] #[x_mov, y_mov] #+ relEuclideanMovement[:100]
def load():
X = np.zeros((49, 104))
y = np.zeros(49)
for i in range(1, 24):
print(i)
x_temp = load_data("trajectories/t" + str(i))
if len(x_temp) == 104:
print(x_temp)
X[i] = x_temp
y[i] = 1
for i in range(24, 49):
print(i)
x_temp = load_data("trajectories/t" + str(i))
if len(x_temp) == 104:
print(x_temp)
X[i] = x_temp
np.save("trainData_X", X, allow_pickle=True)
np.save("trainData_y", y, allow_pickle=True)
def train():
# load()
X = np.load("trainData_X.npy", allow_pickle=True)
y = np.load("trainData_y.npy", allow_pickle=True)
X_train = np.concatenate([X[:15], X[23:39]])
X_test = np.concatenate([X[15:23], X[39:48]])
y_train = np.append(y[:15], y[23:39])
y_test = np.append(y[15:23], y[39:48])
# remove zero rows
zeroRows = np.all(X_train == 0, axis=1)
X_train = X_train[~zeroRows]
y_train = y_train[~zeroRows]
zeroRows = np.all(X_test == 0, axis=1)
X_test = X_test[~zeroRows]
y_test = y_test[~zeroRows]
#
# print(X_test)
# print(y_test)
model = Model(X, y, X_test, y_test)
model.sequential()
if __name__ == '__main__':
# frames = load_frames("trajectories/t1")
# frames_filtered = []
# for f in frames:
# frame = Frame(f)
# frames_filtered.append(frame.out)
# plot(frames)
# plot(frames_filtered)
# load()
train()