mood prediction works
This commit is contained in:
parent
62ff8dd1f5
commit
861139929e
@ -14,7 +14,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
from predict import generate_midi, predictmood
|
from predict import generate_midi
|
||||||
import os
|
import os
|
||||||
from flask import send_file, request
|
from flask import send_file, request
|
||||||
import pretty_midi
|
import pretty_midi
|
||||||
@ -25,29 +25,55 @@ else:
|
|||||||
from io import StringIO
|
from io import StringIO
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
import mido
|
||||||
|
import tempfile
|
||||||
|
from usingMusicNN import predictmood
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
app = Flask(__name__, static_url_path='', static_folder=os.path.abspath('../static'))
|
app = Flask(__name__, static_url_path='', static_folder=os.path.abspath('../static'))
|
||||||
|
|
||||||
|
HappyNote = 0
|
||||||
|
SadNote = 1
|
||||||
|
|
||||||
|
def HappyTrack():
|
||||||
|
track = mido.MidiTrack()
|
||||||
|
track.append(mido.Message('note_on', note=HappyNote, velocity=3, time=6))
|
||||||
|
return track
|
||||||
|
|
||||||
|
def SadTrack():
|
||||||
|
track = mido.MidiTrack()
|
||||||
|
track.append(mido.Message('note_on', note=SadNote, velocity=3, time=6))
|
||||||
|
return track
|
||||||
|
|
||||||
@app.route('/predict', methods=['POST'])
|
@app.route('/predict', methods=['POST'])
|
||||||
def predict():
|
def predict():
|
||||||
now = time.time()
|
now = time.time()
|
||||||
values = json.loads(request.data)
|
values = json.loads(request.data)
|
||||||
midi_data = pretty_midi.PrettyMIDI(StringIO(''.join(chr(v) for v in values)))
|
midi_data = pretty_midi.PrettyMIDI(StringIO(''.join(chr(v) for v in values)))
|
||||||
print values
|
|
||||||
duration = float(request.args.get('duration'))
|
duration = float(request.args.get('duration'))
|
||||||
ret_midi = generate_midi(midi_data, duration)
|
ret_midi = generate_midi(midi_data, duration)
|
||||||
return send_file(ret_midi, attachment_filename='return.mid',
|
|
||||||
mimetype='audio/midi', as_attachment=True)
|
|
||||||
|
|
||||||
@app.route('/getmood', methods=['POST'])
|
# Store the received midi file in a temporary file to be able to use it with mido
|
||||||
def getmood():
|
mfile = tempfile.NamedTemporaryFile()
|
||||||
values = json.loads(request.data)
|
midi_data.write(mfile)
|
||||||
midi_data = pretty_midi.PrettyMIDI(StringIO(''.join(chr(v) for v in values)))
|
mfile.seek(0)
|
||||||
ret_file = predictmood(midi_data)
|
|
||||||
print ret_file
|
midofile = mido.MidiFile(mfile.name)
|
||||||
return send_file(ret_file, attachment_filename='return.json', as_attachment=True)
|
|
||||||
|
mood = predictmood(midofile)
|
||||||
|
print mood
|
||||||
|
|
||||||
|
# Add a new track with the first note indicating the mood
|
||||||
|
midi_to_mod = mido.MidiFile(ret_midi.name)
|
||||||
|
midi_to_mod.tracks.append(HappyTrack() if mood == 'happy' else SadTrack())
|
||||||
|
|
||||||
|
ret_file = tempfile.NamedTemporaryFile()
|
||||||
|
midi_to_mod.save(ret_file.name)
|
||||||
|
ret_file.seek(0)
|
||||||
|
|
||||||
|
return send_file(ret_file, attachment_filename='return.mid',
|
||||||
|
mimetype='audio/midi', as_attachment=True)
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
def index():
|
def index():
|
||||||
|
@ -63,6 +63,16 @@ class AI extends events.EventEmitter{
|
|||||||
this.emit('keyUp', note.midi, note.noteOff + now)
|
this.emit('keyUp', note.midi, note.noteOff + now)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// We added another track to the midi file that contains data we want to send back to the client
|
||||||
|
// the first note of this new track is used as a bool value for happy/sad
|
||||||
|
if (response.tracks[2]['notes'][0]['midi'] === 0) {
|
||||||
|
console.log('happy')
|
||||||
|
window.isSad = 0;
|
||||||
|
}
|
||||||
|
else if (response.tracks[2]['notes'][0]['midi'] === 1) {
|
||||||
|
console.log('sad')
|
||||||
|
window.isSad = 1;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
this._lastPhrase = -1
|
this._lastPhrase = -1
|
||||||
this.emit('sent')
|
this.emit('sent')
|
||||||
|
@ -45,12 +45,30 @@ class Glow {
|
|||||||
this._aiVisible = false
|
this._aiVisible = false
|
||||||
this._aiGlow.classList.remove('visible')
|
this._aiGlow.classList.remove('visible')
|
||||||
this._userGlow.classList.add('visible')
|
this._userGlow.classList.add('visible')
|
||||||
|
// Update the class of glow to show the correct color
|
||||||
|
if (window.isSad) {
|
||||||
|
this._userGlow.classList.add('sad')
|
||||||
|
this._userGlow.classList.remove('happy')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this._userGlow.classList.add('happy')
|
||||||
|
this._userGlow.classList.remove('sad')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!this._aiVisible){
|
if (!this._aiVisible){
|
||||||
this._aiVisible = true
|
this._aiVisible = true
|
||||||
this._aiGlow.classList.add('visible')
|
this._aiGlow.classList.add('visible')
|
||||||
this._userGlow.classList.remove('visible')
|
this._userGlow.classList.remove('visible')
|
||||||
|
// Update the class of glow to show the correct color
|
||||||
|
if (window.isSad) {
|
||||||
|
this._userGlow.classList.add('sad')
|
||||||
|
this._userGlow.classList.remove('happy')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this._userGlow.classList.add('happy')
|
||||||
|
this._userGlow.classList.remove('sad')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
$blue : rgb(30, 183, 235);
|
$blue : rgb(30, 183, 235);
|
||||||
$orange : rgb(249, 187, 45);
|
$orange : rgb(249, 187, 45);
|
||||||
|
$green : rgb(0, 225, 0);
|
||||||
|
$red: rgb(255, 0, 0);
|
||||||
|
|
||||||
$font-family: 'Quicksand', sans-serif;
|
$font-family: 'Quicksand', sans-serif;
|
@ -15,9 +15,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$glowReach : 60%;
|
/*$glowReach : 60%;*/
|
||||||
|
$glowReach : 80%;
|
||||||
|
|
||||||
#ai {
|
/*#ai {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $orange 0%, black $glowReach);
|
background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $orange 0%, black $glowReach);
|
||||||
}
|
}
|
||||||
@ -25,5 +26,15 @@
|
|||||||
#user {
|
#user {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $blue 0%, black $glowReach);
|
background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $blue 0%, black $glowReach);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
.happy {
|
||||||
|
opacity: 0;
|
||||||
|
background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $green 0%, black $glowReach);
|
||||||
|
}
|
||||||
|
|
||||||
|
.sad {
|
||||||
|
opacity: 0;
|
||||||
|
background-image: radial-gradient(ellipse farthest-corner at 50% 0px, $red 0%, black $glowReach);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user