-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheye_extractor.py
More file actions
66 lines (48 loc) · 1.89 KB
/
eye_extractor.py
File metadata and controls
66 lines (48 loc) · 1.89 KB
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
#!/usr/bin/python
from SimpleCV import HaarCascade, Image
class EyeExtractor:
def __init__(self, box_width=25, box_height=20):
self.face_cascade = HaarCascade("./HaarCascades/face.xml")
self.eyes_cascade = HaarCascade("./HaarCascades/two_eyes_big.xml")
self.box_width = box_width
self.box_height = box_height
def getLeftEye(self, image):
left_eye, x, y ,w, h = None, None, None, None, None
both_eyes = self.__findBothEyes(image)
eyes = both_eyes['eyes']
if (eyes is not None):
eyes_x = both_eyes['x']
eyes_y = both_eyes['y']
box_width = eyes.width()
box_height = eyes.height()
x,y,w,h = self.__getLeftEyeAbsoluteDimensions(eyes_x, eyes_y,
box_width, box_height)
left_eye = image.crop(x,y,w,h)
return {'lefteye':left_eye, 'x':x, 'y':y, 'width':w, 'height':h}
def __getLeftEyeAbsoluteDimensions(self, eyes_x, eyes_y, box_width, box_height):
x = eyes_x + (box_width * 2/3)
y = eyes_y
return x, y, self.box_width, self.box_height
def __findBothEyes(self, image):
eyes, eyes_x, eyes_y = None, None, None
faces = image.findHaarFeatures(self.face_cascade)
if ( faces is not None ):
face = self.__getBiggestFeature(faces)
face_x, face_y = self.__getTopLeftCooridnates(face)
two_eyes = face.crop().findHaarFeatures(self.eyes_cascade)
if ( two_eyes is not None ):
eyes = self.__getBiggestFeature(two_eyes)
eyes_x, eyes_y = self.__getTopLeftCooridnates(eyes)
eyes_x, eyes_y = self.__getAbsoluteEyePosition(face_x, face_y,
eyes_x, eyes_y)
return {'eyes':eyes, 'x':eyes_x, 'y':eyes_y}
def __getBiggestFeature(self, features):
return features.sortArea()[-1]#.crop()
def __getTopLeftCooridnates(self, feature):
x = feature.x - (feature.width()/2)
y = feature.y - (feature.height()/2)
return x,y
def __getAbsoluteEyePosition(self, face_x, face_y, eyes_x, eyes_y):
x = face_x + eyes_x
y = face_y + eyes_y
return x,y