<s> import pyvision as pv <EOL> from EyesFile import EyesFile <EOL> import os . path <EOL> def faceFromEyes ( eye1 , eye2 ) : <EOL> '''<STR_LIT>''' <EOL> truth_rect = pv . BoundingRect ( eye1 , eye2 ) <EOL> truth_rect . w = <NUM_LIT> * truth_rect . w <EOL> truth_rect . h = truth_rect . w <EOL> truth_rect . x = truth_rect . x - <NUM_LIT> * truth_rect . w <EOL> truth_rect . y = truth_rect . y - <NUM_LIT> * truth_rect . w <EOL> return truth_rect <EOL> class FaceDatabase : <EOL> class FaceObject : <EOL> def __init__ ( self ) : <EOL> self . key = None <EOL> self . person_id = None <EOL> self . image = None <EOL> self . left_eye = None <EOL> self . right_eye = None <EOL> self . nose = None <EOL> self . mouth = None <EOL> self . face_rect = None <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def __repr__ ( self ) : <EOL> w , h , rx , ry , rw , rh = - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> <EOL> if self . image != None : <EOL> w , h = self . image . size <EOL> if self . face_rect != None : <EOL> rx , ry , rw , rh = self . face_rect . asTuple ( ) <EOL> else : <EOL> rect = faceFromEyes ( self . left_eye , self . right_eye ) <EOL> rx , ry , rw , rh = rect . asTuple ( ) <EOL> return "<STR_LIT>" % ( self . key , w , h , rx , ry , rw , rh ) <EOL> def __init__ ( self ) : <EOL> pass <EOL> def keys ( self ) : <EOL> pass <EOL> def __getitem__ ( self , key ) : <EOL> pass <EOL> class ScrapShotsDatabase ( FaceDatabase ) : <EOL> def __init__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . image_ext = "<STR_LIT>" <EOL> coord_name = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT:data>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . eyes_file = EyesFile ( coord_name ) <EOL> gender_file = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT:data>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> f = open ( gender_file ) <EOL> self . gender = { } <EOL> for line in f : <EOL> name , gender = line . split ( ) <EOL> name = name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> self . gender [ name ] = gender <EOL> self . image_path = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> def keys ( self ) : <EOL> return self . eyes_file . files ( ) <EOL> def __getitem__ ( self , key ) : <EOL> assert self . eyes_file . hasFile ( key ) <EOL> face_obj = FaceDatabase . FaceObject ( ) <EOL> face_obj . key = key <EOL> face_obj . person_id = key [ : <NUM_LIT:5> ] <EOL> leye , reye = self . eyes_file . getEyes ( key ) [ <NUM_LIT:0> ] <EOL> face_obj . left_eye = leye <EOL> face_obj . right_eye = reye <EOL> im_name = os . path . join ( self . image_path , key + self . image_ext ) <EOL> im = pv . Image ( im_name ) <EOL> face_obj . image = im <EOL> face_obj . gender = self . gender [ key ] <EOL> return face_obj <EOL> class FERETDatabase ( FaceDatabase ) : <EOL> def __init__ ( self , image_path , image_ext = "<STR_LIT>" , coord_file = None ) : <EOL> '''<STR_LIT>''' <EOL> self . image_path = image_path <EOL> self . image_ext = image_ext <EOL> if coord_file == None : <EOL> coord_name = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> self . eyes_file = EyesFile ( coord_name ) <EOL> else : <EOL> self . eyes_file = EyesFile ( coord_name ) <EOL> def keys ( self ) : <EOL> return self . eyes_file . files ( ) <EOL> def __getitem__ ( self , key ) : <EOL> assert self . eyes_file . hasFile ( key ) <EOL> face_obj = FaceDatabase . FaceObject ( ) <EOL> face_obj . key = key <EOL> face_obj . person_id = key [ : <NUM_LIT:5> ] <EOL> leye , reye = self . eyes_file . getEyes ( key ) [ <NUM_LIT:0> ] <EOL> face_obj . left_eye = leye <EOL> face_obj . right_eye = reye <EOL> im_name = os . path . join ( self . image_path , key + self . image_ext ) <EOL> im = pv . Image ( im_name ) <EOL> face_obj . image = im <EOL> return face_obj <EOL> class PIE_ILLUM_Database ( FaceDatabase ) : <EOL> def __init__ ( self , image_path , image_ext = "<STR_LIT>" , coord_file = None ) : <EOL> '''<STR_LIT>''' <EOL> self . image_path = image_path <EOL> self . image_ext = image_ext <EOL> coord_name = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> pie_months = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> eyes = { } <EOL> for line in open ( coord_name ) : <EOL> month , sub , pose , lx , ly , rx , ry = line . split ( '<STR_LIT:U+002C>' ) <EOL> lx = float ( lx ) <EOL> ly = float ( ly ) <EOL> rx = float ( rx ) <EOL> ry = float ( ry ) <EOL> key = ( month , sub , int ( pose ) ) <EOL> eyes [ key ] = ( pv . Point ( lx , ly ) , pv . Point ( rx , ry ) ) <EOL> self . eyes = eyes <EOL> keys = [ ] <EOL> for month in pie_months : <EOL> month_dir = os . path . join ( self . image_path , month ) <EOL> for sub in os . listdir ( month_dir ) : <EOL> if sub [ <NUM_LIT:0> ] != '<STR_LIT:0>' or len ( sub ) != <NUM_LIT:5> : <EOL> continue <EOL> illum_dir = os . path . join ( month_dir , sub , "<STR_LIT>" ) <EOL> for filename in os . listdir ( illum_dir ) : <EOL> pose , illum = filename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:_>' ) <EOL> pose = int ( pose ) <EOL> illum = int ( illum ) <EOL> key = ( month , sub , pose , illum ) <EOL> keys . append ( key ) <EOL> self . key_list = keys <EOL> def keys ( self ) : <EOL> return self . key_list <EOL> def __getitem__ ( self , key ) : <EOL> assert key in self . key_list <EOL> face_obj = FaceDatabase . FaceObject ( ) <EOL> month , sub , pose , illum = key <EOL> pose = int ( pose ) <EOL> illum = int ( illum ) <EOL> face_obj . key = key <EOL> face_obj . person_id = key [ <NUM_LIT:1> ] <EOL> filename = os . path . join ( self . image_path , month , sub , "<STR_LIT>" , "<STR_LIT>" % ( pose , illum ) + self . image_ext ) <EOL> face_obj . image = pv . Image ( filename ) <EOL> key = ( month , sub , pose ) <EOL> if self . eyes . has_key ( key ) : <EOL> leye , reye = self . eyes [ key ] <EOL> face_obj . left_eye = leye <EOL> face_obj . right_eye = reye <EOL> return face_obj <EOL> class PIE_ILLUM_Database_c27 ( FaceDatabase ) : <EOL> def __init__ ( self , image_path , image_ext = "<STR_LIT>" , coord_file = None ) : <EOL> '''<STR_LIT>''' <EOL> self . image_path = image_path <EOL> self . image_ext = image_ext <EOL> coord_name = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> pie_months = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> eyes = { } <EOL> for line in open ( coord_name ) : <EOL> data = line . split ( ) <EOL> if len ( data ) != <NUM_LIT:5> : <EOL> print "<STR_LIT>" , data , "<STR_LIT:>>" <EOL> print "<STR_LIT>" , coord_name <EOL> continue <EOL> sub , lx , ly , rx , ry = data <EOL> lx = float ( lx ) <EOL> ly = float ( ly ) <EOL> rx = float ( rx ) <EOL> ry = float ( ry ) <EOL> key = sub <EOL> eyes [ key ] = ( pv . Point ( lx , ly ) , pv . Point ( rx , ry ) ) <EOL> self . eyes = eyes <EOL> keys = [ ] <EOL> for month in pie_months : <EOL> month_dir = os . path . join ( self . image_path , month ) <EOL> for sub in os . listdir ( month_dir ) : <EOL> if sub [ <NUM_LIT:0> ] != '<STR_LIT:0>' or len ( sub ) != <NUM_LIT:5> : <EOL> continue <EOL> illum_dir = os . path . join ( month_dir , sub , "<STR_LIT>" ) <EOL> for filename in os . listdir ( illum_dir ) : <EOL> pose , illum = filename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:_>' ) <EOL> pose = int ( pose ) <EOL> illum = int ( illum ) <EOL> if pose != <NUM_LIT> : <EOL> continue <EOL> key = ( month , sub , pose , illum ) <EOL> keys . append ( key ) <EOL> self . key_list = keys <EOL> def keys ( self ) : <EOL> return self . key_list <EOL> def __getitem__ ( self , key ) : <EOL> assert key in self . key_list <EOL> face_obj = FaceDatabase . FaceObject ( ) <EOL> month , sub , pose , illum = key <EOL> pose = int ( pose ) <EOL> illum = int ( illum ) <EOL> face_obj . key = key <EOL> face_obj . person_id = key [ <NUM_LIT:1> ] <EOL> filename = os . path . join ( self . image_path , month , sub , "<STR_LIT>" , "<STR_LIT>" % ( pose , illum ) + self . image_ext ) <EOL> face_obj . image = pv . Image ( filename ) <EOL> key = sub <EOL> if self . eyes . has_key ( key ) : <EOL> leye , reye = self . eyes [ key ] <EOL> face_obj . left_eye = leye <EOL> face_obj . right_eye = reye <EOL> return face_obj </s>
