def hunt(imgx,typ ,db): global xtyp,tfile imgy = stdSize(imgx,typ) hcnt = 0 xtyp = typ typex = { # lim limh ms mx dz 'wt' : [130.0,150.0, 450, 4000, 60], 'h2o': [ 60.0, 80.0, 300, 1500, 35], 'fat': [ 9.0, 15.0, 500, 3000, 45] #300 } exlist = { 'wt' : [8, 2, 11 ], 'h2o' : [2, 8 ], 'fat' : [7, 2 ] } # 200 5 600 3000 works h20 txlist = { 'wt' : [ 160, 140 ], 'h2o' : [ 127 ,190, 160, 140 ], #160, 140, 205 ], 'fat' : [ 160, 190, 205 ] } lim = typex[typ][0] #limt[0] limh = typex[typ][1] ms = typex[typ][2] mx = typex[typ][3] dz = typex[typ][4] #imgy.save(Gd) for iex in exlist[typ]: if db: print '-----rdNumber ms', ms, ' mx', mx #, imgy.area() if db: print( ['erode image',iex]) for tx in txlist[typ]: #img = imgy.binarize(tx).invert() #img = imgy.erode(iex) img = imgy.copy() cvs(db,img) hcnt = hcnt + 1 # n is the pattern, rmx the max size, rms min size for iHunt logs n,rmx,rms = rdNumber(img,ex=iex, ms=ms ,dz=dz , mx = mx ,tval=tx ) if db: print ' Hunt 1 n ', hcnt, n # exit here nn = 0; j = -1 n.reverse() #nn = 100 * n[1] + 10 * n[2] + n[3] + n[4]/10.0 if db : print 'n' ,n #cpause(['nreverse',n],Gd) for j, xin in enumerate(n): nn = nn + xin * 10**(j-1) if (nn > lim) and nn < limh: iHunt.append((typ, hcnt, iex, tx, ms,rms, mx,rmx, nn )) print 'hunt rtn' ,nn cvs(db,img,typ) return(nn) print '>>>>>>>>>>>>>>>>>hunt ' ,typ, 'failed' return (0) # eliminate hunt2 for now
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 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