<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import cv2 <EOL> import video <EOL> from common import anorm2 , draw_str <EOL> from time import clock <EOL> lk_params = dict ( winSize = ( <NUM_LIT:15> , <NUM_LIT:15> ) , <EOL> maxLevel = <NUM_LIT:2> , <EOL> criteria = ( cv2 . TERM_CRITERIA_EPS | cv2 . TERM_CRITERIA_COUNT , <NUM_LIT:10> , <NUM_LIT> ) ) <EOL> feature_params = dict ( maxCorners = <NUM_LIT> , <EOL> qualityLevel = <NUM_LIT> , <EOL> minDistance = <NUM_LIT:7> , <EOL> blockSize = <NUM_LIT:7> ) <EOL> class App : <EOL> def __init__ ( self , video_src ) : <EOL> self . track_len = <NUM_LIT:10> <EOL> self . detect_interval = <NUM_LIT:5> <EOL> self . tracks = [ ] <EOL> self . cam = video . create_capture ( video_src ) <EOL> self . frame_idx = <NUM_LIT:0> <EOL> def run ( self ) : <EOL> while True : <EOL> ret , frame = self . cam . read ( ) <EOL> frame_gray = cv2 . cvtColor ( frame , cv2 . COLOR_BGR2GRAY ) <EOL> vis = frame . copy ( ) <EOL> if len ( self . tracks ) > <NUM_LIT:0> : <EOL> img0 , img1 = self . prev_gray , frame_gray <EOL> p0 = np . float32 ( [ tr [ - <NUM_LIT:1> ] for tr in self . tracks ] ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> p1 , st , err = cv2 . calcOpticalFlowPyrLK ( img0 , img1 , p0 , None , ** lk_params ) <EOL> p0r , st , err = cv2 . calcOpticalFlowPyrLK ( img1 , img0 , p1 , None , ** lk_params ) <EOL> d = abs ( p0 - p0r ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) . max ( - <NUM_LIT:1> ) <EOL> good = d < <NUM_LIT:1> <EOL> new_tracks = [ ] <EOL> for tr , ( x , y ) , good_flag in zip ( self . tracks , p1 . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) , good ) : <EOL> if not good_flag : <EOL> continue <EOL> tr . append ( ( x , y ) ) <EOL> if len ( tr ) > self . track_len : <EOL> del tr [ <NUM_LIT:0> ] <EOL> new_tracks . append ( tr ) <EOL> cv2 . circle ( vis , ( x , y ) , <NUM_LIT:2> , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , - <NUM_LIT:1> ) <EOL> self . tracks = new_tracks <EOL> cv2 . polylines ( vis , [ np . int32 ( tr ) for tr in self . tracks ] , False , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) ) <EOL> draw_str ( vis , ( <NUM_LIT:20> , <NUM_LIT:20> ) , '<STR_LIT>' % len ( self . tracks ) ) <EOL> if self . frame_idx % self . detect_interval == <NUM_LIT:0> : <EOL> mask = np . zeros_like ( frame_gray ) <EOL> mask [ : ] = <NUM_LIT:255> <EOL> for x , y in [ np . int32 ( tr [ - <NUM_LIT:1> ] ) for tr in self . tracks ] : <EOL> cv2 . circle ( mask , ( x , y ) , <NUM_LIT:5> , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> p = cv2 . goodFeaturesToTrack ( frame_gray , mask = mask , ** feature_params ) <EOL> if p is not None : <EOL> for x , y in np . float32 ( p ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> self . tracks . append ( [ ( x , y ) ] ) <EOL> self . frame_idx += <NUM_LIT:1> <EOL> self . prev_gray = frame_gray <EOL> cv2 . imshow ( '<STR_LIT>' , vis ) <EOL> ch = <NUM_LIT> & cv2 . waitKey ( <NUM_LIT:1> ) <EOL> if ch == <NUM_LIT> : <EOL> break <EOL> def main ( ) : <EOL> import sys <EOL> try : <EOL> video_src = sys . argv [ <NUM_LIT:1> ] <EOL> except : <EOL> video_src = <NUM_LIT:0> <EOL> print __doc__ <EOL> App ( video_src ) . run ( ) <EOL> cv2 . destroyAllWindows ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
