-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.py
56 lines (43 loc) · 1.33 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
53
54
55
56
# Third-party imports
from flask import Flask, jsonify, request, render_template, redirect
from flask_pymongo import PyMongo
from werkzeug import secure_filename
import base64
from mood import main_func
# Initialize app and database
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'moodmusic'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/moodmusic'
mongo = PyMongo(app)
# Setup routes
@app.route('/')
def index():
return redirect("/cam")
@app.route('/player')
def player():
return render_template("player.html")
@app.route('/cam')
def cam():
return render_template("cam.html")
@app.route('/music', methods=['GET'])
def music():
songs = mongo.db.songs
output = []
for s in songs.find({'mood' : request.args.get("mood")}):
output.append({'id' : s['id'],'title' : s['title'], 'album': s['album'], 'artist' : s['artist'], 'albumart' : s['albumart'], 'url' : s['url']})
return jsonify(output)
@app.route('/emotion', methods=['GET', 'POST'])
def emotion():
# Get img data
img_data = request.form['img']
img_data = img_data[23:]
# Decode the img and save as snap.jpg
f = open("snap.jpg", "wb")
f.write(base64.b64decode(img_data.encode('ascii')))
f.close()
# Run script for emotion recognition
mood = main_func()
return redirect("/player?mood=" + mood)
# Main
if __name__ == '__main__':
app.run(debug=True)