def _init_mi(self): """ Setup a MusicImage with our classifier """ self._mi = IllMusicImage(self._file,classifier=self._classifier)
else: classified = [] return seg['text'],seg['inside'],seg['outside'],classified if __name__ == '__main__': from gamera.core import * from class_dynamic import Classifier_with_remove from ill_music import IllMusicImage import sys #LOG_FILENAME = '/tmp/logging_example.out' FORMAT = "%(asctime)-15s %(levelname)s [%(name)s.%(funcName)s] %(message)s" logging.basicConfig(level=logging.DEBUG,format=FORMAT) init_gamera() c = Classifier_with_remove(training_filename="preomr_edited_cnn.xml") c.set_k(1) filename = sys.argv[-1] #c.classifier.load_settings("gasettings.txt") mi = IllMusicImage(load_image(filename),classifier=c) ret = mi.without() ret.save_PNG("%s_Removed.png"%filename) logging.debug("Done with %s"%filename) ret = mi.color_segment(classified_box=True) ret.save_PNG("%s_ColorSegment.png"%filename) logging.debug("Done with %s"%filename)
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
""" seg = self.ccs_overall() if classify: classified = seg['classified'] else: classified = [] return seg['text'], seg['inside'], seg['outside'], classified if __name__ == '__main__': from gamera.core import * from class_dynamic import Classifier_with_remove from ill_music import IllMusicImage import sys #LOG_FILENAME = '/tmp/logging_example.out' FORMAT = "%(asctime)-15s %(levelname)s [%(name)s.%(funcName)s] %(message)s" logging.basicConfig(level=logging.DEBUG, format=FORMAT) init_gamera() c = Classifier_with_remove(training_filename="preomr_edited_cnn.xml") c.set_k(1) filename = sys.argv[-1] #c.classifier.load_settings("gasettings.txt") mi = IllMusicImage(load_image(filename), classifier=c) ret = mi.without() ret.save_PNG("%s_Removed.png" % filename) logging.debug("Done with %s" % filename) ret = mi.color_segment(classified_box=True) ret.save_PNG("%s_ColorSegment.png" % filename) logging.debug("Done with %s" % filename)
if __name__ == '__main__': import sys import os from ill_music import IllMusicImage FORMAT = "%(asctime)-15s %(levelname)s [%(name)s.%(funcName)s] %(message)s" logging.basicConfig(level=logging.DEBUG,format=FORMAT) def ensure_dir(dir): if not os.path.exists(dir): logging.debug("%s dir dint exits. Creating it.",dir) os.mkdir(dir) init_gamera() filename=sys.argv[-1] basename=filename[:-4] mi = IllMusicImage(filename,training_filename="preomr_edited_cnn.xml") ensure_dir("texttest") ensure_dir("texttest/%s"%basename) outputbase = "texttest/%s"%basename t = mi._text() # show row projections p,sp = t._possible_text_areas_projection(mi.without_insidestaves_info()) # add after threshold image mp = max(p) fac = mi._image.ncols/mp logging.debug("Resize proj factor: %d, max:%d, ncols:%d, max*nc:%d",fac,mp,mi._image.ncols,mp*fac) r =mi.with_row_projections(fac=fac) r = mi.draw_y_proj(p,image=r,color=RGBPixel(100,0,0),fac=fac) r.save_PNG("%s/%s-01-rowprojections.png"%(outputbase,basename)) logging.debug("01-rowprojections.png")
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from gamera.core import * import logging import sys import re from class_dynamic import Classifier_with_remove from ill_music import IllMusicImage if __name__ == '__main__': FORMAT = "%(asctime)-15s %(levelname)s [%(name)s.%(funcName)s] %(message)s" logging.basicConfig(level=logging.INFO,format=FORMAT) init_gamera() files = [] if sys.argv[1][-4:] == ".xml": c = Classifier_with_remove(training_filename=sys.argv[1]) files = sys.argv[2:] else: c = Classifier_with_remove(training_filename="preomr_edited_cnn.xml") files = sys.argv[1:] c.set_k(1) sys.stdout.flush() for imgname in files: m = re.match(r"^(.*)\.[^\.]+$",imgname) noend = m.group(1) mi = IllMusicImage(imgname,classifier=c) without = mi.without() without.save_PNG("%s_clean.png"%noend) print "%s_clean.png"%noend sys.stdout.flush()
if __name__ == '__main__': import sys import os from ill_music import IllMusicImage FORMAT = "%(asctime)-15s %(levelname)s [%(name)s.%(funcName)s] %(message)s" logging.basicConfig(level=logging.DEBUG, format=FORMAT) def ensure_dir(dir): if not os.path.exists(dir): logging.debug("%s dir dint exits. Creating it.", dir) os.mkdir(dir) init_gamera() filename = sys.argv[-1] basename = filename[:-4] mi = IllMusicImage(filename, training_filename="preomr_edited_cnn.xml") ensure_dir("texttest") ensure_dir("texttest/%s" % basename) outputbase = "texttest/%s" % basename t = mi._text() # show row projections p, sp = t._possible_text_areas_projection(mi.without_insidestaves_info()) # add after threshold image mp = max(p) fac = mi._image.ncols / mp logging.debug("Resize proj factor: %d, max:%d, ncols:%d, max*nc:%d", fac, mp, mi._image.ncols, mp * fac) r = mi.with_row_projections(fac=fac) r = mi.draw_y_proj(p, image=r, color=RGBPixel(100, 0, 0), fac=fac) r.save_PNG("%s/%s-01-rowprojections.png" % (outputbase, basename))