-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assigment1-part2-1.py
432 lines (350 loc) · 14.5 KB
/
Assigment1-part2-1.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
import cv2
# import cv
import pylab
from SIGBTools import RegionProps
from SIGBTools import getLineCoordinates
from SIGBTools import ROISelector
from SIGBTools import getImageSequence
from SIGBTools import getCircleSamples2
import numpy as np
import sys
#this imports are from the part 2
from scipy.cluster.vq import *
#from scipy.misc import imresize
from matplotlib.pyplot import *
import math
inputFile = "Sequences/eye1.avi"
outputFile = "eyeTrackerResult.mp4"
#--------------------------
# Global variable
#--------------------------
global imgOrig, leftTemplate, rightTemplate, frameNr
imgOrig = []
#These are used for template matching
leftTemplate = []
rightTemplate = []
frameNr = 0
def GetPupil(gray, thr, structuringElementSize):
'''Given a gray level image, gray and threshold value return a list of pupil locations'''
#this will create the temporal image in color.
tempResultImg = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
cv2.imshow("TempResults", tempResultImg)
props = RegionProps()
GetPupilKMeans(gray)
val, binI = cv2.threshold(gray, thr, 255, cv2.THRESH_BINARY_INV)
# binI = cv2.adaptiveThreshold(gray, 255, cv2.cv.CV_ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 5, 10)
cv2.imshow("Threshold", binI)
# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (structuringElementSize, structuringElementSize))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
binI = cv2.erode(binI, kernel, iterations=1)
binI = cv2.dilate(binI, kernel, iterations=1)
#Calculate blobs
contours, hierarchy = cv2.findContours(binI, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
pupils = []
accepted = []
sliderVals = getSliderVals()
for contour in contours:
p = props.CalcContourProperties(contour, ['Area', 'Length', 'Centroid', 'Extend', 'ConvexHull', 'Boundingbox',
'EquivDiameter'])
if p['Area'] < sliderVals['minGlint'] or p['Area'] > sliderVals['maxGlint']:
continue
# if p['Area'] < 700 or p['Area'] > 6000:
# continue
# print(p['Extend'])
if p['Extend'] < 0.07:
continue
# Figure out if contour is a circle
hull = p['ConvexHull']
# c = p['Centroid']
center, radius = cv2.minEnclosingCircle(hull)
circle = np.array(getCircleSamples2(center, radius)).astype(int)
retval = cv2.matchShapes(circle, hull, cv2.cv.CV_CONTOURS_MATCH_I1, 0)
if retval > 0.05:
continue
# accepted.append(circle)
accepted.append(contour)
# c = p['Centroid']
pupil = (center, (p['EquivDiameter'], p['EquivDiameter']), 0.0)
pupils.append(pupil)
# cv2.drawContours(binI, accepted, -1, (255, 0, 0))
# cv2.imshow("Threshold",binI)
return pupils
def GetGlints(gray, thr):
''' Given a gray level image, gray and threshold
value return a list of glint locations'''
glints = []
props = RegionProps()
val, binI = cv2.threshold(gray, thr, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binI, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
accepted = []
for contour in contours:
p = props.CalcContourProperties(contour, ['Area', 'Length', 'Centroid', 'Extend', 'ConvexHull', 'Boundingbox',
'EquivDiameter'])
if p['Area'] < 50 or p['Area'] > 500:
continue
# print(p['Extend'])
if p['Extend'] < 0.07:
continue
accepted.append(contour)
c = p['Centroid']
glint = (c[0], c[1])
glints.append(glint)
# cv2.drawContours(binI, accepted, -1, (255, 0, 0))
# cv2.imshow("Threshold",binI)
return glints
def GetIrisUsingThreshold(gray, pupil):
''' Given a gray level image, gray and threshold
value return a list of iris locations'''
# YOUR IMPLEMENTATION HERE !!!!
pass
def circularHough(gray):
''' Performs a circular hough transform of the image, gray and shows the detected circles
The circe with most votes is shown in red and the rest in green colors '''
#See help for http://opencv.itseez.com/modules/imgproc/doc/feature_detection.html?highlight=houghcircle#cv2.HoughCircles
blur = cv2.GaussianBlur(gray, (31, 31), 11)
dp = 6
minDist = 30
highThr = 20 #High threshold for canny
accThr = 850 #accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected
maxRadius = 50
minRadius = 155
circles = cv2.HoughCircles(blur, cv2.cv.CV_HOUGH_GRADIENT, dp, minDist, None, highThr, accThr, maxRadius, minRadius)
#Make a color image from gray for display purposes
gColor = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
if (circles != None):
#print circles
all_circles = circles[0]
M, N = all_circles.shape
k = 1
for c in all_circles:
cv2.circle(gColor, (int(c[0]), int(c[1])), c[2], (int(k * 255 / M), k * 128, 0))
K = k + 1
c = all_circles[0, :]
cv2.circle(gColor, (int(c[0]), int(c[1])), c[2], (0, 0, 255), 5)
cv2.imshow("hough", gColor)
def GetIrisUsingNormals(gray, pupil, normalLength):
''' Given a gray level image, gray and the length of the normals, normalLength
return a list of iris locations'''
# YOUR IMPLEMENTATION HERE !!!!
pass
def GetIrisUsingSimplifyedHough(gray, pupil):
''' Given a gray level image, gray
return a list of iris locations using a simplified Hough transformation'''
# YOUR IMPLEMENTATION HERE !!!!
pass
def GetEyeCorners(leftTemplate, rightTemplate, pupilPosition=None):
pass
def FilterPupilGlint(pupils, glints):
''' Given a list of pupil candidates and glint candidates returns a list of pupil and glints'''
# '''
# ((376.09381103515625, 268.9991760253906), (73.526696559450443, 73.526696559450443), 0.0)
# (399.90821256038646, 300.33494363929145)
# (350.97811447811443, 298.86531986531986)
# (635.5166666666667, 375.525)
# (635.515587529976, 110.9040767386091)
# '''
#starts here
glintFiltered = []
for pupil in pupils:
#print "This is pupil", pupil
a1, a2 = [ -1, 10 ** 6], [-1, 10 ** 6]
for glint in glints:
a1 = [glints[glint], calculateItDis(pupils[pupil][0], glints[glint][0])]
a2 = [glints[glint], calculateItDis(pupils[pupil][0], glints[glint][0])]
# min1 = calculateItDis(pupils[pupil][0], glints[glint][0])
# top = max(a1[1], a2[1])
# if min1 < top:
if a1[1] != -1:
glintFiltered.append(a1[0])
if a2[1] != -1:
glintFiltered.append(a2[1])
return pupils, glintFiltered
#print "This is glint", glint
#calculate max value
# min1 = pupil[1]
# min2 = glint[1]
# print "This is m1", m1
# print "This is m2", m2
# This is m1 (376.09381103515625, 268.9991760253906)
# This is m2 399.90821256
# max1 = pupil[0]
# max2 = glint[0]
def calculateItDis(x1, x2):
distancia = math.sqrt(math.pow(x1[0] - x2[0], 2) + math.pow(x1[1] - x2[1], 2))
print distancia
return distancia
def GetPupilKMeans(gray, K=2, distanceWeight=2, reSize=(40, 40)):
smallI = cv2.resize(gray, reSize)
M, N = smallI.shape
X, Y = np.meshgrid(range(M), range(N))
z = smallI.flatten()
x = X.flatten()
y = Y.flatten()
o = len(x)
features = np.zeros((o, 3))
features[:, 0] = z
features[:, 1] = y / distanceWeight
features[:, 2] = x / distanceWeight
features = np.array(features, 'f')
centroids, variance = kmeans(features, K)
label, distance = vq(features, centroids)
labelIm = np.array(np.reshape(label, (M, N)))
f = figure(1)
imshow(labelIm)
f.canvas.draw()
f.show()
def update(I):
'''Calculate the image features and display the result based on the slider values'''
#global drawImg
global frameNr, drawImg
img = I.copy()
sliderVals = getSliderVals()
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Do the cool thigs
#GetPupilKMeans(gray)
pupils = GetPupil(gray, sliderVals['pupilThr'], sliderVals['structSize'])
glints = GetGlints(gray, sliderVals['glintThr'])
FilterPupilGlint(pupils, glints)
#Do template matching
global leftTemplate
global rightTemplate
GetEyeCorners(leftTemplate, rightTemplate)
#Display results
global frameNr, drawImg
x, y = 10, 10
#setText(img,(x,y),"Frame:%d" %frameNr)
sliderVals = getSliderVals()
# for non-windows machines we print the values of the threshold in the original image
if sys.platform != 'win32':
step = 18
cv2.putText(img, "pupilThr :" + str(sliderVals['pupilThr']), (x, y + step), cv2.FONT_HERSHEY_PLAIN, 1.0,
(255, 255, 255), lineType=cv2.CV_AA)
cv2.putText(img, "glintThr :" + str(sliderVals['glintThr']), (x, y + 2 * step), cv2.FONT_HERSHEY_PLAIN, 1.0,
(255, 255, 255), lineType=cv2.CV_AA)
cv2.imshow('Result', img)
#Uncomment these lines as your methods start to work to display the result in the
#original image
for pupil in pupils:
cv2.ellipse(img, pupil, (0, 255, 0), 1)
C = int(pupil[0][0]), int(pupil[0][1])
cv2.circle(img, C, 2, (0, 0, 255), 4)
for glint in glints:
C = int(glint[0]), int(glint[1])
cv2.circle(img, C, 2, (255, 0, 255), 5)
cv2.imshow("Result", img)
#For Iris detection - Week 2
#circularHough(gray)
#copy the image so that the result image (img) can be saved in the movie
drawImg = img.copy()
def printUsage():
print "Q or ESC: Stop"
print "SPACE: Pause"
print "r: reload video"
print 'm: Mark region when the video has paused'
print 's: toggle video writing'
print 'c: close video sequence'
def run(fileName, resultFile='eyeTrackingResults.avi'):
''' MAIN Method to load the image sequence and handle user inputs'''
global imgOrig, frameNr, drawImg
setupWindowSliders()
props = RegionProps()
cap, imgOrig, sequenceOK = getImageSequence(fileName)
videoWriter = 0
frameNr = 0
if (sequenceOK):
update(imgOrig)
printUsage()
frameNr = 0
saveFrames = False
while (sequenceOK):
sliderVals = getSliderVals()
frameNr = frameNr + 1
ch = cv2.waitKey(1)
#Select regions
if (ch == ord('m')):
if (not sliderVals['Running']):
roiSelect = ROISelector(imgOrig)
pts, regionSelected = roiSelect.SelectArea('Select left eye corner', (400, 200))
if (regionSelected):
leftTemplate = imgOrig[pts[0][1]:pts[1][1], pts[0][0]:pts[1][0]]
if ch == 27:
break
if (ch == ord('s')):
if ((saveFrames)):
videoWriter.release()
saveFrames = False
print "End recording"
else:
imSize = np.shape(imgOrig)
videoWriter = cv2.VideoWriter(resultFile, cv.CV_FOURCC('D', 'I', 'V', '3'), 15.0,
(imSize[1], imSize[0]), True) #Make a video writer
saveFrames = True
print "Recording..."
if (ch == ord('q')):
break
if (ch == 32): #Spacebar
sliderVals = getSliderVals()
cv2.setTrackbarPos('Stop/Start', 'Threshold', not sliderVals['Running'])
if (ch == ord('r')):
frameNr = 0
sequenceOK = False
cap, imgOrig, sequenceOK = getImageSequence(fileName)
update(imgOrig)
sequenceOK = True
sliderVals = getSliderVals()
if (sliderVals['Running']):
sequenceOK, imgOrig = cap.read()
if (sequenceOK): #if there is an image
update(imgOrig)
if (saveFrames):
videoWriter.write(drawImg)
videoWriter.release()
#--------------------------
# UI related
#--------------------------
def setText(dst, (x, y), s):
cv2.putText(dst, s, (x + 1, y + 1), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness=2, lineType=cv2.CV_AA)
cv2.putText(dst, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), lineType=cv2.CV_AA)
def setupWindowSliders():
''' Define windows for displaying the results and create trackbars'''
cv2.namedWindow("Result")
cv2.namedWindow('Threshold')
cv2.namedWindow("TempResults")
cv2.createTrackbar('structSize', 'Threshold', 1, 255, onSlidersChange)
#Threshold value for the pupil intensity
cv2.createTrackbar('pupilThr', 'Threshold', 110, 255, onSlidersChange)
#Threshold value for the glint intensities
cv2.createTrackbar('glintThr', 'Threshold', 240, 255, onSlidersChange)
#define the minimum and maximum areas of the pupil
cv2.createTrackbar('minSize', 'Threshold', 20, 200, onSlidersChange)
cv2.createTrackbar('maxSize', 'Threshold', 200, 200, onSlidersChange)
#define the minimum and maximum areas of the glint
cv2.createTrackbar('minGlint', 'Threshold', 500, 6700, onSlidersChange)
cv2.createTrackbar('maxGlint', 'Threshold', 6700, 6700, onSlidersChange)
#Value to indicate whether to run or pause the video
cv2.createTrackbar('Stop/Start', 'Threshold', 0, 1, onSlidersChange)
cv2.moveWindow("Result", 800, 0)
cv2.moveWindow("TempResults", 800, 500)
def getSliderVals():
'''Extract the values of the sliders and return these in a dictionary'''
sliderVals = {}
sliderVals['structSize'] = cv2.getTrackbarPos('structSize', 'Threshold')
sliderVals['pupilThr'] = cv2.getTrackbarPos('pupilThr', 'Threshold')
sliderVals['glintThr'] = cv2.getTrackbarPos('glintThr', 'Threshold')
sliderVals['minGlint'] = cv2.getTrackbarPos('minGlint', 'Threshold')
sliderVals['maxGlint'] = cv2.getTrackbarPos('maxGlint', 'Threshold')
sliderVals['minSize'] = 50 * cv2.getTrackbarPos('minSize', 'Threshold')
sliderVals['maxSize'] = 50 * cv2.getTrackbarPos('maxSize', 'Threshold')
sliderVals['Running'] = 1 == cv2.getTrackbarPos('Stop/Start', 'Threshold')
return sliderVals
def onSlidersChange(dummy=None):
''' Handle updates when slides have changed.
This function only updates the display when the video is put on pause'''
global imgOrig;
sv = getSliderVals()
if (not sv['Running']): # if pause
update(imgOrig)
#--------------------------
# main
#--------------------------
run(inputFile)