File size: 1,827 Bytes
49638b7
 
 
eb9378a
 
49638b7
eb9378a
 
673b3b9
eb9378a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49638b7
16fdb75
 
105dec4
 
16fdb75
49638b7
673b3b9
 
16fdb75
d24f1be
49638b7
 
 
d24f1be
 
49638b7
 
673b3b9
d24f1be
4f6d9ee
49638b7
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import torch
import requests
import music21
import subprocess
from torchvision import transforms
from PIL import Image


DEFAULT_FILE_NAME = 'sheet_music'


# converting everything into the key of C major or A minor
def convert_to_key():
    # major conversions
    majors = dict(
        [("A-", 4), ("A", 3), ("B-", 2), ("B", 1), ("C", 0), ("D-", -1), ("D", -2), ("E-", -3), ("E", -4), ("F", -5),
         ("G-", 6), ("G", 5)])
    minors = dict(
        [("A-", 1), ("A", 0), ("B-", -1), ("B", -2), ("C", -3), ("D-", -4), ("D", -5), ("E-", 6), ("E", 5), ("F", 4),
         ("G-", 3), ("G", 2)])

    score = music21.converter.parse(f'{DEFAULT_FILE_NAME}.musicxml')
    key = score.analyze('key')
    file_name = 'C_file'
    if key.mode == "major":
        halfSteps = majors[key.tonic.name]
        file_name = 'Am_file'

    elif key.mode == "minor":
        halfSteps = minors[key.tonic.name]

    newscore = score.transpose(halfSteps)
    # key = newscore.analyze('key')
    # print(key.tonic.name, key.mode)
    newscore.write('musicxml', f'{file_name}.musicxml')
    # newscore.write('musicxml.pdf', fp="file.pdf")

    return f'{file_name}.musicxml', f'{file_name}_teaser.png'


def run_oemer():
    process = subprocess.Popen(['oemer', f'{DEFAULT_FILE_NAME}.png'])
    process.wait()

def predict(inp):
    img = Image.fromarray(inp)
    img.save(f'{DEFAULT_FILE_NAME}.png')
    run_oemer()
    return convert_to_key()


def run():
    outputs = [gr.File(label='Download musicxml', type='file'), 
               gr.File(label='Download detection image', type='file')]
    demo = gr.Interface(
        fn=predict,
        inputs='image',
        outputs=outputs,
        allow_flagging='never',
    )

    demo.launch(server_name="0.0.0.0", server_port=7860)


if __name__ == "__main__":
    run()