def measure_projections(page, title=None, p=0, m=0): all_measures = [] cols = [] measure = m if not hasattr(page, 'systems'): return pd.DataFrame() for i, system in enumerate(page.systems): nb = len(system['barlines']) - 1 measures = [[] for _ in xrange(nb)] cols += [(title, measure + k, p, i, k) for k in xrange(nb)] for j, staffnum in enumerate(xrange(system['start'], system['stop'] + 1)): staff = bitimage.as_hostimage(page.staves.extract_staff(staffnum)).astype(bool) for k in xrange(nb): measure_img = staff[:, int(system['barlines'][k,:,0].mean()) : int(system['barlines'][k+1,:,0].mean())] scaled_measure = scipy.misc.imresize(measure_img, (5*6, int(30.0 * measure_img.shape[1] / measure_img.shape[0]))) measures[k] += list(scaled_measure.sum(1)) all_measures += measures measure += nb if len(cols): cols = pd.MultiIndex.from_tuples(cols, names='score measure page staff staffmeasure'.split()) all_measures = pd.DataFrame(all_measures, index=cols) return all_measures else: return pd.DataFrame()
def measure_projections(page, title=None, p=0, m=0): all_measures = [] cols = [] measure = m if not hasattr(page, 'systems'): return pd.DataFrame() for i, system in enumerate(page.systems): nb = len(system['barlines']) - 1 measures = [[] for _ in xrange(nb)] cols += [(title, measure + k, p, i, k) for k in xrange(nb)] for j, staffnum in enumerate( xrange(system['start'], system['stop'] + 1)): staff = bitimage.as_hostimage( page.staves.extract_staff(staffnum)).astype(bool) for k in xrange(nb): measure_img = staff[:, int(system['barlines'][k, :, 0].mean() ):int(system['barlines'][k + 1, :, 0].mean())] scaled_measure = scipy.misc.imresize( measure_img, (5 * 6, int(30.0 * measure_img.shape[1] / measure_img.shape[0]))) measures[k] += list(scaled_measure.sum(1)) all_measures += measures measure += nb if len(cols): cols = pd.MultiIndex.from_tuples( cols, names='score measure page staff staffmeasure'.split()) all_measures = pd.DataFrame(all_measures, index=cols) return all_measures else: return pd.DataFrame()
class_num = [] for score in CORPUS: try: score = metaomr.open(score) except Exception: continue while len(score): page = score[0] preprocessing.process(page) if type( page.staff_dist ) is not tuple and page.staff_dist is not None and page.staff_dist >= 8: image, scale = forest.scale_img(page) pred = forest.predict(forest.classifier, image, get_classes=False) images.append(bitimage.as_hostimage(image)) class_num.append(pred.get()) del image del page del score[0] opencl.q.finish() gc.collect() if len(images) > 25: break import numpy as np nums = np.concatenate([c.ravel() for c in class_num]) NUM_PATCHES = 100 CUTOFF = np.percentile(nums, 100.0 * NUM_PATCHES / len(nums))
images = [] class_num = [] for score in CORPUS: try: score = metaomr.open(score) except Exception: continue while len(score): page = score[0] preprocessing.process(page) if type(page.staff_dist) is not tuple and page.staff_dist is not None and page.staff_dist >= 8: image, scale = forest.scale_img(page) pred = forest.predict(forest.classifier, image, get_classes=False) images.append(bitimage.as_hostimage(image)) class_num.append(pred.get()) del image del page del score[0] opencl.q.finish() gc.collect() if len(images) > 25: break import numpy as np nums=np.concatenate([c.ravel() for c in class_num]) NUM_PATCHES = 100 CUTOFF = np.percentile(nums, 100.0*NUM_PATCHES/len(nums))
if type(page.staff_dist) is not int: raise Exception('staffsize failed') page.process() except Exception, e: print imslpid, p, '-', e pages[p] = None for movement in xrange(len(mvmt_start) - 1): mvmt_path = 'mvmt%d' % movement for p in xrange( mvmt_start[movement, 0], mvmt_start[movement + 1, 0] if mvmt_start[movement + 1, 1] == 0 else mvmt_start[movement + 1, 0] + 1): page = pages[p] if page is None: continue img = bitimage.as_hostimage(page.img)[:, :page.orig_size[1]] if p == mvmt_start[movement, 0] and mvmt_start[movement, 1] > 0: new_system = page.systems[mvmt_start[movement, 1]]['start'] b = page.boundaries[new_system] boundary = np.repeat(b[:, 1], b[1, 0] - b[0, 0]) img = img[:, :min(page.orig_size[1], len(boundary))] boundary = boundary[:img.shape[1]] img[np.arange(img.shape[0])[:, None] < boundary[None, :]] = 0 elif (p == mvmt_start[movement + 1, 0] and 0 < mvmt_start[movement + 1, 1] < len(page.systems)): new_system = page.systems[mvmt_start[movement + 1, 1]]['start'] b = page.boundaries[new_system] boundary = np.repeat(b[:, 1], b[1, 0] - b[0, 0]) img = img[:, :min(page.orig_size[1], len(boundary))] boundary = boundary[:img.shape[1]] img[np.arange(img.shape[0])[:, None] >= boundary[None, :]] = 0
if type(page.staff_dist) is not int: raise Exception('staffsize failed') page.process() except Exception, e: print imslpid, p, '-', e pages[p] = None for movement in xrange(len(mvmt_start) - 1): mvmt_path = 'mvmt%d' % movement for p in xrange(mvmt_start[movement, 0], mvmt_start[movement+1, 0] if mvmt_start[movement+1, 1] == 0 else mvmt_start[movement+1, 0] + 1): page = pages[p] if page is None: continue img = bitimage.as_hostimage(page.img)[:, :page.orig_size[1]] if p == mvmt_start[movement, 0] and mvmt_start[movement, 1] > 0: new_system = page.systems[mvmt_start[movement,1]]['start'] b = page.boundaries[new_system] boundary = np.repeat(b[:,1], b[1,0] - b[0,0]) img = img[:, :min(page.orig_size[1], len(boundary))] boundary = boundary[:img.shape[1]] img[np.arange(img.shape[0])[:, None] < boundary[None, :]] = 0 elif (p == mvmt_start[movement+1, 0] and 0 < mvmt_start[movement+1, 1] < len(page.systems)): new_system = page.systems[mvmt_start[movement+1,1]]['start'] b = page.boundaries[new_system] boundary = np.repeat(b[:,1], b[1,0] - b[0,0]) img = img[:, :min(page.orig_size[1], len(boundary))] boundary = boundary[:img.shape[1]] img[np.arange(img.shape[0])[:, None] >= boundary[None, :]] = 0