PupilTrackingMethod.h 2.27 KB
Newer Older
1 2 3
#ifndef PUPILTRACKINGMETHOD_H
#define PUPILTRACKINGMETHOD_H

4 5
#include <QFuture>
#include <deque>
6
#include <memory>
7 8
#include <string>

9 10
#include "opencv2/core.hpp"

11 12 13 14
#include "pupil-detection/PupilDetectionMethod.h"

#include "utils.h"

15
class TrackedPupil : public Pupil {
16
public:
17 18 19 20 21 22 23
    TrackedPupil(const Timestamp& ts, const Pupil& pupil)
        : Pupil(pupil)
        , ts(ts)
    {
    }

    TrackedPupil()
24
        : TrackedPupil(0, Pupil())
25 26 27 28
    {
    }

    Timestamp ts;
29 30
};

31
class PupilTrackingMethod {
32
public:
33
    virtual ~PupilTrackingMethod() = default;
34

35 36
    // Tracking and detection logic
    virtual void detectAndTrack(const Timestamp& ts, const cv::Mat& frame, const cv::Rect& roi, Pupil& pupil, std::shared_ptr<PupilDetectionMethod> pupilDetectionMethod = nullptr, const float& minPupilDiameterPx = -1, const float& maxPupilDiameterPx = -1);
37

38
    virtual std::string description() = 0;
39 40

protected:
41 42 43
    cv::Size expectedFrameSize = { 0, 0 };
    TrackedPupil previousPupil;
    std::deque<TrackedPupil> previousPupils;
44

45
    Timestamp maxAge = 300;
46
    float minDetectionConfidence = 0.7f;
47

48
    void registerPupil(const Timestamp& ts, Pupil& pupil);
49

50
    void reset();
51 52

private:
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    // Detection implementation (can be overridden by providing a valid pointer to the detectAndTrack method, allowing the user to mix detectors and trackers)
    virtual Pupil detect(const cv::Mat& frame, const cv::Rect& roi, const float& minPupilDiameterPx = -1, const float& maxPupilDiameterPx = -1)
    {
        (void)frame;
        (void)roi;
        (void)minPupilDiameterPx;
        (void)maxPupilDiameterPx;
        qWarning() << Q_FUNC_INFO << "should never be called. Either implement it in the derived class or pass a valid pointer to the detectAndTrack method.";
        return Pupil();
    }

    // Tracking implementation
    virtual void track(const cv::Mat& frame, const cv::Rect& roi, const Pupil& previousPupil, Pupil& pupil, const float& minPupilDiameterPx = -1, const float& maxPupilDiameterPx = -1) = 0;

67 68
    Pupil invokeDetection(const cv::Mat& frame, const cv::Rect& roi, std::shared_ptr<PupilDetectionMethod> pupilDetectionMethod, const float& minPupilDiameterPx, const float& maxPupilDiameterPx);
    cv::Rect estimateTemporalROI(const Timestamp& ts, const cv::Rect& roi);
69 70 71
};

#endif // PUPILTRACKINGMETHOD_H