GazeEstimation.h 8.06 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
};

/*******************************************************************************
 * 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),
83 84 85 86 87
		visualizationTimeS(5),
		minCentralAreaCoverage(0.8f),
		minPeriphericAreaCoverage(0.1f),
		maxReprojectionError(4.f)
	{}
Thiago Santini's avatar
Thiago Santini committed
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

    /*
     * 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;

119 120 121 122
	// Quality check
	float minCentralAreaCoverage;
	float minPeriphericAreaCoverage;
	float maxReprojectionError;
Thiago Santini's avatar
Thiago Santini committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    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);
143 144 145 146 147 148 149
		settings->setValue("CalibMe/rangeFactor", rangeFactor);

		// Quality check
		settings->setValue("quality/minCentralAreaCoverage", minCentralAreaCoverage);
		settings->setValue("quality/minPeriphericAreaCoverage", minPeriphericAreaCoverage);
		settings->setValue("quality/maxReprojectionError", maxReprojectionError);
	}
Thiago Santini's avatar
Thiago Santini committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    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       );
170 171 172 173 174 175
		set(settings, "CalibMe/rangeFactor", rangeFactor          );

		set(settings, "quality/minCentralAreaCoverage", minCentralAreaCoverage );
		set(settings, "quality/minPeriphericAreaCoverage", minPeriphericAreaCoverage );
		set(settings, "quality/maxReprojectionError", maxReprojectionError );
	}
Thiago Santini's avatar
Thiago Santini committed
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
};

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();
205
	void printAccuracyInfo(const cv::Mat &errors, const QString &which, const double &diagonal, float &mu, float &sigma);
Thiago Santini's avatar
Thiago Santini committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

    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;

221 222 223 224 225 226 227
	float centralHullCoverage;
	float peripheralHullCoverage;
	float evaluationRegionCoverage;
	float meanEvaluationError;
	float stdEvaluationError;

	std::vector<cv::Point> interpolationHull;
Thiago Santini's avatar
Thiago Santini committed
228 229 230 231
    std::vector<EvaluationRegion> evaluationRegions;
    std::vector<ErrorVector> errorVectors;
    QElapsedTimer autoVisualizationTimer;
    cv::Mat vis;
Thiago Santini's avatar
Thiago Santini committed
232 233 234
	cv::Mat overlay;
	int lastOverlayIdx;

Thiago Santini's avatar
Thiago Santini committed
235 236 237 238 239 240 241 242

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);

243
    void updateInterpolationHull(const std::vector<CollectionTuple> &tuples);
Thiago Santini's avatar
Thiago Santini committed
244 245 246 247
    void drawGazeEstimationInfo(DataTuple &dataTuple);
};

#endif // GAZEESTIMATION_H