def OnChangeFeatures(self, event): #--------------------------------------------------------------------------- from gamera import plugin from gamera.core import ONEBIT from gamera.core import ImageBase from gamera.args import Args, Check if self.classifier is None: gui.message("No classifier loaded") return allFeatures = [x[0] for x in plugin.methods_flat_category("Features", ONEBIT)] allFeatures.sort() existingFeatures = [x[0] for x in ImageBase.get_feature_functions(self.classifier.features)[0]] featureControls = [] for f in allFeatures: featureControls.append(Check('', f, default=(f in existingFeatures))) dialog = Args(featureControls, name = "Feature selection", title="Select the features you want to use for optimization") result = dialog.show(self) if result is None: gui_util.message("No change applied") return selectedFeatures = [name for check, name in zip(result, allFeatures) if check] self.classifier.change_feature_set(selectedFeatures) self.UpdatePanels()
def save_image(image, filename): """**save_image** (Image(ALL) *image*, String *filename*) Saves an image to a file. The file type is automatically determined from the extension. """ from gamera import plugin methods = plugin.methods_flat_category("File") methods = [y for x, y in methods if x.startswith("save") and x != "save_image"] if len(methods) == 0: raise RuntimeError( "There don't seem to be any imported plugins that can save files. Try running init_gamera(), or explicitly loading the plugins that support file saving, such as tiff_support and png_support." ) for method in methods: for ext in method.exts: if os.path.splitext(filename)[1][1:].lower() == ext.lower(): method.__call__(image, filename) return # For backward compatibility, fall back to tiff if # we can't automatically determine the filetype by # the extension from gamera.plugins import _tiff_support _tiff_support.save_tiff(image, filename)
def OnChangeFeatures(self, event): # --------------------------------------------------------------------------- from gamera import plugin from gamera.core import ONEBIT from gamera.core import ImageBase from gamera.args import Args, Check if self.classifier == None: gui.message("No classifier loaded") return allFeatures = [x[0] for x in plugin.methods_flat_category("Features", ONEBIT)] allFeatures.sort() existingFeatures = [x[0] for x in ImageBase.get_feature_functions(self.classifier.features)[0]] featureControls = [] for f in allFeatures: featureControls.append(Check("", f, default=(f in existingFeatures))) dialog = Args( featureControls, name="Feature selection", title="Select the features you want to use for optimization" ) result = dialog.show(self) if result == None: gui_util.message("No change applied") return selectedFeatures = [name for check, name in zip(result, allFeatures) if check] self.classifier.change_feature_set(selectedFeatures) self.UpdatePanels()
def get_file_extensions(mode): from gamera import plugin import os.path from gamera.backport import sets methods = plugin.methods_flat_category("File") methods = [ y for x, y in methods if x.startswith(mode) and not x.endswith("image") ] if len(methods) == 0: raise RuntimeError( "There don't seem to be any imported plugins that can %s files. Try running init_gamera() or explictly loading file i/o plugins such as tiff_support and png_support." % mode) extensions = sets.Set() types = [] for method in methods: wildcards = ";".join( ["*.%s;*.%s" % (ext.lower(), ext.upper()) for ext in method.exts]) type = "%s Files (%s)|%s" % (method.exts[0].upper(), wildcards, wildcards) types.append(type) # We have to cast the lists to sets here to make Python 2.3.0 happy. extensions.update(sets.Set(method.exts)) extensions.update(sets.Set([x.upper() for x in method.exts])) all_extensions = ";".join(["*.%s" % x for x in extensions]) types.insert(0, "All images (%s)|%s" % (all_extensions, all_extensions)) types.append("All files (*.*)|*.*") return "|".join(types)
def save_image(image, filename): """**save_image** (Image(ALL) *image*, String *filename*) Saves an image to a file. The file type is automatically determined from the extension. """ methods = plugin.methods_flat_category("File") methods = [ y for x, y in methods if x.startswith("save") and x != "save_image" ] if len(methods) == 0: raise RuntimeError( "There don't seem to be any imported plugins that can save files. Try running init_gamera(), or explicitly loading the plugins that support file saving, such as tiff_support and png_support." ) for method in methods: for ext in method.exts: if os.path.splitext(filename)[1][1:].lower() == ext.lower(): method.__call__(image, filename) return None # For backward compatibility, fall back to tiff if # we can't automatically determine the filetype by # the extension _tiff_support.save_tiff(image, filename)
def get_feature_functions(cls, features="all"): from gamera import plugin global all_features if all_features is None: all_features = plugin.methods_flat_category("Features", ONEBIT) all_features.sort() if features == "all" or features is None: functions = all_features return functions, cls._get_feature_vector_size(functions) features = util.make_sequence(features) all_strings = True for feature in features: if not util.is_string_or_unicode(feature): all_strings = False break if not all_strings: import plugin all_functions = False if ( type(features) == tuple and len(features) == 2 and type(features[0]) == list and type(features[1]) == int ): all_functions = True for feature in features[0]: if not ( type(feature) == tuple and util.is_string_or_unicode(feature[0]) and issubclass(feature[1], plugin.PluginFunction) ): all_functions = False break if not all_functions: raise ValueError("'%s' is not a valid way to specify a list of features." % str(features)) else: return features else: features.sort() functions = [] for feature in features: found = 0 for i in all_features: if feature == i[0]: functions.append(i) found = 1 break if not found: raise ValueError("'%s' is not a known feature function." % feature) functions.sort() return functions, cls._get_feature_vector_size(functions)
def get_feature_functions(cls, features='all'): from gamera import plugin global all_features if all_features is None: all_features = plugin.methods_flat_category('Features', ONEBIT) all_features.sort() if features == 'all' or features is None: functions = all_features return functions, cls._get_feature_vector_size(functions) features = util.make_sequence(features) all_strings = True for feature in features: if not util.is_string_or_unicode(feature): all_strings = False break if not all_strings: import plugin all_functions = False if (type(features) == tuple and len(features) == 2 and type(features[0]) == list and type(features[1]) == int): all_functions = True for feature in features[0]: if not (type(feature) == tuple and util.is_string_or_unicode(feature[0]) and issubclass(feature[1], plugin.PluginFunction)): all_functions = False break if not all_functions: raise ValueError( "'%s' is not a valid way to specify a list of features." % str(features)) else: return features else: features.sort() functions = [] for feature in features: found = 0 for i in all_features: if feature == i[0]: functions.append(i) found = 1 break if not found: raise ValueError("'%s' is not a known feature function." % feature) functions.sort() return functions, cls._get_feature_vector_size(functions)
def load_image(filename, compression=DENSE): """**load_image** (FileOpen *filename*, Choice *storage_format* = ``DENSE``) Load an image from the given filename. At present, TIFF and PNG files are supported. *storage_format* The type of `storage format`__ to use for the resulting image. .. __: image_types.html#storage-formats""" from gamera import plugin import os.path methods = plugin.methods_flat_category("File") methods = [ y for x, y in methods if x.startswith("load") and x != "load_image" ] if len(methods) == 0: raise RuntimeError( "There don't seem to be any imported plugins that can load files. Try running init_gamera(), or explicitly loading the plugins that support file loading, such as tiff_support and png_support." ) try: filename = filename.encode('utf8') except: pass # First, try being smart by loading by extension for method in methods: for ext in method.exts: if os.path.splitext(filename)[1].lower() == ext.lower(): try: image = method.__call__(filename, compression) except: pass else: return image # Then just try all options for method in methods: try: image = method.__call__(filename, compression) except: pass else: return image raise IOError("'%s' could not be loaded." % filename)
def load_image(filename, compression=DENSE): """**load_image** (FileOpen *filename*, Choice *storage_format* = ``DENSE``) Load an image from the given filename. At present, TIFF and PNG files are supported. *storage_format* The type of `storage format`__ to use for the resulting image. .. __: image_types.html#storage-formats""" from gamera import plugin methods = plugin.methods_flat_category("File") methods = [y for x, y in methods if x.startswith("load") and x != "load_image"] if len(methods) == 0: raise RuntimeError( "There don't seem to be any imported plugins that can load files. Try running init_gamera(), or explicitly loading the plugins that support file loading, such as tiff_support and png_support." ) # First, try being smart by loading by extension for method in methods: for ext in method.exts: if os.path.splitext(filename)[1].lower() == ext.lower(): try: image = method.__call__(filename, compression) except: pass else: return image # Then just try all options for method in methods: try: image = method.__call__(filename, compression) except: pass else: return image raise IOError("'%s' could not be loaded." % filename)
def get_file_extensions(mode): from gamera import plugin from gamera.backport import sets methods = plugin.methods_flat_category("File") methods = [y for x, y in methods if x.startswith(mode) and not x.endswith("image")] if len(methods) == 0: raise RuntimeError("There don't seem to be any imported plugins that can %s files. Try running init_gamera() or explictly loading file i/o plugins such as tiff_support and png_support." % mode) extensions = sets.Set() types = [] for method in methods: wildcards = ";".join(["*.%s;*.%s" % (ext.lower(), ext.upper()) for ext in method.exts]) type = "%s Files (%s)|%s" % (method.exts[0].upper(), wildcards, wildcards) types.append(type) # We have to cast the lists to sets here to make Python 2.3.0 happy. extensions.update(sets.Set(method.exts)) extensions.update(sets.Set([x.upper() for x in method.exts])) all_extensions = ";".join(["*.%s" % x for x in extensions]) types.insert(0, "All images (%s)|%s" % (all_extensions, all_extensions)) types.append("All files (*.*)|*.*") return "|".join(types)
def aomr_remaining_pages(outdir, classifier): aomr_opts = { 'staff_finder': 0, 'lines_per_staff': 4, 'staff_removal': 0, 'binarization': 0, 'discard_size': 14 } for dirpath, dirnames, filenames in os.walk(outdir): if dirpath == outdir: continue if ".git" in dirpath.split("/"): continue folder_no = os.path.basename(dirpath) pnum = int(folder_no) lg.debug("Processing page {0}".format(pnum)) # these files give us problems. if pnum in [41, 87, 100]: lg.debug("Skipping page {0}".format(pnum)) continue corrpg = "{0}_corr_page_glyphs.xml".format(folder_no.zfill(4)) # badpg = "bad_{0}_corr_page_glyphs.xml".format(folder_no.zfill(4)) if not corrpg in filenames: # we need to perform aomr. original_image = os.path.join(dirpath, "{0}_staves_only.tiff".format(folder_no.zfill(4))) aomr_obj = AomrObject(original_image, **aomr_opts) try: lg.debug("Finding Staves") s = aomr_obj.find_staves() except Exception, e: lg.debug("Cannot find staves: {0} because {1}".format(pnum, e)) continue if not s: lg.debug("No staves were found on page {0}".format(pnum)) continue try: aomr_obj.remove_stafflines() except Exception, e: lg.debug("Cannot remove stafflines: {0} because {1}".format(pnum, e)) continue cknn = knn.kNNNonInteractive(classifier, 'all', True, 1) ccs = aomr_obj.img_no_st.cc_analysis() func = classify.BoundingBoxGroupingFunction(4) 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 from page {0}".format(pnum)) 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") gamera_xml.WriteXMLFile(glyphs=classified_image, with_features=True).write_filename(os.path.join(dirpath, "{0}_uncorr_page_glyphs.xml".format(folder_no.zfill(4)))) del aomr_obj del classified_image del cknn
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