<s> import numpy <EOL> import cv2 <EOL> import argparse <EOL> import time <EOL> import random <EOL> import math <EOL> import imutils <EOL> from collections import Counter <EOL> TRACKER_POINTS = <NUM_LIT> <EOL> CRAZY_LINE_DISTANCE = <NUM_LIT:50> <EOL> CRAZY_LINE_LIMIT = <NUM_LIT:100> * TRACKER_POINTS / <NUM_LIT:1000> <EOL> ABSDIFF_ANGLE = <NUM_LIT:20> <EOL> LINE_THICKNESS = <NUM_LIT:3> <EOL> CONTOUR_LIMIT = <NUM_LIT:10> <EOL> TARGET_HEIGHT = <NUM_LIT> <EOL> DELTA_LIMIT_DIVISOR = <NUM_LIT:3> <EOL> ap = argparse . ArgumentParser ( ) <EOL> ap . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> ap . add_argument ( "<STR_LIT>" , "<STR_LIT>" , type = int , default = <NUM_LIT> , help = "<STR_LIT>" ) <EOL> args = vars ( ap . parse_args ( ) ) <EOL> if args . get ( "<STR_LIT>" , None ) is None : <EOL> cap = cv2 . VideoCapture ( <NUM_LIT:0> ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> else : <EOL> cap = cv2 . VideoCapture ( args [ "<STR_LIT>" ] ) <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> while True : <EOL> color = numpy . random . randint ( <NUM_LIT:0> , <NUM_LIT:255> , ( TRACKER_POINTS , <NUM_LIT:3> ) ) <EOL> ret , old_frame = cap . read ( ) <EOL> old_frame = imutils . resize ( old_frame , height = TARGET_HEIGHT ) <EOL> old_gray = cv2 . cvtColor ( old_frame , cv2 . COLOR_BGR2GRAY ) <EOL> height , width = old_frame . shape [ : <NUM_LIT:2> ] <EOL> p1 = numpy . random . randint ( width , size = ( TRACKER_POINTS , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> for y in p1 : <EOL> if y [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] > height : <EOL> y [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] = numpy . random . random_integers ( height ) <EOL> p1 = p1 . astype ( numpy . float32 ) <EOL> mask = numpy . zeros_like ( old_frame ) <EOL> mask_delta = numpy . zeros_like ( old_frame ) <EOL> mask_white = numpy . ones_like ( old_frame ) <EOL> white_color = numpy . array ( [ <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ] ) <EOL> total_crazy_lines = <NUM_LIT:0> <EOL> most_common_angle = None <EOL> while True : <EOL> mask_biggest_contour = numpy . zeros_like ( old_frame ) <EOL> ret , frame = cap . read ( ) <EOL> frame = imutils . resize ( frame , height = TARGET_HEIGHT ) <EOL> frame_gray = cv2 . cvtColor ( frame , cv2 . COLOR_BGR2GRAY ) <EOL> delta_value = <NUM_LIT:0> <EOL> p2 , st , err = cv2 . calcOpticalFlowPyrLK ( old_gray , frame_gray , p1 , None , ** lk_params ) <EOL> if p2 is None : <EOL> break <EOL> good_points2 = p2 [ st == <NUM_LIT:1> ] <EOL> good_points1 = p1 [ st == <NUM_LIT:1> ] <EOL> angles_array = [ ] <EOL> for i , ( good_point2 , good_point1 ) in enumerate ( zip ( good_points2 , good_points1 ) ) : <EOL> x2 , y2 = good_point2 . ravel ( ) <EOL> x1 , y1 = good_point1 . ravel ( ) <EOL> distance = math . hypot ( x2 - x1 , y2 - y1 ) <EOL> if distance >= CRAZY_LINE_DISTANCE : <EOL> total_crazy_lines += <NUM_LIT:1> <EOL> angle = math . atan2 ( y2 - y1 , x2 - x1 ) <EOL> angle = math . degrees ( angle ) <EOL> angle = round ( angle ) <EOL> angles_array . append ( angle ) <EOL> if most_common_angle != None : <EOL> if abs ( most_common_angle - angle ) > ABSDIFF_ANGLE : <EOL> cv2 . line ( mask_delta , ( x2 , y2 ) , ( x1 , y1 ) , white_color , LINE_THICKNESS ) <EOL> cv2 . line ( mask , ( x2 , y2 ) , ( x1 , y1 ) , color [ i ] . tolist ( ) , <NUM_LIT:1> ) <EOL> if angles_array : <EOL> most_common_angle = Counter ( angles_array ) . most_common ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> mask_delta_gray = cv2 . cvtColor ( mask_delta , cv2 . COLOR_BGR2GRAY ) <EOL> thresh = cv2 . threshold ( mask_delta_gray , <NUM_LIT:12> , <NUM_LIT:255> , cv2 . THRESH_BINARY ) [ <NUM_LIT:1> ] <EOL> thresh = cv2 . dilate ( thresh , None , iterations = <NUM_LIT:1> ) <EOL> ( cnts , _ ) = cv2 . findContours ( thresh . copy ( ) , cv2 . RETR_EXTERNAL , cv2 . CHAIN_APPROX_SIMPLE ) <EOL> contour_area_stack = [ ] <EOL> contour_dictionary = { } <EOL> biggest_contour_coordinates = None <EOL> frame_final_form = cv2 . add ( frame , mask_white ) <EOL> if cnts : <EOL> for c in cnts : <EOL> contour_area_stack . append ( cv2 . contourArea ( c ) ) <EOL> if cv2 . contourArea ( c ) > args [ "<STR_LIT>" ] : <EOL> ( x , y , w , h ) = cv2 . boundingRect ( c ) <EOL> cv2 . rectangle ( frame , ( x , y ) , ( x + w , y + h ) , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <NUM_LIT:2> ) <EOL> contour_dictionary [ cv2 . contourArea ( c ) ] = ( x , y , w , h ) <EOL> delta_value = max ( contour_area_stack ) <EOL> if contour_dictionary : <EOL> biggest_contour_coordinates = contour_dictionary [ delta_value ] <EOL> if biggest_contour_coordinates : <EOL> x = biggest_contour_coordinates [ <NUM_LIT:0> ] <EOL> y = biggest_contour_coordinates [ <NUM_LIT:1> ] <EOL> w = biggest_contour_coordinates [ <NUM_LIT:2> ] <EOL> h = biggest_contour_coordinates [ <NUM_LIT:3> ] <EOL> cv2 . rectangle ( mask_biggest_contour , ( x , y ) , ( x + w , y + h ) , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , - <NUM_LIT:1> ) <EOL> if delta_value > ( height * width / DELTA_LIMIT_DIVISOR ) : <EOL> break <EOL> if len ( contour_dictionary ) > CONTOUR_LIMIT : <EOL> total_crazy_lines = <NUM_LIT:0> <EOL> most_common_angle1 = None <EOL> angles_array1 = [ ] <EOL> most_common_angle2 = None <EOL> angles_array2 = [ ] <EOL> most_common_angle3 = None <EOL> angles_array3 = [ ] <EOL> most_common_angle4 = None <EOL> angles_array4 = [ ] <EOL> mask_delta = numpy . zeros_like ( old_frame ) <EOL> for i , ( good_point2 , good_point1 ) in enumerate ( zip ( good_points2 , good_points1 ) ) : <EOL> x2 , y2 = good_point2 . ravel ( ) <EOL> x1 , y1 = good_point1 . ravel ( ) <EOL> distance = math . hypot ( x2 - x1 , y2 - y1 ) <EOL> if distance >= CRAZY_LINE_DISTANCE : <EOL> total_crazy_lines += <NUM_LIT:1> <EOL> angle = math . atan2 ( y2 - y1 , x2 - x1 ) <EOL> angle = math . degrees ( angle ) <EOL> angle = round ( angle ) <EOL> if ( x1 <= ( width / <NUM_LIT:2> ) ) and ( y1 <= ( height / <NUM_LIT:2> ) ) : <EOL> angles_array1 . append ( angle ) <EOL> if most_common_angle1 != None : <EOL> if abs ( most_common_angle1 - angle ) > ABSDIFF_ANGLE : <EOL> cv2 . line ( mask_delta , ( x2 , y2 ) , ( x1 , y1 ) , white_color , LINE_THICKNESS ) <EOL> if ( x1 >= ( width / <NUM_LIT:2> ) ) and ( y1 <= ( height / <NUM_LIT:2> ) ) : <EOL> angles_array2 . append ( angle ) <EOL> if most_common_angle2 != None : <EOL> if abs ( most_common_angle2 - angle ) > ABSDIFF_ANGLE : <EOL> cv2 . line ( mask_delta , ( x2 , y2 ) , ( x1 , y1 ) , white_color , LINE_THICKNESS ) <EOL> if ( x1 <= ( width / <NUM_LIT:2> ) ) and ( y1 >= ( height / <NUM_LIT:2> ) ) : <EOL> angles_array3 . append ( angle ) <EOL> if most_common_angle3 != None : <EOL> if abs ( most_common_angle3 - angle ) > ABSDIFF_ANGLE : <EOL> cv2 . line ( mask_delta , ( x2 , y2 ) , ( x1 , y1 ) , white_color , LINE_THICKNESS ) <EOL> if ( x1 >= ( width / <NUM_LIT:2> ) ) and ( y1 >= ( height / <NUM_LIT:2> ) ) : <EOL> angles_array4 . append ( angle ) <EOL> if most_common_angle3 != None : <EOL> if abs ( most_common_angle3 - angle ) > ABSDIFF_ANGLE : <EOL> cv2 . line ( mask_delta , ( x2 , y2 ) , ( x1 , y1 ) , white_color , LINE_THICKNESS ) <EOL> cv2 . line ( mask , ( x2 , y2 ) , ( x1 , y1 ) , color [ i ] . tolist ( ) , <NUM_LIT:1> ) <EOL> if angles_array1 : <EOL> most_common_angle1 = Counter ( angles_array1 ) . most_common ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if angles_array2 : <EOL> most_common_angle2 = Counter ( angles_array2 ) . most_common ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if angles_array3 : <EOL> most_common_angle3 = Counter ( angles_array3 ) . most_common ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if angles_array4 : <EOL> most_common_angle4 = Counter ( angles_array4 ) . most_common ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> mask_delta_gray = cv2 . cvtColor ( mask_delta , cv2 . COLOR_BGR2GRAY ) <EOL> thresh = cv2 . threshold ( mask_delta_gray , <NUM_LIT:12> , <NUM_LIT:255> , cv2 . THRESH_BINARY ) [ <NUM_LIT:1> ] <EOL> thresh = cv2 . dilate ( thresh , None , iterations = <NUM_LIT:1> ) <EOL> ( cnts , _ ) = cv2 . findContours ( thresh . copy ( ) , cv2 . RETR_EXTERNAL , cv2 . CHAIN_APPROX_SIMPLE ) <EOL> contour_area_stack = [ ] <EOL> contour_dictionary = { } <EOL> biggest_contour_coordinates = None <EOL> frame_final_form = cv2 . add ( frame , mask_white ) <EOL> if cnts : <EOL> for c in cnts : <EOL> contour_area_stack . append ( cv2 . contourArea ( c ) ) <EOL> if cv2 . contourArea ( c ) > args [ "<STR_LIT>" ] : <EOL> ( x , y , w , h ) = cv2 . boundingRect ( c ) <EOL> cv2 . rectangle ( frame , ( x , y ) , ( x + w , y + h ) , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <NUM_LIT:2> ) <EOL> contour_dictionary [ cv2 . contourArea ( c ) ] = ( x , y , w , h ) <EOL> delta_value = max ( contour_area_stack ) <EOL> if contour_dictionary : <EOL> biggest_contour_coordinates = contour_dictionary [ delta_value ] <EOL> if biggest_contour_coordinates : <EOL> x = biggest_contour_coordinates [ <NUM_LIT:0> ] <EOL> y = biggest_contour_coordinates [ <NUM_LIT:1> ] <EOL> w = biggest_contour_coordinates [ <NUM_LIT:2> ] <EOL> h = biggest_contour_coordinates [ <NUM_LIT:3> ] <EOL> cv2 . rectangle ( mask_biggest_contour , ( x , y ) , ( x + w , y + h ) , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , - <NUM_LIT:1> ) <EOL> if delta_value > ( height * width / DELTA_LIMIT_DIVISOR ) : <EOL> break <EOL> else : <EOL> pass <EOL> mask_biggest_contour_gray = cv2 . cvtColor ( mask_biggest_contour , cv2 . COLOR_BGR2GRAY ) <EOL> thresh_biggest_contour = cv2 . threshold ( mask_biggest_contour_gray , <NUM_LIT:12> , <NUM_LIT:255> , cv2 . THRESH_BINARY ) [ <NUM_LIT:1> ] <EOL> thresh_final_form = cv2 . bitwise_and ( thresh , thresh , mask = thresh_biggest_contour ) <EOL> thresh_final_form = cv2 . dilate ( thresh_final_form , None , iterations = <NUM_LIT:1> ) <EOL> frameDelta = cv2 . bitwise_and ( frame_final_form , frame_final_form , mask = thresh_final_form ) <EOL> if total_crazy_lines >= CRAZY_LINE_LIMIT : <EOL> break <EOL> frame_with_mask = cv2 . add ( frame , mask ) <EOL> cv2 . imshow ( '<STR_LIT>' , frame_with_mask ) <EOL> cv2 . imshow ( '<STR_LIT>' , mask_delta ) <EOL> cv2 . imshow ( '<STR_LIT>' , mask_biggest_contour ) <EOL> cv2 . imshow ( '<STR_LIT>' , frameDelta ) <EOL> k = cv2 . waitKey ( <NUM_LIT:30> ) & <NUM_LIT> <EOL> if k == <NUM_LIT> : <EOL> break <EOL> old_gray = frame_gray . copy ( ) <EOL> p1 = good_points2 . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> cv2 . destroyAllWindows ( ) <EOL> cap . release ( ) </s>
