-
-
Save berak/eb3476a644c3b901054ed91f83d245a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
#include <iostream> | |
#include <deque> | |
#include <map> | |
using namespace cv; | |
using namespace std; | |
int main(int argc, char **argv) | |
{ | |
string blink = "C:/data/blink/"; | |
string dummy; | |
// let's use seperate label vectors, since your annotations have "closeRight" and "closeLeft" | |
// (skipping those would mean, we're losing too many positives !) | |
// initially assume | |
// they're all "non-blink": | |
vector<int> tlabels_L(15832,0); | |
vector<int> tlabels_R(15832,0); | |
// then fill in the ground-truth positives: | |
ifstream anno(blink + "blink8_groundtruth.txt"); | |
//getline(anno, dummy); | |
// ^ you do not need to skip the 1st line, imho. | |
// this looks wrong, too: | |
//while ( getline (anno,dummy) ) | |
while ( anno.good() ) | |
{ | |
int a; | |
anno >> a; | |
anno >> dummy ; | |
if (dummy == "close") | |
{ // only "Full blink used here. data file contains frame, state (close)" | |
tlabels_L[a] = 1; | |
tlabels_R[a] = 1; | |
} | |
if (dummy == "closeLeft") | |
{ | |
tlabels_L[a] = 1; | |
} | |
if (dummy == "closeRight") | |
{ | |
tlabels_R[a] = 1; | |
} | |
} | |
// assuming, you have 2 txt files with your EAR values, | |
// let's read them, | |
// and write a 13-element time-series and label to csv. | |
ofstream csv("ear.csv"); | |
ifstream in_L(blink + "EAR_OUTPUT_left_EYE.txt"); | |
ifstream in_R(blink + "EAR_OUTPUT_right_EYE.txt"); | |
deque<float> el,er; // our queues | |
for (size_t i=0; i<15832; i++) | |
{ | |
if (! in_L.good()) | |
{ | |
cerr << "bad left file " << i << endl; | |
break; | |
} | |
float ear_L; | |
in_L >> ear_L; | |
el.push_back(ear_L); | |
if (! in_R.good()) | |
{ | |
cerr << "bad right file " << i << endl; | |
break; | |
} | |
float ear_R; | |
in_R >> ear_R; | |
el.push_back(ear_R); | |
// write out queues for left & right eye | |
// "backdated" to t-6 ! (center of the 13 elem queue) | |
if (i >= 13) { | |
csv << tlabels_L[i-6] << ","; | |
for (int k=0; k<13; k++) | |
csv << el[k] << ","; | |
csv << endl; | |
csv << tlabels_R[i-6] << ","; | |
for (int k=0; k<13; k++) | |
csv << er[k] << ","; | |
csv << endl; | |
el.pop_front(); | |
er.pop_front(); | |
} | |
} | |
// train & test with SVM: | |
Ptr<ml::SVM> svm = ml::SVM::create(); | |
Ptr<ml::TrainData> tdata = ml::TrainData::loadFromCSV("ear.csv",0,0,1); | |
tdata->setTrainTestSplitRatio(0.8); | |
Mat data = tdata->getTrainSamples(); | |
Mat labels = tdata->getTrainResponses(); | |
labels.convertTo(labels, CV_32S); // hrrm! | |
svm->train(data,0,labels); | |
Mat vdata = tdata->getTestSamples(); | |
Mat vlabels = tdata->getTestResponses(); | |
Mat results; | |
svm->predict(vdata,results); | |
float correct = countNonZero(results == vlabels); | |
float accuracy = correct / results.total(); | |
cerr << "accuracy: " << accuracy << endl; | |
// accuracy alone is not enough here, since it might | |
// simply have missed all positives ! | |
Mat_<float> confusion(2,2, 0.0f); | |
for (int i=0; i<results.rows; i++) { | |
int p = (int)results.at<float>(i); | |
int t = (int)vlabels.at<float>(i); | |
confusion(p,t) ++; | |
} | |
cerr << "confusion:\n" << confusion << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment