GazeEstimation.h 7.24 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
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
#ifndef GAZEESTIMATION_H
#define GAZEESTIMATION_H

#include <vector>
#include <deque>
#include <map>

#include <QObject>

#include "gaze-estimation/GazeEstimationMethod.h"
#include "gaze-estimation/PolyFit.h"
#include "gaze-estimation/Homography.h"

#include "Reference.h"

/*******************************************************************************
 * Helper Classes
 ******************************************************************************/
class EvaluationRegion {
public:
    EvaluationRegion(int x, int y, int w, int h)
    : x(x), y(y), w(w), h(h),
      rw2(pow(w/2.0,2)),
      rh2(pow(h/2.0,2)),
      mn(DBL_MAX),
      selected(NULL) {}
    int x, y, w, h, rw2, rh2;
    double mn;
    CollectionTuple *selected;
    void eval(CollectionTuple* t) {
    cv::Point p = to2D(t->field.collectionMarker.center);
    double d = (pow(p.x - x, 2) / rw2  + pow(p.y - y, 2) / rh2);
    if ( d <= 1 && d < mn) {
        mn = d;
        selected = t;
    }
    }
    void draw(cv::Mat &in, int r = 3) {
        cv::RotatedRect el(cv::Point(x,y), cv::Size(w,h), 0);
        if (selected){
            cv::ellipse(in, el, CV_GREEN, 0.5*r);
Thiago Santini's avatar
Thiago Santini committed
42
			cv::circle( in, to2D(selected->field.collectionMarker.center), r, CV_ALMOST_BLACK, -1);
Thiago Santini's avatar
Thiago Santini committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
            cv::circle( in, to2D(selected->field.collectionMarker.center), r+1, CV_GREEN, r);
        } else
            cv::ellipse(in, el, CV_RED, 0.5*r);
    }
};

class ErrorVector {
public:
    ErrorVector(cv::Point3f gt, cv::Point3f gaze) :
        gt(gt), gaze(gaze) {}

    cv::Point3f gt;
    cv::Point3f gaze;
    void draw(cv::Mat &in, int w = 2, cv::Scalar color = CV_RED) {
        cv::line( in, cv::Point(gt.x, gt.y), cv::Point(gaze.x, gaze.y), color, w);
    }
59
    double magnitude() { return cv::norm(gt - gaze); }
Thiago Santini's avatar
Thiago Santini committed
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
};

/*******************************************************************************
 * Gaze Estimation
 ******************************************************************************/
class GazeEstimationConfig
{
public:
    GazeEstimationConfig() :
        samplingTimeMs(500),
        useMedian(false),
        removeOutliers(true),
        pupilRatioOutliers(true),
        pupilPositionOutliers(true),
        pupilOutlineOutliers(true),
        autoEvaluation(true),
        granularity(2),
        horizontalStride(0.15),
        verticalStride(0.15),
        rangeFactor(0.95),
        inputType(GazeEstimationMethod::BINOCULAR_MEAN_POR),
        gazeEstimationMethod("POLY_X_Y_XY_XX_YY_XYY_YXX_XXYY"),
        visualize(true),
        visualizationTimeS(5)
    {}

    /*
     * Gaze Estimation Widget
     */
    int samplingTimeMs;
    bool useMedian;

    /*
     * Gaze Estimation
     */
    // outlier removal
    bool removeOutliers;
    bool pupilRatioOutliers;
    bool pupilPositionOutliers;
    bool pupilOutlineOutliers;

    // Method
    GazeEstimationMethod::InputType inputType;
    QString gazeEstimationMethod;

    // Visualization
    bool visualize;
    int visualizationTimeS;

    // CalibMe
    bool autoEvaluation;
    int granularity;
    double horizontalStride;
    double verticalStride;
    double rangeFactor;


    void save(QSettings *settings)
    {
        settings->sync();
        settings->setValue("samplingTimeMs", samplingTimeMs);
        settings->setValue("useMedian", useMedian);
        settings->setValue("inputType", inputType);
        settings->setValue("gazeEstimationMethod", gazeEstimationMethod);
        settings->setValue("visualize", visualize);
        settings->setValue("visualizationTimeS", visualizationTimeS);

        // CalibMe
        settings->setValue("CalibMe/removeOutliers", removeOutliers);
        settings->setValue("CalibMe/pupilRatioOutliers", pupilRatioOutliers);
        settings->setValue("CalibMe/pupilPositionOutliers", pupilPositionOutliers);
        settings->setValue("CalibMe/pupilOutlineOutliers", pupilOutlineOutliers);
        settings->setValue("CalibMe/autoEvaluation", autoEvaluation);
        settings->setValue("CalibMe/granularity", granularity);
        settings->setValue("CalibMe/horizontalStride", horizontalStride);
        settings->setValue("CalibMe/verticalStride", verticalStride);
        settings->setValue("CalibMe/rangeFactor", rangeFactor);
    }

