<s> """<STR_LIT>""" <EOL> import cv2 <EOL> from time import clock <EOL> class OpenCV_Cam ( object ) : <EOL> def __init__ ( self , src = None ) : <EOL> self . start_cam ( src ) <EOL> self . __fcount , self . __frate , self . __start = <NUM_LIT:0> , <NUM_LIT:0> , clock ( ) <EOL> @ staticmethod <EOL> def cam_count ( ) : <EOL> cam_idx = <NUM_LIT:0> <EOL> cap = cv2 . VideoCapture ( cam_idx ) <EOL> while cap . read ( ) [ <NUM_LIT:0> ] : <EOL> cam_idx += <NUM_LIT:1> <EOL> cap = cv2 . VideoCapture ( cam_idx ) <EOL> return cam_idx <EOL> def start_cam ( self , src = None ) : <EOL> if src is not None : <EOL> self . cam = VideoCapture ( src ) <EOL> if not self . cam . isOpened ( ) : <EOL> raise ValueError ( '<STR_LIT>' + src + '<STR_LIT>' ) <EOL> return <EOL> idx = <NUM_LIT:1> <EOL> cam1 , cam2 = cv2 . VideoCapture ( <NUM_LIT:0> ) , cv2 . VideoCapture ( <NUM_LIT:1> ) <EOL> while ( cam2 . read ( ) [ <NUM_LIT:0> ] ) : <EOL> cam1 . release ( ) <EOL> cam1 = cam2 <EOL> idx += <NUM_LIT:1> <EOL> cam2 = cv2 . VideoCapture ( idx ) <EOL> self . cam = cam1 <EOL> if not self . cam . isOpened ( ) : <EOL> raise Error ( '<STR_LIT>' ) <EOL> @ property <EOL> def size ( self ) : <EOL> w = self . cam . get ( cv2 . cv . CV_CAP_PROP_FRAME_WIDTH ) <EOL> h = self . cam . get ( cv2 . cv . CV_CAP_PROP_FRAME_HEIGHT ) <EOL> return ( int ( w ) , int ( h ) ) <EOL> @ size . setter <EOL> def size ( self , shape ) : <EOL> self . cam . set ( cv2 . cv . CV_CAP_PROP_FRAME_WIDTH , shape [ <NUM_LIT:0> ] ) <EOL> self . cam . set ( cv2 . cv . CV_CAP_PROP_FRAME_HEIGHT , shape [ <NUM_LIT:1> ] ) <EOL> def read ( self ) : <EOL> self . __fcount += <NUM_LIT:1> <EOL> self . __frame = self . cam . read ( ) [ <NUM_LIT:1> ] <EOL> if self . __fcount == <NUM_LIT:10> : <EOL> end = clock ( ) <EOL> self . __frate = <NUM_LIT:10> / ( end - self . __start ) <EOL> self . __start = clock ( ) <EOL> self . __fcount = <NUM_LIT:0> <EOL> return self . __frame <EOL> @ property <EOL> def frame_rate ( self ) : <EOL> return self . __frate <EOL> @ property <EOL> def info ( self ) : <EOL> vars = [ x [ <NUM_LIT:12> : ] for x in dir ( cv2 . cv ) if '<STR_LIT>' in x ] <EOL> ret = { } <EOL> for p in vars : <EOL> cmd = '<STR_LIT>' + p + '<STR_LIT>' + p + '<STR_LIT:)>' <EOL> exec cmd <EOL> return ret <EOL> def set ( self , property , value ) : <EOL> cmd = '<STR_LIT>' + property + '<STR_LIT:U+002CU+0020>' + str ( value ) + '<STR_LIT:)>' <EOL> print cmd <EOL> exec cmd <EOL> def cam_loop ( self , func = lambda x : x , params = ( ) ) : <EOL> while True : <EOL> input = self . read ( ) <EOL> output = func ( input , * params ) <EOL> window_name = func . __name__ <EOL> if window_name == '<STR_LIT>' : window_name = '<STR_LIT>' <EOL> cv2 . imshow ( window_name , output ) <EOL> k = cv2 . waitKey ( <NUM_LIT:5> ) <EOL> if k == <NUM_LIT> : <EOL> break <EOL> elif k == ord ( '<STR_LIT:p>' ) : <EOL> info = self . info <EOL> for i in info : <EOL> print i , '<STR_LIT:=>' , info [ i ] <EOL> elif k == ord ( '<STR_LIT:s>' ) : <EOL> p = raw_input ( '<STR_LIT>' ) <EOL> if ( '<STR_LIT>' + p ) not in dir ( cv2 . cv ) : <EOL> print p , '<STR_LIT>' <EOL> print '<STR_LIT>' , [ x [ <NUM_LIT:12> : ] for x in dir ( cv2 . cv ) if '<STR_LIT>' in x ] <EOL> else : <EOL> v = raw_input ( '<STR_LIT>' ) <EOL> self . set ( p , v ) <EOL> elif k == ord ( '<STR_LIT:f>' ) : <EOL> print self . frame_rate <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . cam . release ( ) <EOL> def release ( self ) : <EOL> self . cam . release ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print __doc__ <EOL> cam = OpenCV_Cam ( ) <EOL> cam . size = ( <NUM_LIT> , <NUM_LIT> ) <EOL> info = cam . info <EOL> for i in info : <EOL> print i , '<STR_LIT:=>' , info [ i ] <EOL> cam . cam_loop ( ) </s>
