forked from stephansigg/tools_pcapParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feature_Video.py
executable file
·293 lines (201 loc) · 7.56 KB
/
Feature_Video.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cPickle as pickle
import matplotlib.pyplot as plt
from collections import deque, defaultdict, Counter
import time
import Features
# ## XXX testing...
import Visualization
## input
#IN = "/home/mario/public/Recordings/Plots/AH/Day2/rec1_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/AH/Day2/rec2_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/AH/Day4/rec1_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/session2/typing1_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/session2/reading1_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/session2/empty1_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/session2/typing4_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/AH/Clean1/rec1_features.pickle"
#IN = "/home/mario/public/Recordings/Plots/AH/Clean1/rec2_features.pickle"
IN = "/tmp/paper/plots/ETH_officeH81_activity_CH5_5min_2videoStreams_130712_features.pickle"
## experimental histogram feature
def histogram(data):
c = Counter(data)
total = float(len(data))
return [x[1] / total for x in c.most_common()]
def sum_histograms(h1, h2):
h_sum = list()
for i in range(max(len(h1), len(h2))):
try:
a = h1[i]
except IndexError:
a = 0
try:
b = h2[i]
except IndexError:
b = 0
h_sum.append(a + b)
return h_sum
def feature_histogram1(units):
h_all = list()
for unit in units:
h_u = histogram(unit.get_values())
h_all = sum_histograms(h_all, h_u)
return h_all
class Struct(object):
pass
class Data_Source(object):
def __init__(self, infile):
self.infile = infile
self.part = self._unpickle()
self.i = -1
self.activities = False
# self.activities = set(["empty", "sit", "type"])
# self.activities = set(["up", "down"])
self.colormapping = defaultdict(lambda: "orange")
# self.colormapping = defaultdict(lambda: "black")
self.colormapping["INVALID"] = "gray"
self.colormapping["empty"] = "black"
self.colormapping["type"] = "green"
self.colormapping["sit"] = "blue"
self.colormapping["up"] = "red"
self.colormapping["down"] = "red"
# self.colormapping["down"] = "green"
def _unpickle(self):
print "Unpickling:", self.infile
print
pickle_input = open(self.infile, 'rb')
part = pickle.load(pickle_input)
pickle_input.close()
return part
def _map_color(self, label):
if "INVALID" in label:
return self.colormapping["INVALID"]
return self.colormapping[label]
## return plot data
def get(self):
try:
window = self.part.windows[self.i]
except IndexError:
raise StopIteration
## XXX MARIO
label = window.get_common_label(1)
# means = [ u.calc_mean() for u in window.units ]
flat = window.get_values_flat()
# histo = histogram(window.get_values_flat())
# histo1 = feature_histogram1(window.units)
s = Struct()
# s.points = means
# s.points = (0, means[0] - means[-1], 0, means[3] - means[-1], 0)
s.points = flat
# s.points = histo
# s.points = histo1
s.label = unicode(label, encoding="utf-8")
s.color = self._map_color(label)
s.progress = (self.i, len(self.part.windows))
## XXX debug output
# print s.points
return s
## return next window
def next(self):
self.i += 1
return self.get()
## XXX MARIO testing...
def next_in_selection(self):
point = self.next()
if self.activities:
while ( not point.label in self.activities ):
point = self.next()
return point
def show_all(self):
Visualization.plot_data_pointsXX(self.part.windows, xlim=None, ylim=None, filename=False, dims=None)
########################################################################
class Video(object):
def __init__(self, data):
## XXX MARIO
self.line_type = "."
# self.line_type = ""
self.data = data
self.lines = deque()
self.timer_handle = None
## init matplotlib
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
self.manager = plt.get_current_fig_manager()
# fig.canvas.mpl_connect('key_press_event', key_press) # <- register key_press event handler
## XXX
# plt.xlim(0, 3)
# plt.ylim(0, 1)
self.status_label = plt.figtext(0.02, 0.02, "STATUS")
# self.label = self.ax.text(0.02, 0.02, "label")
# self.label = self.ax.text(0.95, 0.01, 'colored text in axes coords',
# verticalalignment='bottom', horizontalalignment='right',
# transform=self.ax.transAxes,
# color='green', fontsize=15)
self.label = self.ax.text(0.02, 0.98, "label",
horizontalalignment='left',
verticalalignment='top',
# color=data.color,
fontsize=16,
transform = self.ax.transAxes)
## timer event
def timer_event(self):
# get next window
try:
## XXX MARIO
# data = self.data.next()
data = self.data.next_in_selection()
# --> video finished
except StopIteration:
return
y = data.points
x = range(len(y))
## remove old line
try:
old_line = self.lines.popleft()
## XXX MARIO
for l in old_line:
self.ax.lines.remove(l)
# --> no old lines, so we don't have to remove any
except IndexError:
pass
## * draw new line *
line = self.ax.plot( x, y, self.line_type, c=data.color )
self.lines.append(line)
## draw label
# self.label = self.ax.text(0.02, 0.98, data.label,
# horizontalalignment='left',
# verticalalignment='top',
# color=data.color,
# transform = self.ax.transAxes)
self.label.set_text(data.label)
self.label.set_color(data.color)
## progress
self.status_label.set_text(str(data.progress[0]) + " / " + str(data.progress[1])
+ ", " + data.label )
# print data.label, Features.calc_mean(y)
plt.draw()
## XXX
if ( data.label in ("up", "down")):
time.sleep(2)
## set new timer
self._set_timer(self.speed)
def _set_timer(self, time):
# clear timer, if any
if (self.timer_handle):
self.manager.window.after_cancel(self.timer_handle)
# * set timer *
self.timer_handle = self.manager.window.after(time, self.timer_event)
def play(self, speed):
self.speed = speed
self._set_timer(speed)
plt.show()
print "quit."
#####################################################################################
if __name__ == "__main__":
# pickled data
data = Data_Source(IN)
# data.show_all() ## XXX testing: old plot feautre..
# video
video = Video(data)
video.play(200)