-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreTrackingAnnotation.py
311 lines (251 loc) · 9.39 KB
/
preTrackingAnnotation.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
#import matplotlib.pyplot as plt
#import matplotlib.widgets as widgets
import numpy as np
import cv2
import copy
import sys
if sys.version_info >= (3, 0):
import tkinter
# import filedialog
from tkinter import filedialog as tkFileDialog
else:
import Tkinter # python 3: tkinter
import tkFileDialog # python 3: filedialog
#import trackFlies as tf
import os.path
import glob
import skvideo.io
import scipy.io as sio
import argparse
def updateImage(x, y):
global arenaX, arenaY, rectRadius, rectCenterX, rectCenterY, showFrame
xdelta = abs(arenaX-x);
ydelta = abs(arenaY-y);
delta = max(xdelta,ydelta);
rectRadius = delta/2;
rectCenterX = arenaX;
rectCenterY = arenaY;
img = copy.copy(showFrame);
cv2.circle(img, (arenaX, arenaY), delta, (0, 255, 0), 3)
cv2.imshow('display',img)
def areaCallback(event, x, y, flags, param):
global currentlyDrawing, arenaX, arenaY
if event == cv2.EVENT_LBUTTONDOWN and currentlyDrawing == False:
arenaX = x;
arenaY = y;
if event == cv2.EVENT_LBUTTONDOWN:
currentlyDrawing = True;
elif event == cv2.EVENT_LBUTTONUP:
currentlyDrawing = False;
if currentlyDrawing:
updateImage(x,y);
def updateCenterImage(x, y):
global arenaX, arenaY, showFrame
img = copy.copy(showFrame);
cv2.circle(img, (x, y), 5, (0, 255, 0), 3)
cv2.imshow('display',img)
def centerCallback(event, x, y, flags, param):
global currentlyDrawing, arenaX, arenaY
if event == cv2.EVENT_LBUTTONDOWN and currentlyDrawing == False:
arenaX = x;
arenaY = y;
if event == cv2.EVENT_LBUTTONDOWN:
currentlyDrawing = True;
arenaX = x;
arenaY = y;
if event == cv2.EVENT_LBUTTONUP:
currentlyDrawing = False;
if currentlyDrawing:
updateCenterImage(x,y);
def trackerCallback(position):
global angle
M = cv2.getRotationMatrix2D((width/4,height/4),position-180,1);
angle = position;
img = copy.copy(showFrame);
img = cv2.warpAffine(img,M,(int(width/2),int(height/2)));
cv2.imshow('display',img);
def findMovieFile(fName):
print(fName)
if fName[len(fName)-4:len(fName)] == '.mp4' or fName[len(fName)-4:len(fName)] == '.avi':
return fName
else:
nName = glob.glob(fName + '/*.avi')
if len(nName) > 0:
return nName[0]
else:
nName = glob.glob(fName + '/*.mp4')
# avoid collision with the *Cmp.mp4 files generated by Pip's tracker - for some reason they will be the first match...
# this cleans the list of MP4 files of the ones containing "Cmp.mp4"
nName = [thisName for thisName in nName if "Cmp.mp4" not in thisName]
if len(nName) > 0:
return nName[0]
else:
return None
# these are the global variables that we want to assign
currentlyDrawing = False;
arenaX = 0
arenaY = 0
delta = 0
rectCenterX = 0
rectCenterY = 0
rectRadius = 0
angle = 0
micCenters = []
centerX = 0
centerY = 0
radius = 0
newAnnotation = True
applyAll = False
# step zero: choose a directory that you wish to annotate all of
parser = argparse.ArgumentParser()
parser.add_argument('initialdir',type=str,default='.', help='Movie to analyze')
pargs = parser.parse_args()
dirName = tkFileDialog.askdirectory(initialdir=pargs.initialdir)
names = sorted(glob.glob(dirName + '/*'))
cv2.namedWindow('display')
for movieName in names:
if not os.path.isdir(movieName):
continue
# step one: load fly video
print(movieName)
fileName = findMovieFile(movieName)
print(fileName)
if fileName is None:
continue
if not applyAll:
try:
videoInfo = skvideo.io.ffprobe(fileName)
width = int(videoInfo['video']['@width'])
height = int(videoInfo['video']['@height'])
videodata = skvideo.io.vreader(fileName)
frame = next(videodata)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
except:
continue
cv2.resizeWindow('display',width, height)
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Should we use this annotation? (y/n/a/q)', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.circle(showFrame,(centerX,centerY),int(radius),(0, 255, 0), 3)
cv2.imshow('display',showFrame)
key = cv2.waitKey(0)
if key == ord('y'):
newAnnotation = False
elif key == ord('n'):
newAnnotation = True
arenaX = 0
arenaY = 0
delta = 0
rectCenterX = 0
rectCenterY = 0
rectRadius = 0
angle = 0
micCenters = []
centerX = 0
centerY = 0
radius = 0
elif key == ord('a'):
applyAll = True
newAnnotation = False
elif key == ord('q'):
exit()
print('got key ' + chr(key))
if newAnnotation:
# for each of these, let the user press 'x' to reset, 'q' to quit at any time
# step two: choose area that contains fly arena
resetThis = True
while resetThis == True:
resetThis = False
print((width,height))
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Choose arena (press enter when done)', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.imshow('display',showFrame)
cv2.setMouseCallback('display',centerCallback)
key = cv2.waitKey(0)
if key == 'x':
resetThis = True
break
if key == 'q':
exit()
centerX, centerY = arenaX, arenaY;
# step three: select arena width
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Select LEFT boundary', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.imshow('display',showFrame)
cv2.setMouseCallback('display',centerCallback) #boundary callback
key = cv2.waitKey(0)
if key == 'x':
resetThis = True
break
if key == 'q':
exit()
leftX, leftY = arenaX, arenaY;
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Select RIGHT boundary', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.imshow('display',showFrame)
cv2.setMouseCallback('display',centerCallback) #boundary callback
key = cv2.waitKey(0)
rightX, rightY = arenaX, arenaY;
if key == 'x':
resetThis = True
break
if key == 'q':
exit()
# show detected circle
radius = np.sqrt((leftX - rightX)**2 + (leftY - rightY)**2)/2;
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Here is the arena...I hope that is correct!', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.circle(showFrame,(centerX,centerY),int(radius),(0, 255, 0), 3)
cv2.imshow('display',showFrame)
key = cv2.waitKey(0)
if key == 'x':
resetThis = True
break
if key == 'q':
exit()
# step four: choose microphone centers, in order. show example of the proper ordering, then display best fit centers
resetThis = True
while resetThis == True:
resetThis = False
for micNum in list(range(16)):
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Choose microphone number ' + str(micNum), (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.imshow('display',showFrame)
cv2.setMouseCallback('display',centerCallback)
key = cv2.waitKey(0)
if key == 'x':
micCenters = []
resetThis = True
break
if key == 'q':
exit()
micCenters.append((arenaX*2,arenaY*2))
# we want to show the estimated position of the microphones to make sure we have them all...
# maybe this should be shown in the 'align microphone' phase
# step five: choose area that shows blinky light
# choose blinky light area
resetThis = True
while resetThis == True:
resetThis = False
frame2 = copy.copy(frame);
cv2.putText(frame2, 'Select blinky light', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2,(int(width/2),int(height/2)));
cv2.imshow('display',showFrame)
cv2.setMouseCallback('display',areaCallback) #boundary callback
key = cv2.waitKey(0)
if key == 'x':
resetThis = True
break
if key == 'q':
exit()
else:
pass
print('saving ' + fileName[:-4] + '_annotated2.mat')
sio.savemat(fileName[:-4] + '_annotated2.mat',{'width':width,'height':height,'arenaCenter':(centerX*2,centerY*2),'arenaRadius':radius*2,
'LEDcenter':(rectCenterX*2,rectCenterY*2),'LEDradius':rectRadius*2,'micCenters':micCenters})
cv2.destroyAllWindows()