-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc-extractAttention.py
executable file
·337 lines (235 loc) · 10.4 KB
/
abc-extractAttention.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/python
"""Convert a spot the difference attention time file to
the attention at each frame
OR Extract the frame number of an event from the attention file
TODO Allow user to choose how to handle transition periods
"""
import os
import sys
import numpy as np
import pandas as pd
import argparse
import math
binarycoding = [("tablet", 1)]
def loadAttentionFile(infile, participant):
# Load the attentionfile
colnames = ["eventtype",
"null",
"attTransStarthms",
"attTransStartss",
"attTransStartms",
"attTransEndhms",
"attTransEndss",
"attTransEndms",
"attDurationhms",
"attDurationss",
"attDurationms",
"annotation"]
wantcols = ["eventtype",
"attTransStartss",
"attTransEndss",
"attDurationss",
"annotation"]
recoding = [("TV_to_+tablet", "TV_to_tablet"),
("Tv", "TV"),
("start _tablet", "start_tablet"),
("TV_to_tablet", "tablet"),
("tablet_to_TV", "TV")]
if participant == "P16":
print "Partcipant 16 found; dropping missing columns for load"
missingcols = ["attTransStartms","attTransEndms","attDurationms"]
colnames = list(filter(lambda x: x not in missingcols, colnames))
attention = pd.read_table(infile, header = None, names = colnames)
attention = attention[wantcols]
# Recode typos
for oldval, newval in recoding:
attention['annotation'] = attention['annotation'].replace(oldval, newval)
attention['eventtype'] = attention['eventtype'].replace("annotations", "annotation")
# Drop missing annotations
attention = attention[attention['annotation'].notnull()]
attention['attTransMidss'] = attention['attTransStartss'] + (attention['attTransEndss'] - attention['attTransStartss'])/2.0
return attention
def loadAttention(infile, participant):
attentionAndEvents = loadAttentionFile(infile, participant)
attention = attentionAndEvents[attentionAndEvents['eventtype'] == "attention"]
return attention
def loadEvents(infile, participant):
attentionAndEvents = loadAttentionFile(infile, participant)
events = attentionAndEvents[attentionAndEvents['eventtype'] == "annotation"]
return events
def getAttention(frametime, timestamps, attention):
if len(timestamps) != len(attention):
print "Timestamps and attentions must be the same length"
quit()
if frametime < 0:
return None
df = pd.DataFrame({"times": timestamps, "attention": attention})
earliertimes = df[df["times"] <= frametime]
if len(earliertimes) == 0:
return df["attention"][0]
else:
return earliertimes.iloc[-1]["attention"]
def loadExternalEvents(infile, participant):
# Load an external event file. This converts the free text annotations
# indicating the start and end of each part of the experiment to
# a standardised form. It also loads standardised events and times
# where these weren't specified in the original annotation file
extEvents = pd.read_csv(infile)
extEvents = extEvents[extEvents['participantCode'] == participant]
return extEvents
def getMaxTime(attentionFile, participant):
""" Get the maximum timestamp in a file """
attention = loadAttentionFile(attentionFile, participant)
maxtime = max(attention["attTransEndss"])
return maxtime
def getOffset(offsetfile, participant):
print "TODO put in library"
offsets = pd.read_csv(offsetfile)
offsetrow = offsets[offsets["participantCode"] == participant]
if len(offsetrow) != 1:
Exception("Could not get offset for participant")
return int(offsetrow["delta"])
def loadFramemap(mapfile, frameOffset):
print "TOOD put this in a library!"
framemap = pd.read_csv(mapfile,
header = None, names = ["kinectframe", "frametime"])
framemap["reltime"] = framemap["frametime"] - framemap["frametime"][0]
framemap["webcamframe"] = framemap["reltime"].apply(lambda x:
round(x * args.fps) + offset)
framemap["webcamtime"] = (framemap["webcamframe"] - 1) / args.fps
return framemap
########################################
parser = argparse.ArgumentParser(description = "Convert a ground-truth tracking file to the attention at each frame or extract the frame number of an event")
parser.add_argument("--attentionfile",
dest = "attentionfile", type = str, required = True,
help = "The attention file to convert")
parser.add_argument("--outputfile",
dest = "outputfile", type = str, required = False,
help = "The output file")
parser.add_argument("--participant",
dest = "participant", type = str, required = True,
help = "The participant code; Pxx")
parser.add_argument("--event",
dest = "event", type = str, required = False,
help = "Return the frame(s) a specified event occured in")
parser.add_argument("--externaleventfile",
dest = "externaleventfile", type = str, required = False,
help = "file mapping the long annotation for each participant to a standardised annotation. May also contain additional timestamps for events that were not recorded in the attention file")
parser.add_argument("--skipfile",
dest = "skipfile", type = str, required = False,
help = "If specified, program will *only* output a skipfile in a format suitable for the CppMT object tracking pipeline")
parser.add_argument("--fps", dest="fps", type = int, required = False, default = 30)
parser.add_argument(
"--framemap",
dest = "framemap",
type = str,
required = False,
help = "A list of frames and times that each video frame occured at. Only needed if this is *different* from framelist used to encode the behaviours")
parser.add_argument(
"--offsetfile",
dest = "offsetfile",
type = str,
required = False,
help = "A file containing the participant codes and frame offsets. Only needed when the video we're generating an attention file for didn't start at the same time as the ones used to encode the behaviours")
args = parser.parse_args()
fps = args.fps
if args.skipfile and not args.event:
print "An event must be specified when outputting a skipfile"
quit()
if args.skipfile is None and args.outputfile is None:
print "Skipfile or outfile must be specified"
quit()
# Load data to do Kinect frame mapping
if bool(args.framemap is None) != bool(args.offsetfile is None):
raise Exception("Must specify an offset time file AND a framemap")
if not(args.framemap is None):
offset = getOffset(args.offsetfile, args.participant)
framemap = loadFramemap(args.framemap, offset)
if args.event is None:
attention = loadAttention(args.attentionfile, args.participant)
# Get the maximum and minimum frames to encode
maxtime = getMaxTime(args.attentionfile, args.participant)
mintime = min(attention['attTransStartss'])
frames = range(int(mintime * fps), int(maxtime * fps))
times = [x / float(fps) for x in frames]
attention = [getAttention(x, attention["attTransMidss"], attention["annotation"]) for x in times]
outdata = pd.DataFrame({"frame": frames,
"bbx": 150,
"bby": 150,
"bbw": 150,
"bbh": 150,
"pred": attention},
columns = ["frame", "bbx", "bby", "bbw", "bbh", "pred"])
# Recode to numeric states
for oldval, newval in binarycoding:
outdata["pred"] = outdata["pred"].replace(oldval, newval)
#Anything that isn't touched goes to 0
goodvals = [x[1] for x in binarycoding]
outdata["pred"] = np.where([w in goodvals for w in outdata["pred"]],
outdata["pred"], 0)
else:
if args.externaleventfile is None:
print "Must specify the external event file"
quit()
print "Extracting events"
events = loadEvents(args.attentionfile, args.participant)
eventMapping = loadExternalEvents(args.externaleventfile, args.participant)
eventOfInterest = eventMapping[eventMapping['event'] == args.event]
if len(eventOfInterest) == 0:
print "Event not found"
quit()
if len(eventOfInterest) > 1:
print "Multiple events found"
quit()
if not math.isnan(eventOfInterest['timestamp']):
print "Extracting event directly from time"
eventtimeframe = eventOfInterest['timestamp']
if len(eventtimeframe) != 1:
print "Incorrect number of events found"
quit()
eventtime = eventtimeframe.iloc[0]
eventframe = int(eventtime * fps)
else:
print "Extracting event via attention file"
events = loadEvents(args.attentionfile, args.participant)
eventrow = events[events['annotation'] == eventOfInterest['annotation'].iloc[0]]
if len(eventrow) != 1:
print "Incorrect number of events found"
quit()
eventtime = eventrow['attTransMidss'].iloc[0]
# Load the attention to check we've not overrun the end
attention = loadAttention(args.attentionfile, args.participant)
maxencodedattention = max(attention['attTransMidss'])
print "Max encoded attention:", maxencodedattention
print "Event occured at (webcam time):", eventtime
if maxencodedattention <= eventtime:
print "WARNING: Event is after ground truth file"
print "Assuming last attention persisted to specified time"
eventframe = int(eventtime * fps)
outdata = pd.DataFrame({"frame": eventframe,
"bbx": 150,
"bby": 150,
"bbw": 150,
"bbh": 150,
"pred": 1},
columns = ["frame", "bbx", "bby", "bbw", "bbh", "pred"],
index = [eventframe])
# Frames are 1 indexed in abc-classify:
outdata["frame"] = outdata["frame"] + 1
# This is all in webcam-frames. If we have defined a kinect mapping
# we need to output in this
if args.framemap is not None:
outdata = outdata.merge(framemap[["webcamframe", "kinectframe"]],
left_on="frame",right_on="webcamframe", sort = True)
print outdata
del outdata['webcamframe']
outdata["frame"] = outdata["kinectframe"]
del outdata["kinectframe"]
outdata.dropna(inplace = True)
if args.skipfile is not None:
print "Outputting skipfile"
with open(args.skipfile, "w") as f:
f.write(str(int(eventtime * 1000)))
else:
outdata.to_csv(args.outputfile, index = False)
print outdata