Ejemplo n.º 1
0
def load_ideal_set():
    global IDEAL_SET
    if IDEAL_SET is None:
        ideal_imgs = glob.glob('resources/ideal_set/*.png')
        IDEAL_SET = []
        for img in ideal_imgs:
            page, = metaomr.open(img)
            IDEAL_SET.append(KanungoImage(normalized_page(page)))
    return IDEAL_SET
Ejemplo n.º 2
0
import gc
from forest_config import COLOR_LABELS
import sys
import os
import numpy

IMSLP = '../IMSLP'
all_imslp = list(numpy.random.choice(os.listdir(IMSLP), 10))
CORPUS = [os.path.join(IMSLP, p) for p in all_imslp]

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]
Ejemplo n.º 3
0
import metaomr

INPUT = 'samples/IMSLP73123.pdf'

score = metaomr.open(INPUT)
for page in score:
  page.process()
  page.show()
import pylab
pylab.show()
Ejemplo n.º 4
0
# Test staff/system recognition on solo piano sheet music.
# This assumes an even number of staves where systems are 2 staves each.
import env
import metaomr
from metaomr import structure
import sys
import gc
import logging

good_pages = 0
total_pages = 0
for doc in sys.argv[1:]:
    logging.warn(doc)
    pages = metaomr.open(doc)
    for i, page in enumerate(pages):
        structure.staffsize.staffsize(page)
        if type(page.staff_dist) is tuple:
            print 'Multiple staff sizes at %s p.%d' % (doc, i)
        else:
            structure.process(page)
            # All staves should start at the same x coordinate, except possibly
            # at the start of a movement where 2 staves are indented
            # All staves should always end at the same x coordinate
            MAX_DIST = 2*page.staff_dist
            starts = page.staves[:, 0]
            start_maxdiff = max(starts) - min(starts) if len(starts) else 0
            if start_maxdiff > MAX_DIST:
                # Search for start of a movement
                min_start = min(starts)
                max_start = max(starts)
                if (((starts - min_start) >= MAX_DIST).sum() == 2
Ejemplo n.º 5
0
from PIL import Image
import gc
import pandas as pd

sonatas = pd.DataFrame.from_csv('resources/beethoven_sonatas.csv', header=None)
mvmts = pd.DataFrame.from_csv('results/beethoven_movements.csv')

dir_path = sys.argv[1]
imslpid = re.search('IMSLP[0-9]+', dir_path).group(0)
if imslpid not in sonatas.index or imslpid in mvmts.index:
    sys.exit(0)

import metaomr
from metaomr import bitimage, deskew
pages = sorted(glob.glob(os.path.join(dir_path, '*.pbm')))
pages = [metaomr.open(page)[0] for page in pages]
if len(pages) == 0:
    sys.exit(0)
for p, page in enumerate(pages):
    sys.stderr.write('%d ' % p)
    try:
        page.preprocess()
        if type(page.staff_dist) is not int:
            raise Exception('staffsize failed')
        page.layout()
        gc.collect()
    except Exception, e:
        print imslpid, p, '-', e
        pages[p] = None

mvmt_start = []
Ejemplo n.º 6
0
import env
import metaomr
from metaomr.staves import validation
from metaomr import orientation, staffsize
import numpy as np
import gzip

import sys
import cPickle

pages = metaomr.open(sys.argv[1])
output = sys.argv[2]

staves = []
for i, page in enumerate(pages):
    staffsize.staffsize(page)
    if type(page.staff_dist) is tuple or page.staff_dist is None:
        continue
    orientation.rotate(page)
    staffsize.staffsize(page)

    validator = validation.StaffValidation(page)
    scores = validator.score_staves()
    ns = page.staves.nostaff().get()
    for i, score in enumerate(scores['score'][:-1]):
        staff = page.staves()[i]
        y0 = int(staff[:,1].min() - page.staff_dist*2)
        y1 = int(staff[:,1].max() + page.staff_dist*2)
        if score >= 0.9 and y0 >= 0:
            img = ns[y0:y1]
            img = np.unpackbits(img).reshape((-1, 4096))
Ejemplo n.º 7
0
import env
import metaomr
from metaomr.staves import validation
from metaomr import orientation, staffsize
import numpy as np
import gzip

import sys
import cPickle

pages = metaomr.open(sys.argv[1])
output = sys.argv[2]

staves = []
for i, page in enumerate(pages):
    staffsize.staffsize(page)
    if type(page.staff_dist) is tuple or page.staff_dist is None:
        continue
    orientation.rotate(page)
    staffsize.staffsize(page)

    validator = validation.StaffValidation(page)
    scores = validator.score_staves()
    ns = page.staves.nostaff().get()
    for i, score in enumerate(scores['score'][:-1]):
        staff = page.staves()[i]
        y0 = int(staff[:, 1].min() - page.staff_dist * 2)
        y1 = int(staff[:, 1].max() + page.staff_dist * 2)
        if score >= 0.9 and y0 >= 0:
            img = ns[y0:y1]
            img = np.unpackbits(img).reshape((-1, 4096))
Ejemplo n.º 8
0
# We can test scores for piano solo because we know what to expect
# (an even number of staves and staff systems with 2 staves each.)
# This test can be used with any scores from IMSLP or other sources.

import metaomr
import metaomr.opencl
import gc
import sys

PASS = 0
TOTAL = 0
for path in sys.argv[1:]:
    score = metaomr.open(path)
    for i,page in enumerate(score):
        page.process()
        TOTAL += 1
        if len(page.staves) < 10 or len(page.staves) > 16:
            print "Wrong number of staves on", path, "page", i
        elif len(page.staves) % 2 != 0:
            print "Odd number of staves on", path, "page", i
        else:
            for start, end, barlines in page.barlines:
                if end - start != 2:
                    print "Wrong system size", path, "page", i
                    break
            else:
                PASS += 1
    metaomr.opencl.q.finish()
    del score
    del page
    gc.collect()
Ejemplo n.º 9
0
from PIL import Image
import gc
import pandas as pd

sonatas = pd.DataFrame.from_csv('resources/beethoven_sonatas.csv', header=None)
mvmts = pd.DataFrame.from_csv('results/beethoven_movements.csv')

dir_path = sys.argv[1]
imslpid = re.search('IMSLP[0-9]+', dir_path).group(0)
if imslpid not in sonatas.index or imslpid in mvmts.index:
    sys.exit(0)

import metaomr
from metaomr import bitimage, deskew
pages = sorted(glob.glob(os.path.join(dir_path, '*.pbm')))
pages = [metaomr.open(page)[0] for page in pages]
if len(pages) == 0:
    sys.exit(0)
for p, page in enumerate(pages):
    sys.stderr.write('%d ' % p)
    try:
        page.preprocess()
        if type(page.staff_dist) is not int:
            raise Exception('staffsize failed')
        page.layout()
        gc.collect()
    except Exception, e:
        print imslpid, p, '-', e
        pages[p] = None

mvmt_start = []
Ejemplo n.º 10
0
    "small_treble_clef": 5,
    "small_bass_clef": 5,
    "beam": 1
}

