PupiTtrackingMethod.cpp 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "PupilTrackingMethod.h"

#include <QtConcurrent/QtConcurrent>
#include <QFuture>

using namespace std;
using namespace cv;

void PupilTrackingMethod::reset()
{
	previousPupils.clear();
	previousPupil = TrackedPupil();
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
	pupilDiameterKf.statePost.ptr<float>(0)[0] = 0.5*expectedFrameSize.width;
}

void PupilTrackingMethod::registerPupil( const Timestamp &ts, Pupil &pupil ) {
	Mat measurement = ( Mat_<float>(1,1) << pupil.majorAxis() );
	//if (predictedMaxPupilDiameter > 0) {
	//	float &majorAxis = measurement.ptr<float>(0)[0];
	//	if ( majorAxis > predictedMaxPupilDiameter) {
	//		pupil.clear();
	//		return;
	//	}
	//}

	if (pupil.confidence > minDetectionConfidence) {
		previousPupil = TrackedPupil(ts, pupil);
		previousPupils.emplace_back( previousPupil );
		pupilDiameterKf.correct(measurement);
	} else
		previousPupil = TrackedPupil();

	//if (pupil.confidence > minDetectionConfidence) {
	//	previousPupil = TrackedPupil(ts, pupil);
	//	previousPupils.push_back( previousPupil );
	//} else
	//	previousPupil = TrackedPupil();
38 39
}

40 41 42 43 44 45
void PupilTrackingMethod::predictMaxPupilDiameter() {
	predictedMaxPupilDiameter = 1.5*pupilDiameterKf.predict().ptr<float>(0)[0];
	if (previousPupils.size() < 20)
		predictedMaxPupilDiameter = -1;
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
void PupilTrackingMethod::run(const Timestamp &ts, const cv::Mat &frame, const cv::Rect &roi, Pupil &pupil, PupilDetectionMethod &pupilDetectionMethod)
{
	cv::Size frameSize = { frame.cols, frame.rows };
	if (expectedFrameSize != frameSize ) {
		// Reference frame changed. Let's start over!
		expectedFrameSize = frameSize;
		reset();
	}

	// Remove old samples
	while (!previousPupils.empty()) {
		if (ts - previousPupils.front().ts > maxAge)
			previousPupils.pop_front();
		else
			break;
	}

63 64 65 66
	pupil.clear();
	predictMaxPupilDiameter();

	if ( previousPupil.confidence == NO_CONFIDENCE ) {
67
		pupil = pupilDetectionMethod.runWithConfidence(frame, roi, -1, -1);
68 69 70 71 72 73
	} else {
		run(frame, roi, previousPupil, pupil);
	}

	registerPupil(ts, pupil);
	return;
74
}