-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.h
275 lines (201 loc) · 6.25 KB
/
tracker.h
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef TRACKER_H
#define TRACKER_H
#include <QThread>
#include <QObject>
#include <QPixmap>
#include "opencv2/opencv.hpp"
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include <opencv2/highgui/highgui_c.h>
#include "Ewma.h"
#include <opencv2/tracking.hpp>
#include <opencv2/tracking/tracking_legacy.hpp>
#include <QScreen>
#include <QApplication>
#include <QMessageBox>
#include <QCursor>
#include <QTimer>
#include <QDebug>
class PointMosse
{
public:
cv::Rect2d facialRectangle;
cv::Ptr<cv::legacy::Tracker> facialTracker;
//Ewma facialFilterX;
//Ewma facialFilterY;
bool needInit = false;
cv::Point2f pointNew;
cv::Point2f pointOld;
cv::Point2f pointDelta;
};
class Tracker : public QThread
{
Q_OBJECT
public:
explicit Tracker(QObject *parent = nullptr);
signals:
void inDisplay(QPixmap pixmap);
// void outDisplay(QPixmap pixmap);
void cursorPosition(QPoint qp);
void cursorDisplacement(float dist);
void quickClickSignal(bool detect);
void timerClickSignal(bool detect);
void newFrame();
void checkStart();
public slots:
// void tracking();
void frameTracking();
void stopTracking();
void preTrackingActions();
private:
bool stopped = false;
public:
QImage frameQImage;
QPixmap frameQPixmap;
// int facetrackerTimeout = 300;
int trackerTimeout = 80;
QTimer* faceTrackerTimer;
int faceFPScounter = 0;
int faceFPS = 3;
int cameraIndex = 0;
//FaceTracker *facetracker;
// QElapsedTimer *fpsTimer;
private:
void run() override;
public:
void initiateTracker();
void geoSmileCalculation(cv::Mat& im, std::vector<cv::Point2f>& landmarks);
void calclMousePos(cv::Point2f deltaPoint, int faceH);
bool Tracking();
int getAngleABC(cv::Point2d a, cv::Point2d b, cv::Point2d c);
cv::Point2f rotate2d(const cv::Point2f& inPoint, const double& angRad);
cv::Point2f rotatePoint(const cv::Point2f& inPoint, const cv::Point2f& center, const double& angRad);
cv::Rect calculateCrop(cv::Rect _crop, cv::Size _frame);
void OnSetMousehook();
void OnUnhookMouse();
void UnhookMouse();
void calculateMosseTrackers(cv::Mat _frame, cv::Mat _frame_gray);
bool initFacialTracker(int i, cv::Mat _frame_gray);
void addNewPoints();
void addFacialTracker(cv::Point2f _pt, cv::Rect2d _trackingRectangle);
cv::Point2i cursorPos;
cv::Point2i oldDelta;
float velocityScale;
float velocitySlider = 3;
float velocityK = 0.046;
bool openFlag = true;
bool turnOffClick = true;
bool mouseHookPause = false;
bool buttonStop = false;
//float pauseTime = 5.0;
bool faceInitiatedFlag = false;
bool loadedFrontalfaceModelFlag = false;
bool loadedFacemarkModelFlag = false;
bool loadedSmileModelFlag = false;
cv::VideoCapture cap;
cv::CascadeClassifier frontalFaceDetector;
cv::CascadeClassifier smileDetector;
CvTermCriteria termcrit;
int minFaceNeighbors = 5;
cv::Ptr<cv::face::Facemark> facemark;
bool facemarkDetectedFlag = false;
bool faceDetectedFlag = false;
bool firstFaceCropFlag = true;
cv::Mat frame, frame_gray, faceROI, equalizerROI;
// Vector of returned faces
std::vector<cv::Rect> faces, oldfaces;
std::vector<std::vector<cv::Point2f> > landmarks;
bool needToTrackerInit = true;
bool needToAddPoints = false;
std::vector<cv::Point2f> pointsT;
cv::Point2f trackPoint;
cv::Point2f trackMousePoint;
cv::Point2f smileAngle;
float smilingAnCalcCorr;
float correctedAng;
Ewma ewmaSmileFilterObj;
Ewma ewmasmilingAnCalcCorrObj;
Ewma facialFilterX;
Ewma facialFilterY;
cv::String inptext;
cv::String facetext = "Face not detected";
cv::String tempinptext;
#define POINTCOLOR cv::Scalar(0, 79, 241)
#define CORRECTIONCOLOR cv::Scalar(50, 255, 50)
#define CORRECTIONALERTCOLOR cv::Scalar(1, 4, 209)
float dxTrackPointSum = 0;
float dyTrackPointSum = 0;
float dxTrackPoint = 0;
float dyTrackPoint = 0;
float olDdDMouseX = 0;
float olDdDMouseY = 0;
cv::Rect cropToTrack;
cv::Rect cropEqualizer;
std::vector<uchar> status;
std::vector<float> err;
#define QUALITY_LEVEL 0.001
double minDistRatio = 4.0; // cels num per crop (more -> dencer)
double minDist = 10;
int minCornersCount = 7;
int maxCornersCount = 25;
#define ULTRASMALLMOVECOLOR cv::Scalar(1, 209, 4)
#define SMALLMOVECOLOR cv::Scalar(1, 209, 4)
#define ULTRASMALL_DELTA_LIMIT 8000 // ultrasmall/small movement, bigger -> more sensitive
double ultraSmallDelta;
bool allowMove;
#define PI 3.1415926535
float captureHorizontal;
float captureVertical;
float horSensitivity = 1.6;
float verSensitivity = 1.8;
float horScale = 8;
float verScale = 8;
int horLimit = 0;
int vertLimit = 0;
float deltaXpos = 0;
float deltaYpos = 0;
int mouseX = 0;
int mouseY = 0;
float mdt = 0.04;
float mds;
float mv;
int correctedDX, correctedDY;
std::vector<cv::Rect> smiles;
int noseNoseLenth = 1;
int eyeEyeLenth = 1;
float rotationHeadRatio = 1;
int smilingAngle = 60;
cv::Point corrAB, corrBC;
int smilingTriggerAngle = 100;
cv::Rect smileAIrect;
int minNeighborsSmileDetector = 60;
bool detectSmileGeoFlag = false;
bool detectSmileAIFlag = false;
bool detectDwellFlag = true;
bool smileGeoDetectedFlag = false;
bool smileAIDetectedFlag = false;
bool isQuickClick = true;
bool quickSmileUnlocked = true;
// Timer moveLockTimer;
bool skipHook = false;
bool needEqualize = false;
cv::Point2f dMouse;
cv::Point2f dMouseOld;
bool flipCameraFlag = true;
bool showVideoFlag = true;
double ewmaAlpha = 0.6;
double ewmaSmileAlpha = 0.6;
//0.1 Less smoothing - faster to detect changes, but more prone to noise
//0.01 More smoothing - less prone to noise, but slower to detect changes
float boxWidth;
float boxHeight;
float boxRatio = 2.0;
std::vector<PointMosse> pointsMosse;
int activeTrackers = 0;
bool skipframe = true;
cv::Rect2d trackingRectangle;
};
#endif // TRACKER_H