-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
86 lines (64 loc) · 2.63 KB
/
analysis.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
import sklearn
from sklearn.neural_network import MLPClassifier
from sklearn.neural_network import MLPRegressor
from sklearn import datasets, linear_model
import cPickle
from emotion import *
# Returns a classifier that analyzes the correlation between data.
def trainOnData(data):
X = [pair[0] for pair in data]
Y = [pair[1] for pair in data]
#clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(15,), random_state=1)
clf = MLPRegressor(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(15,), random_state=1)
clf.fit(X, Y)
return clf
# Parses emotion from the return value of the Microsoft Emotion API.
def parseEmotion(data):
best = 0
for i in range(1, len(data)):
if data[i]['faceRectangle']['width'] > data[best]['faceRectangle']['width']:
best = i
print("Target face is number " + str(best + 1) + " of " + str(len(data)))
return [data[best]['scores'][em] for em in emotions]
# Makes a prediction about a vector based on a classifier.
def makePrediction(clf, vec):
return clf.predict([vec])[0]
def saveClassifier(filename, clf):
with open(filename, 'wb') as fid:
cPickle.dump(clf, fid)
def loadClassifier(filename):
with open(filename, 'rb') as fid:
return cPickle.load(fid)
def rebuildClassifier():
# Input is [sad, happy, disgust, anger, surprise, fear, neutral, contempt]
# Output is [rap, hardrock, metal, classical, jazz, pop, classrock, edm, country]
X = [
[1, 0, 0, 0, 0, 0, 0, 0], # Sad
[1, 0, 0, 0, 0, 0, 0, 0], # Sad
[1, 0, 0, 0, 0, 0, 0, 0], # Sad
[0, 1, 0, 0, 0, 0, 0, 0], # Happy
[0, 1, 0, 0, 0, 0, 0, 0], # Happy
[0, 1, 0, 0, 0, 0, 0, 0], # Happy
[0, 0, 0, 1, 0, 0, 0, 0], # Angry
[0, 0, 0, 1, 0, 0, 0, 0], # Angry
[0, 0, 0, 1, 0, 0, 0, 0], # Angry
[0, 0, 0, 0, 0, 0, 1, 0], # Neutral
[0, 0, 0, 0, 0, 0, 1, 0], # Neutral
[0, 0, 0, 0, 0, 0, 1, 0] # Neutral
]
Y = [
[0, 0, 0, 1, 0, 0, 0, 0, 0], # Sad
[0, 0, 0, 0, 1, 0, 0, 0, 0], # Sad
[0, 0, 0, 0, 0, 0, 0, 0, 1], # Sad
[0, 0, 0, 0, 0, 1, 0, 0, 0], # Happy
[0, 0, 0, 0, 0, 0, 1, 0, 0], # Happy
[0, 0, 0, 0, 0, 0, 0, 1, 0], # Happy
[1, 0, 0, 0, 0, 0, 0, 0, 0], # Angry
[0, 1, 0, 0, 0, 0, 0, 0, 0], # Angry
[0, 0, 1, 0, 0, 0, 0, 0, 0], # Angry
[0, 0, 0, 0, 0, 0, 1, 0, 0], # Neutral
[0, 0, 0, 0, 0, 0, 0, 1, 0], # Neutral
[0, 0, 0, 0, 0, 0, 0, 0, 1] # Neutral
]
clf = trainOnData([(X[i], Y[i]) for i in range(len(Y))])
saveClassifier('./classifier.pkl', clf)