<s> import numpy as np <EOL> import os <EOL> import unittest <EOL> from pyvision . face . FaceRecognizer import FaceRecognizer <EOL> from pyvision . analysis . FaceAnalysis import FaceRecognitionTest <EOL> import pyvision . vector . PCA <EOL> from pyvision . analysis . face import EyesFile <EOL> import pyvision as pv <EOL> PCA_L1 = <NUM_LIT:1> <EOL> PCA_L2 = <NUM_LIT:2> <EOL> PCA_COS = <NUM_LIT:3> <EOL> PCA_NO_NORM = <NUM_LIT:1> <EOL> PCA_MEAN_STD_NORM = <NUM_LIT:2> <EOL> PCA_MEAN_UNIT_NORM = <NUM_LIT:3> <EOL> PCA_UNIT_NORM = <NUM_LIT:4> <EOL> class PCA ( FaceRecognizer ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , face_size = ( <NUM_LIT> , <NUM_LIT> ) , left_eye = pv . Point ( <NUM_LIT:32> , <NUM_LIT> ) , right_eye = pv . Point ( <NUM_LIT> , <NUM_LIT> ) , normalize = PCA_MEAN_STD_NORM , measure = PCA_COS , whiten = True , drop_front = <NUM_LIT:2> , basis_vectors = <NUM_LIT:100> ) : <EOL> '''<STR_LIT>''' <EOL> FaceRecognizer . __init__ ( self ) <EOL> self . face_size = face_size <EOL> self . pca = pyvision . vector . PCA . PCA ( ) <EOL> self . norm = normalize <EOL> self . trained = False <EOL> self . whiten = whiten <EOL> self . drop_front = drop_front <EOL> self . basis_vectors = basis_vectors <EOL> self . measure = measure <EOL> self . left_eye = left_eye <EOL> self . right_eye = right_eye <EOL> def cropFace ( self , im , eyes ) : <EOL> left , right = eyes <EOL> affine = pv . AffineFromPoints ( left , right , self . left_eye , self . right_eye , self . face_size ) <EOL> im = affine . transformImage ( im ) <EOL> return im <EOL> def computeFaceRecord ( self , img , rect = None , eyes = None ) : <EOL> '''<STR_LIT>''' <EOL> assert self . trained <EOL> img = self . cropFace ( img , eyes ) <EOL> vec = self . computeVector ( img ) <EOL> fir = self . pca . project ( vec , whiten = True ) <EOL> if self . measure == PCA_COS : <EOL> scale = np . sqrt ( ( fir * fir ) . sum ( ) ) <EOL> fir = ( <NUM_LIT:1.0> / scale ) * fir <EOL> return fir <EOL> def computeVector ( self , img ) : <EOL> '''<STR_LIT>''' <EOL> vec = img . asMatrix2D ( ) . flatten ( ) <EOL> if self . norm == PCA_MEAN_STD_NORM : <EOL> vec = pv . meanStd ( vec ) <EOL> if self . norm == PCA_MEAN_UNIT_NORM : <EOL> vec = pv . meanUnit ( vec ) <EOL> if self . norm == PCA_UNIT_NORM : <EOL> vec = pv . unit ( vec ) <EOL> return vec <EOL> def train ( self ) : <EOL> '''<STR_LIT>''' <EOL> assert self . trained == False <EOL> for img , _ , eyes , _ in self . training_data : <EOL> img = self . cropFace ( img , eyes ) <EOL> vec = self . computeVector ( img ) <EOL> self . pca . addFeature ( vec ) <EOL> self . pca . train ( drop_front = self . drop_front , number = self . basis_vectors ) <EOL> self . trained = True <EOL> def similarity ( self , fir1 , fir2 ) : <EOL> '''<STR_LIT>''' <EOL> assert self . trained == True <EOL> if self . measure == PCA_L1 : <EOL> return ( np . abs ( fir1 - fir2 ) ) . sum ( ) <EOL> if self . measure == PCA_L2 : <EOL> return np . sqrt ( ( ( fir1 - fir2 ) * ( fir1 - fir2 ) ) . sum ( ) ) <EOL> if self . measure == PCA_COS : <EOL> return ( fir1 * fir2 ) . sum ( ) <EOL> raise NotImplementedError ( "<STR_LIT>" % self . measure ) <EOL> def getBasis ( self ) : <EOL> basis = self . pca . getBasis ( ) <EOL> images = [ ] <EOL> print basis . shape <EOL> r , _ = basis . shape <EOL> for i in range ( r ) : <EOL> im = basis [ i , : ] <EOL> im = im . reshape ( self . face_size ) <EOL> im = pv . Image ( im ) <EOL> images . append ( im ) <EOL> print len ( images ) <EOL> return images <EOL> SCRAPS_FACE_DATA = os . path . join ( pyvision . __path__ [ <NUM_LIT:0> ] , "<STR_LIT:data>" , "<STR_LIT>" ) <EOL> PCA_SIZE = ( <NUM_LIT:64> , <NUM_LIT:64> ) <EOL> class _TestFacePCA ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . images = [ ] <EOL> self . names = [ ] <EOL> self . eyes = EyesFile ( os . path . join ( SCRAPS_FACE_DATA , "<STR_LIT>" ) ) <EOL> for filename in self . eyes . files ( ) : <EOL> img = pv . Image ( os . path . join ( SCRAPS_FACE_DATA , filename + "<STR_LIT>" ) ) <EOL> self . images . append ( img ) <EOL> self . names . append ( filename ) <EOL> self . assert_ ( len ( self . images ) == <NUM_LIT> ) <EOL> def test_pca_scraps ( self ) : <EOL> face_test = FaceRecognitionTest . FaceRecognitionTest ( name = '<STR_LIT>' , score_type = FaceRecognitionTest . SCORE_TYPE_HIGH ) <EOL> pca = PCA ( drop_front = <NUM_LIT:2> , basis_vectors = <NUM_LIT> ) <EOL> for im_name in self . eyes . files ( ) : <EOL> im = pv . Image ( os . path . join ( SCRAPS_FACE_DATA , im_name + "<STR_LIT>" ) ) <EOL> rect = self . eyes . getFaces ( im_name ) <EOL> eyes = self . eyes . getEyes ( im_name ) <EOL> pca . addTraining ( im , rect = rect [ <NUM_LIT:0> ] , eyes = eyes [ <NUM_LIT:0> ] ) <EOL> pca . train ( ) <EOL> face_records = { } <EOL> for im_name in self . eyes . files ( ) : <EOL> im = pv . Image ( os . path . join ( SCRAPS_FACE_DATA , im_name + "<STR_LIT>" ) ) <EOL> rect = self . eyes . getFaces ( im_name ) <EOL> eyes = self . eyes . getEyes ( im_name ) <EOL> fr = pca . computeFaceRecord ( im , rect = rect [ <NUM_LIT:0> ] , eyes = eyes [ <NUM_LIT:0> ] ) <EOL> face_records [ im_name ] = fr <EOL> for i_name in face_records . keys ( ) : <EOL> scores = [ ] <EOL> for j_name in face_records . keys ( ) : <EOL> similarity = pca . similarity ( face_records [ i_name ] , face_records [ j_name ] ) <EOL> scores . append ( ( j_name , similarity ) ) <EOL> face_test . addSample ( i_name , scores ) <EOL> self . assertAlmostEqual ( face_test . rank1_rate , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( face_test . rank1_bounds [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . assertAlmostEqual ( face_test . rank1_bounds [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> roc = face_test . getROCAnalysis ( ) <EOL> _ = roc . getFAR ( far = <NUM_LIT> ) </s>
