<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import cv2 <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 find_squares ( img ) : <EOL> img = cv2 . GaussianBlur ( img , ( <NUM_LIT:5> , <NUM_LIT:5> ) , <NUM_LIT:0> ) <EOL> squares = [ ] <EOL> for gray in cv2 . split ( img ) : <EOL> for thrs in xrange ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT> ) : <EOL> if thrs == <NUM_LIT:0> : <EOL> bin = cv2 . Canny ( gray , <NUM_LIT:0> , <NUM_LIT:50> , apertureSize = <NUM_LIT:5> ) <EOL> bin = cv2 . dilate ( bin , None ) <EOL> else : <EOL> retval , bin = cv2 . threshold ( gray , thrs , <NUM_LIT:255> , cv2 . THRESH_BINARY ) <EOL> contours , hierarchy = cv2 . findContours ( bin , cv2 . RETR_LIST , cv2 . CHAIN_APPROX_SIMPLE ) <EOL> for cnt in contours : <EOL> cnt_len = cv2 . arcLength ( cnt , True ) <EOL> cnt = cv2 . approxPolyDP ( cnt , <NUM_LIT> * cnt_len , True ) <EOL> if len ( cnt ) == <NUM_LIT:4> and cv2 . contourArea ( cnt ) > <NUM_LIT:1000> 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> : <EOL> squares . append ( cnt ) <EOL> return squares <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from glob import glob <EOL> for fn in glob ( '<STR_LIT>' ) : <EOL> img = cv2 . imread ( fn ) <EOL> squares = find_squares ( img ) <EOL> cv2 . drawContours ( img , squares , - <NUM_LIT:1> , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , <NUM_LIT:3> ) <EOL> cv2 . imshow ( '<STR_LIT>' , img ) <EOL> ch = <NUM_LIT> & cv2 . waitKey ( ) <EOL> if ch == <NUM_LIT> : <EOL> break <EOL> cv2 . destroyAllWindows ( ) </s>
