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

Source Code for Module pyvision.analysis.FaceAnalysis.EyesFile

  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 .FaceDetectionTest import is_success 
 37  #from os.path import basename,splitext 
 38  #from pyvision.types.Point import Point 
 39  #from pyvision.types.Rect import Rect,BoundingRect 
 40   
41 -class EyesFile:
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
52 - def __init__(self,filename):
53 ''' 54 Inits and reads in the data. 55 ''' 56 self.filename = filename 57 self.images = {} 58 59 self._readEyesFile()
60
61 - def files(self):
62 ''' 63 @returns: a list of all image filenames 64 ''' 65 names = self.images.keys() 66 names.sort() 67 return names
68
69 - def hasFile(self,filename):
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
75 - def findFace(self,filename,rect):
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 # Get a list of face bounding boxes. 96 # 97 # @param filename name of the image file 98 # @return list of face rectangles
99 - def getFaces(self,filename):
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 # Get a list of eye points. 111 # 112 # @param filename name of the image file 113 # @returns a list of eye coordinates: [(leye,reye),(leye,reye),...]
114 - def getEyes(self,filename):
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
124 - def _readEyesFile(self):
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 #print line 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 #print fname,eye1,eye2,truth_rect 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 #print line, 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 #print fname,eye1,eye2,truth_rect 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
178 - def _parseName(self,fname):
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