<s> import cv2 . cv as cv <EOL> import time <EOL> from pydmtx import DataMatrix <EOL> import numpy <EOL> import sys <EOL> import math <EOL> '''<STR_LIT>''' <EOL> def absnorm8 ( im , im8 ) : <EOL> """<STR_LIT>""" <EOL> ( minVal , maxVal , _ , _ ) = cv . MinMaxLoc ( im ) <EOL> cv . ConvertScaleAbs ( im , im8 , <NUM_LIT:255> / max ( abs ( minVal ) , abs ( maxVal ) ) , <NUM_LIT:0> ) <EOL> return im8 <EOL> font = cv . InitFont ( cv . CV_FONT_HERSHEY_SIMPLEX , <NUM_LIT:1.0> , <NUM_LIT:1.0> , thickness = <NUM_LIT:2> , lineType = cv . CV_AA ) <EOL> if <NUM_LIT:0> : <EOL> started = time . time ( ) <EOL> print dm_write . decode ( bg . width , bg . height , buffer ( bg . tostring ( ) ) , max_count = <NUM_LIT:1> , min_edge = <NUM_LIT:12> , max_edge = <NUM_LIT> , shape = DataMatrix . DmtxSymbol10x10 ) <EOL> print "<STR_LIT>" , time . time ( ) - started <EOL> class DmtxFinder : <EOL> def __init__ ( self ) : <EOL> self . cache = { } <EOL> self . dm = DataMatrix ( ) <EOL> def Cached ( self , name , rows , cols , type ) : <EOL> key = ( name , rows , cols ) <EOL> if not key in self . cache : <EOL> self . cache [ key ] = cv . CreateMat ( rows , cols , type ) <EOL> return self . cache [ key ] <EOL> def find0 ( self , img ) : <EOL> started = time . time ( ) <EOL> self . dm . decode ( img . width , <EOL> img . height , <EOL> buffer ( img . tostring ( ) ) , <EOL> max_count = <NUM_LIT:4> , <EOL> ) <EOL> print "<STR_LIT>" , time . time ( ) - started <EOL> found = { } <EOL> for i in range ( self . dm . count ( ) ) : <EOL> stats = dm_read . stats ( i + <NUM_LIT:1> ) <EOL> print stats <EOL> found [ stats [ <NUM_LIT:0> ] ] = stats [ <NUM_LIT:1> ] <EOL> return found <EOL> def find ( self , img ) : <EOL> started = time . time ( ) <EOL> gray = self . Cached ( '<STR_LIT>' , img . height , img . width , cv . CV_8UC1 ) <EOL> cv . CvtColor ( img , gray , cv . CV_BGR2GRAY ) <EOL> sobel = self . Cached ( '<STR_LIT>' , img . height , img . width , cv . CV_16SC1 ) <EOL> sobely = self . Cached ( '<STR_LIT>' , img . height , img . width , cv . CV_16SC1 ) <EOL> cv . Sobel ( gray , sobel , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> cv . Sobel ( gray , sobely , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> cv . Add ( sobel , sobely , sobel ) <EOL> sobel8 = self . Cached ( '<STR_LIT>' , sobel . height , sobel . width , cv . CV_8UC1 ) <EOL> absnorm8 ( sobel , sobel8 ) <EOL> cv . Threshold ( sobel8 , sobel8 , <NUM_LIT> , <NUM_LIT> , cv . CV_THRESH_BINARY ) <EOL> sobel_integral = self . Cached ( '<STR_LIT>' , img . height + <NUM_LIT:1> , img . width + <NUM_LIT:1> , cv . CV_32SC1 ) <EOL> cv . Integral ( sobel8 , sobel_integral ) <EOL> d = <NUM_LIT:16> <EOL> _x1y1 = cv . GetSubRect ( sobel_integral , ( <NUM_LIT:0> , <NUM_LIT:0> , sobel_integral . cols - d , sobel_integral . rows - d ) ) <EOL> _x1y2 = cv . GetSubRect ( sobel_integral , ( <NUM_LIT:0> , d , sobel_integral . cols - d , sobel_integral . rows - d ) ) <EOL> _x2y1 = cv . GetSubRect ( sobel_integral , ( d , <NUM_LIT:0> , sobel_integral . cols - d , sobel_integral . rows - d ) ) <EOL> _x2y2 = cv . GetSubRect ( sobel_integral , ( d , d , sobel_integral . cols - d , sobel_integral . rows - d ) ) <EOL> summation = cv . CloneMat ( _x2y2 ) <EOL> cv . Sub ( summation , _x1y2 , summation ) <EOL> cv . Sub ( summation , _x2y1 , summation ) <EOL> cv . Add ( summation , _x1y1 , summation ) <EOL> sum8 = self . Cached ( '<STR_LIT>' , summation . height , summation . width , cv . CV_8UC1 ) <EOL> absnorm8 ( summation , sum8 ) <EOL> cv . Threshold ( sum8 , sum8 , <NUM_LIT> , <NUM_LIT> , cv . CV_THRESH_BINARY ) <EOL> cv . ShowImage ( "<STR_LIT>" , sum8 ) <EOL> seq = cv . FindContours ( sum8 , cv . CreateMemStorage ( ) , cv . CV_RETR_EXTERNAL ) <EOL> subimg = cv . GetSubRect ( img , ( d / <NUM_LIT:2> , d / <NUM_LIT:2> , sum8 . cols , sum8 . rows ) ) <EOL> t_cull = time . time ( ) - started <EOL> seqs = [ ] <EOL> while seq : <EOL> seqs . append ( seq ) <EOL> seq = seq . h_next ( ) <EOL> started = time . time ( ) <EOL> found = { } <EOL> print '<STR_LIT>' , len ( seqs ) <EOL> for seq in seqs : <EOL> area = cv . ContourArea ( seq ) <EOL> if area > <NUM_LIT:1000> : <EOL> rect = cv . BoundingRect ( seq ) <EOL> edge = int ( ( <NUM_LIT> / <NUM_LIT> ) * math . sqrt ( area ) / <NUM_LIT:2> + <NUM_LIT:0.5> ) <EOL> candidate = cv . GetSubRect ( subimg , rect ) <EOL> sym = self . dm . decode ( candidate . width , <EOL> candidate . height , <EOL> buffer ( candidate . tostring ( ) ) , <EOL> max_count = <NUM_LIT:1> , <EOL> ) <EOL> if sym : <EOL> onscreen = [ ( d / <NUM_LIT:2> + rect [ <NUM_LIT:0> ] + x , d / <NUM_LIT:2> + rect [ <NUM_LIT:1> ] + y ) for ( x , y ) in self . dm . stats ( <NUM_LIT:1> ) [ <NUM_LIT:1> ] ] <EOL> found [ sym ] = onscreen <EOL> else : <EOL> print "<STR_LIT>" <EOL> t_brute = time . time ( ) - started <EOL> print "<STR_LIT>" , t_cull , "<STR_LIT>" , t_brute <EOL> return found <EOL> bg = cv . CreateMat ( <NUM_LIT> , <NUM_LIT> , cv . CV_8UC3 ) <EOL> cv . Set ( bg , cv . RGB ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> df = DmtxFinder ( ) <EOL> cv . NamedWindow ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> def mkdmtx ( msg ) : <EOL> dm_write = DataMatrix ( ) <EOL> dm_write . encode ( msg ) <EOL> pi = dm_write . image <EOL> cv_im = cv . CreateImageHeader ( pi . size , cv . IPL_DEPTH_8U , <NUM_LIT:3> ) <EOL> cv . SetData ( cv_im , pi . tostring ( ) ) <EOL> return cv_im <EOL> test = [ ] <EOL> y = <NUM_LIT:10> <EOL> for j in range ( <NUM_LIT:7> ) : <EOL> r = <NUM_LIT> + j * <NUM_LIT:4> <EOL> mr = r * math . sqrt ( <NUM_LIT:2> ) <EOL> y += mr * <NUM_LIT> <EOL> test += [ ( str ( deg ) + "<STR_LIT>" [ j ] , ( <NUM_LIT:50> + deg * <NUM_LIT:11> , y ) , math . pi * deg / <NUM_LIT> , r ) for deg in range ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:10> ) ] <EOL> for ( msg , ( x , y ) , angle , r ) in test : <EOL> map = cv . CreateMat ( <NUM_LIT:2> , <NUM_LIT:3> , cv . CV_32FC1 ) <EOL> corners = [ ( x + r * math . cos ( angle + th ) , y + r * math . sin ( angle + th ) ) for th in [ <NUM_LIT:0> , math . pi / <NUM_LIT:2> , math . pi , <NUM_LIT:3> * math . pi / <NUM_LIT:4> ] ] <EOL> src = mkdmtx ( msg ) <EOL> ( sx , sy ) = cv . GetSize ( src ) <EOL> cv . GetAffineTransform ( [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( sx , <NUM_LIT:0> ) , ( sx , sy ) ] , corners [ : <NUM_LIT:3> ] , map ) <EOL> temp = cv . CreateMat ( bg . rows , bg . cols , cv . CV_8UC3 ) <EOL> cv . Set ( temp , cv . RGB ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> cv . WarpAffine ( src , temp , map ) <EOL> cv . Or ( temp , bg , bg ) <EOL> cv . ShowImage ( "<STR_LIT>" , bg ) <EOL> scribble = cv . CloneMat ( bg ) <EOL> if <NUM_LIT:0> : <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> df . find ( bg ) <EOL> for ( sym , coords ) in df . find ( bg ) . items ( ) : <EOL> print sym <EOL> cv . PolyLine ( scribble , [ coords ] , <NUM_LIT:1> , cv . CV_RGB ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:1> , lineType = cv . CV_AA ) <EOL> Xs = [ x for ( x , y ) in coords ] <EOL> Ys = [ y for ( x , y ) in coords ] <EOL> where = ( ( min ( Xs ) + max ( Xs ) ) / <NUM_LIT:2> , max ( Ys ) - <NUM_LIT:50> ) <EOL> cv . PutText ( scribble , sym , where , font , cv . RGB ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) ) <EOL> cv . ShowImage ( "<STR_LIT>" , scribble ) <EOL> cv . WaitKey ( ) <EOL> cv . DestroyAllWindows ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> capture = cv . CaptureFromCAM ( <NUM_LIT:0> ) <EOL> while True : <EOL> img = cv . QueryFrame ( capture ) <EOL> cv . ShowImage ( "<STR_LIT>" , img ) <EOL> print df . find ( img ) <EOL> cv . WaitKey ( <NUM_LIT:6> ) </s>
