示例#1
0
文件: wmodel.py 项目: AI42/ocropy
 def setLine(self,image,cseg=None):
     # print "WS",image.shape,cseg.shape
     # ocrolib.write_image_gray("_wmodel.png",image)
     assert mean(image)<0.5*amax(image)
     if amax(image)<1e-6: raise BadImage()
     self.image = array(image*(1.0/amax(image)),'f')
     if cseg is None:
         if "segmenter" not in dir(self):
             self.segmenter = lineseg.DPSegmentLine()
         cseg = self.segmenter.charseg(amax(image)-image)
         morph.showlabels(cseg)
     self.cseg = cseg
     self.line_params = lineproc.seg_geometry(cseg,math=0)
示例#2
0
 def setLine(self, image, cseg=None):
     # print "WS",image.shape,cseg.shape
     # ocrolib.write_image_gray("_wmodel.png",image)
     assert mean(image) < 0.5 * amax(image)
     if amax(image) < 1e-6: raise BadImage()
     self.image = array(image * (1.0 / amax(image)), 'f')
     if cseg is None:
         if "segmenter" not in dir(self):
             self.segmenter = lineseg.DPSegmentLine()
         cseg = self.segmenter.charseg(amax(image) - image)
         morph.showlabels(cseg)
     self.cseg = cseg
     self.line_params = lineproc.seg_geometry(cseg, math=0)
示例#3
0
文件: lineest.py 项目: beanyh/yeobaek
def compute_geomaps(fnames,shapedict,old_model,use_gt=1,size=32,debug=0,old_order=1):
    """Given a shape dictionary and an existing line geometry
    estimator, compute updated geometric maps for each entry
    in the shape dictionary."""
    if debug>0: gray(); ion()
    shape = (shapedict.k,size,size)
    bls = zeros(shape)
    xls = zeros(shape)
    count = 0
    for fno,fname in enumerate(fnames):
        if fno%20==0: print fno,fname,count
        if use_gt:
            # don't use lines with many capital letters for training because
            # they result in bad models
            gt = ocrolib.read_text(ocrolib.fvariant(fname,"txt","gt"))
            if len(re.sub(r'[^A-Z]','',gt))>=0.3*len(re.sub(r'[^a-z]','',gt)): continue
            if len(re.sub(r'[^0-9]','',gt))>=0.3*len(re.sub(r'[^a-z]','',gt)): continue
        image = 1-ocrolib.read_image_gray(fname)
        if debug>0 and fno%debug==0: clf(); subplot(411); imshow(image)
        try:
            blp,xlp = old_model.lineFit(image,order=old_order)
        except:
            traceback.print_exc()
            continue
        blimage = zeros(image.shape)
        h,w = image.shape
        for x in range(w): blimage[clip(int(polyval(blp,x)),0,h-1),x] = 1
        xlimage = zeros(image.shape)
        for x in range(w): xlimage[clip(int(polyval(xlp,x)),0,h-1),x] = 1
        if debug>0 and fno%debug==0: 
            subplot(413); imshow(xlimage+0.3*image)
            subplot(414); imshow(blimage+0.3*image)
        try: 
            seg = lineseg.ccslineseg(image)
        except: 
            continue
        if debug>0 and fno%debug==0: subplot(412); morph.showlabels(seg)
        shape = None
        for sub,transform,itransform_add in extract_chars(seg):
            if shape is None: shape = sub.shape
            assert sub.shape==shape
            count += 1
            best = shapedict.predict1(sub)
            bls[best] += transform(blimage)
            xls[best] += transform(xlimage)
        if debug==1: ginput(1,100)
        elif debug>1: ginput(1,0.01)
    for i in range(len(bls)): bls[i] *= bls[i].shape[1]*1.0/max(1e-6,sum(bls[i]))
    for i in range(len(xls)): xls[i] *= xls[i].shape[1]*1.0/max(1e-6,sum(xls[i]))
    return bls,xls
示例#4
0
 def charseg(self, line):
     """Segment a text line into potential character parts."""
     assert mean(line) > 0.5 * amax(line)
     line0 = amax(line) - line
     ccuts = contourcuts(line0,
                         maxdist=self.maxdist,
                         minrange=self.minrange,
                         mincdist=self.mincdist,
                         sigma=self.sigma,
                         r=self.rr,
                         s=self.s)
     line = maximum(0, line0 - ccuts)
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,
                         imweight=self.imweight,
                         bweight=self.bweight,
                         diagweight=self.diagweight,
                         debug=self.debug,
                         r=self.r)
     tracks = array(tracks < 0.5 * amax(tracks), 'i')
     tracks, _ = morph.label(tracks)
     self.tracks = tracks
     stracks = morph.spread_labels(tracks)
     rsegs = stracks * (line0 > 0.5 * amax(line0))
     if self.debug:
         figure("temp")
         subplot(311)
         morph.showlabels(tracks)
         subplot(312)
         morph.showlabels(stracks)
         subplot(313)
         morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