labeled_data = glob.glob('labels/*.png')
all_patches = []
all_patch_labels = []
for labels in labeled_data:
    # patches for this file
    patches = []
    patch_labels = []

    label_img = scipy.misc.imread(labels)
    image_path = os.path.join('samples', os.path.basename(labels))
    image, = metaomr.open(image_path)
    staffsize.staffsize(image)
    assert image.staff_dist >= 8
    image_scale = 8.0 / image.staff_dist
    image = scipy.misc.imresize(
        image.byteimg[:image.orig_size[0], :image.orig_size[1]].astype(bool),
        image_scale,
        interp='nearest')
    num_label_patches = 0
    for label_type, label_name in COLOR_LABELS.iteritems():
        our_patches = []
        our_patch_labels = []
        is_label = label_img[:, :, :3] == np.array([[label_type]])
        # When scaling down, need to ensure each label pixel maps to some
        # new pixel and isn't overwritten by the background
        label_y, label_x = np.where(is_label.all(axis=-1))
Ejemplo n.º 11
0
import metaomr
from metaomr import preprocessing, forest, bitimage
import scipy.misc
import sys
from timeit import Timer
import numpy as np
#from pylab import *

image, = metaomr.open(sys.argv[1])
preprocessing.process(image)
assert type(image.staff_dist) is not tuple and image.staff_dist >= 8
image_scale = 8.0 / image.staff_dist
image = scipy.misc.imresize(
    image.byteimg[:image.orig_size[0], :image.orig_size[1]].astype(bool),
    image_scale,
    interp='nearest')
