CameraWidget.h 3.17 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3
#ifndef CAMERAWIDGET_H
#define CAMERAWIDGET_H

4 5
#include <memory>

Thiago Santini's avatar
Thiago Santini committed
6
#include <QAction>
7
#include <QCamera>
8
#include <QCameraInfo>
Thiago Santini's avatar
Thiago Santini committed
9
#include <QFont>
10
#include <QLineF>
11
#include <QMainWindow>
Thiago Santini's avatar
Thiago Santini committed
12
#include <QMessageBox>
13 14 15
#include <QMouseEvent>
#include <QPainter>
#include <QThread>
Thiago Santini's avatar
Thiago Santini committed
16

17 18
#include "ERWidget.h"

Thiago Santini's avatar
Thiago Santini committed
19
#include "opencv2/calib3d.hpp"
20
#include "opencv2/imgproc.hpp"
Thiago Santini's avatar
Thiago Santini committed
21 22

#include "Camera.h"
23
#include "CameraCalibration.h"
Thiago Santini's avatar
Thiago Santini committed
24 25
#include "EyeImageProcessor.h"
#include "FieldImageProcessor.h"
26
#include "ImageProcessor.h"
Thiago Santini's avatar
Thiago Santini committed
27 28 29 30 31 32 33

#include "DataRecorder.h"

#include "Synchronizer.h"

#include "utils.h"

Thiago Santini's avatar
Thiago Santini committed
34 35
#include "Overlay.h"

Thiago Santini's avatar
Thiago Santini committed
36 37 38 39
namespace Ui {
class CameraWidget;
}

40 41
class CameraWidget : public ERWidget, InputWidget {
    Q_OBJECT
Thiago Santini's avatar
Thiago Santini committed
42 43

public:
44 45
    CameraWidget(QString id, ImageProcessor::Type type, QWidget* parent = nullptr);
    ~CameraWidget() override;
Thiago Santini's avatar
Thiago Santini committed
46 47 48 49

signals:
    void setCamera(QCameraInfo cameraInfo);
    void newROI(QPointF sROI, QPointF eROI);
50
    void newMaxRadius(double maxRadius);
Thiago Santini's avatar
Thiago Santini committed
51 52
    void newData(EyeData data);
    void newData(FieldData data);
53
    void newClick(Timestamp, QPoint, QSize);
54
    void injectFrame(Timestamp t, const cv::Mat& frame);
Thiago Santini's avatar
Thiago Santini committed
55 56

public slots:
57
    void preview(Timestamp t, const cv::Mat& frame);
Thiago Santini's avatar
Thiago Santini committed
58 59
    void preview(EyeData data);
    void preview(FieldData data);
60
    void preview(const DataTuple& data);
Thiago Santini's avatar
Thiago Santini committed
61 62 63
    void options(QAction* action);
    void noCamera(QString msg);

64 65
    void startRecording() override;
    void stopRecording() override;
66
    void startRecordingWithoutVideo();
Thiago Santini's avatar
Thiago Santini committed
67

68 69 70
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
Thiago Santini's avatar
Thiago Santini committed
71

72
    void validatePoint(QPointF& point);
Thiago Santini's avatar
Thiago Santini committed
73

74 75 76
    void requestCameraCalibrationSample() { cameraCalibrationSampleRequested = true; }
    void sendCameraCalibrationSample(const cv::Mat& frame);
    void onCameraCalibrationFinished(bool success);
Thiago Santini's avatar
Thiago Santini committed
77

78 79
    void cameraChanged(QCameraInfo currentCamera, QCameraViewfinderSettings viewFinderSettings);

Thiago Santini's avatar
Thiago Santini committed
80 81 82
private:
    ImageProcessor::Type type;

83
    Ui::CameraWidget* ui;
Thiago Santini's avatar
Thiago Santini committed
84

85 86
    Camera* camera;
    QThread* cameraThread;
Thiago Santini's avatar
Thiago Santini committed
87

88 89
    ImageProcessor* imageProcessor;
    QThread* processorThread;
Thiago Santini's avatar
Thiago Santini committed
90

91 92
    DataRecorderThread* recorder;
    QThread* recorderThread;
Thiago Santini's avatar
Thiago Santini committed
93

94 95
    QActionGroup* optionsGroup;
    QAction* optionAction;
Thiago Santini's avatar
Thiago Santini committed
96

Thiago Santini's avatar
Thiago Santini committed
97 98
    MovingAverage<Timestamp> dtEstimator;
    Timestamp lastTimestamp;
99
    Timestamp lastFrameRateUpdate;
Thiago Santini's avatar
Thiago Santini committed
100 101 102
    void updateFrameRate(Timestamp t);

    QPointF sROI, eROI;
103 104 105 106 107 108 109
    void setROI(const QPointF& s, const QPointF& e)
    {
        sROI = s;
        eROI = e;
        emit newROI(sROI, eROI);
    }

110 111 112
    QLineF userMaxPupilRadius;
    double userMaxPupilRatio;

113
    Timestamp lastUpdate;
Thiago Santini's avatar
Thiago Santini committed
114 115 116 117 118
    Timestamp updateIntervalMs;
    Timestamp maxAgeMs;
    bool shouldUpdate(Timestamp t);
    bool isDataRecent(Timestamp t);

119
    QImage previewImage(const cv::Mat& frame);
Thiago Santini's avatar
Thiago Santini committed
120

121 122
    std::shared_ptr<CameraCalibration> cameraCalibration;
    bool cameraCalibrationSampleRequested;
Thiago Santini's avatar
Thiago Santini committed
123

124 125
    QString intrinsicsFileName;

126 127
    QSize frameSize = { 0, 0 };
    void updateWidgetSize(const int& width, const int& height);
Thiago Santini's avatar
Thiago Santini committed
128

129 130 131 132
    // Drawing functionality
    cv::Mat rgb, resized;
    EyeOverlay eyeOverlay;
    FieldOverlay fieldOverlay;
Thiago Santini's avatar
Thiago Santini committed
133 134 135
};

#endif // CAMERAWIDGET_H