def openTifFile(self, event): tiffilename = "" foldir = "" global swap2 alist = [] dialog = Args([FileOpen("Send a .tif or .png file to Aruspix", tiffilename, "*.tif;*.png"), # #FileOpen("or a .png file", tiffilename, "*.tif;*.png"), Directory("or all .tif and .png files in folder", foldir)], name = "Open") params = dialog.show() #print; print "params"; print params; print if not params: return if params[0]: # print 'it is a single file: ' # print params[0] alist.append(params[0]) else: ## It is a folder # print "yes! a folder"; print alist = Tools.dirEntries(params[1], True, 'tif', 'png') #app_path = '/Users/gabriel/Documents/code/aruspix/Debug/Aruspix.app/Contents/MacOS/Aruspix' app_path = '/Applications/Aruspix.app/Contents/MacOS/Aruspix' #print; print 'alist: '; print alist; print for i_file in alist: print 'Processing '; print i_file; print o_file = os.path.splitext(i_file); #print 'o_file'; print o_file; print if o_file[1] == '.png': # print; print "PNG" f = 'convert ' + i_file + ' -compress None ' + o_file[0] + '.tif' subprocess.Popen(f, shell = True) f = app_path + ' -q -e Rec -p ' + o_file[0] + '.tif ' + o_file[0] + '.axz' p = subprocess.Popen(f, shell = True) p.wait() subprocess.Popen("rm " + o_file[0] + '.tif' , shell=True) # erasing the created TIF else: # print; print "TIF" f = app_path + ' -q -e Rec -p ' + i_file + ' ' + o_file[0] + '.axz' p = subprocess.Popen(f, shell = True) p.wait() if params[0]: axfilename = o_file[0] + '.axz' #; print "axfilename: "; print axfilename; print axfile = AxFile(axfilename, "") swap2 = axfile.get_img0() #; print"swap: "; print swap2; print name = var_name.get("aruspix_img",\ self._shell.locals) self._shell.run("%s = %s.swap2.visualize()"\ % (name, self.label )) print 'Done!' print
def main(options): init_gamera() for dirpath, dirnames, filenames in os.walk(options['axz']): for f in filenames: if f.startswith("."): continue lg.debug("Processing: {0}".format(f)) pagenum = f.split("_")[2].split('.')[0] outdir = os.path.join(options['out'], pagenum) os.mkdir(outdir) axzfile = os.path.join(dirpath, f) ax = AxFile(axzfile, "") axtmp = ax.tmpdir img0 = ax.get_img0() staff_text = img0.extract(3) lyrics = img0.extract(4) titles = img0.extract(5) if options['all']: # store each in a separate file stfile = os.path.join(outdir, "{0}_staff_text.tiff".format(pagenum)) lg.debug("Saving {0}".format(stfile)) save_image(staff_text, stfile) lfile = os.path.join(outdir, "{0}_lyrics.tiff".format(pagenum)) lg.debug("Saving {0}".format(lfile)) save_image(lyrics, lfile) tfile = os.path.join(outdir, "{0}_titles.tiff".format(pagenum)) lg.debug("Saving {0}".format(tfile)) save_image(titles, tfile) if options['mga']: or_image = staff_text.or_image(lyrics, False) # now merge in the titles, but do it in place on the new images. or_image.or_image(titles, True) all_layers = os.path.join(outdir, "{0}_all_text_layers.tiff".format(pagenum)) lg.debug("Saving {0}".format(all_layers)) save_image(or_image, all_layers) elif options['mgs']: or_image == staff_text.or_image(titles, False) some_layers = os.path.join(outdir, "{0}_some_text_layers.tiff".format(pagenum)) lg.debug("Saving {0}".format(some_layers)) save_image(or_image, some_layers) lg.debug("====> Finished processing {0} <=====".format(f))
def process_axz_directory(axz_directory, outputdir, skip=False): print "Processing AXZ Folder" axzfiles_processed = [] for dirpath, dirnames, filenames in os.walk(axz_directory): for f in filenames: lg.debug("Processing file: {0}".format(f)) if f.startswith("."): continue pagenum = f.split("_")[2].split('.')[0] # create an output directory axzfiles_processed.append(pagenum) if not skip: outdir = os.path.join(outputdir, pagenum) os.mkdir(outdir) axzfile = os.path.join(dirpath, f) ax = AxFile(axzfile, "") axtmp = ax.tmpdir staves = ax.get_img0().extract(0) sfile = os.path.join(outdir, "original_image.tiff") save_image(staves, sfile) return axzfiles_processed
return fileList print print print #dirEntries(r'/Users/gabriel/Documents/imgs', True, 'tif') #LUfolder = '/Users/gabriel/Documents/imgs/Liber_Usualis_WORK/processed_tifs/' #LUfolder = '/Users/gabriel/Desktop/Liber_Usualis_Test_Files' LUfolder = './img_test/' for name in dirEntries(LUfolder, True, 'axz'): print name o_file = os.path.splitext(name); print o_file axfile = AxFile(name, LUfolder) #print axfile global swap swap = axfile.get_img0() print swap staves0 = swap.extract(0) #print staves0 staves0.save_tiff(o_file[0] + '_ST.tif')
def openAxFile(self, event): # # let the user choose the parameters # #pp_module="PsaltikiPage" axfilename = "" tmpdir = "" imgdir = "" imgvisual = True imgmusic = True imglyrics = False imgborder = False imgornate = False imgtext = False imgtitle = False imgflat = False imggrey = False #opt = c_opt() # start options dialog dialog = Args([FileOpen("Aruspix file", axfilename, "*.axz"), #Directory("or all Aruspix Files in folder", imgdir), Directory("Temporary directory (optional)", tmpdir), Check("Visualize image","",True), Check("Extract music staves","",True), Check("Extract lyrics"), Check("Extract title elements"), Check("Extract ornate letters"), Check("Extract text in staff"), Check("Extract borders"), Check("Flatten as BW"), Check("Original greyscale image")], name = "Select options") params = dialog.show() if not params: return i = 0 axfilename = params[i]; i+=1 if axfilename == "": return tmpdir = params[i]; i+=1 if tmpdir == None: tmpdir = "" imgvisual = params[i]; i+=1 imgmusic = params[i]; i+=1 imglyrics = params[i]; i+=1 imgborders = params[i]; i+=1 imgornate = params[i]; i+=1 imgtext = params[i]; i+=1 imgtitle = params[i]; i+=1 imgflat = params[i]; i+=1 imggrey = params[i]; i+=1 axfile = AxFile(axfilename, tmpdir) global swap swap = axfile.get_img0() if imgvisual == True: name = var_name.get("aruspix_img",\ self._shell.locals) self._shell.run("%s = %s.swap.visualize()"\ % (name, self.label )) if imgmusic == True: name = var_name.get("staves",\ self._shell.locals) self._shell.run("%s = %s.swap.extract(0)"\ % (name, self.label )) if imglyrics == True: name = var_name.get("lyrics",\ self._shell.locals) self._shell.run("%s = %s.swap.extract(4)"\ % (name, self.label )) if imgborders == True: name = var_name.get("borders",\ self._shell.locals) self._shell.run("%s = %s.swap.extract(1)"\ % (name, self.label )) if imgornate == True: name=var_name.get("ornate_letters",\ self._shell.locals) self._shell.run("%s = %s.swap.extract(2)"\ % (name, self.label )) if imgtext == True: name=var_name.get("text_in_staff",\ self._shell.locals) self._shell.run("%s = %s.swap.extract(3)"\ % (name, self.label )) if imgtitle == True: name=var_name.get("titles",\ self._shell.locals) self._shell.run("%s = %s.swap.extract(5)"\ % (name, self.label )) if imgflat == True: name=var_name.get("all",\ self._shell.locals) self._shell.run("%s = %s.swap.flatten()"\ % (name, self.label )) if imggrey == True: swap = axfile.get_img1() name=var_name.get("original",\ self._shell.locals) self._shell.run("%s = %s.swap.image_copy()"\ % (name, self.label ))
def process_axz_directory(directory, class_glyphs, class_weights, outputdir): print "Processing AXZ Folder" for dirpath, dirnames, filenames in os.walk(directory): # if os.path.abspath(directory) == os.path.abspath(dirpath): # continue for f in filenames: if f == ".DS_Store": continue pagenum = f.split("_")[-1].strip('.axz') print "Loading page ", str(pagenum) # create an output directory outdir = os.path.join(outputdir, pagenum) os.mkdir(outdir) axzfile = os.path.join(dirpath, f) ax = AxFile(axzfile, "") axtmp = ax.tmpdir staves = ax.get_img0().extract(0) # shutil.move(tfile[1], os.path.join(outdir, "original_image.tiff")) sfile = os.path.join(outdir, "original_image.tiff") save_image(staves, sfile) # lg.debug("Tempfile is: {0}".format(tfile[1])) # grab and remove the staves aomr_opts = { 'lines_per_staff': 4, 'staff_finder': 0, 'staff_removal': 0, 'binarization': 0, 'discard_size': 12 # GVM, was 6 } aomr_obj = AomrObject(sfile, **aomr_opts) try: lg.debug("Finding Staves") s = aomr_obj.find_staves() except Exception, e: lg.debug("Cannot find staves: {0} because {1}".format(pagenum, e)) continue lg.debug("S is: {0}".format(s)) if not s: lg.debug("no staves were found") os.remove(sfile) os.rmdir(outdir) try: aomr_obj.remove_stafflines() except Exception, e: lg.debug("Cannot remove stafflines: {0} because {1}".format(pagenum, e)) continue cknn = knn.kNNNonInteractive(class_glyphs, 'all', True, 1) # cknn.load_settings(class_weights) ccs = aomr_obj.img_no_st.cc_analysis() func = classify.BoundingBoxGroupingFunction(4) # classified_image = cknn.group_and_update_list_automatic(ccs, grouping_function, max_parts_per_group=4, max_graph_size=16) classified_image = cknn.group_and_update_list_automatic( ccs, grouping_function=func, max_parts_per_group=4, max_graph_size=16 ) lg.debug("save all the files into this directory") cknn.save_settings(os.path.join(outdir, "classifier_settings.xml")) cknn.generate_features_on_glyphs(classified_image) s = SymbolTable() for split in plugin.methods_flat_category("Segmentation", ONEBIT): s.add("_split." + split[0]) s.add("_group") s.add("_group._part") avg_punctum_col = aomr_obj.average_punctum(cknn.get_glyphs()) print 'average punctum column size = ', avg_punctum_col glyphs_center_of_mass = aomr_obj.x_projection_vector(cknn.get_glyphs(), avg_punctum_col, aomr_opts.get('discard_size')) print 'center of mass for each glyph = ', glyphs_center_of_mass gamera_xml.WriteXMLFile(glyphs=classified_image, with_features=True).write_filename(os.path.join(outdir, "page_glyphs.xml")) # gamera_xml.WriteXMLFile(symbol_table=s).write_filename(os.path.join(outdir, "symbol_table.xml")) # cknn.to_xml_filename(os.path.join(outdir, "classifier_glyphs.xml"), with_features=True) save_image(aomr_obj.img_no_st, os.path.join(outdir, "source_image.tiff")) # clean up del aomr_obj.img_no_st del aomr_obj del classified_image del ax del cknn