forked from UCSD-TIES/DVS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacePhoto.py
More file actions
197 lines (159 loc) · 6.62 KB
/
FacePhoto.py
File metadata and controls
197 lines (159 loc) · 6.62 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
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
import cv2.cv as cv
import cv2
import time
from PIL import Image
import sys
from Eye import *
import PIL
import os
""" A class to perform actions on a photo of a face
This class has two child classes:
HorizonalPhoto
VerticalPhoto
"""
# NOTE: photoImg is a photo of a face
DEBUG = False
class FacePhoto():
""" This class has attributes:
IplImage facePhoto - a photo of the whole face
string path - the path to the photo of the whole face
Eye left - the left eye object
Eye right - the right eye object
"""
def __init__(self, photoImg, photoPath):
""" Initializes eye objects
Calls findEyes() to initialize the eye attributes.
Args:
photo photoImg - an image of a face
Return:
None
"""
# Initialize the face photo to the value passed in
self.facePhoto = photoImg
self.path = photoPath
# Initialize the other attributes to None so that they exist
self.left = None
self.right = None
if DEBUG:
print "In the facePhoto __init__"
# Set attributes intialized to None by finding them.
self.findEyes()
################# Utility Methods ######################
def findEyes(self):
""" Detects eyes in a photo and initializes relevant attributes
Uses opencv libarary methods to detect a face and then detect the
eyes in that face. If there are exactly two eye regions found it
populates the region attributes. If not exactly two
eye regions are found the method returns false.
Args:
None
Return:
bool - True if there were no issues. False for any error
"""
#imcolor = cv.LoadImage(self.path)
imcolor = self.facePhoto
#Path setups
cwd = os.path.dirname(os.path.abspath(sys.argv[0]))
cwd += "/opencv/haarcascades/"
frontalface = cwd + "haarcascade_frontalface_default.xml"
eye = cwd + "haarcascade_eye.xml"
faceCascade = cv.Load(frontalface)
eyeCascade = cv.Load(eye)
haarEyes = cv.Load(eye)
storage = cv.CreateMemStorage()
detectedEyes = cv.HaarDetectObjects(imcolor,haarEyes,storage)
if DEBUG:
print "detectedEyes = " + str(detectedEyes)
if len(detectedEyes) == 2:
if DEBUG:
# Draw the rectangle
cv.Rectangle(imcolor,(detectedEyes[0][0][0], detectedEyes[0][0][1]),
(detectedEyes[0][0][0] + detectedEyes[0][0][2],
detectedEyes[0][0][1] + detectedEyes[0][0][3]),cv.RGB(155,155,200),2)
cv.Rectangle(imcolor,(detectedEyes[1][0][0], detectedEyes[1][0][1]),
(detectedEyes[1][0][0] + detectedEyes[1][0][2],
detectedEyes[1][0][1] + detectedEyes[1][0][3]),cv.RGB(155,155,200),2)
cv.ShowImage("Face with eyes",imcolor)
cv.WaitKey(0)
cv.DestroyWindow("Face with eyes")
left = (detectedEyes[0][0][0], detectedEyes[0][0][1],
detectedEyes[0][0][0] + detectedEyes[0][0][2],
detectedEyes[0][0][1] + detectedEyes[0][0][3])
right = (detectedEyes[1][0][0], detectedEyes[1][0][1],
detectedEyes[1][0][0] + detectedEyes[1][0][2],
detectedEyes[1][0][1] + detectedEyes[1][0][3])
if DEBUG:
print "left: " + str(left)
print "right: " + str(right)
self.setEyes(left, right)
return True
if DEBUG:
print "Found more or less than 2 eyes, returning false"
return False
def eyeRemove(self, region):
""" Crops an eye from the facePhoto and returns it as a seperate photo
This method takes in a region which is interpreted to be a region representing
and eye and crops the eye out. It then returns the cropped photo
Args:
region region - a region representing the eye
Return:
cv2.cv.cvmat eyePhoto - a photo of just the eye
"""
# really takes in four points per region
crop = (region[0],region[1], region[2] - region[0], region[3] - region[1])
if DEBUG:
print "Region passed to eye remove: " + str(region)
print "And here's crop: " + str(crop)
print "Before crop we have type: " + str(type(self.facePhoto))
print self.facePhoto
cv.ShowImage("We're cropping", self.facePhoto)
cv.WaitKey(0)
cv.DestroyWindow("We're cropping")
eye = cv.GetSubRect(self.facePhoto, crop)
#eye = face.crop(region)
if DEBUG:
print "After crop we have type: " + str(type(eye))
cv.ShowImage("Cropped", eye)
cv.WaitKey(0)
cv.DestroyWindow("Cropped")
return eye
##################### Getters ############################
def getEyes(self):
""" Returns a tuple of the left and right eye objects """
leftEye = self.getLeftEye()
rightEye = self.getRightEye()
return (leftEye, rightEye)
def getLeftEye(self):
""" Returns the left eye object """
return self.left
def getRightEye():
""" Returns the right eye object """
return self.right
##################### Setters ############################
def setEyes(self, leftRegion, rightRegion):
""" Sets or resets both eye objects """
if DEBUG:
print "We're here in setEyes now"
print "leftregion: " + str(leftRegion)
print "rightRegion: " + str(rightRegion)
self.setLeftEye(leftRegion)
self.setRightEye(rightRegion)
return "setEyes successfully called"
def setLeftEye(self,region):
""" Constructs a new Eye object and stores it in left """
if DEBUG:
print "And we're setting the left eye now"
# Crop out a photo of the eye to pass the Eye constructor
left_eyePhoto = self.eyeRemove(region)
# Constructs the left eye
self.left = Eye(left_eyePhoto, region)
return "setLeftEye successfully called"
def setRightEye(self,region):
""" Constructs a new Eye object and stores it in right """
if DEBUG:
print "And we're setting the right eye now"
# Crop out a photo of the eye to pass the Eye constructor
right_eyePhoto = self.eyeRemove(region)
# Constructs the right eye
self.right = Eye(right_eyePhoto, region)
return "setRightEye successfully called"