Last active
December 29, 2016 10:15
-
-
Save sarmadm/b6ab86b86613b0407730bc409a519ed6 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; | |
// there are 5000 frames in the db, first assume | |
// they're all "non-blink": | |
vector<int> tlabels(15832,0); | |
// then fill in the ground-truth positives: | |
ifstream anno(blink + "blink8_groundtruth.txt"); | |
getline(anno, dummy); | |
while ( getline (anno,dummy) ) | |
{ | |
int a; | |
anno >> a; | |
anno >> dummy ; | |
if (dummy == "close") | |
{ // only "Full blink used here. data file contains frame, state (close)" | |
fill(tlabels.begin() + a, 1); | |
} | |
// for negative blink (half). | |
else | |
{ | |
fill(tlabels.begin() + a, 0); | |
} | |
} | |
// parse 5000 annotation(landmarks) files, | |
// select p27,p28,p29,p30 for left eye, | |
// and p32,p33,p34,p35 for right eye. | |
// calculate eye-aspect-ratio (ear) for both, | |
// and write a 13-element time-series to csv. | |
ofstream csv("ear.csv"); | |
deque<float> el,er; | |
for (size_t i=0; i<15832; i++) | |
{ | |
ifstream in_L(blink + "EAR_OUTPUT_left_EYE.txt"); | |
if (! in_L.good()) { | |
cerr << "bad file " << i << endl; | |
continue; | |
} | |
// skip first 30 lines (3 lines offset + 27) , why it is 30 ? is this the case for blink8 data ? | |
string dummy_L; | |
for (int j=0; j<15832; j++) | |
{ | |
getline(in_L, dummy_L); | |
el.push_back(ear_l); | |
} | |
for (size_t i=0; i<15832; i++) | |
{ | |
ifstream in_R(blink + "EAR_OUTPUT_RIGHT_EYE.txt"); | |
if (! in_R.good()) { | |
cerr << "bad file " << i << endl; | |
continue; | |
} | |
// skip first 30 lines (3 lines offset + 27) , why it is 30 ? is this the case for blink8 data ? | |
string dummy_R; | |
for (int j=0; j<15832; j++) | |
{ | |
getline(in_R, dummy_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[i-6] << ","; | |
for (int k=0; k<13; k++) | |
csv << el[k] << ","; | |
csv << endl; | |
csv << tlabels[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.7); | |
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); | |
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; | |
// additionally, do ROC analysis. | |
// we need raw output, so another prediction required: | |
svm->predict(vdata, results, ml::StatModel::RAW_OUTPUT); | |
normalize(results, results, 1, 0, NORM_MINMAX); | |
// svm gives distances, needed are probs | |
results = 1.0f - results; | |
std::vector<Point2f> roc; | |
ROC::curve(results, vlabels, roc, 100); | |
cerr << "AUC " << ROC::auc(roc) << endl; | |
Mat roc_draw(480, 640, CV_8UC3, Scalar::all(255)); | |
ROC::draw(roc, roc_draw, Scalar(255,0,0)); | |
imshow("ROC",roc_draw); | |
waitKey(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment