<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import cv2 <EOL> import video <EOL> class App ( object ) : <EOL> def __init__ ( self , video_src ) : <EOL> self . cam = video . create_capture ( video_src ) <EOL> ret , self . frame = self . cam . read ( ) <EOL> cv2 . namedWindow ( '<STR_LIT>' ) <EOL> cv2 . setMouseCallback ( '<STR_LIT>' , self . onmouse ) <EOL> self . selection = None <EOL> self . drag_start = None <EOL> self . tracking_state = <NUM_LIT:0> <EOL> self . show_backproj = False <EOL> def onmouse ( self , event , x , y , flags , param ) : <EOL> x , y = np . int16 ( [ x , y ] ) <EOL> if event == cv2 . EVENT_LBUTTONDOWN : <EOL> self . drag_start = ( x , y ) <EOL> self . tracking_state = <NUM_LIT:0> <EOL> if self . drag_start : <EOL> if flags & cv2 . EVENT_FLAG_LBUTTON : <EOL> h , w = self . frame . shape [ : <NUM_LIT:2> ] <EOL> xo , yo = self . drag_start <EOL> x0 , y0 = np . maximum ( <NUM_LIT:0> , np . minimum ( [ xo , yo ] , [ x , y ] ) ) <EOL> x1 , y1 = np . minimum ( [ w , h ] , np . maximum ( [ xo , yo ] , [ x , y ] ) ) <EOL> self . selection = None <EOL> if x1 - x0 > <NUM_LIT:0> and y1 - y0 > <NUM_LIT:0> : <EOL> self . selection = ( x0 , y0 , x1 , y1 ) <EOL> else : <EOL> self . drag_start = None <EOL> if self . selection is not None : <EOL> self . tracking_state = <NUM_LIT:1> <EOL> def show_hist ( self ) : <EOL> bin_count = self . hist . shape [ <NUM_LIT:0> ] <EOL> bin_w = <NUM_LIT> <EOL> img = np . zeros ( ( <NUM_LIT> , bin_count * bin_w , <NUM_LIT:3> ) , np . uint8 ) <EOL> for i in xrange ( bin_count ) : <EOL> h = int ( self . hist [ i ] ) <EOL> cv2 . rectangle ( img , ( i * bin_w + <NUM_LIT:2> , <NUM_LIT:255> ) , ( ( i + <NUM_LIT:1> ) * bin_w - <NUM_LIT:2> , <NUM_LIT:255> - h ) , ( int ( <NUM_LIT> * i / bin_count ) , <NUM_LIT:255> , <NUM_LIT:255> ) , - <NUM_LIT:1> ) <EOL> img = cv2 . cvtColor ( img , cv2 . COLOR_HSV2BGR ) <EOL> cv2 . imshow ( '<STR_LIT>' , img ) <EOL> def run ( self ) : <EOL> while True : <EOL> ret , self . frame = self . cam . read ( ) <EOL> vis = self . frame . copy ( ) <EOL> hsv = cv2 . cvtColor ( self . frame , cv2 . COLOR_BGR2HSV ) <EOL> mask = cv2 . inRange ( hsv , np . array ( ( <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT> ) ) , np . array ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) ) <EOL> if self . selection : <EOL> x0 , y0 , x1 , y1 = self . selection <EOL> self . track_window = ( x0 , y0 , x1 - x0 , y1 - y0 ) <EOL> hsv_roi = hsv [ y0 : y1 , x0 : x1 ] <EOL> mask_roi = mask [ y0 : y1 , x0 : x1 ] <EOL> hist = cv2 . calcHist ( [ hsv_roi ] , [ <NUM_LIT:0> ] , mask_roi , [ <NUM_LIT:16> ] , [ <NUM_LIT:0> , <NUM_LIT> ] ) <EOL> cv2 . normalize ( hist , hist , <NUM_LIT:0> , <NUM_LIT:255> , cv2 . NORM_MINMAX ) ; <EOL> self . hist = hist . reshape ( - <NUM_LIT:1> ) <EOL> self . show_hist ( ) <EOL> vis_roi = vis [ y0 : y1 , x0 : x1 ] <EOL> cv2 . bitwise_not ( vis_roi , vis_roi ) <EOL> vis [ mask == <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> if self . tracking_state == <NUM_LIT:1> : <EOL> self . selection = None <EOL> prob = cv2 . calcBackProject ( [ hsv ] , [ <NUM_LIT:0> ] , self . hist , [ <NUM_LIT:0> , <NUM_LIT> ] , <NUM_LIT:1> ) <EOL> prob &= mask <EOL> term_crit = ( cv2 . TERM_CRITERIA_EPS | cv2 . TERM_CRITERIA_COUNT , <NUM_LIT:10> , <NUM_LIT:1> ) <EOL> track_box , self . track_window = cv2 . CamShift ( prob , self . track_window , term_crit ) <EOL> if self . show_backproj : <EOL> vis [ : ] = prob [ ... , np . newaxis ] <EOL> try : cv2 . ellipse ( vis , track_box , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) , <NUM_LIT:2> ) <EOL> except : print track_box <EOL> cv2 . imshow ( '<STR_LIT>' , vis ) <EOL> ch = <NUM_LIT> & cv2 . waitKey ( <NUM_LIT:5> ) <EOL> if ch == <NUM_LIT> : <EOL> break <EOL> if ch == ord ( '<STR_LIT:b>' ) : <EOL> self . show_backproj = not self . show_backproj <EOL> cv2 . destroyAllWindows ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> try : video_src = sys . argv [ <NUM_LIT:1> ] <EOL> except : video_src = <NUM_LIT:0> <EOL> print __doc__ <EOL> App ( video_src ) . run ( ) </s>
