<s> '''<STR_LIT>''' <EOL> import optparse <EOL> import csv <EOL> import os <EOL> import pyvision as pv <EOL> from pyvision . face . CascadeDetector import CascadeDetector <EOL> from pyvision . face . FilterEyeLocator import FilterEyeLocator <EOL> import PIL <EOL> import random <EOL> EXTENSIONS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def parseOptions ( ) : <EOL> usage = "<STR_LIT>" <EOL> parser = optparse . OptionParser ( usage ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = False , <EOL> action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = <NUM_LIT:1.0> , type = '<STR_LIT:float>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = <NUM_LIT:1.0> , type = '<STR_LIT:float>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = None , type = '<STR_LIT:int>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> parser . error ( "<STR_LIT>" ) <EOL> return options , args <EOL> def processFaces ( im , face_detect , locate_eyes ) : <EOL> faces = face_detect ( im ) <EOL> return locate_eyes ( im , faces ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> options , args = parseOptions ( ) <EOL> image_names = [ ] <EOL> for dirpath , dirnames , filenames in os . walk ( args [ <NUM_LIT:0> ] ) : <EOL> for filename in filenames : <EOL> extension = filename . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> extension = extension . upper ( ) <EOL> if ( options . extension == None and extension in EXTENSIONS ) or ( options . extension != None and options . extension . upper ( ) == extension ) : <EOL> pathname = os . path . join ( dirpath , filename ) <EOL> image_names . append ( pathname ) <EOL> if options . sample != None : <EOL> image_names = random . sample ( image_names , options . sample ) <EOL> f = open ( args [ <NUM_LIT:1> ] , '<STR_LIT:wb>' ) <EOL> csv_file = csv . writer ( f ) <EOL> headers = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> csv_file . writerow ( headers ) <EOL> ilog = None <EOL> if options . log_dir != None : <EOL> print "<STR_LIT>" <EOL> ilog = pv . ImageLog ( options . log_dir ) <EOL> face_detect = CascadeDetector ( image_scale = <NUM_LIT> * options . scale ) <EOL> locate_eyes = FilterEyeLocator ( ) <EOL> c = <NUM_LIT:0> <EOL> for pathname in image_names : <EOL> c += <NUM_LIT:1> <EOL> im = pv . Image ( pathname ) <EOL> scale = options . log_scale <EOL> log_im = pv . AffineScale ( scale , ( int ( scale * im . width ) , int ( scale * im . height ) ) ) . transformImage ( im ) <EOL> results = processFaces ( im , face_detect , locate_eyes ) <EOL> if options . rotate : <EOL> rot_image = pv . Image ( im . asPIL ( ) . transpose ( PIL . Image . ROTATE_90 ) ) <EOL> more_results = processFaces ( rot_image , face_detect , locate_eyes ) <EOL> for face , eye1 , eye2 in more_results : <EOL> results . append ( [ pv . Rect ( im . width - face . y - face . h , face . x , face . h , face . w ) , <EOL> pv . Point ( im . width - eye1 . Y ( ) , eye1 . X ( ) ) , <EOL> pv . Point ( im . width - eye2 . Y ( ) , eye2 . X ( ) ) ] ) <EOL> rot_image = pv . Image ( im . asPIL ( ) . transpose ( PIL . Image . ROTATE_180 ) ) <EOL> more_results = processFaces ( rot_image , face_detect , locate_eyes ) <EOL> for face , eye1 , eye2 in more_results : <EOL> results . append ( [ pv . Rect ( im . width - face . x - face . w , im . height - face . y - face . h , face . w , face . h ) , <EOL> pv . Point ( im . width - eye1 . X ( ) , im . height - eye1 . Y ( ) ) , <EOL> pv . Point ( im . width - eye2 . X ( ) , im . height - eye2 . Y ( ) ) ] ) <EOL> rot_image = pv . Image ( im . asPIL ( ) . transpose ( PIL . Image . ROTATE_270 ) ) <EOL> more_results = processFaces ( rot_image , face_detect , locate_eyes ) <EOL> for face , eye1 , eye2 in more_results : <EOL> results . append ( [ pv . Rect ( face . y , im . height - face . x - face . w , face . h , face . w ) , <EOL> pv . Point ( eye1 . Y ( ) , im . height - eye1 . X ( ) ) , <EOL> pv . Point ( eye2 . Y ( ) , im . height - eye2 . X ( ) ) ] ) <EOL> n_faces = <NUM_LIT:0> <EOL> for face , eye1 , eye2 in results : <EOL> csv_file . writerow ( [ pathname , n_faces , face . x , face . y , face . w , face . h , eye1 . X ( ) , eye1 . Y ( ) , eye2 . X ( ) , eye2 . Y ( ) ] ) <EOL> if ilog != None : <EOL> log_im . annotateRect ( scale * face ) <EOL> log_im . annotatePoint ( scale * eye1 ) <EOL> log_im . annotatePoint ( scale * eye2 ) <EOL> n_faces += <NUM_LIT:1> <EOL> print "<STR_LIT>" % ( c , len ( image_names ) , n_faces , pathname ) <EOL> if ilog != None : <EOL> basename = os . path . basename ( pathname ) <EOL> basename = basename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> ilog . log ( log_im , label = basename ) <EOL> if ilog != None : <EOL> ilog . show ( ) </s>
