<s> import os <EOL> import cv2 <EOL> import re <EOL> import time <EOL> from threading import RLock <EOL> from codependent_thread import CodependentThread <EOL> from image_misc import cv2_imshow_rgb , cv2_read_file_rgb , read_cam_frame , crop_to_square <EOL> class InputImageFetcher ( CodependentThread ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , settings ) : <EOL> CodependentThread . __init__ ( self , settings . input_updater_heartbeat_required ) <EOL> self . daemon = True <EOL> self . lock = RLock ( ) <EOL> self . quit = False <EOL> self . latest_frame_idx = - <NUM_LIT:1> <EOL> self . latest_frame_data = None <EOL> self . latest_frame_is_from_cam = False <EOL> self . static_file_mode = True <EOL> self . settings = settings <EOL> self . static_file_stretch_mode = self . settings . static_file_stretch_mode <EOL> self . capture_device = settings . input_updater_capture_device <EOL> self . no_cam_present = ( self . capture_device is None ) <EOL> self . bound_cap_device = None <EOL> self . sleep_after_read_frame = settings . input_updater_sleep_after_read_frame <EOL> self . latest_cam_frame = None <EOL> self . freeze_cam = False <EOL> self . latest_static_filename = None <EOL> self . latest_static_frame = None <EOL> self . static_file_idx = None <EOL> self . static_file_idx_increment = <NUM_LIT:0> <EOL> def bind_camera ( self ) : <EOL> print '<STR_LIT>' <EOL> if self . no_cam_present : <EOL> print '<STR_LIT>' <EOL> else : <EOL> self . bound_cap_device = cv2 . VideoCapture ( self . capture_device ) <EOL> if self . bound_cap_device . isOpened ( ) : <EOL> print '<STR_LIT>' % self . capture_device <EOL> else : <EOL> print '<STR_LIT>' % self . capture_device <EOL> self . bound_cap_device = None <EOL> self . no_cam_present = True <EOL> print '<STR_LIT>' <EOL> def free_camera ( self ) : <EOL> if self . no_cam_present : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' <EOL> del self . bound_cap_device <EOL> self . bound_cap_device = None <EOL> print '<STR_LIT>' <EOL> def set_mode_static ( self ) : <EOL> with self . lock : <EOL> self . static_file_mode = True <EOL> def set_mode_cam ( self ) : <EOL> with self . lock : <EOL> if self . no_cam_present : <EOL> print '<STR_LIT>' <EOL> else : <EOL> self . static_file_mode = False <EOL> assert self . bound_cap_device != None , '<STR_LIT>' <EOL> def toggle_input_mode ( self ) : <EOL> with self . lock : <EOL> if self . static_file_mode : <EOL> self . set_mode_cam ( ) <EOL> else : <EOL> self . set_mode_static ( ) <EOL> def set_mode_stretch_on ( self ) : <EOL> with self . lock : <EOL> if not self . static_file_stretch_mode : <EOL> self . static_file_stretch_mode = True <EOL> self . latest_static_frame = None <EOL> def set_mode_stretch_off ( self ) : <EOL> with self . lock : <EOL> if self . static_file_stretch_mode : <EOL> self . static_file_stretch_mode = False <EOL> self . latest_static_frame = None <EOL> def toggle_stretch_mode ( self ) : <EOL> with self . lock : <EOL> if self . static_file_stretch_mode : <EOL> self . set_mode_stretch_off ( ) <EOL> else : <EOL> self . set_mode_stretch_on ( ) <EOL> def run ( self ) : <EOL> while not self . quit and not self . is_timed_out ( ) : <EOL> if self . static_file_mode : <EOL> self . check_increment_and_load_image ( ) <EOL> else : <EOL> if self . freeze_cam and self . latest_cam_frame is not None : <EOL> if not self . latest_frame_is_from_cam : <EOL> self . _increment_and_set_frame ( self . latest_cam_frame , True ) <EOL> else : <EOL> frame_full = read_cam_frame ( self . bound_cap_device ) <EOL> frame = crop_to_square ( frame_full ) <EOL> with self . lock : <EOL> self . latest_cam_frame = frame <EOL> self . _increment_and_set_frame ( self . latest_cam_frame , True ) <EOL> time . sleep ( self . sleep_after_read_frame ) <EOL> print '<STR_LIT>' <EOL> def get_frame ( self ) : <EOL> '''<STR_LIT>''' <EOL> with self . lock : <EOL> return ( self . latest_frame_idx , self . latest_frame_data ) <EOL> def increment_static_file_idx ( self , amount = <NUM_LIT:1> ) : <EOL> with self . lock : <EOL> self . static_file_idx_increment += amount <EOL> def _increment_and_set_frame ( self , frame , from_cam ) : <EOL> assert frame is not None <EOL> with self . lock : <EOL> self . latest_frame_idx += <NUM_LIT:1> <EOL> self . latest_frame_data = frame <EOL> self . latest_frame_is_from_cam = from_cam <EOL> def check_increment_and_load_image ( self ) : <EOL> with self . lock : <EOL> if ( self . static_file_idx_increment == <NUM_LIT:0> and <EOL> self . static_file_idx is not None and <EOL> not self . latest_frame_is_from_cam and <EOL> self . latest_static_frame is not None ) : <EOL> return <EOL> available_files = [ ] <EOL> match_flags = re . IGNORECASE if self . settings . static_files_ignore_case else <NUM_LIT:0> <EOL> for filename in os . listdir ( self . settings . static_files_dir ) : <EOL> if re . match ( self . settings . static_files_regexp , filename , match_flags ) : <EOL> available_files . append ( filename ) <EOL> assert len ( available_files ) != <NUM_LIT:0> , ( '<STR_LIT>' % <EOL> ( self . settings . static_files_dir , self . settings . static_files_regexp , os . getcwd ( ) ) ) <EOL> if self . static_file_idx is None : <EOL> self . static_file_idx = <NUM_LIT:0> <EOL> self . static_file_idx = ( self . static_file_idx + self . static_file_idx_increment ) % len ( available_files ) <EOL> self . static_file_idx_increment = <NUM_LIT:0> <EOL> if self . latest_static_filename != available_files [ self . static_file_idx ] or self . latest_static_frame is None : <EOL> self . latest_static_filename = available_files [ self . static_file_idx ] <EOL> im = cv2_read_file_rgb ( os . path . join ( self . settings . static_files_dir , self . latest_static_filename ) ) <EOL> if not self . static_file_stretch_mode : <EOL> im = crop_to_square ( im ) <EOL> self . latest_static_frame = im <EOL> self . _increment_and_set_frame ( self . latest_static_frame , False ) </s>
