forked from sc-2020-siit/MusicSheetsRecognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
52 lines (37 loc) · 1.46 KB
/
app.py
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
import numpy as np
from flask import Flask, render_template, request, flash, redirect, url_for, session
import secrets
from model.predict import predict_and_create_midi
from model.utils import process_image, midi_to_musicxml
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__)
app.secret_key = secrets.token_urlsafe(16)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/result')
def result():
return render_template('result.html', musicXml=session['musicXml'])
def allowed_file(content_type):
return content_type.split('/')[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload_image():
if 'image' not in request.files:
flash('No image', 'error')
return redirect(request.url)
file = request.files['image']
if file.filename == '':
flash('No selected image', 'error')
return redirect(request.url)
if file and allowed_file(file.content_type):
nparr = np.fromstring(file.read(), np.uint8)
result = process_image(nparr) # input for neural network
midi_path = predict_and_create_midi(result, file.filename, 32, app) # midi file path
musicXml = midi_to_musicxml(midi_path)
session['musicXml'] = musicXml
return redirect(url_for('result')), 201
else:
flash('Accepted formats are png, jpg and jpeg', 'error')
return redirect(request.url)
if __name__ == '__main__':
app.run(debug=True)