padded_image = np.zeros((2048, 2048), np.uint8)
padded_image[:image.shape[0], :image.shape[1]] = image
bit_image = bitimage.as_bitimage(padded_image)
t = Timer(lambda: forest.predict(forest.classifier, bit_image))
print t.timeit(number=1)
#display_image = zeros(image.shape + (3,), int8)
#display_image[:] = np.where(image, 0, 255)[:, :, None]
#PATCH_SIZE = 17
#for y in xrange(PATCH_SIZE / 2, image.shape[0] - PATCH_SIZE/2 - 1):
#    patches = np.zeros((image.shape[1] - (PATCH_SIZE/2)*2 - 1, PATCH_SIZE*PATCH_SIZE), int)
#    for i,x in enumerate(xrange(PATCH_SIZE / 2, image.shape[1] - PATCH_SIZE/2 - 1)):
#        patches[i] = image[y - PATCH_SIZE/2 : y + PATCH_SIZE/2 + 1,
#                      x - PATCH_SIZE/2 : x + PATCH_SIZE/2 + 1].ravel()
#    pred_class = classifier.predict(patches)
#    for i,x in enumerate(xrange(PATCH_SIZE / 2, image.shape[1] - PATCH_SIZE/2 - 1)):
Ejemplo n.º 12
0
import signal
import sys
import subprocess
import tempfile
tmpdir = tempfile.mkdtemp()

try:
    output = sys.argv[1]

    pagedata = pandas.DataFrame(columns='staff_sens staff_spec time'.split())
    for i, filename in enumerate(sorted(glob.glob(TESTSET +
                                                  '/*-nostaff.png'))):
        gc.collect()
        fileid = os.path.basename(filename).split('-')[0]
        orig_file = re.sub('-nostaff', '', filename)
        page, = metaomr.open(orig_file)
        nostaff, = metaomr.open(filename)

        staffsize.staffsize(page)
        if type(page.staff_dist) is tuple or page.staff_dist is None:
            continue

        page_gamera = page.byteimg[:page.orig_size[0], :page.orig_size[1]]
        nostaff_gamera = nostaff.byteimg[:page.orig_size[0], :page.
                                         orig_size[1]]
        page_gamera = gamera.plugins.numpy_io.from_numpy(
            page_gamera.astype(np.uint16))
        nostaff_gamera = gamera.plugins.numpy_io.from_numpy(
            nostaff_gamera.astype(np.uint16))

        baselineStaves = None
Ejemplo n.º 13
0
    "small_treble_clef": 5,
    "small_bass_clef": 5,
    "beam": 1
}

labeled_data = glob.glob('labels/*.png')
all_patches = []
all_patch_labels = []
for labels in labeled_data:
    # patches for this file
    patches = []
    patch_labels = []

    label_img = scipy.misc.imread(labels)
    image_path = os.path.join('samples', os.path.basename(labels))
    image, = metaomr.open(image_path)
    staffsize.staffsize(image)
    assert image.staff_dist >= 8
    image_scale = 8.0 / image.staff_dist
    image = scipy.misc.imresize(image.byteimg[:image.orig_size[0],
                                              :image.orig_size[1]].astype(bool),
                                image_scale,
                                interp='nearest')
    num_label_patches = 0
    for label_type, label_name in COLOR_LABELS.iteritems():
        our_patches = []
        our_patch_labels = []
        is_label = label_img[:, :, :3] == np.array([[label_type]])
        # When scaling down, need to ensure each label pixel maps to some
        # new pixel and isn't overwritten by the background
        label_y, label_x = np.where(is_label.all(axis=-1))
