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 import os
35 import pyvision as pv
36 from .FaceDetectionTest import is_success
37
38
39
40
42 '''
43 Parses and interfaces with an eyes file. Eyes files can be comma separated
44 (.csv) files or white space separated files.
45
46 Each line of the file should have an image name and then the left X,
47 left Y, right X, and right Y. Extensions will be automatically striped from
48 image filenames.
49
50 '''
51
53 '''
54 Inits and reads in the data.
55 '''
56 self.filename = filename
57 self.images = {}
58
59 self._readEyesFile()
60
62 '''
63 @returns: a list of all image filenames
64 '''
65 names = self.images.keys()
66 names.sort()
67 return names
68
70 '''@returns: True if filename is in index or False otherwise'''
71 fname = self._parseName(filename)
72 return self.images.has_key(fname)
73
74
76 '''
77 Returns the eye coordinates given a face detection rectangle. This is
78 useful if you have a face detector and want to simulate eye detection
79
80 @param filename: image filename
81 @param rect: rectangle that bounds the face that you want eye coordinates.
82 @returns: [image_name, leye, reye, face_rect] or None if the rectangle is
83 not near a face.
84 '''
85 fname = self._parseName(filename)
86 if self.images.has_key(fname):
87 faces = self.images[fname]
88 for each in faces:
89 truth_rect = each[3]
90 if is_success(truth_rect,rect):
91 return each
92 return None
93
94
95
96
97
98
100 fname = self._parseName(filename)
101 if self.images.has_key(fname):
102 faces = self.images[fname]
103 boxes = []
104 for _,_,_,box in faces:
105 boxes.append(box)
106 return boxes
107 return []
108
109
110
111
112
113
115 fname = self._parseName(filename)
116 if self.images.has_key(fname):
117 faces = self.images[fname]
118 eyes = []
119 for _,left,right,_ in faces:
120 eyes.append([left,right])
121 return eyes
122 return []
123
125 '''
126 Private: Do not call directly. Reads the eye file.
127 '''
128 if self.filename[-4:] == '.csv':
129 f = open(self.filename,'r')
130 for line in f:
131
132 line = line.split(',')
133 if len(line) < 5:
134 continue
135 for i in range(1,len(line),4):
136 fname = self._parseName(line[0])
137 if len(line) < i+4:
138 print "Warning in %s image %s: Count of numbers is not a multiple of four."%(self.filename,fname)
139 break
140 eye1 = pv.Point(float(line[i+0]),float(line[i+1]))
141 eye2 = pv.Point(float(line[i+2]),float(line[i+3]))
142
143 truth_rect = pv.BoundingRect(eye1,eye2)
144 truth_rect.w = 2.0 * truth_rect.w
145 truth_rect.h = truth_rect.w
146 truth_rect.x = truth_rect.x - 0.25*truth_rect.w
147 truth_rect.y = truth_rect.y - 0.3*truth_rect.w
148
149
150
151 if not self.images.has_key(fname):
152 self.images[fname] = []
153
154 self.images[fname].append([fname,eye1,eye2,truth_rect])
155
156 else:
157 f = open(self.filename,'r')
158 for line in f:
159
160 line = line.split()
161 fname = self._parseName(line[0])
162 eye1 = pv.Point(float(line[1]),float(line[2]))
163 eye2 = pv.Point(float(line[3]),float(line[4]))
164
165 truth_rect = pv.BoundingRect(eye1,eye2)
166 truth_rect.w = 2.0 * truth_rect.w
167 truth_rect.h = truth_rect.w
168 truth_rect.x = truth_rect.x - 0.25*truth_rect.w
169 truth_rect.y = truth_rect.y - 0.3*truth_rect.w
170
171
172
173 if not self.images.has_key(fname):
174 self.images[fname] = []
175
176 self.images[fname].append([fname,eye1,eye2,truth_rect])
177
179 '''
180 Private: Do not call directly. Parses the base filename.
181 '''
182 fname = os.path.basename(fname)
183 fname = os.path.splitext(fname)[0]
184 return fname
185