-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.py
120 lines (97 loc) · 2.75 KB
/
driver.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Emotion analysis module
from emotion import *
from analysis import *
# Song player module
from picture import *
from gui import *
import os
import os.path
import time
import random
# The list of currently supported genres
genres = [
'rap',
'hard rock',
'heavy metal',
'classical',
'jazz',
'pop',
'classic rock',
'edm',
'country'
]
subemo = [
'happiness',
'sadness',
'anger',
'neutral'
]
if not os.path.isfile('./classifier.pkl'):
print("Building new classifier")
rebuildClassifier()
clf = loadClassifier('./classifier.pkl')
def runCycle(prevEmo, prevState):
print("Beginning procedure...")
# Takes a selfie.
picture()
print("Snap!")
# Gets the emotion data for the selfie.
data = imageRequest('./selfie.jpg')
print("Scanned image.")
print(data)
if os.path.isfile('./selfie.jpg'):
os.remove('./selfie.jpg') # Remove the old picture
# Return if no faces
if len(data) == 0:
print('No faces detected')
return (prevEmo, prevState)
# Gets the emotion vector from the image.
vec = parseEmotion(data)
print("Acquired sentiment.")
for i in range(len(subemo)):
print(subemo[i] + ": " + str(vec[emotions.index(subemo[i])]))
print('')
# Figure out if the dominant emotion changed
domEmo = subemo[0]
for i in range(1, len(subemo)):
if vec[emotions.index(domEmo)] < vec[emotions.index(subemo[i])]:
domEmo = subemo[i]
if prevEmo == domEmo:
print("Dominant mood unchanged")
updateGUI(vec[emotions.index(domEmo)], domEmo, prevState)
return (prevEmo, prevState)
else:
print("Current dominant emotion is " + domEmo)
# Computes the prediction.
pred = makePrediction(clf, vec)
print("Choice vector computed.")
for i in range(len(pred)):
print(genres[i] + ": " + str(pred[i]))
# Introduce a small random variability to each value.
# Remove if bad results are received.
for i in range(len(genres)):
pred[i] += random.random() * 0.03
# Choose the dimension with the highest value.
choice = 0
for i in range(1, len(genres)):
if pred[i] > pred[choice]:
choice = i
# Play the requested genre
if prevState != genres[choice]:
print("Will play " + genres[choice])
search(genres[choice])
else:
print("Alreeady playing " + prevState)
updateGUI(vec[emotions.index(domEmo)], domEmo, genres[choice])
# update The GUI
return (domEmo, genres[choice])
if __name__ == '__main__':
prevType = ''
prevEmo = ''
while True:
pair = runCycle(prevEmo, prevType)
prevEmo = pair[0]
prevType = pair[1]
time.sleep(15)
else:
rebuildClassifier()