This repository has been archived by the owner on Mar 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
220 lines (193 loc) · 7.54 KB
/
main.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Dempster-Shafer Library for Evidence-Theory
* Thilo Michael, Jeffrey Jedele
* 2012
* > lab exercise, main file
*/
#include "dempstershafer.hpp"
#include "learningclassificator.hpp"
#include "csvreader.hpp"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
string *hypotheseses;
string hypothesis_to_string_function(void* element);
void print_frame_stats(int frame_no,
int eye_value,
int mouth_value,
int furrow_value,
double eye_classification,
double mouth_classification,
double furrow_classification,
Evidence& eye_evidence,
Evidence& mouth_evidence,
Evidence& furrow_evidence,
Evidence& combined);
// hypotheseses
string fear("fear");
string surprise("surprise");
string disdain("disdain");
string disgust("disgust");
string anger("anger");
int main(int argc, char** argv) {
// create the CSV reader
if(argc < 2) {
cerr << "Provide .csv file with video data as argument!" << endl;
return EXIT_FAILURE;
}
CSVReader csv(argv[1]);
// create Dempster-Shafer universe
DempsterShaferUniverse universe;
universe.add_hypotheseses(&fear, &surprise, &disdain, &disgust, &anger, NULL);
// create the feature classificator with online learning
LearningClassificator classificator(0.05, 3);
const int EYE_APERTURE = classificator.add_feature(18.0);
const int MOUTH_OPENING = classificator.add_feature(23.0);
const int FURROW_COUNT = classificator.add_feature(527.0);
// create bitset representations of emotions sets to save performance while classifying video frames
bitset<MAX_HYPOTHESESES> fear_and_surprise = universe.bitset_representation(&fear, &surprise, NULL);
bitset<MAX_HYPOTHESESES> disdain_and_disgust = universe.bitset_representation(&disdain, &disgust, NULL);
bitset<MAX_HYPOTHESESES> only_surprise = universe.bitset_representation(&surprise, NULL);
bitset<MAX_HYPOTHESESES> anger_and_disgust = universe.bitset_representation(&anger, &disgust, NULL);
bitset<MAX_HYPOTHESESES> only_anger = universe.bitset_representation(&anger, NULL);
// classify the frames
for(int i=0; i<csv.number_of_rows(); i++) {
vector<int> frame = csv.row(i);
// evidence for eye aperture
Evidence eye_aperture = universe.add_evidence();
double eye_aperture_classification = classificator.classify(EYE_APERTURE, frame.at(1));
eye_aperture_classification *= 0.9; // we don't want 1.0 as mass
if(eye_aperture_classification >= 0.0) {
// large eye aperture
eye_aperture.add_focal_set(eye_aperture_classification, fear_and_surprise);
} else {
// small eye aperture
eye_aperture.add_focal_set(-eye_aperture_classification, disdain_and_disgust);
}
eye_aperture.add_omega_set();
// evidence for mouth opening
Evidence mouth_opening = universe.add_evidence();
double mouth_opening_classification = classificator.classify(MOUTH_OPENING, frame.at(2));
mouth_opening_classification *= 0.9; // we don't want 1.0 as mass
if(mouth_opening_classification >= 0.0) {
// large mouth opening
mouth_opening.add_focal_set(mouth_opening_classification, only_surprise);
} else {
// small mouth opening
mouth_opening.add_focal_set(-mouth_opening_classification, anger_and_disgust);
}
mouth_opening.add_omega_set();
// evidence for furrow count
Evidence furrow_count = universe.add_evidence();
double furrow_count_classification = classificator.classify(FURROW_COUNT, frame.at(3));
furrow_count_classification *= 0.9; // we don't want 1.0 as mass
if(furrow_count_classification >= 0.0) {
// many furrows
furrow_count.add_focal_set(furrow_count_classification, fear_and_surprise);
} else {
// few furrows
furrow_count.add_focal_set(-furrow_count_classification, only_anger);
}
furrow_count.add_omega_set();
// combine the features
Evidence combined_features = eye_aperture & mouth_opening & furrow_count;
// find the most plausible emotion
print_frame_stats(frame.at(0),
frame.at(1),
frame.at(2),
frame.at(3),
eye_aperture_classification,
mouth_opening_classification,
furrow_count_classification,
eye_aperture,
mouth_opening,
furrow_count,
combined_features);
// frame could be classified here to remove the ugly print function
//string* emotion = (string*) combined_features.best_match();
//cout << "Frame: " << frame.at(0) << " classified as " << *emotion << "." << endl;
}
return EXIT_SUCCESS;
}
string hypothesis_to_string_function(void* element) {
string *s = (string*) element;
return *s;
}
// very ugly code for debugging + demonstration below here
void print_frame_stats(int frame_no,
int eye_value,
int mouth_value,
int furrow_value,
double eye_classification,
double mouth_classification,
double furrow_classification,
Evidence& eye_evidence,
Evidence& mouth_evidence,
Evidence& furrow_evidence,
Evidence& combined)
{
printf("---------------------------------\n");
printf("### Frame: %03d ###\n", frame_no);
printf("---------------------------------\n");
printf("(-1.0: far below average, +1.0 far above average)\n");
printf("Eye Aperture: %3d -> %04.2f\n", eye_value, eye_classification);
printf("Mouth Opening: %3d -> %04.2f\n", mouth_value, mouth_classification);
printf("Furrow Count: %3d -> %04.2f\n", furrow_value, furrow_classification);
printf("---------------------------------\n");
printf("(#: Belief, -: Plausability, .: nothing)\n");
string bar("");
int count;
int belief;
int add_plaus;
const int BAR_LENGTH = 50;
// fear
count = 0;
bar = "";
belief = (int) 100*combined.belief(&fear, NULL);
add_plaus = (int) 100*combined.plausability(&fear, NULL) - belief;
for(int i=0; i<belief/(100/BAR_LENGTH); i++, count++) bar.append("#");
for(int i=0; i<add_plaus/(100/BAR_LENGTH); i++, count++) bar.append("-");
for(;count<BAR_LENGTH;count++) bar.append(".");
printf("Fear | %s \n", bar.c_str());
// surprise
count = 0;
bar = "";
belief = (int) 100*combined.belief(&surprise, NULL);
add_plaus = (int) 100*combined.plausability(&surprise, NULL) - belief;
for(int i=0; i<belief/(100/BAR_LENGTH); i++, count++) bar.append("#");
for(int i=0; i<add_plaus/(100/BAR_LENGTH); i++, count++) bar.append("-");
for(;count<BAR_LENGTH;count++) bar.append(".");
printf("Surprise | %s \n", bar.c_str());
// disdain
count = 0;
bar = "";
belief = (int) 100*combined.belief(&disdain, NULL);
add_plaus = (int) 100*combined.plausability(&disdain, NULL) - belief;
for(int i=0; i<belief/(100/BAR_LENGTH); i++, count++) bar.append("#");
for(int i=0; i<add_plaus/(100/BAR_LENGTH); i++, count++) bar.append("-");
for(;count<BAR_LENGTH;count++) bar.append(".");
printf("Disdain | %s \n", bar.c_str());
// disgust
count = 0;
bar = "";
belief = (int) 100*combined.belief(&disgust, NULL);
add_plaus = (int) 100*combined.plausability(&disgust, NULL) - belief;
for(int i=0; i<belief/(100/BAR_LENGTH); i++, count++) bar.append("#");
for(int i=0; i<add_plaus/(100/BAR_LENGTH); i++, count++) bar.append("-");
for(;count<BAR_LENGTH;count++) bar.append(".");
printf("Disgust | %s \n", bar.c_str());
// anger
count = 0;
bar = "";
belief = (int) 100*combined.belief(&anger, NULL);
add_plaus = (int) 100*combined.plausability(&anger, NULL) - belief;
for(int i=0; i<belief/(100/BAR_LENGTH); i++, count++) bar.append("#");
for(int i=0; i<add_plaus/(100/BAR_LENGTH); i++, count++) bar.append("-");
for(;count<BAR_LENGTH;count++) bar.append(".");
printf("Anger | %s \n", bar.c_str());
printf("---------------------------------\n");
cout << "classified as: " << hypothesis_to_string_function(combined.best_match()) << endl;
printf("---------------------------------\n");
cout << endl;
}