<s> import numpy as np <EOL> import cv2 <EOL> from webcam_gui import webcam_gui <EOL> def edge_filter ( frame_in ) : <EOL> frame_gray = cv2 . cvtColor ( frame_in , cv2 . COLOR_BGR2GRAY ) <EOL> frame_blur = cv2 . blur ( frame_gray , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> frame_out = cv2 . Canny ( frame_blur , <NUM_LIT:30> , <NUM_LIT> ) <EOL> return frame_out <EOL> def threshold ( frame_in , min ) : <EOL> frame_blur = cv2 . blur ( frame_in , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> thresh1 , thresh = cv2 . threshold ( frame_blur , min , <NUM_LIT:255> , cv2 . THRESH_BINARY ) <EOL> frame_out = thresh <EOL> return frame_out <EOL> def adap_threshold ( frame_in ) : <EOL> frame_blur = cv2 . blur ( frame_in , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> thresh = cv2 . adaptiveThreshold ( frame_blur , <NUM_LIT:255> , cv2 . ADAPTIVE_THRESH_GAUSSIAN_C , cv2 . THRESH_BINARY , <NUM_LIT:11> , <NUM_LIT:2> ) <EOL> frame_out = thresh <EOL> return frame_out <EOL> def canny_filter ( frame_in ) : <EOL> frame_blur = cv2 . blur ( frame_in , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> canny = cv2 . Canny ( frame_blur , <NUM_LIT:100> , <NUM_LIT:200> ) <EOL> frame_out = canny <EOL> return frame_out <EOL> def gray_filter ( frame_in ) : <EOL> frame_out = cv2 . cvtColor ( frame_in , cv2 . COLOR_BGR2GRAY ) <EOL> return frame_out <EOL> def angle_cos ( p0 , p1 , p2 ) : <EOL> d1 , d2 = ( p0 - p1 ) . astype ( '<STR_LIT:float>' ) , ( p2 - p1 ) . astype ( '<STR_LIT:float>' ) <EOL> return abs ( np . dot ( d1 , d2 ) / np . sqrt ( np . dot ( d1 , d1 ) * np . dot ( d2 , d2 ) ) ) <EOL> def equal ( p0 , p1 , p2 , tolerance ) : <EOL> d1 , d2 = ( p0 - p1 ) . astype ( '<STR_LIT:float>' ) , ( p2 - p1 ) . astype ( '<STR_LIT:float>' ) <EOL> l1 = np . sqrt ( d1 [ <NUM_LIT:0> ] ** <NUM_LIT:2> + d1 [ <NUM_LIT:1> ] ** <NUM_LIT:2> ) <EOL> l2 = np . sqrt ( d2 [ <NUM_LIT:0> ] ** <NUM_LIT:2> + d2 [ <NUM_LIT:1> ] ** <NUM_LIT:2> ) <EOL> if l1 <= l2 * ( <NUM_LIT:1> + tolerance ) and l1 >= l2 * ( <NUM_LIT:1> - tolerance ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def find_square ( frame_in ) : <EOL> frame_out = frame_in . copy ( ) <EOL> frame_gray = cv2 . cvtColor ( frame_in , cv2 . COLOR_BGR2GRAY ) <EOL> thresh = adap_threshold ( frame_gray ) <EOL> frame_blur = cv2 . blur ( thresh , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) <EOL> contours , hry = cv2 . findContours ( frame_blur , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) <EOL> squares = [ ] <EOL> for cnt in contours : <EOL> cnt_len = cv2 . arcLength ( cnt , True ) <EOL> cnt = cv2 . approxPolyDP ( cnt , <NUM_LIT> * cnt_len , True ) <EOL> if cv2 . contourArea ( cnt ) > <NUM_LIT:10> and len ( cnt ) == <NUM_LIT:4> and cv2 . isContourConvex ( cnt ) : <EOL> cnt = cnt . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> max_cos = np . max ( [ angle_cos ( cnt [ i ] , cnt [ ( i + <NUM_LIT:1> ) % <NUM_LIT:4> ] , cnt [ ( i + <NUM_LIT:2> ) % <NUM_LIT:4> ] ) for i in xrange ( <NUM_LIT:4> ) ] ) <EOL> if max_cos < <NUM_LIT:0.1> and equal ( cnt [ <NUM_LIT:0> ] , cnt [ <NUM_LIT:1> ] , cnt [ <NUM_LIT:2> ] , <NUM_LIT> ) : <EOL> squares . append ( cnt ) <EOL> cv2 . drawContours ( frame_out , squares , - <NUM_LIT:1> , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <NUM_LIT:2> ) <EOL> return frame_out <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> webcam_gui ( find_square ) </s>
