<s> """<STR_LIT>""" <EOL> from face3d import AbsFile , Face , Database <EOL> from face3d import utils , algorithms <EOL> import argparse <EOL> import glob <EOL> import os <EOL> import sys <EOL> import numpy <EOL> import threading <EOL> parameters = { "<STR_LIT:N>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT:12> } <EOL> parameter_types = { "<STR_LIT:N>" : int , "<STR_LIT>" : int } <EOL> database = None <EOL> debug = False <EOL> def main ( ) : <EOL> global database , parameters <EOL> success = False <EOL> arguments , parser = argument_parser ( ) <EOL> try : <EOL> for parameter in arguments . parameters . split ( '<STR_LIT:U+002C>' ) : <EOL> key , value = parameter . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> parameters [ key ] = value <EOL> except : <EOL> print "<STR_LIT>" % arguments . parameters <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> for key , value in parameters . iteritems ( ) : <EOL> try : <EOL> parameters [ key ] = parameter_types [ key ] ( value ) <EOL> except : <EOL> print "<STR_LIT>" % key <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> database = Database ( arguments . database ) <EOL> if arguments . enroll : <EOL> success = True <EOL> is_directory , path = arguments . enroll <EOL> if is_directory : <EOL> files = glob . glob ( "<STR_LIT>" % path ) <EOL> thread_count = <NUM_LIT:16> <EOL> chunks = [ files [ i : : thread_count ] for i in range ( thread_count ) ] <EOL> threads = [ ] <EOL> for chunk in chunks : <EOL> thread = threading . Thread ( target = lambda x : [ enroll_face ( c , arguments . person_id , arguments . auto_id ) for c in x ] , args = ( chunk , ) ) <EOL> thread . daemon = True <EOL> threads . append ( thread ) <EOL> thread . start ( ) <EOL> for thread in threads : <EOL> thread . join ( ) <EOL> else : <EOL> enroll_face ( path , arguments . person_id , arguments . auto_id ) <EOL> faces = False <EOL> matrix = False <EOL> rocs = False <EOL> if arguments . authenticate : <EOL> print "<STR_LIT>" % arguments . authenticate <EOL> success = True <EOL> face = Face ( AbsFile ( arguments . authenticate ) ) <EOL> algorithms . process ( face , parameters [ "<STR_LIT:N>" ] , parameters [ "<STR_LIT>" ] ) <EOL> if not faces : faces = list ( database . iterator ( ) ) <EOL> matrix = algorithms . similarity_matrix ( [ face ] + [ face [ <NUM_LIT:2> ] for face in faces ] , limit = <NUM_LIT:1> ) <EOL> methods , _ , _ = matrix . shape <EOL> tresholds = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> for i in range ( methods ) : <EOL> vector = numpy . array ( matrix [ i ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> candidates , = numpy . where ( vector >= tresholds [ i ] ) <EOL> persons = { } <EOL> if len ( candidates ) == <NUM_LIT:0> : <EOL> print "<STR_LIT>" % i <EOL> continue <EOL> print "<STR_LIT>" % i <EOL> for candidate in candidates : <EOL> if candidate == <NUM_LIT:0> : <EOL> continue <EOL> filename , person_id , data = faces [ candidate ] <EOL> if person_id not in persons : <EOL> persons [ person_id ] = [ ] <EOL> persons [ person_id ] . append ( matrix [ i ] [ <NUM_LIT:0> ] [ candidate + <NUM_LIT:1> ] ) <EOL> for person , scores in persons . iteritems ( ) : <EOL> print "<STR_LIT>" % ( person , [ "<STR_LIT>" % s for s in scores ] ) <EOL> if arguments . reevaluate : <EOL> print "<STR_LIT>" <EOL> success = True <EOL> if not faces : faces = list ( database . iterator ( ) ) <EOL> [ algorithms . features_histogram ( face [ <NUM_LIT:2> ] , parameters [ "<STR_LIT:N>" ] , parameters [ "<STR_LIT>" ] ) for face in faces ] <EOL> if arguments . depth_map : <EOL> print "<STR_LIT>" <EOL> success = True <EOL> if not faces : faces = list ( database . iterator ( ) ) <EOL> utils . generate_depth_map ( faces , arguments . depth_map , arguments . draw_key_points ) <EOL> if arguments . feature_map : <EOL> print "<STR_LIT>" <EOL> success = True <EOL> if not faces : faces = list ( database . iterator ( ) ) <EOL> utils . generate_feature_map ( faces , arguments . feature_map ) <EOL> if arguments . similarity_matrix : <EOL> print "<STR_LIT>" <EOL> success = True <EOL> if not faces : faces = list ( database . iterator ( ) ) <EOL> if not matrix : matrix = algorithms . similarity_matrix ( [ face [ <NUM_LIT:2> ] for face in faces ] ) <EOL> utils . generate_similarity_matrix ( matrix , faces , arguments . similarity_matrix ) <EOL> if arguments . roc_curve : <EOL> print "<STR_LIT>" <EOL> success = True <EOL> if not faces : faces = list ( database . iterator ( ) ) <EOL> if not matrix : matrix = algorithms . similarity_matrix ( [ face [ <NUM_LIT:2> ] for face in faces ] ) <EOL> if not rocs : rocs = algorithms . calculate_roc_eer ( matrix , [ face [ <NUM_LIT:1> ] for face in faces ] ) <EOL> utils . generate_roc_curve ( rocs , arguments . roc_curve ) <EOL> if not success : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def enroll_face ( file , person_id = None , auto_id = False , force = False ) : <EOL> filename = os . path . basename ( file ) <EOL> if database . exists ( file ) and not force : <EOL> print "<STR_LIT>" % filename <EOL> return <EOL> if not person_id and not auto_id : <EOL> print "<STR_LIT>" <EOL> return <EOL> print "<STR_LIT>" % filename <EOL> absfile = AbsFile ( file ) <EOL> if auto_id : <EOL> basename = os . path . basename ( file ) <EOL> person_id = basename [ : basename . index ( '<STR_LIT:d>' ) ] <EOL> face = Face ( absfile ) <EOL> try : <EOL> algorithms . process ( face , parameters [ "<STR_LIT:N>" ] , parameters [ "<STR_LIT>" ] ) <EOL> face . compress ( ) <EOL> except : <EOL> print "<STR_LIT>" % file <EOL> if debug : <EOL> raise <EOL> else : <EOL> return <EOL> database . save ( file , person_id , face ) <EOL> def argument_parser ( ) : <EOL> def helper_file_or_directory ( parser ) : <EOL> def _inner ( path ) : <EOL> path = os . path . realpath ( path ) <EOL> if not os . path . exists ( path ) : <EOL> parser . error ( "<STR_LIT>" % path ) <EOL> else : <EOL> if os . path . isdir ( path ) : <EOL> return ( True , path ) <EOL> else : <EOL> return ( False , open ( path , "<STR_LIT:rb>" ) ) <EOL> return _inner <EOL> def helper_file ( parser ) : <EOL> def _inner ( file ) : <EOL> path = os . path . realpath ( file ) <EOL> if not os . path . exists ( file ) or os . path . isdir ( file ) : <EOL> parser . error ( "<STR_LIT>" % path ) <EOL> return file <EOL> return _inner <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> group = parser . add_argument_group ( title = "<STR_LIT>" ) <EOL> group . add_argument ( '<STR_LIT>' , type = helper_file ( parser ) , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , type = helper_file_or_directory ( parser ) , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> group = parser . add_argument_group ( title = "<STR_LIT>" ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , help = '<STR_LIT>' ) <EOL> group . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> return parser . parse_args ( ) , parser <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
