GazeEstimation.cpp 24.4 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3 4 5 6 7 8 9
#include "GazeEstimation.h"

using namespace std;
using namespace cv;

GazeEstimation::GazeEstimation(QObject *parent)
    : QObject(parent),
      calibrated(false),
      isCalibrating(false),
Thiago Santini's avatar
Thiago Santini committed
10 11
	  gazeEstimationMethod(NULL),
	  lastOverlayIdx(0),
Thiago Santini's avatar
Thiago Santini committed
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
      settings(NULL)
{
    availableGazeEstimationMethods.push_back( new PolyFit(PolyFit::POLY_1_X_Y_XY_XX_YY_XYY_YXX_XXYY) );
    availableGazeEstimationMethods.push_back( new PolyFit(PolyFit::POLY_1_X_Y_XY) );
    availableGazeEstimationMethods.push_back( new PolyFit(PolyFit::POLY_1_X_Y_XY_XX_YY) );
    availableGazeEstimationMethods.push_back( new PolyFit(PolyFit::POLY_1_X_Y_XY_XX_YY_XXYY) );
    availableGazeEstimationMethods.push_back( new PolyFit(PolyFit::POLY_1_X_Y_XY_XX_YY_XYY_YXX) );
    availableGazeEstimationMethods.push_back( new PolyFit(PolyFit::POLY_1_X_Y_XY_XX_YY_XYY_YXX_XXX_YYY) );
    availableGazeEstimationMethods.push_back( new Homography() );
}
GazeEstimation::~GazeEstimation()
{
    for (int i=0; i<availableGazeEstimationMethods.size(); i++)
        delete availableGazeEstimationMethods[i];
    availableGazeEstimationMethods.clear();
}

