-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPupilTracker.py
756 lines (625 loc) · 24.7 KB
/
PupilTracker.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
"""
Pupil tracking class.
"""
# Copyright (C) 2016 Alexander Tomlinson
# Distributed under the terms of the GNU General Public License (GPL).
from __future__ import division, print_function
import cv2
import numpy as np
class PupilTracker(object):
"""
Image processing class.
"""
def __init__(self, app):
"""
Constructor.
:param app: parent window
"""
self.app = app
# capture and output
self.cap = None
self.out = None
# frames
self.frame = None
self.display_frame = None
self.orig_frame = None
# frame info
self.frame_num = None
self.num_frames = None
self.vid_size = None
self.display_scale = None
self.scaled_size = None
# pupil and reflection centers
self.cx_pupil = None
self.cy_pupil = None
self.cx_refle = None
self.cy_refle = None
self.scaled_cx = None
self.scaled_cy = None
# param values were set for a 1080p image; this rescales params to whatever the current img size is
self.param_scale = None
# roi and processing params
self.noise_kernel = None
self.dx = None
self.dy = None
self.roi_pupil = None
self.roi_refle = None
self.roi_size = None
self.scaled_roi_size = None
self.can_pip = None
self.tracking = True
# data to track
self.data = None
self.angle = None
self.angle_data = None
def init_cap(self, video_file, window_width):
"""
Creates capture object for video
:param video_file: video path
:param window_width: width of the window
"""
if self.cap is not None:
self.cap.release()
# create capture and get info
if video_file == 'webcam':
self.cap = cv2.VideoCapture(0)
self.num_frames = 200
else:
self.cap = cv2.VideoCapture(video_file)
self.num_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.vid_size = (int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
self.get_set_scaled_size(window_width)
# init data holders
self.data = np.empty((2, self.num_frames, 2))
self.angle_data = np.empty(self.num_frames)
self.clear_data()
# init noise kernel
self.noise_kernel = np.ones((3, 3), np.uint8)
self.param_scale = self.vid_size[0] / 1920
# load first frame
self.load_first_frame()
def release_cap(self):
"""
Destroys cap object.
"""
if self.cap is not None:
self.cap.release()
self.cap = None
else:
raise IOError('VideoCapture not created. Nothing to release.')
def next_frame(self):
"""
Gets next frame.
:return: next frame
:raise EOFError: if at end of video file
:raise IOError: if no video file loaded
"""
if self.cap is not None:
ret, self.frame = self.cap.read()
if ret:
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
self.display_frame = cv2.resize(self.frame,
(self.scaled_size[0],
self.scaled_size[1]))
self.orig_frame = self.display_frame.copy()
self.frame_num += 1
else:
# at end; clear locations and return to first frame
self.roi_pupil = None
self.roi_refle = None
self.roi_size = None
self.load_first_frame()
raise EOFError('Video end.')
else:
raise IOError('No video loaded.')
def prev_frame(self):
"""
Gets previous frame.
:return: previous frame
:raise EOFError: if at beginning of video file
:raise IOError: if no video file loaded
"""
if self.frame_num < 0:
raise EOFError('Already at beginning')
if self.cap is not None:
self.frame_num -= 1
self.cap.set(cv2.CAP_PROP_POS_FRAMES,
self.frame_num)
ret, self.frame = self.cap.read()
if ret:
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
self.display_frame = cv2.resize(self.frame,
(self.scaled_size[0],
self.scaled_size[1]))
self.orig_frame = self.display_frame.copy()
else:
raise IOError('No video loaded.')
def get_frame(self):
"""
Gets the current display frame.
:return: current display frame
"""
if self.display_frame is not None:
return self.display_frame
def load_first_frame(self):
"""
Loads the first frame.
"""
# seek to first frame
if self.cap is not None:
self.cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
self.frame_num = -1
self.next_frame()
# go back a frame so play will start at first frame
self.cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
self.frame_num = -1
# clear data
self.app.toggle_to_dump_data(set_to=False)
self.clear_data()
else:
raise IOError('No video loaded.')
# uncheck save and dump
# self.app.toggle_to_save_video(set_to=False)
def init_out(self, path):
"""
Creates out object to write video to file.
:param path: file save path
"""
if self.out is None:
self.out = cv2.VideoWriter(path,
fourcc=cv2.VideoWriter_fourcc('m',
'p',
'4',
'v'),
fps=60,
frameSize=(960, 540))
else:
raise IOError('VideoWriter already created. Release first.')
def write_out(self):
"""
Writes frames to file.
"""
if self.out is not None:
frame = cv2.cvtColor(self.display_frame, cv2.COLOR_RGB2BGR)
self.out.write(frame)
else:
raise IOError('VideoWriter not created. Nothing with which to '
'write.')
def release_out(self):
"""
Destroys out object.
"""
if self.out is not None:
self.out.release()
self.out = None
print('Recording saved.')
else:
raise IOError('VideoWriter not created. Nothing to release.')
def get_set_scaled_size(self, width):
"""
Tracks the scale of the window relative to original frame size.
:param width: window size
:return: scaled size of video
"""
if self.vid_size is not None:
self.scaled_size = (width,
int(width * self.vid_size[1] / self.vid_size[0]))
self.display_scale = self.vid_size[0] / width
return self.scaled_size
def on_size(self):
"""
Resizes frame on size events.
"""
if self.display_frame is not None:
self.orig_frame = cv2.resize(self.frame,
(self.scaled_size[0],
self.scaled_size[1]))
self.display_frame = self.orig_frame.copy()
else:
raise IOError('No video selected.')
def clear_frame(self):
"""
Clears frame of drawings.
"""
if self.orig_frame is not None:
self.display_frame = self.orig_frame.copy()
else:
raise IOError('Nothing here.')
def clear_rois(self):
"""
Clears out rois.
"""
self.roi_pupil = None
self.roi_refle = None
def clear_data(self):
"""
Fills the data arrays with NaN.
"""
self.data.fill(np.NaN)
self.angle_data.fill(np.NaN)
def dump_data(self, path):
"""
Dumps the data to file.
:param path: file save path
"""
with open(path, 'w') as f:
np.savetxt(f, self.data[0],
delimiter=',',
fmt='%.0f',
header='pupil data\nx,y (pixels)',
footer='end pupil data\n')
np.savetxt(f, self.data[1],
delimiter=',',
fmt='%.0f',
header='reflection data\nx,y (pixels)',
footer='end reflection data\n')
np.savetxt(f, self.angle_data,
delimiter=',',
fmt='%f',
header='angle data\ndegrees',
footer='end angle data')
print('data dumped')
def process_image(self, img, roi=None):
"""
Blurs, grayscales, and ROIs either entire frame or only certain
region.
:param img: frame being processed
:param roi: region of interest being processed
:return: grayscaled, blurred, ROIed frame
"""
if roi is not None:
# roi
self.dx = roi[0][0]
self.dy = roi[0][1]
roi_image = img[roi[0][1]:roi[1][1],
roi[0][0]:roi[1][0]]
# gaussian filter
gauss = cv2.GaussianBlur(roi_image, (5, 5), 0)
else:
self.dx = 0
self.dy = 0
# gaussian filter
gauss = cv2.GaussianBlur(img, (5, 5), 0)
# make grayscale
gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)
return gray
def get_filtered(self, which):
"""
Returns the filtered image blended with the original, to display how
thresholding is happening to help the user better select a threshold.
:param which: whether to return pupil or reflection image
"""
grayed = self.process_image(self.frame)
if which == 'pupil':
_, threshed = cv2.threshold(grayed, self.app.pupil_thresh, 255,
cv2.THRESH_BINARY)
filtered = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE,
self.noise_kernel, iterations=2)
elif which == 'refle':
_, threshed = cv2.threshold(grayed, self.app.refle_thresh, 255,
cv2.THRESH_BINARY)
filtered = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE,
self.noise_kernel, iterations=1)
else:
raise AttributeError('Wrong parameter.')
scaled_filtered = cv2.resize(filtered,
(self.scaled_size[0],
self.scaled_size[1]))
color_scaled_filtered = cv2.cvtColor(scaled_filtered,
cv2.COLOR_GRAY2BGR)
if which == 'pupil':
blended = cv2.addWeighted(color_scaled_filtered, 0.4,
self.display_frame, 0.6,
0)
elif which == 'refle':
blended = cv2.addWeighted(color_scaled_filtered, 0.7,
self.display_frame, 0.3,
0)
else:
raise AttributeError('Wrong parameter.')
return blended
def find_pupils(self, roi=None):
"""
Searches for possible pupils in processed image
:param roi: region of interest
:return: list of possible pupil contours
"""
# roi and gauss
grayed = self.process_image(self.frame, roi)
# threshold and remove noise
_, thresh_pupil = cv2.threshold(grayed, self.app.pupil_thresh, 255,
cv2.THRESH_BINARY)
filtered_pupil = cv2.morphologyEx(thresh_pupil, cv2.MORPH_CLOSE,
self.noise_kernel, iterations=2)
# cv2.imshow('filtered_pupil', filtered_pupil.copy())
# find contours
_, contours_pupil, _ = cv2.findContours(filtered_pupil, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
found_pupils = []
# process contours
if len(contours_pupil) != 0:
for cnt in contours_pupil:
# drop small and large
area = cv2.contourArea(cnt)
if area == 0:
# print('pupil area zero', self.frame_num)
continue
if self.roi_size is None:
if not 2000 < area / self.param_scale < 120000:
# print('pupil too small/large', self.frame_num,
# int(area / self.param_scale))
continue
else:
if not self.param_scale * 2000 < area < self.roi_size**2:
continue
# remove concavities, drop too few points
hull = cv2.convexHull(cnt)
if hull.shape[0] < 5:
# print('too few points', self.frame_num)
continue
# drop too eccentric
circumference = cv2.arcLength(hull, True)
circularity = circumference ** 2 / (4*np.pi*area)
if circularity >= 1.6:
# print('not circle', self.frame_num)
continue
# rescale to full image
hull[:, :, 0] += self.dx
hull[:, :, 1] += self.dy
found_pupils.append(hull)
return found_pupils
def draw_pupil(self, index=None, roi=None, verbose=True):
"""
Draws the currently selected pupil to the frame.
:param index: which pupil in the list of possible pupils to draw
:param roi: region of interest
:param verbose: if true, draws extra content to the frame (roi, etc)
:raise AttributeError: if list of pupils is empty
"""
# if no index passed, means we are tracking single pupil, so will be
# first in list returned
if index is None:
index = 0
# use already selected roi if only adjusting thresholds
if roi == 'pupil':
roi = self.roi_pupil
if not self.tracking:
self.roi_size = None
# get list of pupil contours
cnt_list = self.find_pupils(roi)
if len(cnt_list) > 0:
cnt = cnt_list[index]
else:
raise AttributeError('No pupils found.')
# fit ellipse
ellipse = cv2.fitEllipse(cnt)
# centroid
self.cx_pupil = int(np.rint(ellipse[0][0]))
self.cy_pupil = int(np.rint(ellipse[0][1]))
# angle of ellipse
self.angle = ellipse[2]
if self.angle > 90:
self.angle -= 90
else:
self.angle += 90
# scale for drawing
scaled_cx = int(self.cx_pupil / self.display_scale)
scaled_cy = int(self.cy_pupil / self.display_scale)
self.scaled_cx = scaled_cx
self.scaled_cy = scaled_cy
# draw scaled
cv2.line(self.display_frame,
(scaled_cx-2, scaled_cy),
(scaled_cx+2, scaled_cy),
(255, 255, 255), 1)
cv2.line(self.display_frame,
(scaled_cx, scaled_cy-2),
(scaled_cx, scaled_cy+2),
(255, 255, 255), 1)
scaled_cnt = np.rint(cnt / self.display_scale)
scaled_cnt = scaled_cnt.astype(int)
scaled_ellipse = cv2.fitEllipse(scaled_cnt)
cv2.ellipse(self.display_frame, scaled_ellipse, (0, 255, 100), 1)
if self.roi_size is None:
self.roi_size = int(np.rint(max(ellipse[1][0], ellipse[1][1]) *
1.75))
self.scaled_roi_size = int(self.roi_size / self.display_scale)
# extra drawings
if verbose:
cv2.drawContours(self.display_frame, scaled_cnt, -1, (255, 255,
255), 2)
cv2.rectangle(self.display_frame,
(scaled_cx - self.scaled_roi_size, scaled_cy - self.scaled_roi_size),
(scaled_cx + self.scaled_roi_size, scaled_cy + self.scaled_roi_size),
(255, 255, 255))
# box = cv2.boxPoints(ellipse)
# box = np.int0(box)
# cv2.drawContours(self.display_frame, [box], 0,(0,0,255),1)
# correct out of bounds roi
roi_lu_x = self.cx_pupil - self.roi_size
roi_lu_y = self.cy_pupil - self.roi_size
roi_rl_x = self.cx_pupil + self.roi_size
roi_rl_y = self.cy_pupil + self.roi_size
if roi_lu_x < 0:
roi_lu_x = 0
if roi_lu_y < 0:
roi_lu_y = 0
self.roi_pupil = [(roi_lu_x, roi_lu_y),
(roi_rl_x, roi_rl_y)]
self.tracking = False
def track_pupil(self, verbose=True):
"""
Makes call to draw pupil with proper roi and handles errors.
:param verbose: whether or not to draw extra
"""
if self.roi_pupil is not None:
try:
self.draw_pupil(roi='pupil', verbose=verbose)
try:
self.data[0][self.frame_num] = [self.cx_pupil, self.cy_pupil]
self.angle_data[self.frame_num] = self.angle
except IndexError:
self.frame_num = 0
self.track_pupil(verbose)
self.can_pip = True
self.tracking = True
# TODO: make tracking tracker
# bc when loses roi then resets shape because can't pip...
# except IndexError as e:
# # print(e)
# pass
# no pupils found
except AttributeError:
# print(e)
self.can_pip = False
else:
pass
def find_refle(self, roi=None):
"""
Searches for possible reflections in processed image.
:param roi: region of interest
:return: list of possible reflection contours
"""
# roi and gauss
grayed = self.process_image(self.frame, roi)
# threshold and remove noise
_, thresh_refle = cv2.threshold(grayed, self.app.refle_thresh, 255,
cv2.THRESH_BINARY)
filtered_refle = cv2.morphologyEx(thresh_refle, cv2.MORPH_CLOSE,
self.noise_kernel, iterations=1)
# cv2.imshow('filtered_refle', filtered_refle.copy())
# find contours
_, contours_refle, _ = cv2.findContours(filtered_refle, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
found_reflections = []
# process contours
if len(contours_refle) != 0:
for cnt in contours_refle:
# drop small and large
area = cv2.contourArea(cnt)
if area == 0:
# print('refle area zero', self.frame_num)
continue
if not 80 < area / self.param_scale < 8000:
# print('refle too small/large', self.frame_num, int(area / self.param_scale))
continue
# rescale to full image
cnt[:, :, 0] += self.dx
cnt[:, :, 1] += self.dy
# test squareness
rect = cv2.minAreaRect(cnt)
w, h = rect[1][0], rect[1][1]
squareness = h / w
if not 0.5 < squareness < 2:
# print('refle not square', self.frame_num)
continue
# see if center in roi
if roi is not None:
# rect center
cx = int(rect[0][0])
cy = int(rect[0][1])
if not roi[0][0] < cx < roi[1][0] \
or not \
roi[0][1] < cy < roi[1][1]:
# print('refle not in roi', self.frame_num)
continue
found_reflections.append(cnt)
return found_reflections
def draw_refle(self, index=None, roi=None, verbose=True):
"""
Draws the currently selected reflection to the frame.
:param index: which pupil in the list of possible reflections to draw
:param roi: region of interest
:param verbose: if true, draws extra content to the frame (roi, etc)
:raise AttributeError: if list of reflections is empty
"""
# if no index passed, means we are tracking single reflection
if index is None:
index = 0
# use already selected roi if found pupil or only adjusting thresholds
if roi == 'pupil':
roi = self.roi_pupil
elif roi == 'refle':
roi = self.roi_refle
# get list of reflection contours
cnt_list = self.find_refle(roi)
if len(cnt_list) > 0:
cnt = cnt_list[index]
else:
raise AttributeError('No reflections found.')
# fit rectangle to contour
rect = cv2.minAreaRect(cnt)
# rect center
self.cx_refle = int(rect[0][0])
self.cy_refle = int(rect[0][1])
# reset roi
# TODO: don't let ROI get too small
roi_size = int(np.rint(max(rect[1][0], rect[1][1])) * 1.25)
scaled_roi_size = int(roi_size / self.display_scale)
self.roi_refle = [(self.cx_refle - roi_size, self.cy_refle - roi_size),
(self.cx_refle + roi_size, self.cy_refle + roi_size)]
# scale for drawing
scaled_cx = int(self.cx_refle / self.display_scale)
scaled_cy = int(self.cy_refle / self.display_scale)
# draw
cv2.line(self.display_frame,
(scaled_cx-2, scaled_cy),
(scaled_cx+2, scaled_cy),
(0, 0, 0), 1)
cv2.line(self.display_frame,
(scaled_cx, scaled_cy-2),
(scaled_cx, scaled_cy+2),
(0, 0, 0), 1)
scaled_cnt = np.rint(cnt / self.display_scale)
scaled_cnt = scaled_cnt.astype(int)
scaled_rect = cv2.minAreaRect(scaled_cnt)
box = cv2.boxPoints(scaled_rect)
box = np.int0(box)
cv2.drawContours(self.display_frame, [box], 0, (0, 255, 100), 1)
# draw extra
if verbose:
cv2.rectangle(self.display_frame,
(scaled_cx - scaled_roi_size, scaled_cy - scaled_roi_size),
(scaled_cx + scaled_roi_size, scaled_cy + scaled_roi_size),
(255, 255, 255))
cv2.drawContours(self.display_frame, scaled_cnt, -1, (0, 0, 255), 2)
def track_refle(self, verbose=True):
"""
Makes call to draw reflection with proper roi and handles errors.
:param verbose: whether or not to draw extra
"""
if self.roi_refle is not None:
try:
self.draw_refle(roi='refle', verbose=verbose)
self.data[1][self.frame_num] = [self.cx_refle, self.cy_refle]
# except IndexError as e:
# # print(e)
# pass
# no reflections found
except AttributeError:
# print(e)
pass
else:
pass
def pip(self):
"""
Creates picture in picture of pupil ROI
"""
if self.roi_pupil is not None and self.can_pip:
# get roi
roi_size = self.scaled_roi_size
y1, y2 = self.scaled_cy-roi_size+1, self.scaled_cy+roi_size
x1, x2 = self.scaled_cx-roi_size+1, self.scaled_cx+roi_size
coords = [x1, x2, y1, y2]
for ind, element in enumerate(coords):
if element < 0:
coords[ind] = 0
roi_image = self.display_frame[
coords[2]:coords[3],
coords[0]:coords[1]]
# replace in frame
self.display_frame[0:roi_image.shape[0],
self.display_frame.shape[1]-roi_image.shape[1]:
self.display_frame.shape[1]] = roi_image