def compute_segmentation(binary, scale, blackseps, maxseps, maxcolseps, csminheight, sepwiden, usegauss, vscale, hscale, threshold, quiet): """Given a binary image, compute a complete segmentation into lines, computing both columns and text lines.""" binary = np.array(binary, 'B') # start by removing horizontal black lines, which only # interfere with the rest of the page segmentation binary = remove_hlines(binary, scale) # do the column finding if not quiet: print_info("computing column separators") colseps, binary = compute_colseps(binary, scale, blackseps, maxseps, maxcolseps, csminheight, sepwiden) # now compute the text line seeds if not quiet: print_info("computing lines") bottom, top, boxmap = compute_gradmaps(binary, scale, usegauss, vscale, hscale) seeds = compute_line_seeds(binary, bottom, top, colseps, scale, threshold, vscale) DSAVE("seeds", [bottom, top, boxmap]) # spread the text line seeds to all the remaining # components if not quiet: print_info("propagating labels") llabels = morph.propagate_labels(boxmap, seeds, conflict=0) if not quiet: print_info("spreading labels") spread = morph.spread_labels(seeds, maxdist=scale) llabels = np.where(llabels > 0, llabels, spread * binary) segmentation = llabels * binary return segmentation
def compute_segmentation(binary, scale): """Given a binary image, compute a complete segmentation into lines, computing both columns and text lines. """ print("$$ compute_segmentation: %s %g" % (desc(binary), scale)) binary = np.array(binary, 'B') # start by removing horizontal black lines, which only interfere with the rest of the page # segmentation binary = remove_hlines(binary, scale) # do the column finding print("computing column separators") colseps, binary = compute_colseps(binary, scale) # now compute the text line seeds print("computing lines") bottom, top, boxmap = compute_gradmaps(binary, scale) seeds = compute_line_seeds(binary, bottom, top, colseps, scale) print("seeds=%s" % desc(seeds)) DSAVE("seeds", [bottom, top, boxmap]) # spread the text line seeds to all the remaining components print("propagating labels") llabels = morph.propagate_labels(boxmap, seeds, conflict=0) print("spreading labels: llabels=%s" % desc(llabels)) spread = morph.spread_labels(seeds, maxdist=scale) llabels = np.where(llabels > 0, llabels, spread * binary) segmentation = llabels * binary print("$$ llabels: %s" % desc(llabels)) print("$$ segmentation: %s" % desc(segmentation)) return segmentation
def text_line_segmentation(binary, scale=None, gray=None, num_col = 1): """Given a binary image, compute a complete segmentation into lines, computing both columns and text lines.""" binary = array(binary, 'B') if scale is None: scale = psegutils.estimate_scale(binary) # do the column finding if num_col > 1: colseps, binary = compute_colseps(binary, scale) else: colseps = np.zeros(binary.shape) # now compute the text line seeds bottom, top, boxmap = compute_gradmaps(binary, scale) seeds = compute_line_seeds(binary, bottom, top, colseps, scale) # spread the text line seeds to all the remaining components llabels = morph.propagate_labels(boxmap, seeds, conflict=0) spread = morph.spread_labels(seeds, maxdist=scale) llabels = where(llabels > 0, llabels, spread * binary) segmentation = llabels * binary lines = psegutils.compute_lines(segmentation, scale, 0.8) line_ims = [] for l in lines: if gray is None: binline = psegutils.extract_masked(1-binary, l, pad=0) else: binline = psegutils.extract_masked(gray, l, pad=0) binline = pad_by(binline, 10, invert=False) line_ims.append(binline) return line_ims, lines
def compute_segmentation(binary, scale): """Given a binary image, compute a complete segmentation into lines, computing both columns and text lines.""" binary = array(binary, 'B') # start by removing horizontal black lines, which only # interfere with the rest of the page segmentation binary = remove_hlines(binary, scale) # do the column finding if not args.quiet: print("computing column separators") colseps, binary = compute_colseps(binary, scale) # now compute the text line seeds if not args.quiet: print("computing lines") bottom, top, boxmap = compute_gradmaps(binary, scale) seeds = compute_line_seeds(binary, bottom, top, colseps, scale) ####imsave('/home/gupta/Documents/combinedseeds.png', [bottom,top,boxmap]) # DSAVE("seeds",[bottom,top,boxmap]) # spread the text line seeds to all the remaining # components if not args.quiet: print("propagating labels") llabels = morph.propagate_labels(boxmap, seeds, conflict=0) if not args.quiet: print("spreading labels") spread = morph.spread_labels(seeds, maxdist=scale) llabels = where(llabels > 0, llabels, spread * binary) segmentation = llabels * binary return segmentation
def compute_segmentation(self, binary, scale): """Given a binary image, compute a complete segmentation into lines, computing both columns and text lines.""" binary = np.array(binary, 'B') # start by removing horizontal black lines, which only # interfere with the rest of the page segmentation binary = self.remove_hlines(binary, scale) # do the column finding log.debug("computing column separators") colseps, binary = self.compute_colseps(binary, scale) # now compute the text line seeds log.debug("computing lines") bottom, top, boxmap = self.compute_gradmaps(binary, scale) log.debug("bottom=%s top=%s boxmap=%s", bottom, top, boxmap) seeds = self.compute_line_seeds(binary, bottom, top, colseps, scale) # spread the text line seeds to all the remaining # components log.debug("propagating labels") llabels = morph.propagate_labels(boxmap, seeds, conflict=0) log.debug("spreading labels") spread = morph.spread_labels(seeds, maxdist=scale) llabels = np.where(llabels > 0, llabels, spread * binary) segmentation = llabels * binary return segmentation
def calc(self, objects, scale): if self.binpage is None: return tt = time() bottom, top, boxmap = compute_gradmaps(self.binpage, scale) # DSHOW('hihi', [0.5*bottom+0.5*top,self.binpage, boxmap]) seeds0 = compute_line_seeds(self.binpage, bottom, top, scale) seeds, _ = morph.label(seeds0) llabels = morph.propagate_labels(boxmap, seeds, conflict=0) spread = spread_labels(seeds, maxdist=scale) llabels = where(llabels > 0, llabels, spread * self.binpage) segmentation = llabels * self.binpage self.binpage = ocrolib.remove_noise(self.binpage, args.noise) lines = psegutils.compute_lines(segmentation, scale) binpage_reversed = 1 - self.binpage # print 'pre line ', time() - tt tt = time() self.lines = [] for i, l in enumerate(lines): tt = time() binline = psegutils.extract_masked( binpage_reversed, l, pad=args.pad, expand=args.expand) # black text binline = (1 - binline) le = lineest.CenterNormalizer(binline.shape[0]) # white text binline = binline.astype(float) le.measure(binline) binline = le.normalize(binline) binline = where(binline > 0.5, 0, 1) # black text # print 'line time ', time()-tt print '-----------------------' pilimg = Image.fromarray((binline * 255).astype(uint8)) pred_legacy = pytesseract.image_to_string(pilimg, lang='eng', config='--oem 0 --psm 7') print '00', pred_legacy pred_lstm = pytesseract.image_to_string(pilimg, lang='eng', config='--oem 1 --psm 7') print '11', pred_lstm # ASHOW('line',binline, scale=2.0) ## pred_both = pytesseract.image_to_string(pilimg,lang='vie', config='--oem 2 --psm 7') ## print '22', pred_both result = psegutils.record(bounds=l.bounds, text1=pred_legacy, text2=pred_lstm, img=binline) self.lines.append(result)
def compute_segmentation(binary,scale): """Given a binary image, compute a complete segmentation into lines, computing both columns and text lines.""" binary = array(binary,'B') # start by removing horizontal black lines, which only # interfere with the rest of the page segmentation binary = remove_hlines(binary,scale) # do the column finding colseps,binary = compute_colseps(binary,scale) # now compute the text line seeds bottom,top,boxmap = compute_gradmaps(binary,scale) seeds = compute_line_seeds(binary,bottom,top,colseps,scale) #DSAVE("seeds",[bottom,top,boxmap]) # spread the text line seeds to all the remaining # components llabels = morph.propagate_labels(boxmap,seeds,conflict=0) spread = morph.spread_labels(seeds,maxdist=scale) llabels = where(llabels>0,llabels,spread*binary) segmentation = llabels*binary return segmentation
def printResult(self, outputfile): # Some pre-process # print 'text area before' # cv2.imshow('patch', self.patch) # cv2.waitKey(-1) if self.name == 'CMND cu - 9 so': k = 0.45 else: k = 0.33 patch = sharpen(self.patch) binary = sauvola(patch, w=int(self.template.shape[1] / 24.5 * 2), k=k, scaledown=0.5, reverse=True) binary = cv2.bitwise_and(binary, binary, mask=self.patch_mask) # print 'text area after' # cv2.imshow('patch', binary*255) # cv2.waitKey(-1) dotremoved = binary scale = self.scale # Line extraction copied from Ocropus source code bottom, top, boxmap = compute_gradmaps(dotremoved, scale) seeds0 = compute_line_seeds(dotremoved, bottom, top, scale) seeds, _ = morph.label(seeds0) llabels = morph.propagate_labels(boxmap, seeds, conflict=0) spread = spread_labels(seeds, maxdist=scale) llabels = where(llabels > 0, llabels, spread * dotremoved) segmentation = llabels * dotremoved dotremoved = ocrolib.remove_noise(dotremoved, 8) lines = psegutils.compute_lines(segmentation, scale / 2) binpage_reversed = 1 - dotremoved self.lines = [] readrs = dict.fromkeys(self.linepos1.keys(), u'') lines = sorted(lines, key=lambda x: x.bounds[1].start) for i, l in enumerate(lines): # Line extraction copied from Ocropus source code binline = psegutils.extract_masked(binpage_reversed, l, pad=int(scale / 2), expand=0) # black text binline = (1 - binline) le = lineest.CenterNormalizer(binline.shape[0]) # white text binline = binline.astype(float) le.measure(binline) binline = le.normalize(binline) # print 'normalized' # cv2.imshow('line', binline) # cv2.waitKey(-1) binline = cv2.resize(binline, None, fx=2.0, fy=2.0) # print 'resized' # cv2.imshow('line', binline) # cv2.waitKey(-1) binline = where(binline > 0.5, uint8(0), uint8(255)) # black text # print 'black text' # cv2.imshow('line', binline) # cv2.waitKey(-1) # pilimg = Image.fromarray(binline) pos = l.bounds[0].stop left = (l.bounds[1].start < self.template.shape[1] / 2) # Prediction using Tesseract 4.0 if pos > self.linepos1['idNumber'][0] and pos < self.linepos1[ 'idNumber'][1]: #ID, all numbers pred = ocr( binline, config= '--oem 0 --psm 7 -c tessedit_char_whitelist=0123456789') readrs['idNumber'] += pred + u' ' elif pos > self.linepos1['dateOfBirth'][0] and pos < self.linepos1[ 'dateOfBirth'][1]: # DOB, number, - , / pred = ocr( binline, config= '--oem 1 --psm 7 -c tessedit_char_whitelist=0123456789-/') readrs['dateOfBirth'] += pred + u' ' elif left and pos > self.linepos1['Gender'][ 0] and pos < self.linepos1['Gender'][1]: pred = ocr(binline, config='--oem 1 --psm 7 -l vie') readrs['Gender'] += pred + u' ' elif (not left) and pos > self.linepos1['Dantoc'][ 0] and pos < self.linepos1['Dantoc'][1]: pred = ocr(binline, config='--oem 1 --psm 7 -l vie') readrs['Dantoc'] += pred + u' ' elif pos > self.linepos1['NguyenQuan'][0] and pos < self.linepos1[ 'NguyenQuan'][1]: pred = ocr(binline, config='--oem 1 --psm 7 -l vie') readrs['NguyenQuan'] += pred + u' ' elif pos > self.linepos1['fullName'][0] and pos < self.linepos1[ 'fullName'][1]: pred = ocr(binline, config='--oem 1 --psm 7 -l vie') readrs['fullName'] += pred + u' ' # else: # pred = ocr(binline, config='--oem 1 --psm 7 -l vie') # print 'unknown ', unicode2ascii(pred), 'y:', l.bounds[0], 'x:', l.bounds[1] for k in readrs: readrs[k] = (readrs[k].replace(u'²', u'2').replace(u'º', u'o').replace( u'»', u'-')).strip() if len(readrs[k]) == 0: readrs[k] = None if self.name == 'CMND moi - 12 so': readrs['type'] = 'CMND Mới - 12 Số' elif self.name == 'Can Cuoc Cong Dan': readrs['type'] = 'Căn Cước Công Dân' elif self.name == 'CMND cu - 9 so': readrs['type'] = 'CMND Cũ - 9 Số' readrs['NgayHetHan'] = None outputfile.write(json.dumps(readrs))