<s> import os <EOL> import pyvision as pv <EOL> from pyvision . analysis . FaceAnalysis . FaceDetectionTest import is_success <EOL> class EyesFile : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , filename ) : <EOL> '''<STR_LIT>''' <EOL> self . filename = filename <EOL> self . images = { } <EOL> self . _readEyesFile ( ) <EOL> def files ( self ) : <EOL> '''<STR_LIT>''' <EOL> names = self . images . keys ( ) <EOL> names . sort ( ) <EOL> return names <EOL> def findFace ( self , filename , rect ) : <EOL> fname = self . _parseName ( filename ) <EOL> if self . images . has_key ( fname ) : <EOL> faces = self . images [ fname ] <EOL> for each in faces : <EOL> truth_rect = each [ <NUM_LIT:3> ] <EOL> if is_success ( truth_rect , rect ) : <EOL> return each <EOL> return None <EOL> def getFaces ( self , filename ) : <EOL> fname = self . _parseName ( filename ) <EOL> if self . images . has_key ( fname ) : <EOL> faces = self . images [ fname ] <EOL> boxes = [ ] <EOL> for _ , _ , _ , box in faces : <EOL> boxes . append ( box ) <EOL> return boxes <EOL> return [ ] <EOL> def getEyes ( self , filename ) : <EOL> fname = self . _parseName ( filename ) <EOL> if self . images . has_key ( fname ) : <EOL> faces = self . images [ fname ] <EOL> eyes = [ ] <EOL> for _ , left , right , _ in faces : <EOL> eyes . append ( [ left , right ] ) <EOL> return eyes <EOL> return [ ] <EOL> def _readEyesFile ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . filename [ - <NUM_LIT:4> : ] == '<STR_LIT>' : <EOL> f = open ( self . filename , '<STR_LIT:r>' ) <EOL> for line in f : <EOL> line = line . split ( '<STR_LIT:U+002C>' ) <EOL> fname = self . _parseName ( line [ <NUM_LIT:0> ] ) <EOL> eye1 = pv . Point ( float ( line [ <NUM_LIT:1> ] ) , float ( line [ <NUM_LIT:2> ] ) ) <EOL> eye2 = pv . Point ( float ( line [ <NUM_LIT:3> ] ) , float ( line [ <NUM_LIT:4> ] ) ) <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> if not self . images . has_key ( fname ) : <EOL> self . images [ fname ] = [ ] <EOL> self . images [ fname ] . append ( [ fname , eye1 , eye2 , truth_rect ] ) <EOL> else : <EOL> f = open ( self . filename , '<STR_LIT:r>' ) <EOL> for line in f : <EOL> line = line . split ( ) <EOL> fname = self . _parseName ( line [ <NUM_LIT:0> ] ) <EOL> eye1 = pv . Point ( float ( line [ <NUM_LIT:1> ] ) , float ( line [ <NUM_LIT:2> ] ) ) <EOL> eye2 = pv . Point ( float ( line [ <NUM_LIT:3> ] ) , float ( line [ <NUM_LIT:4> ] ) ) <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> if not self . images . has_key ( fname ) : <EOL> self . images [ fname ] = [ ] <EOL> self . images [ fname ] . append ( [ fname , eye1 , eye2 , truth_rect ] ) <EOL> def _parseName ( self , fname ) : <EOL> '''<STR_LIT>''' <EOL> fname = os . path . basename ( fname ) <EOL> fname = os . path . splitext ( fname ) [ <NUM_LIT:0> ] <EOL> return fname <EOL> class CSU_SRT : <EOL> class ImageRecord : <EOL> def __init__ ( self , filename , subject_id , image_id ) : <EOL> self . filename = filename <EOL> self . subject_id = subject_id <EOL> self . image_id = image_id <EOL> def __init__ ( self , filename ) : <EOL> '''<STR_LIT>''' <EOL> self . images = [ ] <EOL> self . filenames = { } <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> subject_id = <NUM_LIT:0> <EOL> image_id = <NUM_LIT:0> <EOL> filename = None <EOL> for line in f : <EOL> images = line . split ( ) <EOL> if images : <EOL> for image in images : <EOL> name = image . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> image_id += <NUM_LIT:1> <EOL> print name , image_id , subject_id <EOL> ir = CSU_SRT . ImageRecord ( name , subject_id , image_id ) <EOL> self . images . append ( ir ) <EOL> self . filenames [ name ] = ir <EOL> subject_id += <NUM_LIT:1> <EOL> self . total_subjects = subject_id <EOL> self . total_images = image_id <EOL> def getNames ( self ) : <EOL> tmp = self . filenames . keys ( ) <EOL> tmp . sort ( ) <EOL> return tmp ; <EOL> def getRecord ( self , name ) : <EOL> if self . filenames . has_key ( name ) : <EOL> return self . filenames [ name ] <EOL> return None <EOL> class CSU_Dist : <EOL> def __init__ ( self , directory , srt , extention = '<STR_LIT>' ) : <EOL> self . matrix = { } <EOL> self . srt = srt <EOL> count = <NUM_LIT:0> <EOL> for iname in srt . getNames ( ) : <EOL> self . matrix [ iname ] = { } <EOL> filename = directory + '<STR_LIT:/>' + iname + extention <EOL> print "<STR_LIT>" , iname <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> for line in f : <EOL> jname , dist = line . split ( ) <EOL> jname = jname . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if srt . getRecord ( jname ) : <EOL> self . matrix [ iname ] [ jname ] = - float ( dist ) <EOL> count += <NUM_LIT:1> <EOL> print "<STR_LIT>" , count <EOL> def getPosNeg ( self ) : <EOL> names = self . srt . getNames ( ) <EOL> pos = [ ] <EOL> neg = [ ] <EOL> for i in range ( len ( names ) ) : <EOL> for j in range ( i + <NUM_LIT:1> , len ( names ) ) : <EOL> iname = names [ i ] <EOL> jname = names [ j ] <EOL> if self . srt . getRecord ( iname ) . subject_id == self . srt . getRecord ( jname ) . subject_id : <EOL> pos . append ( self . matrix [ iname ] [ jname ] ) <EOL> else : <EOL> neg . append ( self . matrix [ iname ] [ jname ] ) <EOL> return pos , neg <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> srt = CSU_SRT ( "<STR_LIT>" ) <EOL> ebgm_dist = CSU_Dist ( "<STR_LIT>" , srt ) <EOL> pca_dist = CSU_Dist ( "<STR_LIT>" , srt ) <EOL> ebgm_pos , ebgm_neg = ebgm_dist . getPosNeg ( ) <EOL> pca_pos , pca_neg = pca_dist . getPosNeg ( ) <EOL> from pyvis . analysis . roc import * <EOL> ebgm_roc = pv . ROC ( ebgm_pos , ebgm_neg ) <EOL> pca_roc = pv . ROC ( pca_pos , pca_neg ) </s>
