-
Notifications
You must be signed in to change notification settings - Fork 0
/
templ.py
346 lines (312 loc) · 16.2 KB
/
templ.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
336
337
338
339
340
341
342
343
344
345
346
import matplotlib.pyplot as plt
import traceback
from common import Common as Cmn
# control flags
debugProcessLogLine = False
class Templ:
def __init__(self):
#self.tCors = []
pass
@staticmethod
def processLogLine(output, annots, tCors, tCorsI, lBlinks, rBlinks, jBlinks):
if output.startswith("debug_blinks_d1:"):
corsInfo = output.split(" ")
if debugProcessLogLine:
print repr(corsInfo)
if (corsInfo[1:] == ['blinkMeasureSize', 'is', 'zero']
or corsInfo[3:7] == ['shortBmSize', 'is', 'less', 'than']
or corsInfo[3:7] == ['fps', 'of', 'the', 'first']
or corsInfo[3:-1] == ['shortBmSize', 'is', 'big', 'enough']
or corsInfo[3:5] == ['updated', 'maxFramesShortList']
):
return False
fn = int(corsInfo[corsInfo.index("lastF")+1])
ts = float(corsInfo[corsInfo.index("T")+1])
lcor = float(corsInfo[corsInfo.index("La")+1])
rcor = float(corsInfo[corsInfo.index("Ra")+1])
la = float(corsInfo[corsInfo.index("La")+2])
ra = float(corsInfo[corsInfo.index("Ra")+2])
lDiff = float(corsInfo[corsInfo.index("La")+3])
rDiff = float(corsInfo[corsInfo.index("Ra")+3])
lsd = float(corsInfo[corsInfo.index("lSDft")+1])
pl1sd = float(corsInfo[corsInfo.index("lSDft")+2])
ml1sd = float(corsInfo[corsInfo.index("lSDft")+3])
l2sd = float(corsInfo[corsInfo.index("lSDft")+4])
rsd = float(corsInfo[corsInfo.index("rSDft")+1])
pr1sd = float(corsInfo[corsInfo.index("rSDft")+2])
mr1sd = float(corsInfo[corsInfo.index("rSDft")+3])
r2sd = float(corsInfo[corsInfo.index("rSDft")+4])
tCors.append({"fn":fn, "ts":ts, "lcor":lcor, "rcor":rcor, "lDiff":lDiff, "rDiff":rDiff, "la":la, "ra":ra, "lsd":lsd, "pl1sd":pl1sd, "ml1sd":ml1sd, "l2sd":l2sd, "rsd":rsd, "pr1sd":pr1sd, "mr1sd":mr1sd, "r2sd":r2sd})
tCorsI[fn] = len(tCors)-1
if annots[0].has_key(fn):
tCors[-1].update(annots[0][fn])
tCors[-1]["annotEvent"] = "s"
if annots[1].has_key(fn):
tCors[-1].update(annots[1][fn])
tCors[-1]["annotEvent"] = "e"
#Templ.postProcessLogLine(tCors, lBlinks, rBlinks, jBlinks, False)
elif output.startswith("debug_notifications_n1_log1:"):
# debug_notifications_n1_log1: min ratio:%.2f curRatio %.2f
words = output.split(" ")
if len(words) < 4 or words[3] != "minRatio":
return False
fn = int(words[words.index("fn")+1])
curRatio = float(words[words.index("curRatio")+1])
bLen = float(words[words.index("bLen")+1])
try:
tCors[tCorsI[fn]]["cr"] = curRatio
except:
print "not graphing curRatio %d" % fn
try:
tCors[tCorsI[fn]]["bLen"] = bLen
except:
print "not graphing bLen %d" % fn
elif output.startswith("debug_blinks_d4:"):
blinkInfo = output.split(" ")
if blinkInfo[1] == "adding_lBlinkChunks":
lst = lBlinks
eye = "l"
elif blinkInfo[1] == "adding_rBlinkChunks":
lst = rBlinks
eye = "r"
fs = int(blinkInfo[blinkInfo.index("fs")+1])
fe = int(blinkInfo[blinkInfo.index("fe")+1])
start = float(blinkInfo[blinkInfo.index("start")+1])
end = float(blinkInfo[blinkInfo.index("end")+1])
duration = float(blinkInfo[blinkInfo.index("duration")+1])
if start > 1000000000:
start /= 1000.
end /= 1000.
#start = datetime.datetime.fromtimestamp(start)
blinkInfoDict = {"fs":fs, "fe":fe, "start":start, "end":end, "duration":duration}
lst.append(blinkInfoDict)
try:
tCors[tCorsI[fs]][eye+"b"] = "s"
tCors[tCorsI[fe]][eye+"b"] = "e"
except:
print "not graphing blink %s %d-%d" % (eye, fs, fe)
elif output.startswith("debug_blinks_d5:"):
blinkInfo = output.split(" ")
fs = int(blinkInfo[blinkInfo.index("fs")+1])
fe = int(blinkInfo[blinkInfo.index("fe")+1])
start = float(blinkInfo[blinkInfo.index("start")+1])
end = float(blinkInfo[blinkInfo.index("end")+1])
duration = float(blinkInfo[blinkInfo.index("duration")+1])
if start > 1000000000:
start /= 1000.
end /= 1000.
blinkInfoDict = {"fs":fs, "fe":fe, "start":start, "end":end, "duration":duration}
jBlinks.append(blinkInfoDict)
try:
tCors[tCorsI[fs]]["jb"] = "s"
tCors[tCorsI[fe]]["jb"] = "e"
except:
print "not graphing blink j %d-%d" % (eye, fs, fe)
elif output.startswith("exiting"):
return True
return False
@staticmethod
def postProcessLogLine(tCors, lBlinks, rBlinks, jBlinks, isEnd, videoName=None, figparms=None):
cm13_5 = 5.314961
imgDpi = 150
tightLayoutPad = 0.2
figsize = (cm13_5*4, cm13_5*2)
if not isEnd:
window = 600
else:
window = 0
if not isEnd and (len(tCors) == 0 or len(tCors) % window != 0):
return
#print repr(tCors)
if isEnd:
window = 0
aVals = 1.007 if not figparms.has_key('aVals') else figparms['aVals']
jVals = 1.005 if not figparms.has_key('jVals') else figparms['jVals']
lVals = 1.003 if not figparms.has_key('lVals') else figparms['lVals']
rVals = 1.001 if not figparms.has_key('rVals') else figparms['rVals']
pltx = [x["fn"] for x in tCors[-window:]]
pltld = [(xm1["lcor"]-x["lcor"]) for xm1, x in zip((tCors[1:-1]), (tCors[0:]))]
pltasx = [x["fn"] for x in tCors[-window:] if x.has_key("annotEvent") and x["annotEvent"] == "s"]
pltaex = [x["fn"] for x in tCors[-window:] if x.has_key("annotEvent") and x["annotEvent"] == "e"]
pltas = [aVals for x in tCors[-window:] if x.has_key("annotEvent") and x["annotEvent"] == "s"]
pltae = [aVals for x in tCors[-window:] if x.has_key("annotEvent") and x["annotEvent"] == "e"]
pltlxbs = [x["fn"] for x in tCors[-window:] if x.has_key("lb") and x["lb"] == "s"]
pltlxbe = [x["fn"] for x in tCors[-window:] if x.has_key("lb") and x["lb"] == "e"]
pltlbs = [lVals for x in tCors[-window:] if x.has_key("lb") and x["lb"] == "s"]
pltlbe = [lVals for x in tCors[-window:] if x.has_key("lb") and x["lb"] == "e"]
pltrxbs = [x["fn"] for x in tCors[-window:] if x.has_key("rb") and x["rb"] == "s"]
pltrxbe = [x["fn"] for x in tCors[-window:] if x.has_key("rb") and x["rb"] == "e"]
pltrbs = [rVals for x in tCors[-window:] if x.has_key("rb") and x["rb"] == "s"]
pltrbe = [rVals for x in tCors[-window:] if x.has_key("rb") and x["rb"] == "e"]
pltjxbs = [x["fn"] for x in tCors[-window:] if x.has_key("jb") and x["jb"] == "s"]
pltjxbe = [x["fn"] for x in tCors[-window:] if x.has_key("jb") and x["jb"] == "e"]
pltjbs = [jVals for x in tCors[-window:] if x.has_key("jb") and x["jb"] == "s"]
pltjbe = [jVals for x in tCors[-window:] if x.has_key("jb") and x["jb"] == "e"]
blinkRatioX = [x["fn"] for x in tCors[-window:] if x.has_key("cr")]
blinkRatio = [x["cr"] for x in tCors[-window:] if x.has_key("cr")]
bLen = [x["bLen"] for x in tCors[-window:] if x.has_key("bLen")]
lcor, rcor = [x["lcor"] for x in tCors[-window:]], [x["rcor"] for x in tCors[-window:]]
la, ra = [x["la"] for x in tCors[-window:]], [x["ra"] for x in tCors[-window:]]
lDiff, rDiff = [x["lDiff"] for x in tCors[-window:]], [x["rDiff"] for x in tCors[-window:]]
#lsd, rsd = [1-(x["lsd"]*2) for x in tCors[-window:]], [1-(x["rsd"]*2) for x in tCors[-window:]]
plsd1, prsd1 = [x["pl1sd"] for x in tCors[-window:]], [x["pr1sd"] for x in tCors[-window:]]
mlsd1, mrsd1 = [x["ml1sd"] for x in tCors[-window:]], [x["mr1sd"] for x in tCors[-window:]]
lsd2, rsd2 = [1-x["l2sd"] for x in tCors[-window:]], [1-x["r2sd"] for x in tCors[-window:]]
plsd2, mlsd2 = [x["l2sd"] for x in tCors[-window:]], [0-x["l2sd"] for x in tCors[-window:]]
prsd2, mrsd2 = [x["r2sd"] for x in tCors[-window:]], [0-x["r2sd"] for x in tCors[-window:]]
print "pltasx", repr(pltasx)
print "pltas", repr(pltas)
print "pltaex", repr(pltaex)
print "pltae", repr(pltae)
if figparms != None and figparms.has_key('figNums') == True:
figs = figparms['figNums']
else:
figs = [4, 3]
figs = [5]
if 5 in figs:
plt.figure(5, figsize=figsize)
plt.plot(blinkRatioX, blinkRatio, 'ro-', label="levo oko", markeredgecolor='none')
plt.tight_layout(pad=tightLayoutPad)
plt.figure(6, figsize=figsize)
plt.plot(blinkRatioX, bLen, 'ro-', label="levo oko", markeredgecolor='none')
plt.tight_layout(pad=tightLayoutPad)
if 4 in figs:
plt.figure(4, figsize=figsize)
#plt.subplot(211)
lline = plt.plot(pltx, lcor, 'ro-', label="levo oko", markeredgecolor='none')
rline = plt.plot(pltx, rcor, 'bo-', label="desno oko", markeredgecolor='none')
plt.plot(pltx, [1 for x in xrange(len(pltx))], 'g--') # ones
anots1 = plt.plot(pltasx, pltas, 'go', markersize=15.0, label="anno")
anots2 = plt.plot(pltaex, pltae, 'g^', markersize=15.0) # annots of blinks
left1 = plt.plot(pltlxbs, pltlbs, 'ro', markersize=15.0, label="lef") # start & end of blinks
left2 = plt.plot(pltlxbe, pltlbe, 'r^', markersize=15.0) # start & end of blinks
right1 = plt.plot(pltrxbs, pltrbs, 'bo', markersize=15.0, label="rig") # start & end of blinks
right2 = plt.plot(pltrxbe, pltrbe, 'b^', markersize=15.0) # start & end of blinks
both1 = plt.plot(pltjxbs, pltjbs, 'yo', markersize=15.0, label="bot") # start & end of blinks
both2 = plt.plot(pltjxbe, pltjbe, 'y^', markersize=15.0) # start & end of blinks
lsd = plt.plot(pltx, lsd2, 'rs-', markeredgecolor='none')
rsd = plt.plot(pltx, rsd2, 'bs-', markeredgecolor='none')
if figparms != None and figparms.has_key('axis') == True:
plt.axis(
xmin=figparms['axis']['xmin'], xmax=figparms['axis']['xmax'],
ymin=figparms['axis']['ymin'], ymax=figparms['axis']['ymax']
)
ls = [anots1, both1, left1, right1, anots2, both2, left2, right2]
labs = ["", "", "", "",
u"anotirani me\u017eiki", u"zaznani: obe o\u010desi", u"zaznani: levo oko", u"zaznani: desno oko"
]
first_legend = plt.legend(ls, labs, ncol=2, numpoints=1, loc=figparms['legBpos'])
plt.gca().add_artist(first_legend)
plt.legend([lline, rline, lsd, rsd], ['levo oko', 'desno oko', '2. st. odk.', '2. st. odk.'], loc=figparms['legLpos'])
plt.xlabel(u'sli\u010dice', fontsize=30)
plt.ylabel(u'korelacija ujemanja s predlogo', fontsize=30)
plt.tight_layout(pad=tightLayoutPad)
if figparms != None and figparms.has_key('figName') == True:
plt.savefig('/home/developer/other/notes/m/%s.png' % figparms['figName'], dpi=imgDpi, pad_inches=1)
if 1 in figs:
plt.figure(1, figsize=figsize)
#plt.subplot(211)
plt.plot(
pltx, [1 for x in xrange(len(pltx))], 'g--', # ones
pltx, lcor, 'ro-',
#pltx, la, 'ks-',
pltasx, pltas, 'go', pltaex, pltae, 'g^', # annots of blinks
pltlxbs, pltlbs, 'ro', pltlxbe, pltlbe, 'r^', # start & end of blinks
#pltx, lsd, 'y^-',
#pltx, lsd1, 'y^-',
pltx, lsd2, 'gs-',
)
plt.tight_layout()
if 2 in figs:
plt.figure(2, figsize=figsize)
plt.plot(
pltx, [1 for x in xrange(len(pltx))], 'g--', # ones
pltx, rcor, 'bo-',
#pltx, ra, 'ks-',
pltasx, pltas, 'go', pltaex, pltae, 'g^', # annots of blinks
pltrxbs, pltrbs, 'bo', pltrxbe, pltrbe, 'b^', # start & end of blinks
#pltx, rsd, 'y^-',
#pltx, rsd1, 'y^-',
pltx, rsd2, 'gs-',
)
plt.tight_layout()
if 3 in figs:
plt.figure(3, figsize=figsize)
l = plt.plot(pltx, lDiff, 'r--')
r = plt.plot(pltx, rDiff, 'b--')
left1 = plt.plot(pltx, plsd1, 'r^-', markeredgecolor='none')
right1 = plt.plot(pltx, prsd1, 'b^-', markeredgecolor='none')
left2 = plt.plot(pltx, plsd2, 'yo-', markeredgecolor='none')
right2 = plt.plot(pltx, prsd2, 'go-', markeredgecolor='none')
plt.plot(pltx, mlsd1, 'r^-', markeredgecolor='none')
plt.plot(pltx, mrsd1, 'b^-', markeredgecolor='none')
plt.plot(pltx, mlsd2, 'yo-', markeredgecolor='none')
plt.plot(pltx, mrsd2, 'go-', markeredgecolor='none')
if figparms != None and figparms.has_key('axis') == True:
plt.axis(
xmin=figparms['axis']['xmin'], xmax=figparms['axis']['xmax'],
ymin=figparms['axis']['ymin'], ymax=figparms['axis']['ymax']
)
ls = [l,r, left1, right1, left2, right2]
labs = [
'levo oko',
'desno oko',
'levo: 1. st. odk.',
'desno: 1. st. odk.',
'levo: 2. st. odk.',
'desno: 2. st. odk.',
#'1. st. odk.',
#'2. st. odk.',
]
plt.legend(ls, labs, loc=figparms['legBpos'])
plt.xlabel(u'sli\u010dice', fontsize=30)
#plt.ylabel(u'razlika vsote med zgornjim in spodnjim delom obmo\u010dja o\u010di', fontsize=30)
plt.ylabel(u'odvod signala', fontsize=30)
plt.tight_layout(pad=tightLayoutPad)
if figparms != None and figparms.has_key('figName') == True:
plt.savefig('/home/developer/other/notes/m/%s.png' % figparms['figName'], dpi=imgDpi, pad_inches=1)
#plt.subplot(212)
#plt.plot(pltx, pltlYdiff, 'ro-', pltx, pltrYdiff, 'bo-')
if figparms != None and figparms.has_key('show') == True and figparms['show'] == False:
pass
else:
plt.show()
return
# generating excel report
@staticmethod
def buildFndict(tCors):
fndict = {}
for d in tCors:
fndict[d["fn"]] = {
"ts":d["ts"], "lcor":d["lcor"], "rcor":d["rcor"], "l1sd":d["l1sd"], "l2sd":d["l2sd"], "r1sd":d["r1sd"], "r2sd":d["r2sd"]
}
return fndict
@staticmethod
def generateTCSV(vidPrefix, videoAnnot, tCors, lBlinks, rBlinks):
fndict = Templ.buildFndict(tCors)
Cmn.addDetectedBlinks(lBlinks, rBlinks, fndict)
f = file(vidPrefix+videoAnnot)
Cmn.parseAnnotations(f, fndict)
# sort by frame num
fnl = sorted(fndict.items(), key=lambda x:float(x[0]))
return fndict, fnl
@staticmethod
def writeTCSV(path, fnl):
pref = path+"outputs/"
f = file(pref+"out.csv","wb")
for frameNum, data in fnl:
line = ""
line += str(frameNum)
for t in ["lcor", "rcor", "l1sd", "l2sd", "r1sd", "r2sd"]: # "ts",
line += "\t%.6f" % data[t]
for t in ["lbs", "lbe", "rbs", "rbe"]:
line += "\t"
if data.has_key(t):
line += "%.3f" % data[t]
for t in ["anots", "anotc", "anote"]:
line += "\t"
if data.has_key(t):
line += "%.3f" % data[t]
line += "\n"
f.write(line)
f.close()