Ejemplo n.º 14
0
import metaomr.kanungo
import glob
import os.path
import pandas as pd
import numpy as np
from datetime import datetime
import gc
import sys

results = pd.DataFrame()
index = []
path = sys.argv[1]
name = os.path.basename(path)
print name
for i, page in enumerate(sorted(glob.glob(path + "/img-*.pbm"))):
    page, = metaomr.open(page)
    try:
        page.preprocess()
    except Exception, e:
        print e
        results = results.append([np.repeat(np.nan, 8)] * 2)
        index += [(name, i, m) for m in ['ks', 'm']]
        continue
    try:
        for method, fn in (('ks', metaomr.kanungo.test_hists_ks),
                           ('m', metaomr.kanungo.test_hists_mahalanobis)):
            tic = datetime.now()
            result = metaomr.kanungo.est_parameters(
                page, test_fn=metaomr.kanungo.test_hists_mahalanobis)
            toc = datetime.now()
            index.append((name, i, method))
Ejemplo n.º 15
0
# We can test scores for piano solo because we know what to expect
# (an even number of staves and staff systems with 2 staves each.)
# This test can be used with any scores from IMSLP or other sources.

import metaomr
import metaomr.opencl
import gc
import sys

PASS = 0
TOTAL = 0
for path in sys.argv[1:]:
    score = metaomr.open(path)
    for i, page in enumerate(score):
        page.process()
        TOTAL += 1
        if len(page.staves) < 10 or len(page.staves) > 16:
            print "Wrong number of staves on", path, "page", i
        elif len(page.staves) % 2 != 0:
            print "Odd number of staves on", path, "page", i
        else:
            for start, end, barlines in page.barlines:
                if end - start != 2:
                    print "Wrong system size", path, "page", i
                    break
            else:
                PASS += 1
    metaomr.opencl.q.finish()
    del score
    del page
    gc.collect()
Ejemplo n.º 16
0
import metaomr
import argparse
import logging
logging.basicConfig(level=logging.INFO)

parser = argparse.ArgumentParser()
parser.add_argument("-p", "--page", type=int)
parser.add_argument("-s", "--show", dest="show", action="store_true")
parser.add_argument("-S", "--no-show", dest="show", action="store_false")
parser.add_argument("-i", "--interactive", dest="int", action="store_true")
parser.add_argument("-o", "--output", type=str)
parser.set_defaults(show=True)
parser.add_argument("path", type=str, help="path to scanned music")

args = parser.parse_args()
score = metaomr.open(args.path)
if args.page is None:
    if args.output:
        from matplotlib.backends.backend_pdf import PdfPages
        import pylab as p
        with PdfPages(args.output) as pdf:
            for page in score:
                page.process()
                p.figure(figsize=page.orig_size)
                ax = p.Axes(p.gcf(),[0,0,1,1],yticks=[],xticks=[],frame_on=False)
                p.gcf().delaxes(p.gca())
                p.gcf().add_axes(ax)
                page.show()
                pdf.savefig()
                p.close()
    else:
Ejemplo n.º 17
0
    assert page.staff_dist >= 8
    if img is None:
        img = page.img
    scale = 8.0 / page.staff_dist
    scaled_img = bitimage.scale(img, scale, align=64)
    return scaled_img, scale


def classify(forest, page, img=None):
    scaled_img, scale = scale_img(page, img=img)
    return predict(forest, scaled_img)


