<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import cv2 <EOL> from multiprocessing . pool import ThreadPool <EOL> from collections import deque <EOL> from common import clock , draw_str , StatValue <EOL> import video <EOL> class DummyTask : <EOL> def __init__ ( self , data ) : <EOL> self . data = data <EOL> def ready ( self ) : <EOL> return True <EOL> def get ( self ) : <EOL> return self . data <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> print __doc__ <EOL> try : fn = sys . argv [ <NUM_LIT:1> ] <EOL> except : fn = <NUM_LIT:0> <EOL> cap = video . create_capture ( fn ) <EOL> def process_frame ( frame , t0 ) : <EOL> frame = cv2 . medianBlur ( frame , <NUM_LIT> ) <EOL> frame = cv2 . medianBlur ( frame , <NUM_LIT> ) <EOL> return frame , t0 <EOL> threadn = cv2 . getNumberOfCPUs ( ) <EOL> pool = ThreadPool ( processes = threadn ) <EOL> pending = deque ( ) <EOL> threaded_mode = True <EOL> latency = StatValue ( ) <EOL> frame_interval = StatValue ( ) <EOL> last_frame_time = clock ( ) <EOL> while True : <EOL> while len ( pending ) > <NUM_LIT:0> and pending [ <NUM_LIT:0> ] . ready ( ) : <EOL> res , t0 = pending . popleft ( ) . get ( ) <EOL> latency . update ( clock ( ) - t0 ) <EOL> draw_str ( res , ( <NUM_LIT:20> , <NUM_LIT:20> ) , "<STR_LIT>" + str ( threaded_mode ) ) <EOL> draw_str ( res , ( <NUM_LIT:20> , <NUM_LIT> ) , "<STR_LIT>" % ( latency . value * <NUM_LIT:1000> ) ) <EOL> draw_str ( res , ( <NUM_LIT:20> , <NUM_LIT> ) , "<STR_LIT>" % ( frame_interval . value * <NUM_LIT:1000> ) ) <EOL> cv2 . imshow ( '<STR_LIT>' , res ) <EOL> if len ( pending ) < threadn : <EOL> ret , frame = cap . read ( ) <EOL> t = clock ( ) <EOL> frame_interval . update ( t - last_frame_time ) <EOL> last_frame_time = t <EOL> if threaded_mode : <EOL> task = pool . apply_async ( process_frame , ( frame . copy ( ) , t ) ) <EOL> else : <EOL> task = DummyTask ( process_frame ( frame , t ) ) <EOL> pending . append ( task ) <EOL> ch = <NUM_LIT> & cv2 . waitKey ( <NUM_LIT:1> ) <EOL> if ch == ord ( '<STR_LIT:U+0020>' ) : <EOL> threaded_mode = not threaded_mode <EOL> if ch == <NUM_LIT> : <EOL> break <EOL> cv2 . destroyAllWindows ( ) </s>
