def xrdTyp(img,typ,db,trb): '''rdTyp finds the screen area of a digit based on the x y co-ordinates in the tables below. It passes this area to tMatch which returns a number ''' #img = stdSize(imgx,typ) img = erode(img,3) img = dilate(img,5) d = 150 #cvs(1,imgx,'rdTyp input') hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # define range of blue color in HSV h = 46; s = 20; v = 212 lower_blue = np.array([h,s,v]) #np.array([110,50,50]) upper_blue = np.array( [h+d,s+d,v+d]) #np.array([130,255,255]) # Threshold the HSV image to get only blue colors thresh = cv2.inRange(hsv, lower_blue, upper_blue) img = thresh.copy() Y= { # y1 y2 j limit 'wt' : [ 20, 320 , 4 ], 'fat': [ 35, 220 , 3 ], 'h2o': [ 30, 220 , 3 ] } XX = { # 100 10 one tenth 'wt' : [(95, 145) , (165,265) , (280,385 ), (400,510) ], 'fat': [(15, 50 ) , (65,125) , (140,200), (0,0) ], 'h2o': [(5, 55) , (65,120 ) , (135,185), (0,0) ] } n = [] h,w = img.shape #print 'id input w {} h {}'.format(w,h) # look at each digit in the image by xx position for j in range(0,Y[typ][2]): # loops across XX table above y1 = Y[typ][0]; x1 = XX[typ][j][0] y2 = Y[typ][1]; x2 = XX[typ][j][1] digit = img[y1:y2, x1:x2].copy() cv2.imwrite('digTest.png',digit) # save for future debug h,w = digit.shape #print 'xid input w {} h {}'.format(w,h) #cvs(db,digit,'input digit') # n.append( tMatch(digit,typ,db)) # interpret as a number if db: print 'n is ', n n.append(identifyN(digit,trb,0,db,typ) ) # train and test if db: print ' rdTyp n ', n # exit here nn = 0; j = -1 n.reverse() #nn = 100 * n[1] + 10 * n[2] + n[3] + n[4]/10.0 for j, xin in enumerate(n): nn = nn + xin * 10**(j-1) cvs(db,img,typ,50) return(nn)
def rdTyp(imgx,typ,db): '''rdTyp finds the screen area of a digit based on the x y co-ordinates in the tables below. It passes this area to tMatch which returns a number ''' img = stdSize(imgx,typ) d = 150 #cvs(1,imgx,'rdTyp input') hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # define range of blue color in HSV h = 46; s = 20; v = 212 lower_blue = np.array([h,s,v]) #np.array([110,50,50]) upper_blue = np.array( [h+d,s+d,v+d]) #np.array([130,255,255]) # Threshold the HSV image to get only blue colors thresh = cv2.inRange(hsv, lower_blue, upper_blue) img = thresh.copy() Y= { # y1 y2 j limit 'wt' : [ 45, 367 , 4 ], 'fat': [ 84, 344 , 3 ], 'h2o': [ 35, 230 , 3 ] } XX = { # 100 10 one tenth 'wt' : [(152, 200) , (240,360) , (415,530), (580,720) ], 'fat': [(20, 80) , (100,220) , (250,350), (0,0) ], 'h2o': [(10, 90) , (110,210) , (235,320), (0,0) ] } n = [] # look at each digit in the image by xx position for j in range(0,Y[typ][2]): # loops across XX table above y1 =Y[typ][0]; y2 = Y[typ][1]; x1 =XX[typ][j][0]; x2=XX[typ][j][1] digit = img[y1:y2, x1:x2].copy() cv2.imwrite('digTest.png',digit) # save for future debug n.append( tMatch(digit,typ,db)) # interpret as a number if db: print ' rdTyp n ', n # exit here nn = 0; j = -1 n.reverse() #nn = 100 * n[1] + 10 * n[2] + n[3] + n[4]/10.0 for j, xin in enumerate(n): nn = nn + xin * 10**(j-1) cvs(db,img,typ,5000) return(nn) # the decoded number
def evalGame(ROI,fd,rn,db): ''' we obtain the ROI region of interest from Part or as input from the last screen processed by Maintest. We look for blob in the ROI and evaluate them by matching to specific areas in the img. ''' h,w = ROI.shape[:2] # sROI = cv2.resize(ROI,(3*w,3*h)) # this may not be a good idea cxcopy = ROI.copy() # copy to mark up for display # but we did it in CaptureDigits so .. . cmask,cnt,hier = findNumbers(ROI,db) # incorporate hier check if db: print hier.shape, len(cnt) Scon = sorted(cnt, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) cvs(db,cxcopy,'cxcopy',3) lx = [] ; ly = [] ; j = -1 for i, f in enumerate (Scon): area = cv2.contourArea(f) x,y,w,h = cv2.boundingRect(f) cv2.drawContours(cxcopy,[f],0,(255,255,255),1) if db: print ' x {} contour area {}'.format(x,area) if (area > 18 and h >10 or area > 19 and w <9 ): # 18 ok except red panel needs 16 j = j + 1 possible = cmask[y:y+h, x:x+w].copy() if j == len(rn): j = 1; lbx = -1 else: lbx = rn[j] if db: print ' x {} possible n {} w {} h {}'.format(x,lbx ,w,h) parm.lst = identifyN(possible,lbx,db) n = parm.lst[1] cv2.drawContours(cxcopy,[f],0,(0,255,0),1) # draw after capture if n <> -1: lx.append((x,n)) ly.append(n) # approximate order fd.write('{} \n'.format(parm.lst )) if db: print '>>>match evaluate {} <<<'.format(ly) cvs(db,cxcopy,'cxcopy',3) lx = sorted(lx,key = lambda (x,n):x ) lx = [b for (a,b) in lx] return lx # list of numbers in the panel
def __init__(self,fname,db,trb): global tmpf self.img = cv2.imread(fname) if not fname == "input.png": cv2.imwrite("input.png",self.img) self.cropped = Cropx(db,self.img) # crop image [self.wt, self.fat, self.h2o] = Part(self,self.cropped,db) # partition cropped image cv2.imwrite("fatTest.png", self.fat) cv2.imwrite("h2oTest.png", self.h2o) cv2.imwrite("wtTest.png", self.wt) # hunt for the appropriate number in each partitioned image if db: cvs(db,self.wt,'weight',0) self.nwt = xrdTyp(self.wt,'wt',db,trb) self.nfat = xrdTyp(self.fat,'fat',db,trb) self.nh2o = xrdTyp(self.h2o,'h2o',db,trb) #cvs(db,self.cropped,'input',0) return(None)
def rdWeight(imgx,typ,db): img = stdSize(imgx,typ) imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) img = thresh.copy() cvs(1,img,'x') y1 = 45; y2 = y1 + 322; x1 = 152; x2=200 wtx00 = img[y1:y2, x1:x2].copy() w100 = tMatch(wtx00,typ,db) # search for a number # cvs(1,wtx00,'y') y1 = 45; y2 = y1 + 322; x1 = 240; x2=360 wtx01 = img[y1:y2, x1:x2].copy() # cvs(1,wtx01,'y') w010 = tMatch(wtx01,typ,db) # search for a number y1 = 45; y2 = y1 + 322; x1 = 405; x2=535 wtx02 = img[y1:y2, x1:x2].copy() cvs(0,wtx02,'yy') w001 = tMatch(wtx02,typ,db) y1 = 45; y2 = y1 + 322; x1 = 580; x2=720 wtx03 = img[y1:y2, x1:x2].copy() cvs(0,wtx03,'yy') wp1 = tMatch(wtx03,typ,db) print w100, w010, w001, wp1 n = 100 * w100 + 10 * w010 + w001 + float(wp1 / 10.0) print n
def findBlobs(imx,ms,mx,erd,db,tval=127): Erd = erd Drd = int(Erd/2) print 'This is find blobs 1.0 ms {} mx {} erd {} drd {}'.format( ms, mx , Erd ,Drd) imgray = cv2.cvtColor(imx,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,tval,255,0) thresh = erode(thresh,Erd) thresh = dilate(thresh,Drd) #print 'db findBlobs' , db cvs(db,thresh) cmask = thresh.copy() im3,cnt, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # note lamda is expression for leftmost extreme of the contour scon = sorted(cnt, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) rvl = [] for con in scon: area = cv2.contourArea(con) #if db: print area, cnt_len = cv2.arcLength(con, True) cntp = cv2.approxPolyDP(con, 0.02*cnt_len, True) # print len(cntp) # look for suitable contours x,y,w,h = cv2.boundingRect(con) asp = abs( w - h ) #if db: print 'abs|w-h| {} w {} h {} '.format(asp,w,h) if ( len(cntp) > 3 # at least 4 and asp > .1 # ie not round or square and area > ms and area < mx ): # note ( ... ) for implied continuation rvl.append(con) cv2.drawContours(imx,[con], 0, (0,255,255), 2) # yellow else: cv2.drawContours(imx,[con], 0, (0,0,255), 2) # red # if db:cvs(db,imx,t=0) return (rvl,cmask)
def identifyN(p, lb=0, db=0, typ="x"): """ identify creates digit descriptor vectors by counting the contours under a set of masks applied to the digit being examined. Masks are applied by setting the non mask area of the image to zero, black. Other statistics collected are not used but could be input to some future machine learning algorithm""" d = np.zeros_like(p) dx = d.copy() # nothing to see here d = p.copy() h, w = d.shape # print 'id input w {} h {}'.format(w,h) im2 = d.copy() t0 = np.sum(im2) / 255 # cvs(db,d,'digit ' ) db = 1 d = im2.copy() d[:, w / 3 :] = 0 # left 1/3 same as bitwise and L = np.sum(d) / 255 # cvs(db, d, "digit Left ") msk = [] # initialize mask msk = pxCount(d, msk) d = im2.copy() # middle vert third d[:, : w / 3] = 0 # - left 1/3 d[:, 2 * w / 3 :] = 0 # - right 1/3 Mv3 = np.sum(d) / 255 cvs(db, d, "digit v Mid3") msk = pxCount(d, msk) # if typ == "wt": msk[1] = 9 d = im2.copy() d[:, : 2 * w / 3] = 0 R = np.sum(d) / 255 # right 1/3 cvs(db, d, "digit Right ") msk = pxCount(d, msk) d = im2.copy() # upper 1/3 d[h / 3 :, :] = 0 T = np.sum(d) / 255 cvs(db, d, "digit Top ") msk = pxCount(d, msk) d = im2.copy() # middle horiz third d[: h / 3, :] = 0 # - upper 1/3 d[2 * h / 3 :, :] = 0 # - lower 1/3 M3 = np.sum(d) / 255 cvs(db, d, " Mid 1/3") msk = pxCount(d, msk) d = im2.copy() # lower 1/3 d[: 2 * h / 3, :] = 0 # zero upper 2/3 B = np.sum(d) / 255 cvs(1, d, "digit Bottom ") mm = pxCount(d, msk) if 1: print "elif mm == {}: n = {} # {} ".format(mm, lb, typ) # statistics returned as descriptor S = max(L, R, T, B) - min(L, R, T, B) LR = abs(L - R) TB = abs(T - B) # identify the input # L m R T m B # left right top bottom horiz vertical if typ == "wt": # if 1 == 2: pass if mm == [2, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 3 # wt elif mm == [2, 9, 3, 1, 4, 2]: n = 0 # wt elif mm == [2, 9, 2, 1, 4, 1]: n = 0 # wt elif mm == [2, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 4 # wt elif mm == [2, 9, 3, 1, 4, 2]: n = 0 # wt elif mm == [2, 9, 2, 1, 4, 1]: n = 0 # wt elif mm == [1, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 4 # wt elif mm == [2, 9, 3, 1, 4, 2]: n = 0 # wt elif mm == [1, 9, 1, 1, 1, 1]: n = 8 # wt elif mm == [1, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 2, 2, 2, 1]: n = 4 # wt elif mm == [3, 9, 3, 1, 4, 3]: n = 0 # wt elif mm == [1, 9, 1, 1, 1, 1]: n = 8 # wt elif mm == [1, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 4 # wt elif mm == [2, 9, 3, 1, 4, 2]: n = 0 # wt elif mm == [1, 9, 2, 1, 1, 1]: n = 2 # wt elif mm == [1, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 4 # wt elif mm == [2, 9, 3, 1, 4, 2]: n = 0 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 4 # wt elif mm == [2, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [1, 9, 2, 2, 2, 1]: n = 4 # wt elif mm == [2, 9, 3, 1, 4, 2]: n = 0 # wt elif mm == [1, 9, 1, 1, 1, 1]: n = 2 # wt elif mm == [1, 9, 2, 1, 2, 1]: n = 1 # wt elif mm == [2, 9, 1, 1, 1, 1]: n = 3 # wt elif mm == [1, 9, 1, 2, 1, 1]: n = 4 # wt elif mm == [1, 9, 2, 1, 1, 2]: n = 8 # wt elif mm == [1, 9, 1, 1, 1, 1]: n = 8 # wt else: n = -1 elif typ in ["fat", "h2o"]: if 1 == 2: pass elif mm == [2, 2, 3, 1, 4, 2]: n = 0 elif mm == [2, 3, 3, 1, 4, 2]: n = 0 elif mm == [2, 2, 2, 1, 2, 1]: n = 0 elif mm == [2, 2, 3, 1, 4, 2]: n = 0 elif mm == [3, 2, 2, 1, 4, 2]: n = 0 elif mm == [2, 2, 3, 1, 4, 2]: n = 0 elif mm == [2, 3, 3, 1, 4, 2]: n = 0 elif mm == [3, 2, 2, 1, 4, 2]: n = 0 elif mm == [2, 3, 3, 1, 4, 2]: n = 0 elif mm == [2, 2, 2, 1, 2, 1]: n = 1 elif mm == [2, 2, 1, 1, 2, 1]: n = 1 elif mm == [1, 2, 2, 1, 2, 1]: n = 1 elif mm == [2, 2, 2, 1, 2, 1]: n = 1 elif mm == [1, 0, 2, 1, 2, 2]: n = 1 elif mm == [2, 2, 2, 1, 2, 1]: n = 1 elif mm == [1, 2, 2, 1, 2, 1]: n = 1 elif mm == [0, 1, 3, 1, 2, 2]: n = 1 elif mm == [0, 1, 2, 1, 2, 1]: n = 1 elif mm == [1, 4, 1, 1, 1, 1]: n = 2 elif mm == [2, 3, 2, 1, 1, 2]: n = 2 elif mm == [1, 3, 3, 1, 1, 2]: n = 2 elif mm == [2, 3, 3, 1, 1, 2]: n = 2 elif mm == [1, 3, 3, 1, 1, 2]: n = 2 elif mm == [3, 2, 1, 1, 1, 1]: n = 3 elif mm == [3, 2, 2, 1, 1, 2]: n = 3 elif mm == [2, 2, 1, 1, 1, 1]: n = 3 elif mm == [2, 3, 1, 1, 1, 2]: n = 3 elif mm == [1, 1, 2, 2, 2, 1]: n = 4 elif mm == [2, 1, 2, 1, 1, 1]: n = 5 elif mm == [1, 1, 2, 1, 1, 1]: n = 6 elif mm == [0, 3, 2, 1, 2, 1]: n = 7 elif mm == [0, 2, 2, 1, 2, 1]: n = 7 elif mm == [1, 1, 2, 1, 2, 2]: n = 7 elif mm == [1, 3, 2, 1, 2, 1]: n = 7 elif mm == [0, 3, 2, 1, 2, 1]: n = 7 elif mm == [0, 2, 2, 1, 2, 1]: n = 7 elif mm == [0, 3, 2, 1, 2, 1]: n = 7 elif mm == [1, 3, 2, 1, 2, 1]: n = 7 elif mm == [0, 3, 2, 1, 2, 1]: n = 7 elif mm == [1, 1, 1, 1, 1, 1]: n = 8 elif mm == [3, 2, 1, 1, 1, 2]: n = 9 else: n = -1 else: n = -1 lb = int(lb) descriptor.lst = [lb, n, t0, L, R, T, B, S, LR, TB, M3, Mv3] d = d - d return n
lb = int(lb) descriptor.lst = [lb, n, t0, L, R, T, B, S, LR, TB, M3, Mv3] d = d - d return n def prtTable(digits, labels): db = 1 dts = np.zeros((10, descriptor.lstN), dtype="int32") for d, lb in zip(digits, labels): # print ' ',descriptor.head ## if lb in range(10): n = identifyN(d, lb, db) # print 'input {} id n {} '.format(lb,n) if __name__ == "__main__": db = 1 for typ in ["wt", "h2o", "fat"]: fwt = typ + "Test.png" imgx = cv2.imread(fwt) cvs(db, imgx, "DigitStat") print xrdTyp(imgx, typ, 0) # identifyN(imgx,0,1) cvd() ## print __doc__ ## digits, labels = cwload_digits_lst("c:\\Train\\*.png" ) ## prtTable(digits,labels) ## cvd()
cv2.imwrite("wtTest.png", self.wt) # hunt for the appropriate number in each partitioned image if db: cvs(db,self.wt,'weight',0) self.nwt = xrdTyp(self.wt,'wt',db,trb) self.nfat = xrdTyp(self.fat,'fat',db,trb) self.nh2o = xrdTyp(self.h2o,'h2o',db,trb) #cvs(db,self.cropped,'input',0) return(None) if __name__ == '__main__': global db,trb db = 0 filename = 'ScTest/T2015Doris/20150130_093919.jpg' filename = "input.png" print 'weight.py', filename s = Scale(filename,db,trb) print s.name, 'wt is' ,s.nwt,'iHunt global' for i in iHunt: print i cvs(db,s.cropped ,'test image display',0) # test image display cvd() idGen(0,trb) # close file and cleanup trb.close()
print 'finishing idGen' line = ''' \treturn n if __name__ == '__main__': db = 1 print idRule(['wt', 3,1,1]) ''' trb.write(line) if __name__ == '__main__': db = 1 trb = open('0idGRule.py','w') ## open file fo r write idGen(1,trb) # open file for typ in ['fat']:#,'wt' , 'fat']: fwt = typ + 'Test.png' imgx = cv2.imread(fwt ) print xrdTyp(imgx,typ,db,trb ) cvs(db,imgx,'DigitStat') #identifyN(imgx,0,1) #cvs(db,imgx,'Digitstat') idGen(0,trb) # close file and cleanup trb.close() cvd()
h,w = img.shape[:2] print 'image h {} w {}'.format(h,w) y1 = 0 ; y2 = int(.1 * h) x1 = int (.75 * w) ; x2 = int(w) a = img[ y1:y2, x1:x2 ].copy() return(h,w,a) if __name__ == '__main__': global db db = 1 manifest = 'testFile.txt' mdata = open(manifest,'w') path = 'pics/sc*' #os.getcwd() #get the current directory files = glob.glob( path) for fx in files: img = cv2.imread(fx) h,w,ROI = Part(img,db) cvs(1, ROI, 'input') #cv2.imshow('input',ROI) # cvs(db,ROI) usr = raw_input('what numbers?') print fx , 'has', usr if usr == 'q': break print >> mdata, usr , fx # create a list of user anotated filenames cvd() mdata.close() cvd()
def identifyN(p,lb=0,db=0): ''' identify creates digit descriptor vectors by counting the contours under a set of masks applied to the digit being examined. Masks are applied by setting the non mask area of the image to zero, black. Other statistics collected are not used but could be input to some future machine learning algorithm''' d = np.zeros_like(p) d = p.copy() h,w = d.shape #qqprint 'id input w {} h {}'.format(w,h) im2 = d.copy() t0 = np.sum(im2) / 255 #cvs(db,d,'digit ' ) d = im2.copy() d[:, w/3:] = 0 # left 1/3 same as bitwise and L = np.sum(d)/255 # cvs(db,d,'digit Left ' ) msk = [] # initialize mask msk = pxCount(d,msk) d = im2.copy() # middle vert third d[ :, :w/3 ] = 0 # - left 1/3 d[ :, 2*w/3: ] = 0 # - right 1/3 Mv3 = np.sum(d)/255 cvs(db,d,'digit v Mid3' ) msk = pxCount(d,msk ) d = im2.copy() d[:, :2*w/3] = 0 R = np.sum(d)/255 # right 1/3 cvs(db,d,'digit Right ' ) msk = pxCount(d,msk) d = im2.copy() # upper 1/3 d[ h/3:, : ] = 0 T = np.sum(d)/255 cvs(db,d,'digit Top ' ) msk = pxCount(d,msk) d = im2.copy() # middle horiz third d[ :h/3, : ] = 0 # - upper 1/3 d[ 2*h/3:, : ] = 0 # - lower 1/3 M3 = np.sum(d)/255 cvs(db,d,' Mid 1/3' ) msk = pxCount(d,msk) d = im2.copy() # lower 1/3 d[ :2*h/3, : ] = 0 # zero upper 2/3 B = np.sum(d)/255 cvs(1,d,'digit Bottom ' ) msk = pxCount(d,msk) if db: print 'elif mm == {}: n = {}'.format( mm, lb) # statistics returned as descriptor S = ( max(L,R,T,B) - min(L,R,T,B)) LR = abs(L-R) TB = abs(T-B) # identify the input # L m R T m B # left right top bottom horiz vertical if mm == [9,9,9,9,9,9,9,9]: n = 0 elif mm == [0, 2, 2, 1, 2, 1]: n = 1 elif mm == [3, 2, 2, 1, 1, 2]: n = 3 elif mm == [2, 1, 2, 1, 1, 2]: n = 8 elif mm == [0, 2, 2, 1, 2, 1]: n = 1 elif mm == [1, 3, 4, 2, 7, 1]: n = 4 elif mm == [3, 4, 6, 3, 4, 3]: n = 6 elif mm == [2, 4, 2, 2, 4, 1]: n = 0 elif mm == [1, 2, 2, 1, 2, 1]: n = 7 elif mm == [2, 3, 2, 1, 4, 1]: n = 0 elif mm == [2, 3, 2, 1, 4, 1]: n = 0 else :n = -1 lb = int(lb) descriptor.lst = [lb,n,t0,L,R,T,B,S,LR,TB,M3,Mv3 ] d = d - d return n
if __name__ == '__main__': db = False tst = 'wt' imgx = cv2.imread(tst +'Test.png') #imgx = cv2.imread(fn) img = stdSize(imgx,tst) #cv2.resize(imgx, (1040,410)) # srt = img.copy() h,w = img.shape[:2] #cvs(db,img) ms = 325; mx = 10000; erd =12; tx=160 (cnt,cmask) = findBlobs( img,ms,mx,erd,db,tx) # will modify img to show cnt cnt = sorted(cnt, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) print ' input is {} width {} height {} ms {} mx {} tval {} '\ .format(tst,w, h,ms,mx,tx) x,y,w,h = boundsBlob(cnt) print (x,y,w,h) tl = (x-10,y-10) ; br = ( x + w+10, y+ h+10) # 10 for more room cv2.rectangle(img,(tl),(br),(255,255,255),5) # white rectangle for i, f in enumerate (cnt): cv2.drawContours(img,[f],0,(255,0,0),2) ## x,y,w,h = cv2.boundingRect(f) ## a = cv2.contourArea(f) ## asp = round(abs( 1.00 - float(w)/float(h) ),2) ## print 'x {} y {}\tw {}\th {}\ta {}\tasp {} '.format( x, y , w, h,a,asp) cvs(1,cmask) cvs(1,img,t=0) cvd()
(cnt,cmask) = findBlobs( img,ms,mx,erd,db,tx) # will modify img to show cnt cnt = sorted(cnt, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) print ' input is {} width {} height {} ms {} mx {} tval {} '\ .format(tst,w, h,ms,mx,tx) print len(cnt) ,'blobs were found shape is list' ## x,y,w,h = cv2.boundingRect(cnt) # needs single image or array ## print 'boundingRec {} {} {} {}'.format( x,y,w,h) a,b = analBlob2(cnt) print a,b, 'analBlob2' x,y, w,h = analBlob(cnt) tl = (x-10,y-10) ; br = ( x + w+10, y+ h+10) # 10 for more room cv2.rectangle(cmask,(tl),(br),(255,255,255),5) # white rectangle ## print 'min x {}'.format(minx) for i, f in enumerate (cnt): #print '{} cnt {}'.format(i,f) cv2.drawContours(img,[f],0,(255,0,0),1) #print 'contour array f[0] ', f[0] , x,y,w,h = cv2.boundingRect(f) a = cv2.contourArea(f) asp = round(abs( 1.00 - float(w)/float(h) ),2) print 'x {} y {}\tw {}\th {}\ta {}\tasp {} '.format( x, y , w, h,a,asp) cv2.rectangle(img,(tl),(br),(255,255,255),5) cvs(1,img,'img 3',3) cvd()
def tMatch(im1,typ,db): if db: cvs(db,im1) path = ('Train/pxl/{}P*.png'.format( typ ) ) #os.getcwd() #print path files = glob.glob(path) rtn = ['0','0','0','0','0','0','0'] for n,f in enumerate(sorted(files)): # read templates from Train if db: print 'Tmatch', f[9:-4], im2 = cv2.imread(f,0) w1, h1 = im1.shape[::-1] # image w2, h2 = im2.shape[::-1] # panel if db: print 'image w {} h {} panel w {} h {}'.format(w1,h1,w2,h2) if w1-w2 < -30: yt1 =int(.5*(h2-h1)); yt2 = yt1 + h1 ; xt1 = 0 ; xt2= w1 if db: print 'crop ',yt1, yt2, xt1, xt2 om2 = im2[yt1:yt2, xt1:xt2].copy() # panel crop else: om2 = cv2.resize(im2, ( w1, h1 ) ) # panel resize w1, h1 = im1.shape[::-1] # image w2, h2 = om2.shape[::-1] # panel if db: print 'om1 w {}, h {} im2 w {} h {}'.format(w1, h1,w2, h2) res = cv2.bitwise_and(im1,om2) pxlcnt = np.sum(res) / 255 # count of pixels if db: print pxlcnt, 'count of pixels' cvs(db,res,'z') PL= { # pxl limit 'wt' : [ 300 ], 'fat': [ 300 ], 'h2o': [ 200 ] } if pxlcnt > PL[typ][0] : rtn[n] = '1' drtn = ''.join(rtn) ''' ptrn translates the results of the pattern analysis into numbers ''' ptrn = { '1110111' : 0, '1100000' : 1, # leading 1 in weight '0000011' : 1, # internal 1 '1011101' : 2, '0011111' : 3, '0101011' : 4, '0111110' : 5, '1111110' : 6, '0000111' : 7, '1111111' : 8, '0111111' : 9 } try: n = ptrn[drtn] print 'number {} ptrn {} '.format(n , drtn) return(n) except KeyError: print 'bad pattern here ' , drtn return 0
def identifyN(p,lb=0,db=0): msk = [] def pxCount(d,lb = 0,s=0 ): global msk jk,cnt4d, hier = cv2.findContours(d,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) m = len(cnt4d) ## if m > 0: ## for cn in cnt4d: ## print cv2.contourArea(cn) if s == 1: msk = [] msk.append(m) return msk d = np.zeros_like(p) d = p.copy() h,w = d.shape im2 = d.copy() t0 = np.sum(im2) / 255 cvs(db,d,'digit ',5) d[:, w/2:] = 0 # same as bitwise and L = np.sum(d)/255 cvs(db,d,'digit ',5) pxCount(d,lb,1) d = im2.copy() d[:, :w/2] = 0 R = np.sum(d)/255 cvs(db,d,'digit ',5) pxCount(d) d = im2.copy() # upper half d[ h/2:, : ] = 0 T = np.sum(d)/255 cvs(db,d,'digit ',5) pxCount(d) d = im2.copy() # lower half d[ :h/2, : ] = 0 # zero upper half B = np.sum(d)/255 cvs(db,d,'digit ',5) pxCount(d) d = im2.copy() # middle horiz third d[ :h/3, : ] = 0 # - upper 1/3 d[ 2*h/3:, : ] = 0 # - lower 1/3 M3 = np.sum(d)/255 cvs(db,d,'digit h Mid 1/3',5) pxCount(d ) d = im2.copy() # middle vert third d[ :, :w/3 ] = 0 # - left 1/3 d[ :, 2*w/3: ] = 0 # - right 1/3 Mv3 = np.sum(d)/255 cvs(db,d,'digit v Mid3',5) mm = pxCount(d ,lb,2) if db: print 'final mask for {} {}'.format(lb,mm) # statistics S = ( max(L,R,T,B) - min(L,R,T,B)) LR = abs(L-R) TB = abs(T-B) # identify the input if (t0 > 75 # L R T B h v # left right top bottom horiz vertical and mm == [1, 1, 1, 1, 2, 2]): n = 0 elif mm == [1, 1, 1, 1, 2, 1]: n = 1 elif mm == [0, 1, 1, 1, 2, 1]: n = 1 # hack for bad threshold elif mm == [1, 1, 1, 1, 1, 1] and h == 11 : n = 1 elif mm == [2, 2, 1, 1, 2, 3]: n = 2 elif mm == [1, 2, 1, 1, 1, 3] :n = 2 # noisy 2 elif mm == [3, 1, 1, 1, 1, 3]: n = 3 elif mm == [3, 1, 1, 1, 2, 3]: n = 3 # noisy 3 elif mm == [1, 1, 1, 1, 2, 2]: n = 4 elif mm == [1, 1, 1, 1, 1, 2]: n = 4 # noisy 4 elif mm == [2, 2, 1, 2, 1, 3]: n = 5 elif mm == [1, 2, 1, 2, 1, 3]: n = 6 elif mm == [2, 1, 1, 1, 1, 2]: n = 7 elif mm == [1, 1, 2, 2, 1, 3]: n = 8 elif mm == [2, 1, 1, 1, 1, 3]: n = 9 elif mm == [1, 1, 1, 2, 1, 3]: n = 9 # noisy 9 else :n = -1 lb = int(lb) parm.lst = [lb,n,t0,L,R,T,B,S,LR,TB,M3,Mv3 ] d = d - d return parm.lst
rn1 = '145283595148' rn2 = '3021312168188' rn3 = '177438101129' rn4 = '10872676794' rn5 = '99201918' rn6 = '3242604254' rn7 = '14753612200200' rn8 = '691482738' dfile = 'digits.txt' fd = open(dfile,'w') for f,rn in zip( [fx5 ],[rn5 ]): #,fx2,fx3,fx4] : #,fx2,fx3,fx4]: h,w,ROI = Part(f,db) cvs(db,ROI,'roi',3) listx = evalGame(ROI,fd,rn,db) # ::-1 is string reverse print 'eval game Harr ',listx print ' rn value was ',map(int,list(rn)) #print 'eval game MatchShape',listy print f fd.close() printsort() cvd()