if __name__ == '__main__':
    import metaomr
    page, = metaomr.open('samples/chopin.pdf')
    page.process()
    f = load_forest('classifier.pkl')
    # Test for cycles
    children = f.children.get()
    tree = cPickle.load(open('classifier.pkl', 'rb')).estimators_[0].tree_
    children_ = np.c_[tree.children_left, tree.children_right]
    leaves = []

    def recurse(i):
        print i
        if children[i, 0] < 0:
            leaves.append(i)
            return
        else:
            recurse(children[i, 0])
Ejemplo n.º 18
0
import metaomr

INPUT = 'samples/IMSLP73123.pdf'

score = metaomr.open(INPUT)
for page in score:
    page.process()
    page.show()
import pylab
pylab.show()
Ejemplo n.º 19
0
import gc
from forest_config import COLOR_LABELS
import sys
import os
import numpy

IMSLP = '../IMSLP'
all_imslp = list(numpy.random.choice(os.listdir(IMSLP), 10))
CORPUS = [os.path.join(IMSLP,p) for p in all_imslp]

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()
Ejemplo n.º 20
0
import metaomr
from metaomr import preprocessing, forest, bitimage
import scipy.misc
import sys
from timeit import Timer
import numpy as np
#from pylab import *

image, = metaomr.open(sys.argv[1])
preprocessing.process(image)
assert type(image.staff_dist) is not tuple and image.staff_dist >= 8
image_scale = 8.0 / image.staff_dist
image = scipy.misc.imresize(image.byteimg[:image.orig_size[0],
                                          :image.orig_size[1]].astype(bool),
                            image_scale,
                            interp='nearest')
padded_image = np.zeros((2048, 2048), np.uint8)
padded_image[:image.shape[0], :image.shape[1]] = image
bit_image = bitimage.as_bitimage(padded_image)
t = Timer(lambda: forest.predict(forest.classifier, bit_image))
print t.timeit(number=1)
#display_image = zeros(image.shape + (3,), int8)
#display_image[:] = np.where(image, 0, 255)[:, :, None]
#PATCH_SIZE = 17
#for y in xrange(PATCH_SIZE / 2, image.shape[0] - PATCH_SIZE/2 - 1):
#    patches = np.zeros((image.shape[1] - (PATCH_SIZE/2)*2 - 1, PATCH_SIZE*PATCH_SIZE), int)
#    for i,x in enumerate(xrange(PATCH_SIZE / 2, image.shape[1] - PATCH_SIZE/2 - 1)):
#        patches[i] = image[y - PATCH_SIZE/2 : y + PATCH_SIZE/2 + 1,
#                      x - PATCH_SIZE/2 : x + PATCH_SIZE/2 + 1].ravel()
#    pred_class = classifier.predict(patches)
#    for i,x in enumerate(xrange(PATCH_SIZE / 2, image.shape[1] - PATCH_SIZE/2 - 1)):
Ejemplo n.º 21
0
    k = randint(0, 4)
    return nu, a0, a, b0, b, k


columns = pd.MultiIndex.from_product([['real', 'estimate'],
                                      'nu a0 a b0 b k'.split()])
columns = columns.append(
    pd.MultiIndex.from_product([['estimate'],
                                ['stat', 'time', 'status', 'nfev']]))