    void load(QSettings *settings)
    {
        settings->sync();
        set(settings, "samplingTimeMs", samplingTimeMs);
        set(settings, "useMedian", useMedian);
        set(settings, "inputType", inputType);
        set(settings, "gazeEstimationMethod", gazeEstimationMethod );
        set(settings, "visualize", visualize);
        set(settings, "visualizationTimeS", visualizationTimeS);

        //CalibMe
        set(settings, "CalibMe/removeOutliers", removeOutliers       );
        set(settings, "CalibMe/pupilRatioOutliers", pupilRatioOutliers   );
        set(settings, "CalibMe/pupilPositionOutliers", pupilPositionOutliers);
        set(settings, "CalibMe/pupilOutlineOutliers", pupilOutlineOutliers );
        set(settings, "CalibMe/autoEvaluation", autoEvaluation       );
        set(settings, "CalibMe/granularity", granularity          );
        set(settings, "CalibMe/horizontalStride", horizontalStride     );
        set(settings, "CalibMe/verticalStride", verticalStride       );
        set(settings, "CalibMe/rangeFactor", rangeFactor          );
    }
};

class GazeEstimation : public QObject
{
    Q_OBJECT

public:
    explicit GazeEstimation(QObject *parent = 0);
    ~GazeEstimation();
    QSettings *settings;
    std::vector<GazeEstimationMethod*> availableGazeEstimationMethods;

signals:
    void gazeEstimationDone(DataTuple dataTuple);
    void calibrationFinished(bool status, QString msg);

public slots:
    void loadTuplesFromFile(CollectionTuple::TupleType type, QString fileName);
    void saveTuplesToFile(CollectionTuple::TupleType type, QString fileName);
    void saveTuplesToFile(const std::vector<CollectionTuple*> &tuples, QString fileName, QFlags<QIODevice::OpenModeFlag> flags);
    void addTuple(CollectionTuple tuple);
    void addTuples(std::vector<CollectionTuple> tuples);
    void reset(CollectionTuple::TupleType type);

    void setCalibrating(bool v);

    void calibrate();
    void estimate(DataTuple dataTuple);
    void evaluate();
189
    void printAccuracyInfo(const cv::Mat &errors, const QString &which, const double &diagonal);
Thiago Santini's avatar
Thiago Santini committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    void updateConfig();

private:
    bool calibrated;
    bool isCalibrating;
    GazeEstimationConfig cfg;
    std::vector<CollectionTuple> collectedTuples;
    std::vector<CollectionTuple*> calibrationTuples;
    std::vector<CollectionTuple*> evaluationTuples;


    GazeEstimationMethod *gazeEstimationMethod;
    QMutex cfgMutex;

    std::vector<cv::Point> interpolationHull;
    std::vector<EvaluationRegion> evaluationRegions;
    std::vector<ErrorVector> errorVectors;
    QElapsedTimer autoVisualizationTimer;
    cv::Mat vis;
Thiago Santini's avatar
Thiago Santini committed
210 211 212
	cv::Mat overlay;
	int lastOverlayIdx;

Thiago Santini's avatar
Thiago Santini committed
213 214 215 216 217 218 219 220

private slots:
    void detectOutliers();
    bool isPupilRatioValid(const EyeData &prev, const EyeData &cur);
    void findPupilPositionOutliers(const cv::Mat &pupil, cv::Mat &mask);
    bool isPupilOutlineValid(const EyeData &cur);
    void selectEvaluationTuples(const int g, const double dx, const double dy, const double rf);

221
    void updateInterpolationHull(const std::vector<CollectionTuple> &tuples);
Thiago Santini's avatar
Thiago Santini committed
222 223 224 225
    void drawGazeEstimationInfo(DataTuple &dataTuple);
};

#endif // GAZEESTIMATION_H