void GazeEstimation::addTuple(CollectionTuple tuple)
{
    collectedTuples.push_back(tuple);
}
void GazeEstimation::addTuples(std::vector<CollectionTuple> tuples)
{
    collectedTuples.insert( collectedTuples.end(), tuples.begin(), tuples.end());
}
void GazeEstimation::reset(CollectionTuple::TupleType type)
{
    for (size_t i=collectedTuples.size(); i-->0;)
        if (collectedTuples[i].tupleType == type)
Thiago Santini's avatar
Thiago Santini committed
41
			collectedTuples.erase(collectedTuples.begin() + i);
Thiago Santini's avatar
Thiago Santini committed
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

bool GazeEstimation::isPupilOutlineValid(const EyeData &cur)
{
    if (cur.pupil.size.width <= 0 || cur.pupil.size.height <= 0)
        return false;
    return true;
}
bool GazeEstimation::isPupilRatioValid(const EyeData &prev, const EyeData &cur)
{
    // TODO: parametrize me
    int temporalThresholdMs = 500;
    double ratioThreshold = 0.75;

    // too far away in time, disconsider
    if (cur.timestamp - prev.timestamp > temporalThresholdMs)
        return true;

    std::pair<double, double> values = std::minmax(
                                               cur.pupil.size.area(),
                                               prev.pupil.size.area()
                                               );

    if (values.second == 0)
        return true;

    return (values.first / values.second < ratioThreshold) ? false : true;
}
void GazeEstimation::findPupilPositionOutliers(const Mat &pupil, Mat &mask)
{
    unsigned int outliers = 0;
    double sigmaThreshold = 2.7;
    while (true) {
        Scalar mu, s;
        meanStdDev(pupil, mu, s);
        for (int i=0; i<pupil.rows; i++) {
            if (mask.at<uchar>(i) == 0)
                continue;
            if (  pupil.at<Vec2f>(i)[0] < mu[0] - sigmaThreshold*s[0]
               || pupil.at<Vec2f>(i)[0] > mu[0] + sigmaThreshold*s[0]
               || pupil.at<Vec2f>(i)[1] < mu[1] - sigmaThreshold*s[1]
               || pupil.at<Vec2f>(i)[1] > mu[1] + sigmaThreshold*s[1]) {
                outliers++;
                mask.at<uchar>(i) = 0;
            }
        }
        if (outliers <= 0)
            break;
        outliers = 0;
    }
}
void GazeEstimation::detectOutliers()
{
    // reset outlier information
    for (unsigned int i=0; i<calibrationTuples.size(); i++)
        calibrationTuples[i]->outlierDesc = CollectionTuple::OD_INLIER;

    // Check for input type and pupil validity
    CollectionTuple *t;
    for (unsigned int i=0; i<calibrationTuples.size(); i++) {
        t = calibrationTuples[i];
        switch (cfg.inputType) {
            case GazeEstimationMethod::BINOCULAR:
            case GazeEstimationMethod::BINOCULAR_MEAN_POR:
                if ( !t->lEye.validPupil || ! t->rEye.validPupil )
                    t->outlierDesc = CollectionTuple::OD_MISSING_INPUT;
                break;
            case GazeEstimationMethod::MONO_LEFT:
                if ( !t->lEye.validPupil )
                    t->outlierDesc = CollectionTuple::OD_MISSING_INPUT;
                break;
            case GazeEstimationMethod::MONO_RIGHT:
                if ( !t->rEye.validPupil )
                    t->outlierDesc = CollectionTuple::OD_MISSING_INPUT;
                break;
        }
    }

    if (!cfg.removeOutliers)
        return;

    if (cfg.pupilRatioOutliers) {
        for (unsigned int i=1; i<calibrationTuples.size(); i++) {
            CollectionTuple *prev = calibrationTuples[i-1];
            CollectionTuple *cur = calibrationTuples[i];
            if (prev->isOutlier() || cur->isOutlier())
                continue;

            bool validLeft = isPupilRatioValid(prev->lEye, cur->lEye);
            bool validRight = isPupilRatioValid(prev->rEye, cur->rEye);

            switch (cfg.inputType) {
                case GazeEstimationMethod::BINOCULAR:
                case GazeEstimationMethod::BINOCULAR_MEAN_POR:
                    if (!validLeft || !validRight)
                        cur->outlierDesc = CollectionTuple::OD_PUPIL_RATIO;
                    break;
                case GazeEstimationMethod::MONO_LEFT:
                    if (!validLeft)
                        cur->outlierDesc = CollectionTuple::OD_PUPIL_RATIO;
                    break;
                case GazeEstimationMethod::MONO_RIGHT:
                    if (!validRight)
                        cur->outlierDesc = CollectionTuple::OD_PUPIL_RATIO;
                    break;
            }
        }
    }

    if (cfg.pupilPositionOutliers) {
        Mat lp(0, 0, CV_32FC2);
        Mat rp(0, 0, CV_32FC2);
        Mat mask(1, (int) calibrationTuples.size(), CV_8U);
        for (unsigned int i=0; i<calibrationTuples.size(); i++) {
            CollectionTuple *t = calibrationTuples[i];
            lp.push_back(t->lEye.pupil.center);
            rp.push_back(t->rEye.pupil.center);
            mask.at<uchar>(i) = t->isOutlier() ? 0 : 1;
        }
        if (cfg.inputType != GazeEstimationMethod::MONO_RIGHT)
                findPupilPositionOutliers(lp, mask);
        if (cfg.inputType != GazeEstimationMethod::MONO_LEFT)
                findPupilPositionOutliers(rp, mask);
        for (unsigned int i=0; i<calibrationTuples.size(); i++)
            if (mask.at<uchar>(i) == (uchar) 0)
                calibrationTuples[i]->outlierDesc = CollectionTuple::OD_PUPIL_POSITION;
    }

    if (cfg.pupilOutlineOutliers) {
        for (unsigned int i=0; i<calibrationTuples.size(); i++) {
            CollectionTuple *cur = calibrationTuples[i];
            if (cur->isOutlier())
                continue;

            bool validLeft = isPupilOutlineValid(cur->lEye);
            bool validRight = isPupilOutlineValid(cur->rEye);

            switch (cfg.inputType) {
                case GazeEstimationMethod::BINOCULAR:
                case GazeEstimationMethod::BINOCULAR_MEAN_POR:
                    if (!validLeft || !validRight)
                        cur->outlierDesc = CollectionTuple::OD_PUPIL_POSITION;
                    break;
                case GazeEstimationMethod::MONO_LEFT:
                    if (!validLeft)
                        cur->outlierDesc = CollectionTuple::OD_PUPIL_POSITION;
                    break;
                case GazeEstimationMethod::MONO_RIGHT:
                    if (!validRight)
                        cur->outlierDesc = CollectionTuple::OD_PUPIL_POSITION;
                    break;
            }
        }
    }

}

void GazeEstimation::calibrate()
{
    calibrated = false;
    autoVisualizationTimer.invalidate();
    errorVectors.clear();
    interpolationHull.clear();
    evaluationRegions.clear();

    QString error = "No gaze estimation method available.";
    if (!gazeEstimationMethod)
        return;

    calibrationTuples.clear();

    for (unsigned int i=0; i<collectedTuples.size(); i++)
        if (collectedTuples[i].isCalibration())
            calibrationTuples.push_back(&collectedTuples[i]);
    if (calibrationTuples.size() <= 0) {
        error = "No calibration tuples available.";
        qInfo() << error;
        emit calibrationFinished(false, error);
        return;
    }

    detectOutliers();

    if (cfg.autoEvaluation)
        selectEvaluationTuples(cfg.granularity, cfg.horizontalStride, cfg.verticalStride, cfg.rangeFactor);
    else
        for (unsigned int i=0; i<calibrationTuples.size(); i++)
            calibrationTuples[i]->autoEval = CollectionTuple::AE_NO;

    vector<CollectionTuple> calibrationInliers;
    for (unsigned int i=0; i<calibrationTuples.size(); i++)
        if (!calibrationTuples[i]->isOutlier())
            calibrationInliers.push_back(*calibrationTuples[i]);

236 237
    updateInterpolationHull(calibrationInliers);

Thiago Santini's avatar
Thiago Santini committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    qInfo() << "Using" << calibrationInliers.size() << "/" << calibrationTuples.size() << "calibration tuples.";
    if (calibrationInliers.size() <= 0) {
        error = "No calibration tuples remaining.";
        qInfo() << error;
        emit calibrationFinished(false, error);
        return;
    }

    calibrated = gazeEstimationMethod->calibrate(calibrationInliers, cfg.inputType, error);

    if (!calibrated) {
        qInfo() << error;
        emit calibrationFinished(false, error);
        return;
    }
    evaluate();
    autoVisualizationTimer.restart();
    emit calibrationFinished(true, "");
}

void GazeEstimation::estimate(DataTuple dataTuple)
{
    if (!gazeEstimationMethod)
        return;

    if (calibrated) {
264
        GazeEstimationMethod::InputType inputType = cfg.inputType;
265 266
		bool lValid = dataTuple.lEye.pupil.center.x > 0 && dataTuple.lEye.pupil.center.y > 0 && dataTuple.lEye.pupil.confidence > 0.66;
		bool rValid = dataTuple.rEye.pupil.center.x > 0 && dataTuple.rEye.pupil.center.y > 0 && dataTuple.rEye.pupil.confidence > 0.66;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

        switch(inputType) {
            case GazeEstimationMethod::BINOCULAR:
            case GazeEstimationMethod::BINOCULAR_MEAN_POR:
                if (!lValid && !rValid) // None valid
                    break;
                if (lValid && !rValid) // Use only left
                    inputType = GazeEstimationMethod::MONO_LEFT;
                if (rValid && !lValid) // Use only right
                    inputType = GazeEstimationMethod::MONO_RIGHT;
                dataTuple.field.gazeEstimate = gazeEstimationMethod->estimateGaze(dataTuple, inputType);
                dataTuple.field.validGazeEstimate = true;
                break;
            case GazeEstimationMethod::MONO_LEFT:
                if (!lValid)
                    break;
                dataTuple.field.gazeEstimate = gazeEstimationMethod->estimateGaze(dataTuple, inputType);
                dataTuple.field.validGazeEstimate = true;
                break;
            case GazeEstimationMethod::MONO_RIGHT:
                if (!rValid)
                    break;
                dataTuple.field.gazeEstimate = gazeEstimationMethod->estimateGaze(dataTuple, inputType);
                dataTuple.field.validGazeEstimate = true;
                break;
        }

Thiago Santini's avatar
Thiago Santini committed
294 295 296 297 298 299 300
    }

    drawGazeEstimationInfo(dataTuple);

    emit gazeEstimationDone(dataTuple);
}

301 302 303 304 305 306 307 308 309 310 311 312 313
void GazeEstimation::printAccuracyInfo(const cv::Mat &errors, const QString &which, const double &diagonal)
{
    Scalar m, std;
    meanStdDev(errors, m, std);
    qInfo() << which.toLatin1().data() <<  "Error ( N =" << errors.rows << "):";
    //qInfo() << QString("m = %1 ( std = %2 ) pixels."
    //    ).arg(m[0], 6, 'f', 2
    //    ).arg(std[0], 6, 'f', 2).toLatin1().data();
    qInfo() << QString("m = %1 ( std = %2 ) % of the image diagonal."
        ).arg(100 * m[0] / diagonal, 6, 'f', 2
        ).arg(100 * std[0] / diagonal, 6, 'f', 2).toLatin1().data();
}

Thiago Santini's avatar
Thiago Santini committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
void GazeEstimation::evaluate()
{
    if (!calibrated)
        return;

    evaluationTuples.clear();
    for (unsigned int i=0; i<collectedTuples.size(); i++)
        if (collectedTuples[i].isEvaluation() || (cfg.autoEvaluation && collectedTuples[i].isAutoEval()) )
            evaluationTuples.push_back(&collectedTuples[i]);

    if (evaluationTuples.size() <= 0 || calibrationTuples.size() <= 0)
        return;

    // evaluate for known points
    errorVectors.clear();
329
    Mat errors;
Thiago Santini's avatar
Thiago Santini committed
330 331 332 333
    for (unsigned int i=0; i<evaluationTuples.size(); i++) {
        Point3f gt = evaluationTuples[i]->field.collectionMarker.center;
        Point3f gaze = gazeEstimationMethod->estimateGaze(*evaluationTuples[i], cfg.inputType);
        errorVectors.push_back(ErrorVector(gt, gaze));
334
        errors.push_back( errorVectors.back().magnitude() );
Thiago Santini's avatar
Thiago Santini committed
335 336
    }

337 338 339 340 341
    double imDiagonal = sqrt( pow(evaluationTuples[0]->field.width,2) + pow(evaluationTuples[0]->field.height, 2) );
    printAccuracyInfo(errors, "Gaze Evaluation", imDiagonal);

    if (cfg.autoEvaluation) {
        double evaluationRegionsCount = pow(1 + 2*cfg.granularity, 2);
342 343 344 345 346 347
        double evaluationRegionsSelected = 0;
        for (unsigned int i=0; i<collectedTuples.size(); i++)
            if ( collectedTuples[i].isAutoEval() )
                evaluationRegionsSelected++;
        qInfo() << QString("Auto Evaluation Region Coverage: %1 %"
                           ).arg(100 * evaluationRegionsSelected / evaluationRegionsCount, 0, 'f', 2).toLatin1().data();
348
    }
Thiago Santini's avatar
Thiago Santini committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
}

/*
 * File loading / saving
 */
template<typename T> static void addData(map<QString, uint> &idxMap, QStringList &tokens, QString key, T &data)
{
    if (idxMap.count(key) == 0)
        return;

    data = QVariant(tokens[idxMap[key]]).value<T>();
}
void GazeEstimation::loadTuplesFromFile(CollectionTuple::TupleType tupleType, QString fileName)
{
    QFile inputFile(fileName);
    if (!inputFile.open(QIODevice::ReadOnly))
        return;
    QTextStream in(&inputFile);
    QStringList header;
    map<QString, uint> idxMap;
    vector<CollectionTuple> tuples;
    while (!in.atEnd())
    {
        QString line = in.readLine();
        QStringList tokens = line.split(gDataSeparator);
        CollectionTuple tuple;

        if (header.isEmpty()) {
            header = tokens;
            for( int i=0; i<tokens.size(); i++ )
                idxMap[tokens[i]] = i;
        } else {
            addData(idxMap, tokens, "timestamp", tuple.timestamp);
            // Field
            addData(idxMap, tokens, "field.timestamp", tuple.field.timestamp);
            addData(idxMap, tokens, "field.gaze.x", tuple.field.gazeEstimate.x);
            addData(idxMap, tokens, "field.gaze.y", tuple.field.gazeEstimate.y);
            addData(idxMap, tokens, "field.gaze.z", tuple.field.gazeEstimate.z);
            addData(idxMap, tokens, "field.gaze.valid", tuple.field.validGazeEstimate);
            addData(idxMap, tokens, "field.collectionMarker.id", tuple.field.collectionMarker.id);
            addData(idxMap, tokens, "field.collectionMarker.x", tuple.field.collectionMarker.center.x);
            addData(idxMap, tokens, "field.collectionMarker.y", tuple.field.collectionMarker.center.y);
            addData(idxMap, tokens, "field.collectionMarker.z", tuple.field.collectionMarker.center.z);
            addData(idxMap, tokens, "field.undistorted", tuple.field.undistorted);
            addData(idxMap, tokens, "field.width", tuple.field.width);
            addData(idxMap, tokens, "field.height", tuple.field.height);
            // TODO: read other markers

            // Left Eye
            addData(idxMap, tokens, "left.timestamp", tuple.lEye.timestamp);
            addData(idxMap, tokens, "left.pupil.x", tuple.lEye.pupil.center.x);
            addData(idxMap, tokens, "left.pupil.y", tuple.lEye.pupil.center.y);
            addData(idxMap, tokens, "left.pupil.width", tuple.lEye.pupil.size.width);
            addData(idxMap, tokens, "left.pupil.height", tuple.lEye.pupil.size.height);
            addData(idxMap, tokens, "left.pupil.angle", tuple.lEye.pupil.angle);
            addData(idxMap, tokens, "left.pupil.valid", tuple.lEye.validPupil);

            // Right Eye
            addData(idxMap, tokens, "right.timestamp", tuple.rEye.timestamp);
            addData(idxMap, tokens, "right.pupil.x", tuple.rEye.pupil.center.x);
            addData(idxMap, tokens, "right.pupil.y", tuple.rEye.pupil.center.y);
            addData(idxMap, tokens, "right.pupil.width", tuple.rEye.pupil.size.width);
            addData(idxMap, tokens, "right.pupil.height", tuple.rEye.pupil.size.height);
            addData(idxMap, tokens, "right.pupil.angle", tuple.rEye.pupil.angle);
            addData(idxMap, tokens, "right.pupil.valid", tuple.rEye.validPupil);

            // Add
            tuple.tupleType = tupleType;
            tuples.push_back(tuple);
        }
    }

    addTuples(tuples);
    inputFile.close();

    calibrate();
}

void GazeEstimation::saveTuplesToFile(CollectionTuple::TupleType tupleType, QString fileName)
{
    vector<CollectionTuple*> pertinentTuples;
    for (unsigned int i=0; i<collectedTuples.size(); i++) {
        if ( tupleType == CollectionTuple::CALIBRATION)
            if (collectedTuples[i].isEvaluation())
                continue;

        if ( tupleType == CollectionTuple::EVALUATION)
            if (collectedTuples[i].isCalibration())
                continue;

        pertinentTuples.push_back(&collectedTuples[i]);
    }

    saveTuplesToFile(pertinentTuples, fileName, QIODevice::WriteOnly);
}

void GazeEstimation::saveTuplesToFile(const std::vector<CollectionTuple *> &tuples, QString fileName, QFlags<QIODevice::OpenModeFlag> flags)
{
    if (fileName.isEmpty() || tuples.size() == 0)
        return;

    QString buffer;
    buffer.append(tuples[0]->header() + gDataNewline);
    for (unsigned int i=0; i<tuples.size(); i++)
        buffer.append(tuples[i]->toQString() + gDataNewline);

    QFile f(fileName);
    f.open(flags);
    QTextStream s(&f);
    if (s.status() == QTextStream::Ok)
        s << buffer.toStdString().c_str();
    f.close();
}

void GazeEstimation::updateConfig()
{
    cfg.load(settings);
    for (int i=0; i<availableGazeEstimationMethods.size(); i++) {
        QString name = availableGazeEstimationMethods[i]->description().c_str();
        if (cfg.gazeEstimationMethod == name)
            gazeEstimationMethod = availableGazeEstimationMethods[i];
    }
    calibrate();
}

void GazeEstimation::selectEvaluationTuples(const int g,  const double dx, const double dy, const double rf)
{
    vector<CollectionTuple*> evaluationCandidates;
    vector<Point> points;
    CollectionTuple* t;

    evaluationRegions.clear();

    /*
     * collect non-outlier candidates
     */
    for (unsigned int i=0; i<calibrationTuples.size(); i++) {
        t = calibrationTuples[i];
        t->autoEval = CollectionTuple::AE_NO;
        if (t->isOutlier())
            continue;
        evaluationCandidates.push_back(t);
        points.push_back( to2D(t->field.collectionMarker.center) );
    }

    if (evaluationCandidates.size() <= 0)
        return;

    /*
     *  exclude points in the hull so we don't lose interpolation range
     */
    convexHull(points, interpolationHull);

//#define DBG_AUTOEVAL
#ifdef DBG_AUTOEVAL
    const Point *ep[1] = { &interpolationHull[0] };
    int epn = (int) interpolationHull.size();
    Mat dbg = Mat::zeros( evaluationCandidates[0]->field.height, evaluationCandidates[0]->field.width, CV_8UC3);
    fillPoly( dbg, ep, &epn, 1, Scalar(255,255,255));
    for (size_t i=evaluationCandidates.size(); i-->0;) {
        Point gaze =  to2D( evaluationCandidates[i]->field.collectionMarker.center );
        circle(dbg, gaze, 5, Scalar(0,255,255), -1);
    }
#endif

    for (size_t i=evaluationCandidates.size(); i-->0;) {
        Point gaze =  to2D( evaluationCandidates[i]->field.collectionMarker.center );
        for (unsigned int j=0; j<interpolationHull.size(); j++) {
            if ( ED(gaze,interpolationHull[j]) <= 1.5 ) {
#ifdef DBG_AUTOEVAL
                circle(dbg, gaze, 5, Scalar(255,0,0), -1);
#endif
                evaluationCandidates.erase(evaluationCandidates.begin()+i);
                break;
            }
        }
    }

    /*
     * Select points that minimize distance to evaluation region center for evaluation
     */
    Point center(t->field.width/2, t->field.height/2);
    int dxPx = dx*t->field.width;
    int dyPx = dy*t->field.height;
    int w = rf*dxPx;
    int h = rf*dyPx;
    vector<Point> evaluationPoints;
    for (int x = -g; x <= g; x++) {
        for (int y = -g; y <= g; y++) {
            EvaluationRegion er( center.x + x*dxPx, center.y + y*dyPx, w, h );
            for (int i=0; i<evaluationCandidates.size(); i++)
                er.eval(evaluationCandidates[i]);
            if (er.selected) {
                er.selected->autoEval = CollectionTuple::AE_YES;
                evaluationPoints.push_back(to2D(er.selected->field.collectionMarker.center));
            }
            evaluationRegions.push_back(er);
#ifdef DBG_AUTOEVAL
            er.draw(dbg);
#endif
        }
    }

    /*
     * Mark calibration points that could bias the auto evaluation
     */
    for (unsigned int i=0; i<evaluationPoints.size(); i++) {
        Point ep = evaluationPoints[i];
        for (unsigned int j=0; j<evaluationCandidates.size(); j++) {
            t = evaluationCandidates[j];
            if (t->autoEval == CollectionTuple::AE_NO) {
                Point cp = to2D(t->field.collectionMarker.center);
                if ( ED(ep, cp) < 1.5 ) {
                    t->autoEval = CollectionTuple::AE_BIASED;
#ifdef DBG_AUTOEVAL
                    circle( dbg, cp, 2, Scalar(255,255,0), -1);
#endif
                }
            }
        }
    }

#ifdef DBG_AUTOEVAL
    imshow("dbg", dbg);
#endif
}

void GazeEstimation::drawGazeEstimationInfo(DataTuple &dataTuple)
{
    dataTuple.showGazeEstimationVisualization = false;

    // TODO: improve this? maybe draw on an overlay canvas that is only updated
    // when something changes. Then we overlay the canvas on a copy of the image
    if (dataTuple.field.input.empty() || !cfg.visualize)
        return;

    bool shouldDisplay = false;
    if (isCalibrating)
        shouldDisplay = true;
    else {
        if (autoVisualizationTimer.isValid()) {
            if (autoVisualizationTimer.elapsed() < cfg.visualizationTimeS*1e3)
                shouldDisplay = true;
            else
                autoVisualizationTimer.invalidate();
        }
    }
    if (!shouldDisplay)
        return;

Thiago Santini's avatar
Thiago Santini committed
599 600
	dataTuple.showGazeEstimationVisualization = true;
	dataTuple.gazeEstimationVisualization = vis;
Thiago Santini's avatar
Thiago Santini committed
601

Thiago Santini's avatar
Thiago Santini committed
602 603 604 605 606 607 608
	// avoid drawing every single frame
	static Timestamp lastGazeEstimationVisualizationTimestamp = 0;
	Timestamp current = gTimer.elapsed();
	bool shouldDraw = current - lastGazeEstimationVisualizationTimestamp > 40;
	if (!shouldDraw)
		return;
	lastGazeEstimationVisualizationTimestamp = current;
Thiago Santini's avatar
Thiago Santini committed
609

Thiago Santini's avatar
Thiago Santini committed
610
	vis  = dataTuple.field.input.clone();
Thiago Santini's avatar
Thiago Santini committed
611 612
    int r = max<int>( 1, 0.003125*max<int>(vis.rows, vis.cols) );

Thiago Santini's avatar
Thiago Santini committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	if (isCalibrating) {

		if (lastOverlayIdx > collectedTuples.size()) // sample removed, restart
			lastOverlayIdx = 0;

		if (lastOverlayIdx == 0)
			overlay = Mat::zeros( vis.rows, vis.cols, CV_8UC3);

		for ( ; lastOverlayIdx < collectedTuples.size(); lastOverlayIdx++) {
			circle(overlay, to2D(collectedTuples[lastOverlayIdx].field.collectionMarker.center), r, CV_ALMOST_BLACK, -1);
			if (collectedTuples[lastOverlayIdx].isCalibration())
				circle(overlay, to2D(collectedTuples[lastOverlayIdx].field.collectionMarker.center), r+1, CV_GREEN, 0.5*r);
			else
				circle(overlay, to2D(collectedTuples[lastOverlayIdx].field.collectionMarker.center), r+1, CV_CYAN, 0.5*r);
		}

	} else {
		// Calibration finished; overlay results and restart
		overlay = Mat::zeros( vis.rows, vis.cols, CV_8UC3);
		lastOverlayIdx = 0;

		if (!interpolationHull.empty()) {
Thiago Santini's avatar
Thiago Santini committed
635 636
            vector< vector<Point> > contours;
            contours.push_back(interpolationHull);
Thiago Santini's avatar
Thiago Santini committed
637
			drawContours(overlay, contours, 0, CV_GREEN, r);
Thiago Santini's avatar
Thiago Santini committed
638 639 640
        }

        for (size_t i=0; i<errorVectors.size(); i++)
Thiago Santini's avatar
Thiago Santini committed
641
			errorVectors[i].draw(overlay, 2, CV_RED);
Thiago Santini's avatar
Thiago Santini committed
642 643 644

        if (cfg.autoEvaluation)
            for (size_t i=0; i<evaluationRegions.size(); i++)
Thiago Santini's avatar
Thiago Santini committed
645
				evaluationRegions[i].draw(overlay, r);
Thiago Santini's avatar
Thiago Santini committed
646

Thiago Santini's avatar
Thiago Santini committed
647 648 649 650 651 652 653 654 655 656 657 658
		for (size_t i=0; i<collectedTuples.size(); i++) {
			if (!collectedTuples[i].isCalibration()) {
				circle(overlay, to2D(collectedTuples[i].field.collectionMarker.center), r, CV_BLACK, -1);
				circle(overlay, to2D(collectedTuples[i].field.collectionMarker.center), r+1, CV_CYAN, 0.5*r);
			}
		}

	}

	// Overlay on visualization image; notice we use CV_ALMOST_BLACK instead of
	// CV_BLACK so we don't need to create an additional mask :-)
	overlay.copyTo(vis, overlay);
Thiago Santini's avatar
Thiago Santini committed
659 660 661 662 663 664 665
}

void GazeEstimation::setCalibrating(bool v)
{
    isCalibrating = v;
}

666 667 668 669 670 671 672 673 674 675 676
void GazeEstimation::updateInterpolationHull(const std::vector<CollectionTuple> &tuples)
{
    if (tuples.size() <= 0)
        return;

    vector<Point> points;
    for (unsigned int i=0; i<tuples.size(); i++)
        points.push_back( to2D(tuples[i].field.collectionMarker.center) );

    convexHull(points, interpolationHull);
}