示例#5
0
 def charseg(self, line):
     """Segment a text line into potential character parts."""
     assert mean(line) > 0.5 * amax(line)
     line = amax(line) - line
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,
                         imweight=self.imweight,
                         bweight=self.bweight,
                         diagweight=self.diagweight,
                         debug=self.debug,
                         r=self.r,
                         threshold=self.threshold)
     tracks = array(tracks < 0.5 * amax(tracks), 'i')
     tracks, _ = morph.label(tracks)
     self.tracks = tracks  # for debuggin
     stracks = morph.spread_labels(tracks)
     rsegs = stracks * (line > 0.5 * amax(line))
     if 0:
         figure("temp")
         subplot(311)
         morph.showlabels(tracks)
         subplot(312)
         morph.showlabels(stracks)
         subplot(313)
         morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
示例#6
0
文件: lineseg.py 项目: AI42/ocropy
 def charseg(self,line):
     """Segment a text line into potential character parts."""
     assert mean(line)>0.5*amax(line)
     line = amax(line)-line
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,imweight=self.imweight,bweight=self.bweight,
                         diagweight=self.diagweight,debug=self.debug,r=self.r,
                         threshold=self.threshold)
     tracks = array(tracks<0.5*amax(tracks),'i')
     tracks,_ = morph.label(tracks)
     self.tracks = tracks # for debuggin
     stracks = morph.spread_labels(tracks)
     rsegs = stracks*(line>0.5*amax(line))
     if 0:
         figure("temp")
         subplot(311); morph.showlabels(tracks)
         subplot(312); morph.showlabels(stracks)
         subplot(313); morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
示例#7
0
文件: lineseg.py 项目: AI42/ocropy
 def charseg(self,line):
     """Segment a text line into potential character parts."""
     assert mean(line)>0.5*amax(line)
     line0 = amax(line)-line
     ccuts = contourcuts(line0,maxdist=self.maxdist,minrange=self.minrange,
                         mincdist=self.mincdist,sigma=self.sigma,r=self.rr,s=self.s)
     line = maximum(0,line0-ccuts)
     # line = line+self.ledge*maximum(0,roll(line,-1,1)-line)
     tracks = dplineseg2(line,imweight=self.imweight,bweight=self.bweight,
                         diagweight=self.diagweight,debug=self.debug,r=self.r)
     tracks = array(tracks<0.5*amax(tracks),'i')
     tracks,_ = morph.label(tracks)
     self.tracks = tracks
     stracks = morph.spread_labels(tracks)
     rsegs = stracks*(line0>0.5*amax(line0))
     if self.debug:
         figure("temp")
         subplot(311); morph.showlabels(tracks)
         subplot(312); morph.showlabels(stracks)
         subplot(313); morph.showlabels(rsegs)
         raw_input()
     return morph.renumber_by_xcenter(rsegs)
示例#8
0
文件: lineseg.py 项目: AI42/ocropy
    subparsers = parser.add_subparsers(dest="subcommand")
    test = subparsers.add_parser("test")
    test.add_argument("--imweight",type=float,default=4,help="image weight (%(default)f")
    test.add_argument("--bweight",type=float,default=-1,help="left border weight (%(default)f)")
    test.add_argument("--diagweight",type=float,default=1,help="additional diagonal weight (%(default)f)")
    test.add_argument("--r",type=int,default=1,help="range for diagonal steps (%(default)d)")
    test.add_argument("--threshold",type=float,default=0.5)
    test.add_argument("files",nargs="+",default=[])
    # test2 = subparsers.add_parser("test2")
    args = parser.parse_args()
    if args.subcommand=="test":
        segmenter = DPSegmentLine(imweight=args.imweight,
                                  bweight=args.bweight,
                                  diagweight=args.diagweight,
                                  r=args.r,
                                  threshold=args.threshold,
                                  debug=1)
        ion(); gray()
        for fname in args.files:
            print fname
            image = ocrolib.read_image_gray(fname)
            segmentation = segmenter.charseg(image)
            figure("output")
            subplot(211); imshow(image)
            subplot(212); morph.showlabels(segmentation)
            raw_input()
        else:
            parser.print_help()
    sys.exit(0)

