<s> '''<STR_LIT>''' <EOL> from SimpleCV . base import * <EOL> from SimpleCV . ImageClass import * <EOL> from SimpleCV . Color import * <EOL> from SimpleCV . Features . Features import Feature , FeatureSet <EOL> class Corner ( Feature ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , i , at_x , at_y ) : <EOL> points = [ ( at_x - <NUM_LIT:1> , at_y - <NUM_LIT:1> ) , ( at_x - <NUM_LIT:1> , at_y + <NUM_LIT:1> ) , ( at_x + <NUM_LIT:1> , at_y + <NUM_LIT:1> ) , ( at_x + <NUM_LIT:1> , at_y - <NUM_LIT:1> ) ] <EOL> super ( Corner , self ) . __init__ ( i , at_x , at_y , points ) <EOL> def draw ( self , color = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . drawCircle ( ( self . x , self . y ) , <NUM_LIT:4> , color , width ) <EOL> class Line ( Feature ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , i , line ) : <EOL> self . image = i <EOL> self . vector = None <EOL> self . yIntercept = None <EOL> self . end_points = copy ( line ) <EOL> if self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] - self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> self . slope = float ( "<STR_LIT>" ) <EOL> else : <EOL> self . slope = float ( self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] - self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) / float ( self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] - self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> at_x = ( line [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] + line [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) / <NUM_LIT:2> <EOL> at_y = ( line [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] + line [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) / <NUM_LIT:2> <EOL> xmin = int ( np . min ( [ line [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , line [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ] ) ) <EOL> xmax = int ( np . max ( [ line [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , line [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ] ) ) <EOL> ymax = int ( np . min ( [ line [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , line [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ] ) ) <EOL> ymin = int ( np . max ( [ line [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , line [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ] ) ) <EOL> points = [ ( xmin , ymin ) , ( xmin , ymax ) , ( xmax , ymax ) , ( xmax , ymin ) ] <EOL> super ( Line , self ) . __init__ ( i , at_x , at_y , points ) <EOL> def draw ( self , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . drawLine ( self . end_points [ <NUM_LIT:0> ] , self . end_points [ <NUM_LIT:1> ] , color , width ) <EOL> def length ( self ) : <EOL> """<STR_LIT>""" <EOL> return float ( spsd . euclidean ( self . end_points [ <NUM_LIT:0> ] , self . end_points [ <NUM_LIT:1> ] ) ) <EOL> def crop ( self ) : <EOL> """<STR_LIT>""" <EOL> tl = self . topLeftCorner ( ) <EOL> return self . image . crop ( tl [ <NUM_LIT:0> ] , tl [ <NUM_LIT:1> ] , self . width ( ) , self . height ( ) ) <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> ( pt1 , pt2 ) = self . end_points <EOL> ( maxx , minx , maxy , miny ) = self . extents ( ) <EOL> d_x = maxx - minx <EOL> d_y = maxy - miny <EOL> if ( d_x == <NUM_LIT:0.0> ) : <EOL> return self . image [ pt1 [ <NUM_LIT:0> ] : pt1 [ <NUM_LIT:0> ] + <NUM_LIT:1> , miny : maxy ] . meanColor ( ) <EOL> if ( d_y == <NUM_LIT:0.0> ) : <EOL> return self . image [ minx : maxx , pt1 [ <NUM_LIT:1> ] : pt1 [ <NUM_LIT:1> ] + <NUM_LIT:1> ] . meanColor ( ) <EOL> error = <NUM_LIT:0.0> <EOL> d_err = d_y / d_x <EOL> px = [ ] <EOL> weights = [ ] <EOL> if ( d_err < <NUM_LIT:1> ) : <EOL> y = miny <EOL> for x in range ( minx , maxx ) : <EOL> px . append ( self . image [ x , y ] ) <EOL> weights . append ( <NUM_LIT:1.0> - abs ( error ) ) <EOL> if ( error > <NUM_LIT:0> ) : <EOL> px . append ( self . image [ x , y + <NUM_LIT:1> ] ) <EOL> weights . append ( error ) <EOL> if ( error < <NUM_LIT:0> ) : <EOL> px . append ( self . image [ x , y - <NUM_LIT:1> ] ) <EOL> weights . append ( abs ( error ) ) <EOL> error = error + d_err <EOL> if ( error >= <NUM_LIT:0.5> ) : <EOL> y = y + <NUM_LIT:1> <EOL> error = error - <NUM_LIT:1.0> <EOL> else : <EOL> x = minx <EOL> for y in range ( miny , maxy ) : <EOL> px . append ( self . image [ x , y ] ) <EOL> weights . append ( <NUM_LIT:1.0> - abs ( error ) ) <EOL> if ( error > <NUM_LIT:0> ) : <EOL> px . append ( self . image [ x + <NUM_LIT:1> , y ] ) <EOL> weights . append ( error ) <EOL> if ( error < <NUM_LIT:0> ) : <EOL> px . append ( self . image [ x - <NUM_LIT:1> , y ] ) <EOL> weights . append ( abs ( error ) ) <EOL> error = error + ( <NUM_LIT:1.0> / d_err ) <EOL> if ( error >= <NUM_LIT:0.5> ) : <EOL> x = x + <NUM_LIT:1> <EOL> error = error - <NUM_LIT:1.0> <EOL> clr_arr = np . array ( px ) <EOL> weight_arr = np . array ( weights ) <EOL> weighted_clrs = np . transpose ( np . transpose ( clr_arr ) * weight_arr ) <EOL> temp = sum ( weighted_clrs ) / sum ( weight_arr ) <EOL> return ( float ( temp [ <NUM_LIT:0> ] ) , float ( temp [ <NUM_LIT:1> ] ) , float ( temp [ <NUM_LIT:2> ] ) ) <EOL> def findIntersection ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if self . slope == float ( "<STR_LIT>" ) : <EOL> x = self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> y = line . slope * ( x - line . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) + line . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> return ( x , y ) <EOL> if line . slope == float ( "<STR_LIT>" ) : <EOL> x = line . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> y = self . slope * ( x - self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) + self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> return ( x , y ) <EOL> m1 = self . slope <EOL> x12 , y12 = self . end_points [ <NUM_LIT:1> ] <EOL> m2 = line . slope <EOL> x22 , y22 = line . end_points [ <NUM_LIT:1> ] <EOL> x = ( m1 * x12 - m2 * x22 + y22 - y12 ) / float ( m1 - m2 ) <EOL> y = ( m1 * m2 * ( x12 - x22 ) - m2 * y12 + m1 * y22 ) / float ( m1 - m2 ) <EOL> return ( x , y ) <EOL> def isParallel ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if self . slope == line . slope : <EOL> return True <EOL> return False <EOL> def isPerpendicular ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if self . slope == float ( "<STR_LIT>" ) : <EOL> if line . slope == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> if line . slope == float ( "<STR_LIT>" ) : <EOL> if self . slope == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> if self . slope * line . slope == - <NUM_LIT:1> : <EOL> return True <EOL> return False <EOL> def imgIntersections ( self , img ) : <EOL> """<STR_LIT>""" <EOL> pixels = [ ] <EOL> if self . slope == float ( "<STR_LIT>" ) : <EOL> for y in range ( self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] + <NUM_LIT:1> ) : <EOL> pixels . append ( ( self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , y ) ) <EOL> else : <EOL> for x in range ( self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] + <NUM_LIT:1> ) : <EOL> pixels . append ( ( x , int ( self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] + self . slope * ( x - self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) ) ) ) <EOL> for y in range ( self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] + <NUM_LIT:1> ) : <EOL> pixels . append ( ( int ( ( ( y - self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) / self . slope ) + self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) , y ) ) <EOL> pixels = list ( set ( pixels ) ) <EOL> matched_pixels = [ ] <EOL> for pixel in pixels : <EOL> if img [ pixel [ <NUM_LIT:0> ] , pixel [ <NUM_LIT:1> ] ] == ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> matched_pixels . append ( pixel ) <EOL> matched_pixels . sort ( ) <EOL> return matched_pixels <EOL> def angle ( self ) : <EOL> """<STR_LIT>""" <EOL> a = <NUM_LIT:0> <EOL> b = <NUM_LIT:1> <EOL> if ( self . end_points [ a ] [ <NUM_LIT:0> ] > self . end_points [ b ] [ <NUM_LIT:0> ] ) : <EOL> b = <NUM_LIT:0> <EOL> a = <NUM_LIT:1> <EOL> d_x = self . end_points [ b ] [ <NUM_LIT:0> ] - self . end_points [ a ] [ <NUM_LIT:0> ] <EOL> d_y = self . end_points [ b ] [ <NUM_LIT:1> ] - self . end_points [ a ] [ <NUM_LIT:1> ] <EOL> return float ( <NUM_LIT> * ( atan2 ( d_y , d_x ) / ( <NUM_LIT:2> * np . pi ) ) ) <EOL> def cropToImageEdges ( self ) : <EOL> """<STR_LIT>""" <EOL> pt1 , pt2 = self . end_points <EOL> pt1 , pt2 = min ( pt1 , pt2 ) , max ( pt1 , pt2 ) <EOL> x1 , y1 = pt1 <EOL> x2 , y2 = pt2 <EOL> w , h = self . image . width - <NUM_LIT:1> , self . image . height - <NUM_LIT:1> <EOL> slope = self . slope <EOL> ep = [ ] <EOL> if slope == float ( '<STR_LIT>' ) : <EOL> if <NUM_LIT:0> <= x1 <= w and <NUM_LIT:0> <= x2 <= w : <EOL> ep . append ( ( x1 , <NUM_LIT:0> ) ) <EOL> ep . append ( ( x2 , h ) ) <EOL> elif slope == <NUM_LIT:0> : <EOL> if <NUM_LIT:0> <= y1 <= w and <NUM_LIT:0> <= y2 <= w : <EOL> ep . append ( ( <NUM_LIT:0> , y1 ) ) <EOL> ep . append ( ( w , y2 ) ) <EOL> else : <EOL> x = ( slope * x1 - y1 ) / slope <EOL> if <NUM_LIT:0> <= x <= w : <EOL> ep . append ( ( int ( round ( x ) ) , <NUM_LIT:0> ) ) <EOL> x = ( slope * x1 + h - y1 ) / slope <EOL> if <NUM_LIT:0> <= x <= w : <EOL> ep . append ( ( int ( round ( x ) ) , h ) ) <EOL> y = - slope * x1 + y1 <EOL> if <NUM_LIT:0> <= y <= h : <EOL> ep . append ( ( <NUM_LIT:0> , ( int ( round ( y ) ) ) ) ) <EOL> y = slope * ( w - x1 ) + y1 <EOL> if <NUM_LIT:0> <= y <= h : <EOL> ep . append ( ( w , ( int ( round ( y ) ) ) ) ) <EOL> ep = list ( set ( ep ) ) <EOL> ep . sort ( ) <EOL> if len ( ep ) == <NUM_LIT:2> : <EOL> if not ( <NUM_LIT:0> < x1 < w and <NUM_LIT:0> < y1 < h ) : <EOL> pt1 = ep [ <NUM_LIT:0> ] <EOL> if not ( <NUM_LIT:0> < x2 < w and <NUM_LIT:0> < y2 < h ) : <EOL> pt2 = ep [ <NUM_LIT:1> ] <EOL> elif len ( ep ) == <NUM_LIT:1> : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> return Line ( self . image , ( pt1 , pt2 ) ) <EOL> def getVector ( self ) : <EOL> if ( self . vector is None ) : <EOL> self . vector = [ float ( self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] - self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) , <EOL> float ( self . end_points [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] - self . end_points [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ] <EOL> return self . vector <EOL> def dot ( self , other ) : <EOL> return np . dot ( self . getVector ( ) , other . getVector ( ) ) <EOL> def cross ( self , other ) : <EOL> return np . cross ( self . getVector ( ) , other . getVector ( ) ) <EOL> def getYIntercept ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . yIntercept is None : <EOL> pt1 , pt2 = self . end_points <EOL> m = self . slope <EOL> self . yIntercept = pt1 [ <NUM_LIT:1> ] - m * pt1 [ <NUM_LIT:0> ] <EOL> return self . yIntercept <EOL> def extendToImageEdges ( self ) : <EOL> """<STR_LIT>""" <EOL> pt1 , pt2 = self . end_points <EOL> pt1 , pt2 = min ( pt1 , pt2 ) , max ( pt1 , pt2 ) <EOL> x1 , y1 = pt1 <EOL> x2 , y2 = pt2 <EOL> w , h = self . image . width - <NUM_LIT:1> , self . image . height - <NUM_LIT:1> <EOL> slope = self . slope <EOL> if not <NUM_LIT:0> <= x1 <= w or not <NUM_LIT:0> <= x2 <= w or not <NUM_LIT:0> <= y1 <= w or not <NUM_LIT:0> <= y2 <= w : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> ep = [ ] <EOL> if slope == float ( '<STR_LIT>' ) : <EOL> if <NUM_LIT:0> <= x1 <= w and <NUM_LIT:0> <= x2 <= w : <EOL> return Line ( self . image , ( ( x1 , <NUM_LIT:0> ) , ( x2 , h ) ) ) <EOL> elif slope == <NUM_LIT:0> : <EOL> if <NUM_LIT:0> <= y1 <= w and <NUM_LIT:0> <= y2 <= w : <EOL> return Line ( self . image , ( ( <NUM_LIT:0> , y1 ) , ( w , y2 ) ) ) <EOL> else : <EOL> x = ( slope * x1 - y1 ) / slope <EOL> if <NUM_LIT:0> <= x <= w : <EOL> ep . append ( ( int ( round ( x ) ) , <NUM_LIT:0> ) ) <EOL> x = ( slope * x1 + h - y1 ) / slope <EOL> if <NUM_LIT:0> <= x <= w : <EOL> ep . append ( ( int ( round ( x ) ) , h ) ) <EOL> y = - slope * x1 + y1 <EOL> if <NUM_LIT:0> <= y <= h : <EOL> ep . append ( ( <NUM_LIT:0> , ( int ( round ( y ) ) ) ) ) <EOL> y = slope * ( w - x1 ) + y1 <EOL> if <NUM_LIT:0> <= y <= h : <EOL> ep . append ( ( w , ( int ( round ( y ) ) ) ) ) <EOL> ep = list ( set ( ep ) ) <EOL> ep . sort ( ) <EOL> return Line ( self . image , ep ) <EOL> class Barcode ( Feature ) : <EOL> """<STR_LIT>""" <EOL> data = "<STR_LIT>" <EOL> def __init__ ( self , i , zbsymbol ) : <EOL> self . image = i <EOL> locs = zbsymbol . location <EOL> if len ( locs ) > <NUM_LIT:4> : <EOL> xs = [ l [ <NUM_LIT:0> ] for l in locs ] <EOL> ys = [ l [ <NUM_LIT:1> ] for l in locs ] <EOL> xmax = np . max ( xs ) <EOL> xmin = np . min ( xs ) <EOL> ymax = np . max ( ys ) <EOL> ymin = np . min ( ys ) <EOL> points = ( ( xmin , ymin ) , ( xmin , ymax ) , ( xmax , ymax ) , ( xmax , ymin ) ) <EOL> else : <EOL> points = copy ( locs ) <EOL> super ( Barcode , self ) . __init__ ( i , <NUM_LIT:0> , <NUM_LIT:0> , points ) <EOL> self . data = zbsymbol . data <EOL> self . points = copy ( points ) <EOL> numpoints = len ( self . points ) <EOL> self . x = <NUM_LIT:0> <EOL> self . y = <NUM_LIT:0> <EOL> for p in self . points : <EOL> self . x += p [ <NUM_LIT:0> ] <EOL> self . y += p [ <NUM_LIT:1> ] <EOL> if ( numpoints ) : <EOL> self . x /= numpoints <EOL> self . y /= numpoints <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __module__ , self . __class__ . __name__ , self . x , self . y , self . data ) <EOL> def draw ( self , color = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . drawLine ( self . points [ <NUM_LIT:0> ] , self . points [ <NUM_LIT:1> ] , color , width ) <EOL> self . image . drawLine ( self . points [ <NUM_LIT:1> ] , self . points [ <NUM_LIT:2> ] , color , width ) <EOL> self . image . drawLine ( self . points [ <NUM_LIT:2> ] , self . points [ <NUM_LIT:3> ] , color , width ) <EOL> self . image . drawLine ( self . points [ <NUM_LIT:3> ] , self . points [ <NUM_LIT:0> ] , color , width ) <EOL> def length ( self ) : <EOL> """<STR_LIT>""" <EOL> sqform = spsd . squareform ( spsd . pdist ( self . points , "<STR_LIT>" ) ) <EOL> return max ( sqform [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , sqform [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] , sqform [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] , sqform [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] ) <EOL> def area ( self ) : <EOL> """<STR_LIT>""" <EOL> sqform = spsd . squareform ( spsd . pdist ( self . points , "<STR_LIT>" ) ) <EOL> a = sqform [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> b = sqform [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> c = sqform [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] <EOL> d = sqform [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] <EOL> p = sqform [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> q = sqform [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] <EOL> s = ( a + b + c + d ) / <NUM_LIT> <EOL> return sqrt ( ( s - a ) * ( s - b ) * ( s - c ) * ( s - d ) - ( a * c + b * d + p * q ) * ( a * c + b * d - p * q ) / <NUM_LIT:4> ) <EOL> class HaarFeature ( Feature ) : <EOL> """<STR_LIT>""" <EOL> classifier = "<STR_LIT>" <EOL> _width = "<STR_LIT>" <EOL> _height = "<STR_LIT>" <EOL> neighbors = '<STR_LIT>' <EOL> featureName = '<STR_LIT:None>' <EOL> def __init__ ( self , i , haarobject , haarclassifier = None , cv2flag = True ) : <EOL> self . image = i <EOL> if cv2flag == False : <EOL> ( ( x , y , width , height ) , self . neighbors ) = haarobject <EOL> elif cv2flag == True : <EOL> ( x , y , width , height ) = haarobject <EOL> at_x = x + width / <NUM_LIT:2> <EOL> at_y = y + height / <NUM_LIT:2> <EOL> points = ( ( x , y ) , ( x + width , y ) , ( x + width , y + height ) , ( x , y + height ) ) <EOL> self . classifier = haarclassifier <EOL> if ( haarclassifier is not None ) : <EOL> self . featureName = haarclassifier . getName ( ) <EOL> super ( HaarFeature , self ) . __init__ ( i , at_x , at_y , points ) <EOL> def draw ( self , color = ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . drawLine ( self . points [ <NUM_LIT:0> ] , self . points [ <NUM_LIT:1> ] , color , width ) <EOL> self . image . drawLine ( self . points [ <NUM_LIT:1> ] , self . points [ <NUM_LIT:2> ] , color , width ) <EOL> self . image . drawLine ( self . points [ <NUM_LIT:2> ] , self . points [ <NUM_LIT:3> ] , color , width ) <EOL> self . image . drawLine ( self . points [ <NUM_LIT:3> ] , self . points [ <NUM_LIT:0> ] , color , width ) <EOL> def __getstate__ ( self ) : <EOL> dict = self . __dict__ . copy ( ) <EOL> if '<STR_LIT>' in dict : <EOL> del dict [ "<STR_LIT>" ] <EOL> return dict <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> crop = self . image [ self . points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : self . points [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , self . points [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : self . points [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ] <EOL> return crop . meanColor ( ) <EOL> def area ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . width ( ) * self . height ( ) <EOL> class Chessboard ( Feature ) : <EOL> """<STR_LIT>""" <EOL> spCorners = [ ] <EOL> dimensions = ( ) <EOL> def __init__ ( self , i , dim , subpixelCorners ) : <EOL> self . dimensions = dim <EOL> self . spCorners = subpixelCorners <EOL> at_x = np . average ( np . array ( self . spCorners ) [ : , <NUM_LIT:0> ] ) <EOL> at_y = np . average ( np . array ( self . spCorners ) [ : , <NUM_LIT:1> ] ) <EOL> posdiagsorted = sorted ( self . spCorners , key = lambda corner : corner [ <NUM_LIT:0> ] + corner [ <NUM_LIT:1> ] ) <EOL> negdiagsorted = sorted ( self . spCorners , key = lambda corner : corner [ <NUM_LIT:0> ] - corner [ <NUM_LIT:1> ] ) <EOL> points = ( posdiagsorted [ <NUM_LIT:0> ] , negdiagsorted [ - <NUM_LIT:1> ] , posdiagsorted [ - <NUM_LIT:1> ] , negdiagsorted [ <NUM_LIT:0> ] ) <EOL> super ( Chessboard , self ) . __init__ ( i , at_x , at_y , points ) <EOL> def draw ( self , no_needed_color = None ) : <EOL> """<STR_LIT>""" <EOL> cv . DrawChessboardCorners ( self . image . getBitmap ( ) , self . dimensions , self . spCorners , <NUM_LIT:1> ) <EOL> def area ( self ) : <EOL> """<STR_LIT>""" <EOL> sqform = spsd . squareform ( spsd . pdist ( self . points , "<STR_LIT>" ) ) <EOL> a = sqform [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> b = sqform [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> c = sqform [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] <EOL> d = sqform [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] <EOL> p = sqform [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> q = sqform [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] <EOL> s = ( a + b + c + d ) / <NUM_LIT> <EOL> return <NUM_LIT:2> * sqrt ( ( s - a ) * ( s - b ) * ( s - c ) * ( s - d ) - ( a * c + b * d + p * q ) * ( a * c + b * d - p * q ) / <NUM_LIT:4> ) <EOL> class TemplateMatch ( Feature ) : <EOL> """<STR_LIT>""" <EOL> template_image = None <EOL> quality = <NUM_LIT:0> <EOL> w = <NUM_LIT:0> <EOL> h = <NUM_LIT:0> <EOL> def __init__ ( self , image , template , location , quality ) : <EOL> self . template_image = template <EOL> self . image = image <EOL> self . quality = quality <EOL> w = template . width <EOL> h = template . height <EOL> at_x = location [ <NUM_LIT:0> ] <EOL> at_y = location [ <NUM_LIT:1> ] <EOL> points = [ ( at_x , at_y ) , ( at_x + w , at_y ) , ( at_x + w , at_y + h ) , ( at_x , at_y + h ) ] <EOL> super ( TemplateMatch , self ) . __init__ ( image , at_x , at_y , points ) <EOL> def _templateOverlaps ( self , other ) : <EOL> """<STR_LIT>""" <EOL> ( maxx , minx , maxy , miny ) = self . extents ( ) <EOL> overlap = False <EOL> for p in other . points : <EOL> if ( p [ <NUM_LIT:0> ] <= maxx and p [ <NUM_LIT:0> ] >= minx and p [ <NUM_LIT:1> ] <= maxy and p [ <NUM_LIT:1> ] >= miny ) : <EOL> overlap = True <EOL> break <EOL> return overlap <EOL> def consume ( self , other ) : <EOL> """<STR_LIT>""" <EOL> ( maxx , minx , maxy , miny ) = self . extents ( ) <EOL> ( maxx0 , minx0 , maxy0 , miny0 ) = other . extents ( ) <EOL> maxx = max ( maxx , maxx0 ) <EOL> minx = min ( minx , minx0 ) <EOL> maxy = max ( maxy , maxy0 ) <EOL> miny = min ( miny , miny0 ) <EOL> self . x = minx <EOL> self . y = miny <EOL> self . points = [ ( minx , miny ) , ( minx , maxy ) , ( maxx , maxy ) , ( maxx , miny ) ] <EOL> self . _updateExtents ( ) <EOL> def rescale ( self , w , h ) : <EOL> """<STR_LIT>""" <EOL> ( maxx , minx , maxy , miny ) = self . extents ( ) <EOL> xc = minx + ( ( maxx - minx ) / <NUM_LIT:2> ) <EOL> yc = miny + ( ( maxy - miny ) / <NUM_LIT:2> ) <EOL> x = xc - ( w / <NUM_LIT:2> ) <EOL> y = yc - ( h / <NUM_LIT:2> ) <EOL> self . x = x <EOL> self . y = y <EOL> self . points = [ ( x , y ) , <EOL> ( x + w , y ) , <EOL> ( x + w , y + h ) , <EOL> ( x , y + h ) ] <EOL> self . _updateExtents ( ) <EOL> def crop ( self ) : <EOL> ( maxx , minx , maxy , miny ) = self . extents ( ) <EOL> return self . image . crop ( minx , miny , maxx - minx , maxy - miny ) <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . dl ( ) . rectangle ( ( self . x , self . y ) , ( self . width ( ) , self . height ( ) ) , color = color , width = width ) <EOL> class Circle ( Feature ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT> <EOL> y = <NUM_LIT> <EOL> r = <NUM_LIT> <EOL> image = "<STR_LIT>" <EOL> points = [ ] <EOL> avgColor = None <EOL> def __init__ ( self , i , at_x , at_y , r ) : <EOL> self . r = r <EOL> self . avgColor = None <EOL> points = [ ( at_x - r , at_y - r ) , ( at_x + r , at_y - r ) , ( at_x + r , at_y + r ) , ( at_x - r , at_y + r ) ] <EOL> super ( Circle , self ) . __init__ ( i , at_x , at_y , points ) <EOL> segments = <NUM_LIT> <EOL> rng = range ( <NUM_LIT:1> , segments + <NUM_LIT:1> ) <EOL> self . mContour = [ ] <EOL> for theta in rng : <EOL> rp = <NUM_LIT> * math . pi * float ( theta ) / float ( segments ) <EOL> x = ( r * math . sin ( rp ) ) + at_x <EOL> y = ( r * math . cos ( rp ) ) + at_y <EOL> self . mContour . append ( ( x , y ) ) <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . dl ( ) . circle ( ( self . x , self . y ) , self . r , color , width ) <EOL> def show ( self , color = Color . GREEN ) : <EOL> """<STR_LIT>""" <EOL> self . draw ( color ) <EOL> self . image . show ( ) <EOL> def distanceFrom ( self , point = ( - <NUM_LIT:1> , - <NUM_LIT:1> ) ) : <EOL> """<STR_LIT>""" <EOL> if ( point [ <NUM_LIT:0> ] == - <NUM_LIT:1> or point [ <NUM_LIT:1> ] == - <NUM_LIT:1> ) : <EOL> point = np . array ( self . image . size ( ) ) / <NUM_LIT:2> <EOL> return spsd . euclidean ( point , [ self . x , self . y ] ) <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . avgColor is None ) : <EOL> mask = self . image . getEmpty ( <NUM_LIT:1> ) <EOL> cv . Zero ( mask ) <EOL> cv . Circle ( mask , ( self . x , self . y ) , self . r , color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , thickness = - <NUM_LIT:1> ) <EOL> temp = cv . Avg ( self . image . getBitmap ( ) , mask ) <EOL> self . avgColor = ( temp [ <NUM_LIT:0> ] , temp [ <NUM_LIT:1> ] , temp [ <NUM_LIT:2> ] ) <EOL> return self . avgColor <EOL> def area ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . r * self . r * pi <EOL> def perimeter ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:2> * pi * self . r <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . r * <NUM_LIT:2> <EOL> def height ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . r * <NUM_LIT:2> <EOL> def radius ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . r <EOL> def diameter ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . r * <NUM_LIT:2> <EOL> def crop ( self , noMask = False ) : <EOL> """<STR_LIT>""" <EOL> if ( noMask ) : <EOL> return self . image . crop ( self . x , self . y , self . width ( ) , self . height ( ) , centered = True ) <EOL> else : <EOL> mask = self . image . getEmpty ( <NUM_LIT:1> ) <EOL> result = self . image . getEmpty ( ) <EOL> cv . Zero ( mask ) <EOL> cv . Zero ( result ) <EOL> cv . Circle ( mask , ( self . x , self . y ) , self . r , color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , thickness = - <NUM_LIT:1> ) <EOL> cv . Copy ( self . image . getBitmap ( ) , result , mask ) <EOL> retVal = Image ( result ) <EOL> retVal = retVal . crop ( self . x , self . y , self . width ( ) , self . height ( ) , centered = True ) <EOL> return retVal <EOL> class KeyPoint ( Feature ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT> <EOL> y = <NUM_LIT> <EOL> r = <NUM_LIT> <EOL> image = "<STR_LIT>" <EOL> points = [ ] <EOL> __avgColor = None <EOL> mAngle = <NUM_LIT:0> <EOL> mOctave = <NUM_LIT:0> <EOL> mResponse = <NUM_LIT> <EOL> mFlavor = "<STR_LIT>" <EOL> mDescriptor = None <EOL> mKeyPoint = None <EOL> def __init__ ( self , i , keypoint , descriptor = None , flavor = "<STR_LIT>" ) : <EOL> self . mKeyPoint = keypoint <EOL> x = keypoint . pt [ <NUM_LIT:1> ] <EOL> y = keypoint . pt [ <NUM_LIT:0> ] <EOL> self . _r = keypoint . size / <NUM_LIT> <EOL> self . _avgColor = None <EOL> self . image = i <EOL> self . mAngle = keypoint . angle <EOL> self . mOctave = keypoint . octave <EOL> self . mResponse = keypoint . response <EOL> self . mFlavor = flavor <EOL> self . mDescriptor = descriptor <EOL> r = self . _r <EOL> points = ( ( x + r , y + r ) , ( x + r , y - r ) , ( x - r , y - r ) , ( x - r , y + r ) ) <EOL> super ( KeyPoint , self ) . __init__ ( i , x , y , points ) <EOL> segments = <NUM_LIT> <EOL> rng = range ( <NUM_LIT:1> , segments + <NUM_LIT:1> ) <EOL> self . points = [ ] <EOL> for theta in rng : <EOL> rp = <NUM_LIT> * math . pi * float ( theta ) / float ( segments ) <EOL> x = ( r * math . sin ( rp ) ) + self . x <EOL> y = ( r * math . cos ( rp ) ) + self . y <EOL> self . points . append ( ( x , y ) ) <EOL> def getObject ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mKeyPoint <EOL> def descriptor ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mDescriptor <EOL> def quality ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mResponse <EOL> def octave ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mOctave <EOL> def flavor ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mFlavor <EOL> def angle ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mAngle <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . dl ( ) . circle ( ( self . x , self . y ) , self . _r , color , width ) <EOL> pt1 = ( int ( self . x ) , int ( self . y ) ) <EOL> pt2 = ( int ( self . x + ( self . radius ( ) * sin ( radians ( self . angle ( ) ) ) ) ) , <EOL> int ( self . y + ( self . radius ( ) * cos ( radians ( self . angle ( ) ) ) ) ) ) <EOL> self . image . dl ( ) . line ( pt1 , pt2 , color , width ) <EOL> def show ( self , color = Color . GREEN ) : <EOL> """<STR_LIT>""" <EOL> self . draw ( color ) <EOL> self . image . show ( ) <EOL> def distanceFrom ( self , point = ( - <NUM_LIT:1> , - <NUM_LIT:1> ) ) : <EOL> """<STR_LIT>""" <EOL> if ( point [ <NUM_LIT:0> ] == - <NUM_LIT:1> or point [ <NUM_LIT:1> ] == - <NUM_LIT:1> ) : <EOL> point = np . array ( self . image . size ( ) ) / <NUM_LIT:2> <EOL> return spsd . euclidean ( point , [ self . x , self . y ] ) <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _avgColor is None ) : <EOL> mask = self . image . getEmpty ( <NUM_LIT:1> ) <EOL> cv . Zero ( mask ) <EOL> cv . Circle ( mask , ( int ( self . x ) , int ( self . y ) ) , int ( self . _r ) , color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , thickness = - <NUM_LIT:1> ) <EOL> temp = cv . Avg ( self . image . getBitmap ( ) , mask ) <EOL> self . _avgColor = ( temp [ <NUM_LIT:0> ] , temp [ <NUM_LIT:1> ] , temp [ <NUM_LIT:2> ] ) <EOL> return self . _avgColor <EOL> def colorDistance ( self , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> return spsd . euclidean ( np . array ( color ) , np . array ( self . meanColor ( ) ) ) <EOL> def perimeter ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:2> * pi * self . _r <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _r * <NUM_LIT:2> <EOL> def height ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _r * <NUM_LIT:2> <EOL> def radius ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _r <EOL> def diameter ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _r * <NUM_LIT:2> <EOL> def crop ( self , noMask = False ) : <EOL> """<STR_LIT>""" <EOL> if ( noMask ) : <EOL> return self . image . crop ( self . x , self . y , self . width ( ) , self . height ( ) , centered = True ) <EOL> else : <EOL> mask = self . image . getEmpty ( <NUM_LIT:1> ) <EOL> result = self . image . getEmpty ( ) <EOL> cv . Zero ( mask ) <EOL> cv . Zero ( result ) <EOL> cv . Circle ( mask , ( int ( self . x ) , int ( self . y ) ) , int ( self . _r ) , color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , thickness = - <NUM_LIT:1> ) <EOL> cv . Copy ( self . image . getBitmap ( ) , result , mask ) <EOL> retVal = Image ( result ) <EOL> retVal = retVal . crop ( self . x , self . y , self . width ( ) , self . height ( ) , centered = True ) <EOL> return retVal <EOL> class Motion ( Feature ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT:0.0> <EOL> y = <NUM_LIT:0.0> <EOL> image = "<STR_LIT>" <EOL> points = [ ] <EOL> dx = <NUM_LIT> <EOL> dy = <NUM_LIT> <EOL> norm_dy = <NUM_LIT> <EOL> norm_dx = <NUM_LIT> <EOL> window = <NUM_LIT:7> <EOL> def __init__ ( self , i , at_x , at_y , dx , dy , wndw ) : <EOL> """<STR_LIT>""" <EOL> self . dx = dx <EOL> self . dy = dy <EOL> self . window = wndw <EOL> sz = wndw / <NUM_LIT:2> <EOL> points = [ ( at_x + sz , at_y + sz ) , ( at_x - sz , at_y + sz ) , ( at_x + sz , at_y + sz ) , ( at_x + sz , at_y - sz ) ] <EOL> super ( Motion , self ) . __init__ ( i , at_x , at_y , points ) <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:1> , normalize = True ) : <EOL> """<STR_LIT>""" <EOL> new_x = <NUM_LIT:0> <EOL> new_y = <NUM_LIT:0> <EOL> if ( normalize ) : <EOL> win = self . window / <NUM_LIT:2> <EOL> w = math . sqrt ( ( win * win ) * <NUM_LIT:2> ) <EOL> new_x = ( self . norm_dx * w ) + self . x <EOL> new_y = ( self . norm_dy * w ) + self . y <EOL> else : <EOL> new_x = self . x + self . dx <EOL> new_y = self . y + self . dy <EOL> self . image . dl ( ) . line ( ( self . x , self . y ) , ( new_x , new_y ) , color , width ) <EOL> def normalizeTo ( self , max_mag ) : <EOL> """<STR_LIT>""" <EOL> if ( max_mag == <NUM_LIT:0> ) : <EOL> self . norm_dx = <NUM_LIT:0> <EOL> self . norm_dy = <NUM_LIT:0> <EOL> return None <EOL> mag = self . magnitude ( ) <EOL> new_mag = mag / max_mag <EOL> unit = self . unitVector ( ) <EOL> self . norm_dx = unit [ <NUM_LIT:0> ] * new_mag <EOL> self . norm_dy = unit [ <NUM_LIT:1> ] * new_mag <EOL> def magnitude ( self ) : <EOL> """<STR_LIT>""" <EOL> return sqrt ( ( self . dx * self . dx ) + ( self . dy * self . dy ) ) <EOL> def unitVector ( self ) : <EOL> """<STR_LIT>""" <EOL> mag = self . magnitude ( ) <EOL> if ( mag != <NUM_LIT> ) : <EOL> return ( float ( self . dx ) / mag , float ( self . dy ) / mag ) <EOL> else : <EOL> return ( <NUM_LIT> , <NUM_LIT> ) <EOL> def vector ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . dx , self . dy ) <EOL> def windowSz ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . window <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> x = int ( self . x - ( self . window / <NUM_LIT:2> ) ) <EOL> y = int ( self . y - ( self . window / <NUM_LIT:2> ) ) <EOL> return self . image . crop ( x , y , int ( self . window ) , int ( self . window ) ) . meanColor ( ) <EOL> def crop ( self ) : <EOL> """<STR_LIT>""" <EOL> x = int ( self . x - ( self . window / <NUM_LIT:2> ) ) <EOL> y = int ( self . y - ( self . window / <NUM_LIT:2> ) ) <EOL> return self . image . crop ( x , y , int ( self . window ) , int ( self . window ) ) <EOL> class KeypointMatch ( Feature ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT> <EOL> y = <NUM_LIT> <EOL> image = "<STR_LIT>" <EOL> points = [ ] <EOL> _minRect = [ ] <EOL> _avgColor = None <EOL> _homography = [ ] <EOL> _template = None <EOL> def __init__ ( self , image , template , minRect , _homography ) : <EOL> self . _template = template <EOL> self . _minRect = minRect <EOL> self . _homography = _homography <EOL> xmax = <NUM_LIT:0> <EOL> ymax = <NUM_LIT:0> <EOL> xmin = image . width <EOL> ymin = image . height <EOL> for p in minRect : <EOL> if ( p [ <NUM_LIT:0> ] > xmax ) : <EOL> xmax = p [ <NUM_LIT:0> ] <EOL> if ( p [ <NUM_LIT:0> ] < xmin ) : <EOL> xmin = p [ <NUM_LIT:0> ] <EOL> if ( p [ <NUM_LIT:1> ] > ymax ) : <EOL> ymax = p [ <NUM_LIT:1> ] <EOL> if ( p [ <NUM_LIT:1> ] < ymin ) : <EOL> ymin = p [ <NUM_LIT:1> ] <EOL> width = ( xmax - xmin ) <EOL> height = ( ymax - ymin ) <EOL> at_x = xmin + ( width / <NUM_LIT:2> ) <EOL> at_y = ymin + ( height / <NUM_LIT:2> ) <EOL> points = [ ( xmin , ymin ) , ( xmin , ymax ) , ( xmax , ymax ) , ( xmax , ymin ) ] <EOL> super ( KeypointMatch , self ) . __init__ ( image , at_x , at_y , points ) <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . dl ( ) . line ( self . _minRect [ <NUM_LIT:0> ] , self . _minRect [ <NUM_LIT:1> ] , color , width ) <EOL> self . image . dl ( ) . line ( self . _minRect [ <NUM_LIT:1> ] , self . _minRect [ <NUM_LIT:2> ] , color , width ) <EOL> self . image . dl ( ) . line ( self . _minRect [ <NUM_LIT:2> ] , self . _minRect [ <NUM_LIT:3> ] , color , width ) <EOL> self . image . dl ( ) . line ( self . _minRect [ <NUM_LIT:3> ] , self . _minRect [ <NUM_LIT:0> ] , color , width ) <EOL> def drawRect ( self , color = Color . GREEN , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . dl ( ) . line ( self . points [ <NUM_LIT:0> ] , self . points [ <NUM_LIT:1> ] , color , width ) <EOL> self . image . dl ( ) . line ( self . points [ <NUM_LIT:1> ] , self . points [ <NUM_LIT:2> ] , color , width ) <EOL> self . image . dl ( ) . line ( self . points [ <NUM_LIT:2> ] , self . points [ <NUM_LIT:3> ] , color , width ) <EOL> self . image . dl ( ) . line ( self . points [ <NUM_LIT:3> ] , self . points [ <NUM_LIT:0> ] , color , width ) <EOL> def crop ( self ) : <EOL> """<STR_LIT>""" <EOL> tl = self . topLeftCorner ( ) <EOL> raw = self . image . crop ( tl [ <NUM_LIT:0> ] , tl [ <NUM_LIT:1> ] , self . width ( ) , self . height ( ) ) <EOL> return raw <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _avgColor is None ) : <EOL> TL = self . topLeftCorner ( ) <EOL> raw = self . image . crop ( TL [ <NUM_LIT:0> ] , TL [ <NUM_LIT:0> ] , self . width ( ) , self . height ( ) ) <EOL> mask = Image ( ( self . width ( ) , self . height ( ) ) ) <EOL> mask . dl ( ) . polygon ( self . _minRect , color = Color . WHITE , filled = TRUE ) <EOL> mask = mask . applyLayers ( ) <EOL> retVal = cv . Avg ( raw . getBitmap ( ) , mask . _getGrayscaleBitmap ( ) ) <EOL> self . _avgColor = retVal <EOL> else : <EOL> retVal = self . _avgColor <EOL> return retVal <EOL> def getMinRect ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _minRect <EOL> def getHomography ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _homography <EOL> """<STR_LIT>""" <EOL> class ShapeContextDescriptor ( Feature ) : <EOL> x = <NUM_LIT> <EOL> y = <NUM_LIT> <EOL> image = "<STR_LIT>" <EOL> points = [ ] <EOL> _minRect = [ ] <EOL> _avgColor = None <EOL> _descriptor = None <EOL> _sourceBlob = None <EOL> def __init__ ( self , image , point , descriptor , blob ) : <EOL> self . _descriptor = descriptor <EOL> self . _sourceBlob = blob <EOL> x = point [ <NUM_LIT:0> ] <EOL> y = point [ <NUM_LIT:1> ] <EOL> points = [ ( x - <NUM_LIT:1> , y - <NUM_LIT:1> ) , ( x + <NUM_LIT:1> , y - <NUM_LIT:1> ) , ( x + <NUM_LIT:1> , y + <NUM_LIT:1> ) , ( x - <NUM_LIT:1> , y + <NUM_LIT:1> ) ] <EOL> super ( ShapeContextDescriptor , self ) . __init__ ( image , x , y , points ) <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . image . dl ( ) . circle ( ( self . x , self . y ) , <NUM_LIT:3> , color , width ) <EOL> class ROI ( Feature ) : <EOL> """<STR_LIT>""" <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> w = <NUM_LIT:0> <EOL> h = <NUM_LIT:0> <EOL> xtl = <NUM_LIT:0> <EOL> ytl = <NUM_LIT:0> <EOL> points = [ ] <EOL> image = None <EOL> subFeatures = [ ] <EOL> _meanColor = None <EOL> def __init__ ( self , x , y = None , w = None , h = None , image = None ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( y , Image ) ) : <EOL> self . image = y <EOL> y = None <EOL> elif ( isinstance ( w , Image ) ) : <EOL> self . image = w <EOL> w = None <EOL> elif ( isinstance ( h , Image ) ) : <EOL> self . image = h <EOL> h = None <EOL> else : <EOL> self . image = image <EOL> if ( image is None and isinstance ( x , ( Feature , FeatureSet ) ) ) : <EOL> if ( isinstance ( x , Feature ) ) : <EOL> self . image = x . image <EOL> if ( isinstance ( x , FeatureSet ) and len ( x ) > <NUM_LIT:0> ) : <EOL> self . image = x [ <NUM_LIT:0> ] . image <EOL> if ( isinstance ( x , Feature ) ) : <EOL> self . subFeatures = FeatureSet ( [ x ] ) <EOL> elif ( isinstance ( x , ( list , tuple ) ) and len ( x ) > <NUM_LIT:0> and isinstance ( x , Feature ) ) : <EOL> self . subFeatures = FeatureSet ( x ) <EOL> result = self . _standardize ( x , y , w , h ) <EOL> if result is None : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return <EOL> self . _rebase ( result ) <EOL> def resize ( self , w , h = None , percentage = True ) : <EOL> """<STR_LIT>""" <EOL> if ( h is None and isinstance ( w , ( tuple , list ) ) ) : <EOL> h = w [ <NUM_LIT:1> ] <EOL> w = w [ <NUM_LIT:0> ] <EOL> if ( percentage ) : <EOL> if ( h is None ) : <EOL> h = w <EOL> nw = self . w * w <EOL> nh = self . h * h <EOL> nx = self . xtl + ( ( self . w - nw ) / <NUM_LIT> ) <EOL> ny = self . ytl + ( ( self . h - nh ) / <NUM_LIT> ) <EOL> self . _rebase ( [ nx , ny , nw , nh ] ) <EOL> else : <EOL> nw = self . w + w <EOL> nh = self . h + h <EOL> nx = self . xtl + ( ( self . w - nw ) / <NUM_LIT> ) <EOL> ny = self . ytl + ( ( self . h - nh ) / <NUM_LIT> ) <EOL> self . _rebase ( [ nx , ny , nw , nh ] ) <EOL> def overlaps ( self , otherROI ) : <EOL> for p in otherROI . points : <EOL> if ( p [ <NUM_LIT:0> ] <= self . maxX ( ) and p [ <NUM_LIT:0> ] >= self . minX ( ) and <EOL> p [ <NUM_LIT:1> ] <= self . maxY ( ) and p [ <NUM_LIT:1> ] >= self . minY ( ) ) : <EOL> return True <EOL> return False <EOL> def translate ( self , x = <NUM_LIT:0> , y = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if ( x == <NUM_LIT:0> and y == <NUM_LIT:0> ) : <EOL> return <EOL> if ( y == <NUM_LIT:0> and isinstance ( x , ( tuple , list ) ) ) : <EOL> y = x [ <NUM_LIT:1> ] <EOL> x = x [ <NUM_LIT:0> ] <EOL> if ( isinstance ( x , ( float , int ) ) and isinstance ( y , ( float , int ) ) ) : <EOL> self . _rebase ( [ self . xtl + x , self . ytl + y , self . w , self . h ] ) <EOL> def toXYWH ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . xtl , self . ytl , self . w , self . h ] <EOL> def toTLAndBR ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ( self . xtl , self . ytl ) , ( self . xtl + self . w , self . ytl + self . h ) ] <EOL> def toPoints ( self ) : <EOL> """<STR_LIT>""" <EOL> tl = ( self . xtl , self . ytl ) <EOL> tr = ( self . xtl + self . w , self . ytl ) <EOL> br = ( self . xtl + self . w , self . ytl + self . h ) <EOL> bl = ( self . xtl , self . ytl + self . h ) <EOL> return [ tl , tr , br , bl ] <EOL> def toUnitXYWH ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . image is None ) : <EOL> return None <EOL> srcw = float ( self . image . width ) <EOL> srch = float ( self . image . height ) <EOL> x , y , w , h = self . toXYWH ( ) <EOL> nx = <NUM_LIT:0> <EOL> ny = <NUM_LIT:0> <EOL> if ( x != <NUM_LIT:0> ) : <EOL> nx = x / srcw <EOL> if ( y != <NUM_LIT:0> ) : <EOL> ny = y / srch <EOL> return [ nx , ny , w / srcw , h / srch ] <EOL> def toUnitTLAndBR ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . image is None ) : <EOL> return None <EOL> srcw = float ( self . image . width ) <EOL> srch = float ( self . image . height ) <EOL> x , y , w , h = self . toXYWH ( ) <EOL> nx = <NUM_LIT:0> <EOL> ny = <NUM_LIT:0> <EOL> nw = w / srcw <EOL> nh = h / srch <EOL> if ( x != <NUM_LIT:0> ) : <EOL> nx = x / srcw <EOL> if ( y != <NUM_LIT:0> ) : <EOL> ny = y / srch <EOL> return [ ( nx , ny ) , ( nx + nw , ny + nh ) ] <EOL> def toUnitPoints ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . image is None ) : <EOL> return None <EOL> srcw = float ( self . image . width ) <EOL> srch = float ( self . image . height ) <EOL> pts = self . toPoints ( ) <EOL> retVal = [ ] <EOL> for p in pts : <EOL> x , y = p <EOL> if ( x != <NUM_LIT:0> ) : <EOL> x = x / srcw <EOL> if ( y != <NUM_LIT:0> ) : <EOL> y = y / srch <EOL> retVal . append ( ( x , y ) ) <EOL> return retVal <EOL> def CoordTransformX ( self , x , intype = "<STR_LIT>" , output = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if ( self . image is None ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( isinstance ( x , ( float , int ) ) ) : <EOL> x = [ x ] <EOL> intype = intype . upper ( ) <EOL> output = output . upper ( ) <EOL> if ( intype == output ) : <EOL> return x <EOL> return self . _transform ( x , self . image . width , self . w , self . xtl , intype , output ) <EOL> def CoordTransformY ( self , y , intype = "<STR_LIT>" , output = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if ( self . image is None ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( isinstance ( y , ( float , int ) ) ) : <EOL> y = [ y ] <EOL> intype = intype . upper ( ) <EOL> output = output . upper ( ) <EOL> if ( intype == output ) : <EOL> return y <EOL> return self . _transform ( y , self . image . height , self . h , self . ytl , intype , output ) <EOL> def CoordTransformPts ( self , pts , intype = "<STR_LIT>" , output = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if ( self . image is None ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( isinstance ( pts , tuple ) and len ( pts ) == <NUM_LIT:2> ) : <EOL> pts = [ pts ] <EOL> intype = intype . upper ( ) <EOL> output = output . upper ( ) <EOL> x = [ pt [ <NUM_LIT:0> ] for pt in pts ] <EOL> y = [ pt [ <NUM_LIT:1> ] for pt in pts ] <EOL> if ( intype == output ) : <EOL> return pts <EOL> x = self . _transform ( x , self . image . width , self . w , self . xtl , intype , output ) <EOL> y = self . _transform ( y , self . image . height , self . h , self . ytl , intype , output ) <EOL> return zip ( x , y ) <EOL> def _transform ( self , x , imgsz , roisz , offset , intype , output ) : <EOL> xtemp = [ ] <EOL> if ( intype == "<STR_LIT>" ) : <EOL> xtemp = [ xt / float ( imgsz ) for xt in x ] <EOL> elif ( intype == "<STR_LIT>" ) : <EOL> xtemp = [ ( xt + offset ) / float ( imgsz ) for xt in x ] <EOL> elif ( intype == "<STR_LIT>" ) : <EOL> xtemp = [ ( ( xt * roisz ) + offset ) / float ( imgsz ) for xt in x ] <EOL> elif ( intype == "<STR_LIT>" ) : <EOL> xtemp = x <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> retVal = [ ] <EOL> if ( output == "<STR_LIT>" ) : <EOL> retVal = [ int ( xt * imgsz ) for xt in xtemp ] <EOL> elif ( output == "<STR_LIT>" ) : <EOL> retVal = [ int ( ( xt * imgsz ) - offset ) for xt in xtemp ] <EOL> elif ( output == "<STR_LIT>" ) : <EOL> retVal = [ int ( ( ( xt * imgsz ) - offset ) / float ( roisz ) ) for xt in xtemp ] <EOL> elif ( output == "<STR_LIT>" ) : <EOL> retVal = xtemp <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> return retVal <EOL> def splitX ( self , x , unitVals = False , srcVals = False ) : <EOL> """<STR_LIT>""" <EOL> retVal = FeatureSet ( ) <EOL> if ( unitVals and srcVals ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( not isinstance ( x , ( list , tuple ) ) ) : <EOL> x = [ x ] <EOL> if unitVals : <EOL> x = self . CoordTransformX ( x , intype = "<STR_LIT>" , output = "<STR_LIT>" ) <EOL> elif not srcVals : <EOL> x = self . CoordTransformX ( x , intype = "<STR_LIT>" , output = "<STR_LIT>" ) <EOL> for xt in x : <EOL> if ( xt < self . xtl or xt > self . xtl + self . w ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> x . insert ( <NUM_LIT:0> , self . xtl ) <EOL> x . append ( self . xtl + self . w ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( x ) - <NUM_LIT:1> ) : <EOL> xstart = x [ i ] <EOL> xstop = x [ i + <NUM_LIT:1> ] <EOL> w = xstop - xstart <EOL> retVal . append ( ROI ( xstart , self . ytl , w , self . h , self . image ) ) <EOL> return retVal <EOL> def splitY ( self , y , unitVals = False , srcVals = False ) : <EOL> """<STR_LIT>""" <EOL> retVal = FeatureSet ( ) <EOL> if ( unitVals and srcVals ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( not isinstance ( y , ( list , tuple ) ) ) : <EOL> y = [ y ] <EOL> if unitVals : <EOL> y = self . CoordTransformY ( y , intype = "<STR_LIT>" , output = "<STR_LIT>" ) <EOL> elif not srcVals : <EOL> y = self . CoordTransformY ( y , intype = "<STR_LIT>" , output = "<STR_LIT>" ) <EOL> for yt in y : <EOL> if ( yt < self . ytl or yt > self . ytl + self . h ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> y . insert ( <NUM_LIT:0> , self . ytl ) <EOL> y . append ( self . ytl + self . h ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( y ) - <NUM_LIT:1> ) : <EOL> ystart = y [ i ] <EOL> ystop = y [ i + <NUM_LIT:1> ] <EOL> h = ystop - ystart <EOL> retVal . append ( ROI ( self . xtl , ystart , self . w , h , self . image ) ) <EOL> return retVal <EOL> def merge ( self , regions ) : <EOL> """<STR_LIT>""" <EOL> result = self . _standardize ( regions ) <EOL> if ( result is not None ) : <EOL> xo , yo , wo , ho = result <EOL> x = np . min ( [ xo , self . xtl ] ) <EOL> y = np . min ( [ yo , self . ytl ] ) <EOL> w = np . max ( [ self . xtl + self . w , xo + wo ] ) - x <EOL> h = np . max ( [ self . ytl + self . h , yo + ho ] ) - y <EOL> if ( self . image is not None ) : <EOL> x = np . clip ( x , <NUM_LIT:0> , self . image . width ) <EOL> y = np . clip ( y , <NUM_LIT:0> , self . image . height ) <EOL> w = np . clip ( w , <NUM_LIT:0> , self . image . width - x ) <EOL> h = np . clip ( h , <NUM_LIT:0> , self . image . height - y ) <EOL> self . _rebase ( [ x , y , w , h ] ) <EOL> if ( isinstance ( regions , ROI ) ) : <EOL> self . subFeatures += regions <EOL> elif ( isinstance ( regions , Feature ) ) : <EOL> self . subFeatures . append ( regions ) <EOL> elif ( isinstance ( regions , ( list , tuple ) ) ) : <EOL> if ( isinstance ( regions [ <NUM_LIT:0> ] , ROI ) ) : <EOL> for r in regions : <EOL> self . subFeatures += r . subFeatures <EOL> elif ( isinstance ( regions [ <NUM_LIT:0> ] , Feature ) ) : <EOL> for r in regions : <EOL> self . subFeatures . append ( r ) <EOL> def rebase ( self , x , y = None , w = None , h = None ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( x , Feature ) ) : <EOL> self . subFeatures . append ( x ) <EOL> elif ( isinstance ( x , ( list , tuple ) ) and len [ x ] > <NUM_LIT:0> and isinstance ( x , Feature ) ) : <EOL> self . subFeatures += list ( x ) <EOL> result = self . _standardize ( x , y , w , h ) <EOL> if result is None : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return <EOL> self . _rebase ( result ) <EOL> def draw ( self , color = Color . GREEN , width = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> x , y , w , h = self . toXYWH ( ) <EOL> self . image . drawRectangle ( x , y , w , h , width = width , color = color ) <EOL> def show ( self , color = Color . GREEN , width = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> self . draw ( color , width ) <EOL> self . image . show ( ) <EOL> def meanColor ( self ) : <EOL> """<STR_LIT>""" <EOL> x , y , w , h = self . toXYWH ( ) <EOL> return self . image . crop ( x , y , w , h ) . meanColor ( ) <EOL> def _rebase ( self , roi ) : <EOL> x , y , w , h = roi <EOL> self . _mMaxX = None <EOL> self . _mMaxY = None <EOL> self . _mMinX = None <EOL> self . _mMinY = None <EOL> self . _mWidth = None <EOL> self . _mHeight = None <EOL> self . mExtents = None <EOL> self . mBoundingBox = None <EOL> self . xtl = x <EOL> self . ytl = y <EOL> self . w = w <EOL> self . h = h <EOL> self . points = [ ( x , y ) , ( x + w , y ) , ( x , y + h ) , ( x + w , y + h ) ] <EOL> self . _updateExtents ( ) <EOL> def _standardize ( self , x , y = None , w = None , h = None ) : <EOL> if ( isinstance ( x , np . ndarray ) ) : <EOL> x = x . tolist ( ) <EOL> if ( isinstance ( y , np . ndarray ) ) : <EOL> y = y . tolist ( ) <EOL> if ( isinstance ( x , ( int , float ) ) and isinstance ( y , ( int , float ) ) and <EOL> isinstance ( w , ( int , float ) ) and isinstance ( h , ( int , float ) ) ) : <EOL> if ( self . image is not None ) : <EOL> x = np . clip ( x , <NUM_LIT:0> , self . image . width ) <EOL> y = np . clip ( y , <NUM_LIT:0> , self . image . height ) <EOL> w = np . clip ( w , <NUM_LIT:0> , self . image . width - x ) <EOL> h = np . clip ( h , <NUM_LIT:0> , self . image . height - y ) <EOL> return [ x , y , w , h ] <EOL> elif ( isinstance ( x , ROI ) ) : <EOL> x , y , w , h = x . toXYWH ( ) <EOL> elif ( isinstance ( x , FeatureSet ) and len ( x ) > <NUM_LIT:0> ) : <EOL> features = [ feat for feat in x if isinstance ( feat , Feature ) ] <EOL> xmax = np . max ( [ feat . maxX ( ) for feat in features ] ) <EOL> xmin = np . min ( [ feat . minX ( ) for feat in features ] ) <EOL> ymax = np . max ( [ feat . maxY ( ) for feat in features ] ) <EOL> ymin = np . min ( [ feat . minY ( ) for feat in features ] ) <EOL> x = xmin <EOL> y = ymin <EOL> w = xmax - xmin <EOL> h = ymax - ymin <EOL> elif ( isinstance ( x , Feature ) ) : <EOL> theFeature = x <EOL> x = theFeature . points [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> y = theFeature . points [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> w = theFeature . width ( ) <EOL> h = theFeature . height ( ) <EOL> elif ( isinstance ( x , ( tuple , list ) ) and len ( x ) == <NUM_LIT:4> and isinstance ( x [ <NUM_LIT:0> ] , ( int , long , float ) ) <EOL> and y == None and w == None and h == None ) : <EOL> x , y , w , h = x <EOL> elif ( isinstance ( x , ( list , tuple ) ) and <EOL> isinstance ( x [ <NUM_LIT:0> ] , ( list , tuple ) ) and <EOL> ( len ( x ) == <NUM_LIT:4> and len ( x [ <NUM_LIT:0> ] ) == <NUM_LIT:2> ) and <EOL> y == None and w == None and h == None ) : <EOL> if ( len ( x [ <NUM_LIT:0> ] ) == <NUM_LIT:2> and len ( x [ <NUM_LIT:1> ] ) == <NUM_LIT:2> and len ( x [ <NUM_LIT:2> ] ) == <NUM_LIT:2> and len ( x [ <NUM_LIT:3> ] ) == <NUM_LIT:2> ) : <EOL> xmax = np . max ( [ x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] ] ) <EOL> ymax = np . max ( [ x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , x [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] , x [ <NUM_LIT:3> ] [ <NUM_LIT:1> ] ] ) <EOL> xmin = np . min ( [ x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] ] ) <EOL> ymin = np . min ( [ x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , x [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] , x [ <NUM_LIT:3> ] [ <NUM_LIT:1> ] ] ) <EOL> x = xmin <EOL> y = ymin <EOL> w = xmax - xmin <EOL> h = ymax - ymin <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> elif ( isinstance ( x , ( tuple , list ) ) and <EOL> isinstance ( y , ( tuple , list ) ) and <EOL> len ( x ) > <NUM_LIT:4> and len ( y ) > <NUM_LIT:4> ) : <EOL> if ( isinstance ( x [ <NUM_LIT:0> ] , ( int , long , float ) ) and isinstance ( y [ <NUM_LIT:0> ] , ( int , long , float ) ) ) : <EOL> xmax = np . max ( x ) <EOL> ymax = np . max ( y ) <EOL> xmin = np . min ( x ) <EOL> ymin = np . min ( y ) <EOL> x = xmin <EOL> y = ymin <EOL> w = xmax - xmin <EOL> h = ymax - ymin <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> elif ( isinstance ( x , ( list , tuple ) ) and <EOL> len ( x ) > <NUM_LIT:4> and len ( x [ <NUM_LIT:0> ] ) == <NUM_LIT:2> and y == None and w == None and h == None ) : <EOL> if ( isinstance ( x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( int , long , float ) ) ) : <EOL> xs = [ pt [ <NUM_LIT:0> ] for pt in x ] <EOL> ys = [ pt [ <NUM_LIT:1> ] for pt in x ] <EOL> xmax = np . max ( xs ) <EOL> ymax = np . max ( ys ) <EOL> xmin = np . min ( xs ) <EOL> ymin = np . min ( ys ) <EOL> x = xmin <EOL> y = ymin <EOL> w = xmax - xmin <EOL> h = ymax - ymin <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> elif ( isinstance ( x , ( list , tuple ) ) and len ( x ) == <NUM_LIT:2> and isinstance ( x [ <NUM_LIT:0> ] , ( list , tuple ) ) and isinstance ( x [ <NUM_LIT:1> ] , ( list , tuple ) ) and y == None and w == None and h == None ) : <EOL> if ( len ( x [ <NUM_LIT:0> ] ) == <NUM_LIT:2> and len ( x [ <NUM_LIT:1> ] ) == <NUM_LIT:2> ) : <EOL> xt = np . min ( [ x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ] ) <EOL> yt = np . min ( [ x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ] ) <EOL> w = np . abs ( x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] - x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> h = np . abs ( x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] - x [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> x = xt <EOL> y = yt <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> elif ( isinstance ( x , ( tuple , list ) ) and isinstance ( y , ( tuple , list ) ) and w == None and h == None ) : <EOL> if ( len ( x ) == <NUM_LIT:2> and len ( y ) == <NUM_LIT:2> ) : <EOL> xt = np . min ( [ x [ <NUM_LIT:0> ] , y [ <NUM_LIT:0> ] ] ) <EOL> yt = np . min ( [ x [ <NUM_LIT:1> ] , y [ <NUM_LIT:1> ] ] ) <EOL> w = np . abs ( y [ <NUM_LIT:0> ] - x [ <NUM_LIT:0> ] ) <EOL> h = np . abs ( y [ <NUM_LIT:1> ] - x [ <NUM_LIT:1> ] ) <EOL> x = xt <EOL> y = yt <EOL> else : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( y == None or w == None or h == None ) : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> elif ( w <= <NUM_LIT:0> or h <= <NUM_LIT:0> ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> if ( self . image is not None ) : <EOL> x = np . clip ( x , <NUM_LIT:0> , self . image . width ) <EOL> y = np . clip ( y , <NUM_LIT:0> , self . image . height ) <EOL> w = np . clip ( w , <NUM_LIT:0> , self . image . width - x ) <EOL> h = np . clip ( h , <NUM_LIT:0> , self . image . height - y ) <EOL> return [ x , y , w , h ] <EOL> def crop ( self ) : <EOL> retVal = None <EOL> if ( self . image is not None ) : <EOL> retVal = self . image . crop ( self . xtl , self . ytl , self . w , self . h ) <EOL> return retVal </s>
