def process_glyphs_directory(glyphs_directory, output_dir): aomr_opts = { 'staff_finder': 0, 'lines_per_staff': 4, 'staff_removal': 0, 'binarization': 0, 'discard_size': 12 } print "Processing glyphs directory" for dirpath, dirnames, filenames in os.walk(glyphs_directory): for f in filenames: if f == 'page_glyphs.xml': folder_no = os.path.basename(dirpath) pnum = int(folder_no) input_filename = os.path.join(dirpath, f) lg.debug("Input filename is {0}".format(input_filename)) output_filename = os.path.join(output_dir, folder_no.zfill(4), f) lg.debug("Output filename is {0}".format(output_filename)) shutil.copy(input_filename, output_filename) original_image = os.path.join(output_dir, folder_no.zfill(4), 'original_image.tiff') mei_file_write = os.path.join(output_dir, folder_no.zfill(4), 'liber-usualis-{0}.mei'.format(folder_no.zfill(4))) glyphs = gamera_xml.glyphs_from_xml(output_filename) aomr_obj = AomrObject(original_image, **aomr_opts) data = aomr_obj.run(glyphs) mei_file = AomrMeiOutput.AomrMeiOutput(data, original_image.split('/')[-2], page_number = pnum) meitoxml.meitoxml(mei_file.md, mei_file_write)
def main(original_file, page_file, outdir): aomr_opts = { 'lines_per_staff': 4, 'staff_finder': 0, # 0: Miyao 'staff_removal': 0, 'binarization': 0, 'discard_size': 12 } #FILES TO PROCESS glyphs = gamera_xml.glyphs_from_xml(page_file) lg.debug("Original file: {0}, page file: {1}".format(original_file, page_file)) page_number = int(original_file.split('/')[-2]) fname = os.path.splitext(original_file.split('/')[-1])[0] + ".mei" file_name = "{0}_{1}".format(page_number, fname) # CREATING AOMR OBJECT, FINDING STAVES, AND RETRIEVING STAFF COORDINATES aomr_obj = AomrObject(original_file, **aomr_opts) st_position = aomr_obj.find_staves() # staves position staff_coords = aomr_obj.staff_coords() sorted_glyphs = aomr_obj.miyao_pitch_find(glyphs) # PITCH FINDING # pitch_find = aomr_obj.pitch_find(glyphs, st_position, aomr_opts.get('discard_size')) # print len(pitch_find) # sorted_glyphs = sorted(proc_glyphs, key=itemgetter(1, 2)) # STRUCTURING THE DATA IN JSON data = {} for s, stave in enumerate(staff_coords): contents = [] for glyph, staff, offset, strt_pos, note, octave, clef_pos, clef in sorted_glyphs: glyph_id = glyph.get_main_id() # lg.debug("Glyph ID: {0}".format(glyph_id)) glyph_type = glyph_id.split(".")[0] glyph_form = glyph_id.split(".")[1:] # lg.debug("sg[1]:{0} s:{1} sg{2}".format(sg[1], s+1, sg)) # structure: g, stave, g.offset_x, note, strt_pos if staff == s+1: j_glyph = { 'type': glyph_type, 'form': glyph_form, 'coord': [glyph.offset_x, glyph.offset_y, glyph.offset_x + glyph.ncols, glyph.offset_y + glyph.nrows], 'strt_pitch': note, 'octv': octave, 'strt_pos': strt_pos, 'clef_pos': clef_pos, 'clef': clef} contents.append(j_glyph) data[s] = {'coord':stave, 'content':contents} #print data # CREATING THE MEI FILE mei_file = AomrMeiOutput.AomrMeiOutput(data, file_name, page_number) meitoxml.meitoxml(mei_file.md, os.path.join(outdir, file_name))
def create_mei_files(outdir): aomr_opts = { 'staff_finder': 0, 'lines_per_staff': 4, 'staff_removal': 0, 'binarization': 0, 'discard_size': 12 } 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) # if not "bad_{0}_corr_page_glyphs.xml".format(folder_no.zfill(4)) in filenames: # continue lg.debug("Generating MEI file for {0}".format(pnum)) if "{0}_corr_page_glyphs.xml".format(folder_no.zfill(4)) in filenames: glyphs = gamera_xml.glyphs_from_xml(os.path.join(dirpath, "{0}_corr_page_glyphs.xml".format(folder_no.zfill(4)))) elif "{0}_uncorr_page_glyphs.xml".format(folder_no.zfill(4)) in filenames: glyphs = gamera_xml.glyphs_from_xml(os.path.join(dirpath, "{0}_uncorr_page_glyphs.xml".format(folder_no.zfill(4)))) else: lg.debug("There was no page glyphs file for page {0}".format(pnum)) continue original_image = os.path.join(dirpath, "{0}_staves_only.tiff".format(folder_no.zfill(4))) mei_file_write = os.path.join(dirpath, '{0}_uncorr.mei'.format(folder_no.zfill(4))) aomr_obj = AomrObject(original_image, **aomr_opts) try: data = aomr_obj.run(glyphs) except OverflowError, e: lg.debug("Could not do detection on {0} because {1}".format(pnum, e)) continue if not data: # no data was returned. lg.debug("No data was found for {0}".format(pnum)) continue mei_file = AomrMeiOutput.AomrMeiOutput(data, "{0}_original_image.tiff".format(pnum), page_number = pnum) meitoxml.meitoxml(mei_file.md, mei_file_write)
for i, n in enumerate(self.glyph['form']): if n == ntype: j = copy.copy(i) - 1 if j == 0: idxarray.append(0) while j: if self.__is_valid_note_indicator(self.glyph['form'][j]): idxarray.append(j) break else: j -= 1 def __is_valid_note_indicator(self, form): # used to test if a form is a valid indicator of a note (and not a q, dot, or anything else) if form.isdigit(): return True elif len(form) == 2 and form.startswith("u") or form.startswith("d"): return True else: return False if __name__ == "__main__": v = AomrMeiOutput(test_data, 'foo..jpg') from pymei.Export import meitoxml meitoxml.meitoxml(v.md, 'testfile.mei') # [1] http://wwvv.newadvent.org/cathen/10765b.htm; Some of the liquescent # neums have special names. Thus the liquescent podatus is called epiphonus, # the liquescent clivis, cephalicus, the liquescent climacus, ancus.
def __note_addition_figurer_outer(self, ntype, idxarray): for i,n in enumerate(self.glyph['form']): if n == ntype: j = copy.copy(i) - 1 if j == 0: idxarray.append(0) while j: if self.__is_valid_note_indicator(self.glyph['form'][j]): idxarray.append(j) break else: j -= 1 def __is_valid_note_indicator(self, form): # used to test if a form is a valid indicator of a note (and not a q, dot, or anything else) if form.isdigit(): return True elif len(form) == 2 and form.startswith("u") or form.startswith("d"): return True else: return False if __name__ == "__main__": v = AomrMeiOutput(test_data, 'foo..jpg') from pymei.Export import meitoxml meitoxml.meitoxml(v.md, 'testfile.mei') # [1] http://wwvv.newadvent.org/cathen/10765b.htm; Some of the liquescent # neums have special names. Thus the liquescent podatus is called epiphonus, # the liquescent clivis, cephalicus, the liquescent climacus, ancus.
from pymei.Helpers import template from pymei.Export import meitoxml d = template.create('mix') meitoxml.meitoxml(d, 'yum.mei')
mei.add_children([meihead, music]) meifile.addelement(mei) return meifile # import hocr and mei files into lists and strip extension where useful hocrfiles=[x.split('.')[0] for x in glob.glob('????.html')] allmeifiles=glob.glob('*.mei') meifiles=[x.split('_')[0] for x in allmeifiles] # for each hocr file: if corresponding mei file exists, open mei and edit - if not, create new mei if options.corrected: for hocrfile in hocrfiles: output_name='%s_corr.mei' % (hocrfile,) if '%s_corr.mei' % (hocrfile,) in allmeifiles else '%s_uncorr.mei' % (hocrfile,) meifile=xmltomei.xmltomei(output_name) if hocrfile in meifiles else create_mei(hocrfile) surface=meifile.search('surface')[0] section=meifile.search('section')[0] add_text_lines(hocrfile, surface, section) meitoxml.meitoxml(meifile, '../mei_corrtxt/%s' % (output_name,)) else: for hocrfile in hocrfiles: meifile=MeiDocument.MeiDocument() mei=mod.mei_() surface=mod.surface_() section=mod.section_() mei.add_children([surface, section]) add_text_lines(hocrfile, surface, section) meifile.addelement(mei) meitoxml.meitoxml(meifile, '../mei_uncorrtxt/%s_mei_fragment.mei' % (hocrfile,))
"strt_pitch": "E", "strt_pos": 5, }, { "type": "neume", "form": ["torculus", "2", "4"], "coord": [213, 179, 26, 35], "strt_pitch": "B", "strt_pos": 5, }, ], }, 2: { "coord": [4, 5, 6, 7], "content": [{"type": "", "form": [], "coord": [], "strt_pitch": "A", "strt_pos": ""}], }, } v = GameraMeiOutput(test_data) from pymei.Export import meitoxml meitoxml.meitoxml(v.md, "testfile.mei") # pdb.set_trace() # [1] http://wwvv.newadvent.org/cathen/10765b.htm; Some of the liquescent # neums have special names. Thus the liquescent podatus is called epiphonus, # the liquescent clivis, cephalicus, the liquescent climacus, ancus.