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

Source Code for Module pyvision.analysis.face

  1  # PyVision License 
  2  # 
  3  # Copyright (c) 2006-2008 David S. Bolme 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  #  
 10  # 1. Redistributions of source code must retain the above copyright 
 11  # notice, this list of conditions and the following disclaimer. 
 12  #  
 13  # 2. Redistributions in binary form must reproduce the above copyright 
 14  # notice, this list of conditions and the following disclaimer in the 
 15  # documentation and/or other materials provided with the distribution. 
 16  #  
 17  # 3. Neither name of copyright holders nor the names of its contributors 
 18  # may be used to endorse or promote products derived from this software 
 19  # without specific prior written permission. 
 20  #  
 21  #  
 22  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 23  # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 24  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 25  # A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR 
 26  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 27  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 28  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 30  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 31  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 32  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  import os 
 35  import pyvision as pv 
 36  from pyvision.analysis.FaceAnalysis.FaceDetectionTest import is_success 
 37   
38 -class EyesFile:
39 ''' 40 Reads and manages the data in an eye coordinate file. 41 ''' 42
43 - def __init__(self,filename):
44 ''' 45 Inits and reads in the data. 46 ''' 47 self.filename = filename 48 self.images = {} 49 50 self._readEyesFile()
51
52 - def files(self):
53 ''' 54 Returns the list of file names. 55 ''' 56 names = self.images.keys() 57 names.sort() 58 return names
59
60 - def findFace(self,filename,rect):
61 fname = self._parseName(filename) 62 if self.images.has_key(fname): 63 faces = self.images[fname] 64 for each in faces: 65 truth_rect = each[3] 66 if is_success(truth_rect,rect): 67 return each 68 return None
69
70 - def getFaces(self,filename):
71 fname = self._parseName(filename) 72 if self.images.has_key(fname): 73 faces = self.images[fname] 74 boxes = [] 75 for _,_,_,box in faces: 76 boxes.append(box) 77 return boxes 78 return []
79
80 - def getEyes(self,filename):
81 fname = self._parseName(filename) 82 if self.images.has_key(fname): 83 faces = self.images[fname] 84 eyes = [] 85 for _,left,right,_ in faces: 86 eyes.append([left,right]) 87 return eyes 88 return []
89
90 - def _readEyesFile(self):
91 ''' 92 Private: Do not call directly. Reads the eye file. 93 ''' 94 if self.filename[-4:] == '.csv': 95 f = open(self.filename,'r') 96 for line in f: 97 #print line, 98 line = line.split(',') 99 fname = self._parseName(line[0]) 100 eye1 = pv.Point(float(line[1]),float(line[2])) 101 eye2 = pv.Point(float(line[3]),float(line[4])) 102 103 truth_rect = pv.BoundingRect(eye1,eye2) 104 truth_rect.w = 2.0 * truth_rect.w 105 truth_rect.h = truth_rect.w 106 truth_rect.x = truth_rect.x - 0.25*truth_rect.w 107 truth_rect.y = truth_rect.y - 0.3*truth_rect.w 108 109 #print fname,eye1,eye2,truth_rect 110 111 if not self.images.has_key(fname): 112 self.images[fname] = [] 113 114 self.images[fname].append([fname,eye1,eye2,truth_rect]) 115 116 else: 117 f = open(self.filename,'r') 118 for line in f: 119 #print line, 120 line = line.split() 121 fname = self._parseName(line[0]) 122 eye1 = pv.Point(float(line[1]),float(line[2])) 123 eye2 = pv.Point(float(line[3]),float(line[4])) 124 125 truth_rect = pv.BoundingRect(eye1,eye2) 126 truth_rect.w = 2.0 * truth_rect.w 127 truth_rect.h = truth_rect.w 128 truth_rect.x = truth_rect.x - 0.25*truth_rect.w 129 truth_rect.y = truth_rect.y - 0.3*truth_rect.w 130 131 #print fname,eye1,eye2,truth_rect 132 133 if not self.images.has_key(fname): 134 self.images[fname] = [] 135 136 self.images[fname].append([fname,eye1,eye2,truth_rect])
137
138 - def _parseName(self,fname):
139 ''' 140 Private: Do not call directly. Parses the base filename. 141 ''' 142 fname = os.path.basename(fname) 143 fname = os.path.splitext(fname)[0] 144 return fname
145 146
147 -class CSU_SRT:
148
149 - class ImageRecord:
150 - def __init__(self,filename,subject_id,image_id):
151 self.filename = filename 152 self.subject_id = subject_id 153 self.image_id = image_id
154
155 - def __init__(self,filename):
156 '''Process a Subject Replicate Table file''' 157 self.images = [] 158 self.filenames = {} 159 160 f = open(filename,'r') 161 162 subject_id = 0 163 image_id = 0 164 filename = None 165 for line in f: 166 images = line.split() 167 if images: 168 for image in images: 169 name = image.split('.')[0] 170 image_id += 1 171 print name, image_id, subject_id 172 ir = CSU_SRT.ImageRecord(name,subject_id,image_id) 173 self.images.append(ir) 174 self.filenames[name] = ir 175 subject_id += 1 176 177 self.total_subjects = subject_id 178 self.total_images = image_id
179
180 - def getNames(self):
181 tmp = self.filenames.keys() 182 tmp.sort() 183 return tmp;
184
185 - def getRecord(self,name):
186 if self.filenames.has_key(name): 187 return self.filenames[name] 188 189 return None
190 191
192 -class CSU_Dist:
193 - def __init__(self,directory,srt,extention='.sfi'):
194 #names = srt.getNames() 195 self.matrix = {} 196 self.srt = srt 197 198 count = 0 199 for iname in srt.getNames(): 200 self.matrix[iname] = {} 201 filename = directory+'/'+iname+extention 202 print "Reading:",iname 203 f = open(filename,'r') 204 for line in f: 205 jname,dist = line.split() 206 jname = jname.split('.')[0] 207 if srt.getRecord(jname): 208 self.matrix[iname][jname] = -float(dist) 209 count += 1 210 print "Read:",count
211 212
213 - def getPosNeg(self):
214 names = self.srt.getNames() 215 pos = [] 216 neg = [] 217 for i in range(len(names)): 218 for j in range(i+1,len(names)): 219 iname = names[i] 220 jname = names[j] 221 if self.srt.getRecord(iname).subject_id == self.srt.getRecord(jname).subject_id: 222 pos.append(self.matrix[iname][jname]) 223 else: 224 neg.append(self.matrix[iname][jname]) 225 return pos,neg
226 227 228 229 230 if __name__ == "__main__": 231 srt = CSU_SRT("/Users/bolme/vision/csuFaceIdBenchmark/imagelists/list640.srt") 232 ebgm_dist = CSU_Dist("/Users/bolme/vision/csuFaceIdBenchmark/distances/feret/EBGM",srt) 233 pca_dist = CSU_Dist("/Users/bolme/vision/csuFaceIdBenchmark/distances/feret/PCA_Euclidean",srt) 234 ebgm_pos, ebgm_neg = ebgm_dist.getPosNeg() 235 pca_pos, pca_neg = pca_dist.getPosNeg() 236 237 from pyvis.analysis.roc import * 238 ebgm_roc = pv.ROC(ebgm_pos,ebgm_neg) 239 pca_roc = pv.ROC(pca_pos,pca_neg) 240