<s> import cv2 <EOL> import numpy as np <EOL> from cam import OpenCV_Cam <EOL> from time import time <EOL> def draw_oriented_polylines ( img , pts_in , is_closed , color_start , thickness = <NUM_LIT:1> , color_end = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> img_out = img <EOL> pts = pts_in . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> if len ( img . shape ) == <NUM_LIT:2> or img . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> img_out = cv2 . cvtColor ( img_out , cv2 . COLOR_GRAY2BGR ) <EOL> cs , ce = color_start , color_end <EOL> n = len ( pts ) <EOL> for idx , pt in enumerate ( pts ) : <EOL> if idx == n and not is_closed : break <EOL> next_pt = pts [ ( idx + <NUM_LIT:1> ) % n ] <EOL> color = ( ( cs [ <NUM_LIT:0> ] * ( n - idx ) + ce [ <NUM_LIT:0> ] * idx ) / n , ( cs [ <NUM_LIT:1> ] * ( n - idx ) + ce [ <NUM_LIT:1> ] * idx ) / n , ( cs [ <NUM_LIT:2> ] * ( n - idx ) + ce [ <NUM_LIT:2> ] * idx ) / n ) <EOL> cv2 . line ( img_out , tuple ( pt ) , tuple ( next_pt ) , color , thickness ) <EOL> def find_polygons ( gray_image_in , edge_num , tolerance = <NUM_LIT:0.1> , area_threshold = <NUM_LIT:100> , convex_only = True , edge_threshold = <NUM_LIT> , orientation = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> img = gray_image_in . copy ( ) <EOL> lo , hi = <NUM_LIT:100> , <NUM_LIT> <EOL> edge = cv2 . Canny ( img , lo , hi ) <EOL> thresh1 , dst = cv2 . threshold ( edge , edge_threshold , <NUM_LIT:255> , cv2 . THRESH_BINARY ) <EOL> ctr , hry = cv2 . findContours ( dst , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) <EOL> if hry is None : return [ ] <EOL> hry = hry [ <NUM_LIT:0> ] <EOL> polygons = [ ] <EOL> for cnt in ctr : <EOL> cnt = cnt . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> epsilon = tolerance * <NUM_LIT:2> * ( ( <NUM_LIT> * cv2 . contourArea ( cnt ) ) ** <NUM_LIT:0.5> ) <EOL> tmp = cv2 . approxPolyDP ( cnt , epsilon , True ) <EOL> if len ( tmp ) != edge_num : continue <EOL> if convex_only and not cv2 . isContourConvex ( tmp ) : continue <EOL> if cv2 . contourArea ( tmp ) < area_threshold : continue <EOL> cross_product = np . cross ( tmp [ <NUM_LIT:1> ] - tmp [ <NUM_LIT:0> ] , tmp [ <NUM_LIT:2> ] - tmp [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> if cross_product * orientation > <NUM_LIT:0> : <EOL> tmp = np . flipud ( tmp ) <EOL> tmp = tmp . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> distance_from_origin = map ( lambda pt : pt [ <NUM_LIT:0> ] ** <NUM_LIT:2> + pt [ <NUM_LIT:1> ] ** <NUM_LIT:2> , tmp ) <EOL> val , idx = min ( ( val , idx ) for ( idx , val ) in enumerate ( distance_from_origin ) ) <EOL> if idx > <NUM_LIT:0> : <EOL> up , down = np . vsplit ( tmp , [ idx ] ) <EOL> tmp = np . vstack ( ( down , up ) ) <EOL> polygons . append ( tmp ) <EOL> return polygons <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> cam = OpenCV_Cam ( ) <EOL> while True : <EOL> img = cam . read ( ) <EOL> polygons = find_polygons ( img , <NUM_LIT:4> ) <EOL> for ctr in polygons : <EOL> draw_oriented_polylines ( img , ctr , <NUM_LIT:0> , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) , <NUM_LIT:2> , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> cv2 . imshow ( '<STR_LIT>' , img ) <EOL> k = cv2 . waitKey ( <NUM_LIT:5> ) <EOL> if k == <NUM_LIT> : <EOL> break </s>
