Esempio n. 1
0
    # scale to half a page width
    r = mi.draw_y_proj(p,image=r,color=RGBPixel(100,0,0),fac=0.5) 
    r.save_PNG("%s/%s-03-words-and-word-projections.png"%(outputbase,basename))
    logging.debug("03-words-and-word-projections.png")


    # Lines
    r = mi.to_rgb()
    bsimg = mi.without_insidestaves_info()
    p2 = p.rspikes(mi._image.ncols-1)
    for myr in p2:
        r.draw_hollow_rect(myr,RGBPixel(100,100,100))
    lines = t._confirmed_text_lines(words)
    for l in lines:
        r.draw_hollow_rect(l.l,RGBPixel(255,0,0))
    occs = mi.ccs_overall()
    for c in occs["text"]:
        r.highlight(c,RGBPixel(0,150,0))
    r.save_PNG("%s/%s-04-lines-and-text.png"%(outputbase,basename))
    logging.debug("04-lines-and-text.png")

    r = mi.color_segment()
    r.save_PNG("%s/%s-05-colorsegmented.png"%(outputbase,basename))






    
Esempio n. 2
0
class Page:

    def __init__(self,origfile,pagenumber,classifier=None):
        self._pagenumber = pagenumber
        self._origfile = origfile
        self._noend = origfile[:-4] # shortcut
        self._classifier = classifier

        # internal logger
        self._l = logging.getLogger(self.__class__.__name__)

        # Internal params init.
        self._file = None
        self._mi = None
        self._nostavesfile = None

    def save(self,filename=None):
        start = time.time()
        if filename is None:
            filename = self._genfilename()

        cmd = ' '.join([gspath,
                            '-dNOPAUSE',
                            '-q',
                            '-r300',
                            '-sDEVICE=tiffg4',
                            '-dBATCH',
                            '-sOutputFile=%s'%filename,
                            '-sPAPERSIZE=a4',
                            '-dFirstPage=%d'%self._pagenumber,
                            '-dLastPage=%d'%self._pagenumber,
                            self._origfile
                        ])
        po = Popen(cmd,shell=True,stdout=PIPE,stderr=STDOUT).stdout
        for l in po.readlines():
            self._l.debug("GS Output:%s",l)
        po.close()
        self._file = filename
        self._l.debug("Saving file %s (duration %f)",
                      filename,(time.time()-start))
        return filename

    def _init_mi(self):
        """ Setup a MusicImage with our classifier """
        self._mi = IllMusicImage(self._file,classifier=self._classifier)

    def save_nostaves(self,filename=None):
        start = time.time()
        if self._file is None:
            self._l.debug("No converted tif page. Forcing one now.")
            self.save()

        if filename is None:
            filename = self._genfilename(postfix="-nostaves",extension=".png")

        if self._mi is None:
            self._init_mi()

        self._mi.without_staves().save_PNG(filename)
        self._nostavesfile = filename
        self._l.debug("Saved file %s (duration %f)",filename,(time.time()-start))
        return filename

    def generate_gamera_script(self,dir=".",filename=None,openfile=None):
        start = time.time()
        if filename is None:
            filename = self._genfilename(dir=dir,extension=".py")
            #self._l.info("Filename not given. Using %s",filename)

        if openfile is None and not self._nostavesfile is None:
            openfile = self._nostavesfile

        if openfile is None:
            raise Exception,"No file to open"

        with open(filename,'w') as gamscript:
            gamscript.write("# Open %s in gamera with a classifier\n"%self._nostavesfile)
            with open("gamscripthead.py") as gamscript_head:
                gamscript.write(gamscript_head.read())

            gamscript.write("\n####\n")
            gamscript.write("image = load_image(\"%s\")\n"%self._nostavesfile)
            gamscript.write("ccs = image.cc_analysis()\n")
            gamscript.write("classifier.display(ccs,image)\n")
        self._l.debug("Saved file %s (duration %f)",filename,(time.time()-start))
        return filename

    def save_color_segmented(self,filename=None):
        start = time.time()
        if self._mi is None:
            self._init_mi()

        if filename is None:
            filename = self._genfilename(postfix="-colorseg",extension=".png")

        color = self._mi.color_segment(classified_box=True)
        color.save_PNG(filename)
        self._l.debug("Saved file %s (duration %f)",
                      filename,(time.time()-start))
        return filename

    def save_without(self,filename=None,type="png"):
        start = time.time()
        if self._mi is None:
            self._init_mi()

        if filename is None:
            filename = self._genfilename(postfix="-without",extension=(".%s"%type))

        color = self._mi.without()
        if type is "png":
            color.save_PNG(filename)
        elif type is "tif":
            color.save_tiff(filename)
        else:
            raise Exception,"Unknown type %s"%type
        self._l.debug("Saved file %s (duration %f)",
                      filename,(time.time()-start))
        return filename


    def gen_count_yaml(self,filename=None):
        if filename is None:
            filename = self._genfilename(postfix="-colorseg",extension=".yaml")
        if self._mi is None:
            self._init_mi()
        c = self._mi.ccs_overall()
        cl = dict([ (k,len(v)) for k,v in c.iteritems() ])
        with open(filename,"w") as f:
            data = {'text':{'found':cl['text'],'count':cl['text']},
                    'dynamics':{'found':cl['classified'], 'count':cl['classified']
                               }
                   }
            yaml.dump(data,f)
        logging.debug("Wrote %s",filename)



    def _genfilename(self,dir=None,postfix="",extension=".tif"):
        if dir is None:
            dir = self._noend
        filename = "%s/%s-page%02d%s%s"%\
                (dir,self._noend,self._pagenumber,postfix,extension)
        if not os.path.exists(dir):
            self._l.debug("%s dir dint exits. Creating it.",dir)
            os.mkdir(dir)

        return filename
Esempio n. 3
0
        r.highlight(c, RGBPixel(0, 200, 0))

    words = wordlist(t._words(image=bsimg, ccs=inccs), inccs,
                     t._min_wordlength)
    for c in words:
        r.draw_hollow_rect(c.w, RGBPixel(255, 0, 0))
    p = t._word_projections(words=words)
    # scale to half a page width
    r = mi.draw_y_proj(p, image=r, color=RGBPixel(100, 0, 0), fac=0.5)
    r.save_PNG("%s/%s-03-words-and-word-projections.png" %
               (outputbase, basename))
    logging.debug("03-words-and-word-projections.png")

    # Lines
    r = mi.to_rgb()
    bsimg = mi.without_insidestaves_info()
    p2 = p.rspikes(mi._image.ncols - 1)
    for myr in p2:
        r.draw_hollow_rect(myr, RGBPixel(100, 100, 100))
    lines = t._confirmed_text_lines(words)
    for l in lines:
        r.draw_hollow_rect(l.l, RGBPixel(255, 0, 0))
    occs = mi.ccs_overall()
    for c in occs["text"]:
        r.highlight(c, RGBPixel(0, 150, 0))
    r.save_PNG("%s/%s-04-lines-and-text.png" % (outputbase, basename))
    logging.debug("04-lines-and-text.png")

    r = mi.color_segment()
    r.save_PNG("%s/%s-05-colorsegmented.png" % (outputbase, basename))