示例#9
0
    subparsers = parser.add_subparsers(dest="subcommand")
    test = subparsers.add_parser("test")
    test.add_argument("--imweight",type=float,default=4,help="image weight (%(default)f")
    test.add_argument("--bweight",type=float,default=-1,help="left border weight (%(default)f)")
    test.add_argument("--diagweight",type=float,default=1,help="additional diagonal weight (%(default)f)")
    test.add_argument("--r",type=int,default=1,help="range for diagonal steps (%(default)d)")
    test.add_argument("--threshold",type=float,default=0.5)
    test.add_argument("files",nargs="+",default=[])
    # test2 = subparsers.add_parser("test2")
    args = parser.parse_args()
    if args.subcommand=="test":
        segmenter = DPSegmentLine(imweight=args.imweight,
                                  bweight=args.bweight,
                                  diagweight=args.diagweight,
                                  r=args.r,
                                  threshold=args.threshold,
                                  debug=1)
        ion(); gray()
        for fname in args.files:
            print fname
            image = ocrolib.read_image_gray(fname)
            segmentation = segmenter.charseg(image)
            figure("output")
            subplot(211); imshow(image)
            subplot(212); morph.showlabels(segmentation)
            raw_input()
        else:
            parser.print_help()
    sys.exit(0)

示例#10
0
    test.add_argument("--r",
                      type=int,
                      default=1,
                      help="range for diagonal steps (%(default)d)")
    test.add_argument("--threshold", type=float, default=0.5)
    test.add_argument("files", nargs="+", default=[])
    # test2 = subparsers.add_parser("test2")
    args = parser.parse_args()
    if args.subcommand == "test":
        segmenter = DPSegmentLine(imweight=args.imweight,
                                  bweight=args.bweight,
                                  diagweight=args.diagweight,
                                  r=args.r,
                                  threshold=args.threshold,
                                  debug=1)
        ion()
        gray()
        for fname in args.files:
            print fname
            image = ocrolib.read_image_gray(fname)
            segmentation = segmenter.charseg(image)
            figure("output")
            subplot(211)
            imshow(image)
            subplot(212)
            morph.showlabels(segmentation)
            raw_input()
        else:
            parser.print_help()
    sys.exit(0)
示例#11
0
def compute_geomaps(fnames,
                    shapedict,
                    old_model,
                    use_gt=1,
                    size=32,
                    debug=0,
                    old_order=1):
    """Given a shape dictionary and an existing line geometry
    estimator, compute updated geometric maps for each entry
    in the shape dictionary."""
    if debug > 0:
        gray()
        ion()
    shape = (shapedict.k, size, size)
    bls = zeros(shape)
    xls = zeros(shape)
    count = 0
    for fno, fname in enumerate(fnames):
        if fno % 20 == 0: print fno, fname, count
        if use_gt:
            # don't use lines with many capital letters for training because
            # they result in bad models
            gt = ocrolib.read_text(ocrolib.fvariant(fname, "txt", "gt"))
            if len(re.sub(r'[^A-Z]', '',
                          gt)) >= 0.3 * len(re.sub(r'[^a-z]', '', gt)):
                continue
            if len(re.sub(r'[^0-9]', '',
                          gt)) >= 0.3 * len(re.sub(r'[^a-z]', '', gt)):
                continue
        image = 1 - ocrolib.read_image_gray(fname)
        if debug > 0 and fno % debug == 0:
            clf()
            subplot(411)
            imshow(image)
        try:
            blp, xlp = old_model.lineFit(image, order=old_order)
        except:
            traceback.print_exc()
            continue
        blimage = zeros(image.shape)
        h, w = image.shape
        for x in range(w):
            blimage[clip(int(polyval(blp, x)), 0, h - 1), x] = 1
        xlimage = zeros(image.shape)
        for x in range(w):
            xlimage[clip(int(polyval(xlp, x)), 0, h - 1), x] = 1
        if debug > 0 and fno % debug == 0:
            subplot(413)
            imshow(xlimage + 0.3 * image)
            subplot(414)
            imshow(blimage + 0.3 * image)
        try:
            seg = lineseg.ccslineseg(image)
        except:
            continue
        if debug > 0 and fno % debug == 0:
            subplot(412)
            morph.showlabels(seg)
        shape = None
        for sub, transform, itransform_add in extract_chars(seg):
            if shape is None: shape = sub.shape
            assert sub.shape == shape
            count += 1
            best = shapedict.predict1(sub)
            bls[best] += transform(blimage)
            xls[best] += transform(xlimage)
        if debug == 1: ginput(1, 100)
        elif debug > 1: ginput(1, 0.01)
    for i in range(len(bls)):
        bls[i] *= bls[i].shape[1] * 1.0 / max(1e-6, sum(bls[i]))
    for i in range(len(xls)):
        xls[i] *= xls[i].shape[1] * 1.0 / max(1e-6, sum(xls[i]))
    return bls, xls