<s> from __future__ import division <EOL> import argparse <EOL> import os <EOL> import time <EOL> import threading <EOL> import Queue <EOL> import cv2 <EOL> import numpy as np <EOL> from tc_common import * <EOL> job_name = '<STR_LIT>' <EOL> start_frame = <NUM_LIT:0> <EOL> end_frame = <NUM_LIT:0> <EOL> frames_count = <NUM_LIT:0> <EOL> current_frame = <NUM_LIT:0> <EOL> capture_direction = <NUM_LIT:1> <EOL> capture_ext = '<STR_LIT>' <EOL> fileSaveParams = [ ] <EOL> def parse_commandline ( ) : <EOL> global job_name , start_frame , end_frame , frames_count <EOL> global current_frame , capture_direction , capture_ext , reverse , brackets <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> job_name = sanitise_job_name ( args . jobname ) <EOL> start_frame = args . start if args . start else <NUM_LIT:0> <EOL> end_frame = args . end if args . end else <NUM_LIT:0> <EOL> frames_count = abs ( end_frame - start_frame ) + <NUM_LIT:1> <EOL> if frames_count == <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> quit ( ) <EOL> capture_direction = <NUM_LIT:1> if end_frame > start_frame else - <NUM_LIT:1> <EOL> current_frame = start_frame <EOL> if args . jpeg : <EOL> print ( '<STR_LIT>' ) <EOL> capture_ext = '<STR_LIT>' <EOL> fileSaveParams = [ int ( cv2 . IMWRITE_JPEG_QUALITY ) , <NUM_LIT> ] <EOL> brackets = args . brackets <EOL> if args . brackets : <EOL> print ( '<STR_LIT>' ) <EOL> reverse = args . reverse <EOL> if args . reverse : <EOL> print ( '<STR_LIT>' ) <EOL> def make_crop ( ) : <EOL> cx , cy = pf . centre <EOL> crop_x = cx + cnf . crop_offset [ <NUM_LIT:0> ] <EOL> crop_y = cy + cnf . crop_offset [ <NUM_LIT:1> ] <EOL> return pf . cropToSlice ( ( crop_x , crop_y , cnf . crop_size [ <NUM_LIT:0> ] , cnf . crop_size [ <NUM_LIT:1> ] ) ) <EOL> q = Queue . Queue ( <NUM_LIT:10> ) <EOL> job_finished = False <EOL> still_writing = True <EOL> def writer ( ) : <EOL> global q , job_finished , still_writing <EOL> write_time = Stopwatch ( ) <EOL> while not job_finished : <EOL> still_writing = True <EOL> while not q . empty ( ) : <EOL> write_time . start ( ) <EOL> fn , img = q . get ( ) <EOL> try : <EOL> cv2 . imwrite ( fn , img , fileSaveParams ) <EOL> t = write_time . stop ( ) <EOL> print ( '<STR_LIT>' . format ( fn , t ) ) <EOL> except : <EOL> t = write_time . stop ( ) <EOL> print ( '<STR_LIT>' . format ( fn , t ) ) <EOL> still_writing = False <EOL> failed_frames = <NUM_LIT:0> <EOL> taking_time = Stopwatch ( ) <EOL> taking_times = [ ] <EOL> def single_picture ( current_frame ) : <EOL> global cnf , capture_ext , fpath , failed_frames <EOL> global taking_time , taking_times <EOL> fname = '<STR_LIT>' . format ( current_frame , capture_ext ) <EOL> taking_time . start ( ) <EOL> img = cam . take_picture ( ) <EOL> t = taking_time . stop ( ) <EOL> taking_times . append ( t ) <EOL> print ( '<STR_LIT>' . format ( current_frame , t ) ) <EOL> if cnf . show_gray : <EOL> img = cv2 . cvtColor ( img , cv2 . COLOR_BGR2GRAY ) <EOL> pf . find ( img ) <EOL> found = pf . found <EOL> if not found : <EOL> print ( '<STR_LIT>' . format ( fname ) ) <EOL> failed_frames += <NUM_LIT:1> <EOL> failedname = '<STR_LIT>' + fname <EOL> failedname = os . path . join ( fpath , failedname ) <EOL> q . put ( ( failedname , img ) ) <EOL> if pf . position != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> found = True <EOL> else : <EOL> failed_frames = <NUM_LIT:0> <EOL> if found : <EOL> img = img [ make_crop ( ) ] <EOL> fname = os . path . join ( fpath , fname ) <EOL> q . put ( ( fname , img ) ) <EOL> def bracket_pictures ( current_frame ) : <EOL> global cnf , capture_ext , fpath , failed_frames <EOL> global taking_time , taking_times <EOL> fnames = [ '<STR_LIT>' . format ( current_frame , capture_ext ) , '<STR_LIT>' . format ( current_frame , capture_ext ) ] <EOL> taking_time . start ( ) <EOL> imgs = cam . take_bracket_pictures ( ) <EOL> t = taking_time . stop ( ) <EOL> taking_times . append ( t ) <EOL> print ( '<STR_LIT>' . format ( current_frame , t ) ) <EOL> if cnf . show_gray : <EOL> imgs [ <NUM_LIT:0> ] = cv2 . cvtColor ( imgs [ <NUM_LIT:0> ] , cv2 . COLOR_BGR2GRAY ) <EOL> imgs [ <NUM_LIT:1> ] = cv2 . cvtColor ( imgs [ <NUM_LIT:1> ] , cv2 . COLOR_BGR2GRAY ) <EOL> pf . find ( imgs [ <NUM_LIT:0> ] ) <EOL> found = pf . found <EOL> if not found : <EOL> print ( '<STR_LIT>' . format ( fnames [ <NUM_LIT:0> ] ) ) <EOL> failed_frames += <NUM_LIT:1> <EOL> failednames = [ os . path . join ( fpath , ( '<STR_LIT>' + fnames [ <NUM_LIT:0> ] ) ) , os . path . join ( fpath , ( '<STR_LIT>' + fnames [ <NUM_LIT:1> ] ) ) ] <EOL> q . put ( ( failednames [ <NUM_LIT:0> ] , imgs [ <NUM_LIT:0> ] ) ) <EOL> q . put ( ( failednames [ <NUM_LIT:1> ] , imgs [ <NUM_LIT:1> ] ) ) <EOL> if pf . position != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> found = True <EOL> else : <EOL> failed_frames = <NUM_LIT:0> <EOL> if found : <EOL> imgs [ <NUM_LIT:0> ] = imgs [ <NUM_LIT:0> ] [ make_crop ( ) ] <EOL> imgs [ <NUM_LIT:1> ] = imgs [ <NUM_LIT:1> ] [ make_crop ( ) ] <EOL> fnames = [ os . path . join ( fpath , fnames [ <NUM_LIT:0> ] ) , os . path . join ( fpath , fnames [ <NUM_LIT:1> ] ) ] <EOL> q . put ( ( fnames [ <NUM_LIT:0> ] , imgs [ <NUM_LIT:0> ] ) ) <EOL> q . put ( ( fnames [ <NUM_LIT:1> ] , imgs [ <NUM_LIT:1> ] ) ) <EOL> def run_job ( ) : <EOL> global q , job_finished <EOL> global cnf , job_name , start_frame , end_frame <EOL> global capture_direction , capture_ext , fpath <EOL> global brackets , reverse <EOL> global pf , tc , cam <EOL> global failed_frames <EOL> max_fails = <NUM_LIT:5> <EOL> job_time = Stopwatch ( ) <EOL> job_time . start ( ) <EOL> job_finished = False <EOL> t = threading . Thread ( target = writer ) <EOL> t . start ( ) <EOL> print ( '<STR_LIT>' . format ( pf . filmType ) ) <EOL> try : <EOL> tc . light_on ( ) <EOL> cam . setup_cam ( cnf . awb_gains , cnf . shutter_speed , cnf . drc , cnf . image_effect ) <EOL> centre_frame ( ) <EOL> frame_time = Stopwatch ( ) <EOL> frame_times = [ ] <EOL> end_frame = end_frame + capture_direction <EOL> for current_frame in range ( start_frame , end_frame , capture_direction ) : <EOL> frame_time . start ( ) <EOL> taking_time . start ( ) <EOL> if not brackets : <EOL> single_picture ( current_frame ) <EOL> else : <EOL> bracket_pictures ( current_frame ) <EOL> if failed_frames >= max_fails : <EOL> print ( '<STR_LIT>' ) <EOL> break <EOL> if not reverse : <EOL> next_frame ( ) <EOL> else : <EOL> prev_frame ( ) <EOL> t = frame_time . stop ( ) <EOL> print ( '<STR_LIT>' . format ( current_frame , t ) ) <EOL> frame_times . append ( t ) <EOL> finally : <EOL> tc . light_off ( ) <EOL> cam . close ( ) <EOL> job_finished = True <EOL> while still_writing : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> jt = job_time . stop ( ) <EOL> minutes = jt // <NUM_LIT> <EOL> seconds = jt % <NUM_LIT> <EOL> job_finished = True <EOL> ave_per_frame = sum ( frame_times ) / len ( frame_times ) <EOL> ave_camera_time = sum ( taking_times ) / len ( taking_times ) <EOL> print ( '<STR_LIT>' % ( len ( frame_times ) ) ) <EOL> print ( '<STR_LIT>' . format ( minutes , seconds ) ) <EOL> print ( '<STR_LIT>' . format ( ave_per_frame ) ) <EOL> print ( '<STR_LIT>' . format ( min ( frame_times ) ) ) <EOL> print ( '<STR_LIT>' . format ( max ( frame_times ) ) ) <EOL> print ( '<STR_LIT>' . format ( ave_camera_time ) ) <EOL> print ( '<STR_LIT>' . format ( min ( taking_times ) ) ) <EOL> print ( '<STR_LIT>' . format ( max ( taking_times ) ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parse_commandline ( ) <EOL> cnf . read_configfile ( job_name ) <EOL> brackets = brackets or cnf . brackets <EOL> pf . init ( filmType = cnf . film_type , imageSize = cam . MAX_IMAGE_RESOLUTION , <EOL> expectedSize = cnf . perf_size , cx = cnf . perf_cx ) <EOL> try : <EOL> pf . setROI ( ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> quit ( ) <EOL> print ( '<STR_LIT>' % ( job_name , start_frame , end_frame , frames_count ) ) <EOL> print ( '<STR_LIT>' % ( cnf . shutter_speed , cnf . awb_gains [ <NUM_LIT:0> ] , cnf . awb_gains [ <NUM_LIT:1> ] ) ) <EOL> print ( '<STR_LIT>' . format ( pf . centre [ <NUM_LIT:0> ] , pf . expectedSize ) ) <EOL> print ( '<STR_LIT>' % ( cnf . crop_offset [ <NUM_LIT:0> ] , cnf . crop_offset [ <NUM_LIT:1> ] , cnf . crop_size [ <NUM_LIT:0> ] , cnf . crop_size [ <NUM_LIT:1> ] ) ) <EOL> if capture_direction > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> fpath = os . path . join ( '<STR_LIT:.>' , job_name ) <EOL> if not os . path . exists ( fpath ) : <EOL> try : <EOL> os . mkdir ( fpath ) <EOL> except : <EOL> print ( '<STR_LIT>' % fpath ) <EOL> quit ( ) <EOL> if not os . path . isdir ( fpath ) : <EOL> print ( '<STR_LIT>' % fpath ) <EOL> quit ( ) <EOL> run_job ( ) </s>