cols = []
results = []
fun = 'ks'
method = 'Nelder-Mead'
for image in IDEAL:
    name = os.path.basename(image).split('.')[0]
    page, = metaomr.open(image)
    kimg = kan.KanungoImage(kan.normalized_page(page)[0])
    for i in xrange(3):
        params = random_params()
        synth = Page(kimg.degrade(params))
        synth.staff_dist = 8
        for maxfev in [25, 50]:
            start = datetime.now()
            est_params = kan.est_parameters(synth,
                                            test_fn=kan.test_hists_ks if fun
                                            == 'ks' else kan.test_hists_chisq,
                                            opt_method=method,
                                            maxfev=maxfev)
            end = datetime.now()
            cols.append((name, fun, maxfev, i))
            results.append(
Ejemplo n.º 22
0
import env
import metaomr
from metaomr import staffsize

import gc
import gzip
import numpy as np
import os
import re
import sys

out = ''
for doc in sys.argv[1:]:
    docname = re.search('IMSLP([0-9]+)', doc).group(0)
    pages = metaomr.open(doc)
    for i, page in enumerate(pages):
        name = docname + '-' + str(i)
        dark = staffsize.dark_runs(page.img)
        staff_thick = np.argmax(dark)
        page.staff_thick = staff_thick
        light = staffsize.light_runs(page)
        out += ','.join([name] + map(str, dark) + map(str, light)) + '\n'
        gc.collect()

output = gzip.open(os.path.join('staffsizes', str(os.getpid())), 'ab')
output.write(out)
Ejemplo n.º 23
0
        b0 = b = 0
    else:
        b0 = random() * 0.2
        b = 0.5 + random() * 2
    k = randint(0, 4)
    return nu, a0, a, b0, b, k

columns = pd.MultiIndex.from_product([['real', 'estimate'], 'nu a0 a b0 b k'.split()])
columns = columns.append(pd.MultiIndex.from_product([['estimate'],['stat','time','status','nfev']]))
cols = []
results = []
fun = 'ks'
method = 'Nelder-Mead'
for image in IDEAL:
    name = os.path.basename(image).split('.')[0]
    page, = metaomr.open(image)
    kimg = kan.KanungoImage(kan.normalized_page(page)[0])
    for i in xrange(3):
        params = random_params()
        synth = Page(kimg.degrade(params))
        synth.staff_dist = 8
        for maxfev in [25, 50]:
            start = datetime.now()
            est_params = kan.est_parameters(synth, test_fn=kan.test_hists_ks if fun == 'ks' else kan.test_hists_chisq, opt_method=method, maxfev=maxfev)
            end = datetime.now()
            cols.append((name, fun, maxfev, i))
            results.append(list(params) + list(est_params.x) + [est_params.fun, (end - start).total_seconds(), est_params.status, est_params.nfev])
            sys.stderr.write('.')
    res = pd.DataFrame(results, columns=columns)
    res.index = pd.MultiIndex.from_tuples(cols)
    res.index.names = 'doc test maxfev num'.split()
Ejemplo n.º 24
0
def scale_img(page, img=None):
    assert page.staff_dist >= 8
    if img is None:
        img = page.img
    scale = 8.0 / page.staff_dist
    scaled_img = bitimage.scale(img, scale, align=64)
    return scaled_img, scale

def classify(forest, page, img=None):
    scaled_img, scale = scale_img(page, img=img)
    return predict(forest, scaled_img)

if __name__ == '__main__':
    import metaomr
    page, = metaomr.open('samples/chopin.pdf')
    page.process()
    f = load_forest('classifier.pkl')
    # Test for cycles
    children = f.children.get()
    tree = cPickle.load(open('classifier.pkl','rb')).estimators_[0].tree_
    children_ = np.c_[tree.children_left, tree.children_right]
    leaves = []
    def recurse(i):
        print i
        if children[i,0] < 0:
            leaves.append(i)
            return
        else:
            recurse(children[i,0])
            recurse(children[i,1])
Ejemplo n.º 25
0
import env
import metaomr
import metaomr.score_matching
import metaomr.image
import glob
import os.path
import pandas as pd
import gc
import sys

results = pd.DataFrame()
path = sys.argv[1]
name = os.path.basename(path)
print name
for i, page in enumerate(sorted(glob.glob(path+"/img-*.pbm"))):
    page, = metaomr.open(page)
    try:
        page.layout()
    except Exception, e:
        print e
        continue
    try:
        score_results = metaomr.score_matching.measure_projections(page, title=name, p=i, m=len(results))
        del page
        gc.collect()
        results = results.append(score_results)
    except Exception, e:
        print 'During measure_projections:', e
        continue
results.to_csv(sys.argv[2])