def searchOthersComponents(G,objects,numbers,img,label,av,med): t = img.shape[0]*img.shape[1] img1 = copy.deepcopy(img) arrow = [] text = [] initial = [] for i in range(1,label): if((np.where(img==i))[0].size != 0): if i not in numbers: j,b = us.crop(img,i) j = np.where(j!=i,255,j) classifierArrowOrText(j,i,av,med,t,numbers,arrow,text,initial) if(len(arrow) > 0): addArrows(G, arrow, img1) if(len(initial) >0): addInitial(G,initial,img1) img1, numbers,label,index = completImage(G,img1,numbers,label) for i in index: objects.append(('c',(i[1],i[2]),i[0],[])) list, objs,nimg,label2 = searchLabelsArrows(G,objects,numbers,img1,initial) numbers2 = [] list2 =[] for l in list: list2.append(l[1]) for o in objs: if(o[2] > 0 and o[2] not in list2): numbers2.append(o[2]) newI = np.ones_like(nimg)*255 newI = np.where(nimg==o[2],0,newI) saveText(G,list,list2,objs,nimg,numbers2,index)
def checkArrow(G,img, x,y,obj1, obj2): nobj1 = str(obj1[1][0])+str(obj1[1][1]) nobj2 = str(obj2[1][0])+str(obj2[1][1]) r1 = gr.getNodeRadius(G,nobj1) r2 = gr.getNodeRadius(G,nobj2) #tamanho = x*y tam = len(np.where(img==0)[0]) dist = np.sqrt((obj1[1][0]-obj2[1][0])**2 +(obj1[1][1]-obj2[1][1])**2) - r1 - r2 if(tam>=int(dist)): j,b = us.crop(img,0) if (x >= y): cont1 = len(np.where(j[0,:]==0)[0]) cont2 = len(np.where(j[x-1,:]==0)[0]) else: cont1 = len(np.where(j[:,0]==0)[0]) cont2 = len(np.where(j[:,y-1]==0)[0]) if((x< 30 and x*2 < y) or (y < 30 and y*2 < x)): return True else: if(x>dist or y >dist): return True else: white = (x*y) - tam if (white > tam and (white/tam)>5): return True return False
def classifierArrowOrText(img,label,av,med,t,numbers,arrow,text,initial): obj = len(np.where(img == label)[0]) x,y = img.shape a = x*y if(av<=med): menor = av maior = med else: menor = med maior = av maior2 = maior*2 menor2 = menor*2 if(obj > 0 and a != t): obj1 = (x*y)-obj if(x>y): ecc = x/y minnor = y else: ecc = y/x minnor = x if (a >= maior2): if(obj/obj1 < 0.3): ims,b = us.crop(img,label) x = checkInitial(ims,label) if(x == 1): numbers.append(label) arrow.append(label) else: initial.append(label) else: initial.append(label) elif (a >= menor): if(obj1 > 0): if(ecc >= 2.5 and obj/obj1 < 1.1): initial.append(label) elif(obj/obj1 < 0.3): numbers.append(label) arrow.append(label) else: text.append(label) elif(a < menor): if(ecc > 3 and minnor > 10): initial.append(label) text.append(label) return numbers, arrow, text,initial
def averageComponents(G,numbers,img,label): counter = 0 med = [] sum = 0 xx,yy = img.shape for i in range(1,label): if((np.where(img==i))[0].size != 0): if i not in numbers: j,b = us.crop(img,i) j = np.where(j!=i,255,j) x,y = j.shape a = x*y if(xx*yy != a): med.append(a) sum +=a counter +=1 med = pd.Series(med) return sum/counter,med.median()
def mapping(G,img,label): obj = [] i = 0 for c in G.nodes(): x0 = gr.getNodeX0(G,c) y0 = gr.getNodeY0(G,c) obj.append(('c', (x0,y0),i)) i = i -1 for i in range(1,label): if(len(np.where(img == i)[0])!=0): k,b = us.crop(img,i) x,y = k.shape minX = np.min(np.where(img == i)[0]) maxX = np.max(np.where(img == i)[0]) minY = np.min(np.where(img == i)[1]) maxY = np.max(np.where(img == i)[1]) x = (maxX-minX)//2 y = (maxY-minY)//2 x = minX+x y = minY+y obj.append(('n',(x,y),i)) ob = [] for i in range(len(obj)): q = qu.PriorityQueue() xi = obj[i][1][0] yi = obj[i][1][1] if(obj[i][0] != 'c'): for j in range(len(obj)): if (i!=j): xj = obj[j][1][0] yj = obj[j][1][1] dist = np.sqrt((xi-xj)**2 +((yi-yj)**2)) q.put((dist,obj[j][2])) q2 = [] while(not q.empty()): f = q.get() q2.append(f) ob.append((obj[i][0],(xi,yi),obj[i][2],q2)) return ob
def discoverArrows(G,img,label,objects,lps,withLps): lps = set(lps) numbers = [] withLps = set(withLps) for i in range(1,label): if((np.where(img==i))[0].size != 0): j,b = us.crop(img,i) t = False if(b): j = np.where(j==i, 0, j) j = np.where(j!=0,255,j) x,y = j.shape j = np.ones_like(img)*255 j = np.where(img==i,0,j) op, img, num = discoverOneArrow(G,j,x,y,img,objects,i,lps,withLps) if(op == 1): numbers.append(num) else: t = True av,med = averageComponents(G,numbers,img,label) searchOthersComponents(G,objects,numbers,img,label,av,med) return img
def arrowDirection(img): j,b = us.crop(img,0) x,y = j.shape arq = open('x.txt', 'w') arq.write(str(j)) arq.close() if (x>=y): nx = x//4 if(nx > 5): nx = 5 count1 = len(np.where(j[0:nx,:]==0)[0]) count2 = len(np.where(j[x-nx:x,:]==0)[0]) if(count1>count2): return 0 else: return 1 else: ny = y //4 count1 = len(np.where(j[:,0:ny]==0)[0]) count2 = len(np.where(j[:,y-ny:y]==0)[0]) if(count1>count2): return 2 else: return 3
def saveText(G,list, list2,objs,nimg,numbers,index): x,y = nimg.shape for n in numbers: j,b = us.crop(nimg,n) newI = np.ones_like(nimg)*255 newI = np.where(nimg==n,0,newI) #problama dos ruidos minX = np.min(np.where(nimg == n)[0]) maxX = np.max(np.where(nimg == n)[0]) minY = np.min(np.where(nimg == n)[1]) maxY = np.max(np.where(nimg == n)[1]) medX = minX + (maxX - minX) //2 medY = minY + (maxY - minY) //2 if(len(np.where(j == n)[0]) > 5): #verifica ruidos cont = 0 while True: if(maxX+cont < x): if(nimg[maxX+cont][medY] != 255 and nimg[maxX+cont][medY] != n): if(nimg[maxX+cont][medY] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[maxX+cont][medY]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(maxY + cont < y): if(nimg[medX][maxY+cont] != 255 and nimg[medX][maxY+cont] != n): if(nimg[medX][maxY+cont] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[medX][maxY+cont]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(minX - cont > 0): if(nimg[minX-cont][medY] != 255 and nimg[minX-cont][medY] != n): if(nimg[minX-cont][medY] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[minX-cont][medY]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(minY - cont > 0): if(nimg[medX][minY-cont] != 255 and nimg[medX][minY-cont] != n): if(nimg[medX][minY-cont] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[medX][minY-cont]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(minX-cont > 0 and minY - cont > 0): if(nimg[minX-cont][minY-cont] != 255 and nimg[minX-cont][minY-cont] != n): if(nimg[minX-cont][minY-cont] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[minX-cont][minY-cont]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(maxX+cont < x-1 and minY - cont > 0): if(nimg[maxX+cont][minY-cont] != 255 and nimg[maxX+cont][minY-cont] != n): if(nimg[maxX+cont][minY-cont] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[maxX+cont][minY-cont]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(minX - cont > 0 and maxY + cont < y-1): if(nimg[minX-cont][maxY+cont] != 255 and nimg[minX-cont][maxY+cont] != n): if(nimg[minX-cont][maxY+cont] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[minX-cont][maxY+cont]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(maxX + cont < x - 1 and maxY + cont < y-1): if(nimg[maxX+cont][maxY+cont] != 255 and nimg[maxX+cont][maxY+cont] != n): if(nimg[maxX+cont][maxY+cont] in list2): text = ocr.labelToText(newI) nn = searchOriginal(list,nimg[maxX+cont][maxY+cont]) e0, e1 = gr.getEdgeByNum(G,nn) if(e0 == -1): for i in index: if(i[0] == nn): e0 = i[3] e1 = i[4] t = gr.getEdgeLabel(G,e0,e1) t = t +","+text gr.setEdgeLabel(G,e0,e1,t) break if(minY - cont < 0 and minX - cont < 0 and maxX + cont > x-1 and maxY + cont > y-1): break cont +=1