Package pyvision :: Package analysis :: Package FaceAnalysis :: Module FaceDatabase
[hide private]
[frames] | no frames]

Source Code for Module pyvision.analysis.FaceAnalysis.FaceDatabase

  1  import pyvision as pv 
  2  from EyesFile import EyesFile 
  3  import os.path 
  4  #import numpy as np 
  5   
6 -def faceFromEyes(eye1, eye2):
7 ''' 8 Given eye coordinates estimate the face rectangle 9 Assumes the face is reasonably horizontal. 10 ''' 11 truth_rect = pv.BoundingRect(eye1, eye2) 12 truth_rect.w = 3.0 * truth_rect.w 13 truth_rect.h = truth_rect.w 14 truth_rect.x = truth_rect.x - 0.33 * truth_rect.w 15 truth_rect.y = truth_rect.y - 0.4 * truth_rect.w 16 return truth_rect
17 18
19 -class FaceDatabase:
20
21 - class FaceObject:
22 - def __init__(self):
23 self.key = None 24 self.person_id = None 25 self.image = None 26 self.left_eye = None 27 self.right_eye = None 28 self.nose = None 29 self.mouth = None 30 self.face_rect = None
31 32 #def __str__(self): 33 # return "<FaceObject %s>"%(self.key,) 34
35 - def __str__(self):
36 return self.__repr__()
37
38 - def __repr__(self):
39 w,h,rx,ry,rw,rh = -1,-1,-1,-1,-1,-1 40 if self.image != None: 41 w,h = self.image.size 42 if self.face_rect != None: 43 rx,ry,rw,rh = self.face_rect.asTuple() 44 else: 45 rect = faceFromEyes(self.left_eye,self.right_eye) 46 rx,ry,rw,rh = rect.asTuple() 47 return "FaceObject(%s, image: w%d h%d, face: x%d y%d w%d h%d)"%(self.key,w,h,rx,ry,rw,rh)
48
49 - def __init__(self):
50 pass
51
52 - def keys(self):
53 pass
54
55 - def __getitem__(self,key):
56 pass
57 58
59 -class ScrapShotsDatabase(FaceDatabase):
60
61 - def __init__(self):
62 ''' Create an object that manages a FERET face database. ''' 63 self.image_ext = ".pgm" 64 65 coord_name = os.path.join(pv.__path__[0],'data','csuScrapShots','coords.txt') 66 self.eyes_file = EyesFile(coord_name) 67 68 gender_file = os.path.join(pv.__path__[0],'data','csuScrapShots','gender.txt') 69 f = open(gender_file) 70 self.gender = {} 71 for line in f: 72 name,gender = line.split() 73 name = name.split('.')[0] 74 self.gender[name] = gender 75 76 self.image_path = os.path.join(pv.__path__[0],'data','csuScrapShots')
77 78 79 80
81 - def keys(self):
82 return self.eyes_file.files()
83
84 - def __getitem__(self,key):
85 assert self.eyes_file.hasFile(key) 86 87 face_obj = FaceDatabase.FaceObject() 88 face_obj.key = key 89 face_obj.person_id = key[:5] 90 91 leye,reye = self.eyes_file.getEyes(key)[0] 92 face_obj.left_eye = leye 93 face_obj.right_eye = reye 94 95 im_name = os.path.join(self.image_path,key+self.image_ext) 96 im = pv.Image(im_name) 97 face_obj.image = im 98 99 face_obj.gender = self.gender[key] 100 101 return face_obj
102
103 -class FERETDatabase(FaceDatabase):
104
105 - def __init__(self,image_path, image_ext=".pgm", coord_file=None):
106 ''' Create an object that manages a FERET face database. ''' 107 self.image_path = image_path 108 self.image_ext = image_ext 109 110 if coord_file == None: 111 coord_name = os.path.join(pv.__path__[0],'analysis','FaceAnalysis','data','coords.3368') 112 self.eyes_file = EyesFile(coord_name) 113 else: 114 self.eyes_file = EyesFile(coord_name)
115 116
117 - def keys(self):
118 return self.eyes_file.files()
119
120 - def __getitem__(self,key):
121 assert self.eyes_file.hasFile(key) 122 123 face_obj = FaceDatabase.FaceObject() 124 face_obj.key = key 125 face_obj.person_id = key[:5] 126 127 leye,reye = self.eyes_file.getEyes(key)[0] 128 face_obj.left_eye = leye 129 face_obj.right_eye = reye 130 131 im_name = os.path.join(self.image_path,key+self.image_ext) 132 im = pv.Image(im_name) 133 face_obj.image = im 134 135 return face_obj
136
137 -class PIE_ILLUM_Database(FaceDatabase):
138
139 - def __init__(self,image_path, image_ext=".jpg", coord_file=None):
140 ''' Create an object that manages a FERET face database. ''' 141 self.image_path = image_path 142 self.image_ext = image_ext 143 144 coord_name = os.path.join(pv.__path__[0], 'analysis', 'FaceAnalysis', 'data', 'pie_illum_coords.csv') 145 pie_months =['oct_2000-nov_2000','nov_2000-dec_2000'] 146 #pie_pose = [27,5,29,9,7] 147 #pie_illum = [19,20,21,5,6,11,12,10,7,8,9] 148 #pie_illum = [19,20,21,6,11,12,7,8,9] 149 150 # Read in PIE Eyes File 151 eyes = {} 152 for line in open(coord_name): 153 #print line 154 month,sub,pose,lx,ly,rx,ry = line.split(',') 155 156 lx = float(lx) 157 ly = float(ly) 158 rx = float(rx) 159 ry = float(ry) 160 161 key = (month,sub,int(pose)) 162 #label = "%s %s %s"%key 163 164 eyes[key] = (pv.Point(lx,ly),pv.Point(rx,ry)) 165 166 self.eyes = eyes 167 168 169 keys = [] 170 # Generate PIE keys 171 for month in pie_months: 172 month_dir = os.path.join(self.image_path,month) 173 for sub in os.listdir(month_dir): 174 if sub[0] != '0' or len(sub) != 5: 175 continue 176 illum_dir = os.path.join(month_dir,sub,"ILLUM") 177 for filename in os.listdir(illum_dir): 178 pose,illum = filename.split('.')[0].split('_') 179 pose = int(pose) 180 illum = int(illum) 181 key = (month,sub,pose,illum) 182 keys.append(key) 183 184 self.key_list = keys
185 186
187 - def keys(self):
188 return self.key_list
189
190 - def __getitem__(self,key):
191 assert key in self.key_list 192 193 face_obj = FaceDatabase.FaceObject() 194 month,sub,pose,illum = key 195 pose = int(pose) 196 illum = int(illum) 197 198 face_obj.key = key 199 face_obj.person_id = key[1] 200 201 filename = os.path.join(self.image_path,month,sub,"ILLUM","%02d_%02d"%(pose,illum)+self.image_ext) 202 203 face_obj.image = pv.Image(filename) 204 205 key = (month,sub,pose) 206 207 if self.eyes.has_key(key): 208 leye,reye = self.eyes[key] 209 210 face_obj.left_eye = leye 211 face_obj.right_eye = reye 212 213 return face_obj
214
215 -class PIE_ILLUM_Database_c27(FaceDatabase):
216
217 - def __init__(self,image_path, image_ext=".jpg", coord_file=None):
218 ''' Create an object that manages a FERET face database. ''' 219 self.image_path = image_path 220 self.image_ext = image_ext 221 222 coord_name = os.path.join(pv.__path__[0], 'analysis', 'FaceAnalysis', 'data', 'pie_illum_c27.txt') 223 pie_months =['oct_2000-nov_2000','nov_2000-dec_2000'] 224 #pie_pose = [27,5,29,9,7] 225 #pie_illum = [19,20,21,5,6,11,12,10,7,8,9] 226 #pie_illum = [19,20,21,6,11,12,7,8,9] 227 228 # Read in PIE Eyes File 229 eyes = {} 230 for line in open(coord_name): 231 #print line 232 data = line.split() 233 if len(data) != 5: 234 print "Warning: expected 5 values: <", data, ">" 235 print " in file:",coord_name 236 continue 237 sub,lx,ly,rx,ry = data 238 239 lx = float(lx) 240 ly = float(ly) 241 rx = float(rx) 242 ry = float(ry) 243 244 key = sub 245 #label = "%s %s %s"%key 246 247 eyes[key] = (pv.Point(lx,ly),pv.Point(rx,ry)) 248 249 self.eyes = eyes 250 251 252 keys = [] 253 # Generate PIE keys 254 for month in pie_months: 255 month_dir = os.path.join(self.image_path,month) 256 for sub in os.listdir(month_dir): 257 if sub[0] != '0' or len(sub) != 5: 258 continue 259 illum_dir = os.path.join(month_dir,sub,"ILLUM") 260 for filename in os.listdir(illum_dir): 261 pose,illum = filename.split('.')[0].split('_') 262 pose = int(pose) 263 illum = int(illum) 264 if pose != 27: 265 continue 266 key = (month,sub,pose,illum) 267 keys.append(key) 268 269 self.key_list = keys
270 271
272 - def keys(self):
273 return self.key_list
274
275 - def __getitem__(self,key):
276 assert key in self.key_list 277 278 face_obj = FaceDatabase.FaceObject() 279 month,sub,pose,illum = key 280 pose = int(pose) 281 illum = int(illum) 282 283 face_obj.key = key 284 face_obj.person_id = key[1] 285 286 filename = os.path.join(self.image_path,month,sub,"ILLUM","%02d_%02d"%(pose,illum)+self.image_ext) 287 288 face_obj.image = pv.Image(filename) 289 290 key = sub 291 292 if self.eyes.has_key(key): 293 leye,reye = self.eyes[key] 294 295 face_obj.left_eye = leye 296 face_obj.right_eye = reye 297 298 return face_obj
299 300 # TODO: Needs unit tests. 301