def main(filter_mode): img = cv2.imread('./images/centers2.png', 0) img_copy = img.copy() rows, cols = img.shape secureDel('dist.txt', FILE_TYPE, False) imgName = ".\\images\\inverted.jpg" erodInvert(img, ks1=(3, 300), ks2=(5, 200), invName=imgName) image = cv2.imread(imgName, 0) txtLines = calculEdges(image) img_copy = cv2.imread('./images/centers.png', 0) for (L1, L2) in txtLines: cv2.line(img_copy, L1[0], L1[1], (0, 0, 0), 2) cv2.line(img_copy, L2[0], L2[1], (0, 0, 0), 2) cv2.imwrite('./images/hough_lines.png', img_copy) with open('nbLines.txt', 'w') as file: file.write("nbLines:" + str(len(txtLines)) + ":\n") img_copy = cv2.imread('./images/centers.png', 0) for i, tLine in enumerate(txtLines): yL1 = tLine[0][0][1] yL2 = tLine[1][0][1] frame = img_copy[yL1:yL2, 0:cols].copy() frmName = './Lines/Line' + str(i + 1) + '.jpg' secureDel(frmName, FILE_TYPE, False) cv2.imwrite(frmName, frame)
def drawDotsLines(frmName, points, Lines=None, id=0) : imgc=cv2.imread(frmName, 0) r, c=imgc.shape blankName='./images/blank.jpg' secureDel(blankName,FILE_TYPE,False) blank_paper(c, r, 300,blank=blankName) img1 = cv2.imread(blankName,0) for p in points : # draw x,y coordinate of center cv2.circle(img1, (p[0], p[1]), 6, (0, 0, 0), 6) cv2.circle(img1, (p[0], p[1]), 5, (0, 0, 0), -1) if (Lines is not None) and (len(Lines)>0) : for L in Lines : cv2.line(img1,L[0],L[1],(0,0,0),2) frmName='./Lines/frames/Line' +str(id)+'.jpg' secureDel(frmName,FILE_TYPE,False) cv2.imwrite(frmName,img1) return frmName
def erodInvert(img, ks1=(20, 200), ks2=(5, 100), invName="inverted.jpg"): img_not = img.copy() kernel = np.ones(ks1, np.uint8) img_not = cv2.erode(img_not, kernel, iterations=1) img_not = cv2.bitwise_not(img_not) # kernel = np.ones(ks2,np.uint8) # dilation = cv2.dilate(img_not,kernel,iterations = 1) # img_not = cv2.bitwise_not(img_not) secureDel(invName, FILE_TYPE, False) cv2.imwrite(invName, img_not)
def onScan(self, event): scn = self.cbScanners.GetValue() pg = self.cbPgs.GetValue() cr = self.cbCls.GetValue() res = self.resolution secureDel("./scanLog.txt", FILE_TYPE) secureDel("./images/ImageScan.tif", FILE_TYPE) cmd = '.\\clscan\\clscan.exe ' cmd += '/SetFileName ".\\images\\ImageScan.tif" ' cmd += '/LogToFile ".\\scanLog.txt" ' cmd += '/SetDeskew ' cmd += '/SetSource Auto ' cmd += '/SetCrop ' cmd += '/SetThreshold "150" ' cmd += '/SetContrast "50" ' cmd += '/SetBrightness "50" ' # cmd+='/ShowUI ' # cmd+='/UseScan2 ' cmd += '/SetDuplex:Y ' cmd += '/SetJpegQuality "100" ' # cmd+='/SetDeskew ' cmd += '/SetResolution "' + str(res) + '" ' if pg != '': cmd += '/SetPageSize "' + pg + '" ' printLog("added :" + pg) else: printLog("not added :" + pg) if cr != '': cmd += '/SetColorType "' + cr + '" ' cmd += '/SetScanner "' + scn + '" ' log = new_popen(cmd) log += new_popen("python AppExec.py -t 0") log += new_popen("python AppExec.py -t 1") #printLog("\n".join(log)) self.scanned = True self.Close()
def BlurErodeThresh(imFilename, ksize=(8, 5), erod=True): img = cv2.imread(imFilename, 0) #img = cv2.blur(img,(5,5)) # median blurring should be done to remove noise img = cv2.medianBlur(img, 7) if erod: kernel = np.ones(ksize, np.uint8) img = cv2.erode(img, kernel, iterations=1) img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] #show_wait_destroy("ThreshImage", img) h, w = img.shape thName = './images/thresh.jpg' secureDel(thName, FILE_TYPE) cv2.imwrite(thName, img[20:(h - 40), 60:(w - 100)])
def main(): img = cv2.imread('./images/thresh.jpg', 0) height, width = img.shape points, cntList = calcul_centers(img) points = calcul_centers(img)[0] blankName = '.\\images\\blank.jpg' secureDel(blankName, FILE_TYPE, False) blank_paper(width, height, 300, blank=blankName) img1 = cv2.imread(blankName, 0) for p in points: # draw x,y coordinate of center cv2.circle(img1, (p[0], p[1]), 5, (0, 0, 0), 5) cv2.circle(img1, (p[0], p[1]), 4, (0, 0, 0), -1) resultName = "./images/centers.png" secureDel(resultName, FILE_TYPE) cv2.imwrite(resultName, img1) img2 = cv2.imread(blankName, 0) for p in points: # draw x,y coordinate of center cv2.circle(img2, (p[0], p[1]), 12, (0, 0, 0), 12) cv2.circle(img2, (p[0], p[1]), 10, (0, 0, 0), -1) resultName = "./images/centers2.png" secureDel(resultName, FILE_TYPE) cv2.imwrite(resultName, img2)
def main(file_name): new_file_name = "./images/resized.tif" secureDel(new_file_name, FILE_TYPE) preProcess(file_name, new_file_name, ksize=(5, 5))
def main(lang='ar'): # load the list of DotsLines objects listDotsLines = [] with open('dLdata', 'rb') as dLfile: dLUnpickler = pk.Unpickler(dLfile) listDotsLines = dLUnpickler.load() printLog("nb DotsLines obj : ", len(listDotsLines)) # load the dictionary for selected language dicBTX = {} dicNum = {} if lang == "ar": # dicBTX=arabic_btx # dicNum=arabic_num with open('arData', 'rb') as arFile: arUnpickler = pk.Unpickler(arFile) dicBTX = arUnpickler.load() dicNum = arUnpickler.load() printLog("nb arabic dict items : ", len(dicBTX), "++", len(dicNum)) elif lang == "fr": # dicBTX=french_btx # dicNum=french_num with open('frData', 'rb') as frFile: frUnpickler = pk.Unpickler(frFile) dicBTX = frUnpickler.load() dicNum = frUnpickler.load() printLog("nb french dict items : ", len(dicBTX), "++", len(dicNum)) ########################################## elif lang == "en": with open('enData', 'rb') as enFile: enUnpickler = pk.Unpickler(enFile) dicBTX = enUnpickler.load() dicNum = enUnpickler.load() printLog("nb english dict items : ", len(dicBTX), "++", len(dicNum)) #lang="ar" secureDel('cod.txt', FILE_TYPE, False) secureDel('text.txt', FILE_TYPE, False) for dL in listDotsLines: checked = False #toggle var curDic = dicBTX col1 = [] col2 = [] h_lines = dL.h_lines v_lines = dL.v_lines nbL = len(v_lines) for i in range(nbL - 1): cL1 = v_lines[i] cL2 = v_lines[i + 1] xL1 = cL1[0][0] xL2 = cL2[0][0] if (xL2 - xL1) < 36 and not checked: col1 = dL.selectDots(cL1, 0) col2 = dL.selectDots(cL2, 0) cod = codeChar(h_lines, col1, col2) # printLog("cod= ", cod) mcod = 0 if cod != "" and chk(cod): mcod = int(cod.strip()) else: continue if mcod in curDic.keys(): asc = curDic[mcod] for c in asc: dL.textLine += c checked = True else: #if mcod not in curDic.keys(): writeMSG( str(mcod) + "line=" + str(dL.id) + " numcol = " + str(i)) checked = False if lang != "ar": dL.textLine += '?' elif (xL2 - xL1) < 50 and checked: checked = False elif (xL2 - xL1) < 75 and not checked: col1 = dL.selectDots(cL1, 0) col2 = [] cod = codeChar(h_lines, col1, col2) mcod = 0 if cod != "" and chk(cod): mcod = int(cod.strip()) else: continue if mcod in curDic.keys(): asc = curDic[mcod] for c in asc: dL.textLine += c checked = False else: #if mcod not in curDic.keys(): writeMSG( str(mcod) + "line=" + str(dL.id) + " numcol = " + str(i)) checked = False if lang != "ar": dL.textLine += '?' elif (xL2 - xL1) > 78: checked = False dL.textLine += ' ' #printLog(dL.textLine.strip()) with open('text.txt', 'a', encoding='latin-1') as wf: wf.write(dL.textLine.strip() + '\n')
if __name__ == '__main__': # When this module is run (not imported) then create the app, the # frame, show it, and start the event loop. # The_program_to_hide = win32gui.GetForegroundWindow() # win32gui.ShowWindow(The_program_to_hide , win32con.SW_HIDE) if not adm.isUserAdmin(): printLog("You're not an admin.", os.getpid()) adm.runAsAdmin() sys.exit(0) else: printLog("You are an admin!", os.getpid()) secureDel("./log.txt", FILE_TYPE) app = wx.App(False) # ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0) # Ask user to login # dlg = LoginDialog() # dlg.ShowModal() # authenticated = dlg.logged_in # dlg.Destroy() authenticated = True if authenticated: #ScannerObj.loadScanners() thw = wThread("th_load_scanners") thw.start() showProgress()
def main() : nbL=0 secureDel('dist2.txt',FILE_TYPE,False) with open ('nbLines.txt','r') as f : ln=f.read() tab=ln.strip().split(':') nbL=int(tab[1]) printLog("nbL = ", nbL) listDotsLines=[] for i in range(nbL) : HL=[] frmName='./Lines/Line' +str(i+1)+'.jpg' img=cv2.imread(frmName, 0) points=calcul_centers(img,echo=False)[0] rows,cols=img.shape imgName="./Lines/frames/inverted_"+str(i+1)+".jpg" erodInvert(img, ks1=(1,400), ks2=(0,0), invName=imgName) img2=cv2.imread(imgName, 0) # convert the grayscale image to binary image ret,thresh = cv2.threshold(img2, 127, 255, 0) # find contour in the binary image contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #printLog("nb contours : ",len(contours)) for c in contours : x,y,w,h = cv2.boundingRect(c) y0=int(y+(h//2)) line0=((0,y0),(cols,y0)) HL.append(line0) points2=[] for p in points : yp=selectLine(HL,p,Y_AXIS) points2.append((p[0],yp)) # drawDotsLines(frmName, points2, HL,(i+1)) frmName=drawDotsLines(frmName, points2, None,(i+1)) img1=cv2.imread(frmName,0) # imvName='./Lines/frames/inverted_' +str(i+1)+'.jpg' imgName='./Lines/frames/vertical_' +str(i+1)+'.jpg' # VerticalMask(img1, ks1=(50,1),ks2=(5,1), # invName=imvName,dilName=imgName) erodInvert(img1, ks1=(100,3), ks2=(0,0), invName=imgName) k=cv2.imread(imgName,0) ret,thresh = cv2.threshold(k,127,255,0) contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #printLog("nb contours : ",len(contours)) v_lines=[] for c in contours : x,y,w,h = cv2.boundingRect(c) xL=int(x+(w//2)) v_lines.append(((xL, 0),(xL, rows))) v_lines.sort() points3=[] for p in points2 : xp=selectLine(v_lines,p,X_AXIS) points3.append((xp,p[1])) frmName=drawDotsLines(frmName, points3, v_lines,(i+1)) dL=DotsLines(i+1, points3, HL, v_lines) listDotsLines.append(dL) writeDist(v_lines,sign="line "+str(i+1)+" : \n") with open('dLdata','wb') as dLfile : dLPickler=pickle.Pickler(dLfile) dLPickler.dump(listDotsLines)