-
Notifications
You must be signed in to change notification settings - Fork 9
/
capture_thread.cpp
247 lines (212 loc) · 8.33 KB
/
capture_thread.cpp
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
#include <QApplication>
#include <QImage>
#include <QTime>
#include <QDebug>
#include "utilities.h"
#include "capture_thread.h"
CaptureThread::CaptureThread(int camera, QMutex *lock):
running(false), cameraID(camera), videoPath(""), data_lock(lock)
{
frame_width = frame_height = 0;
taking_photo = false;
loadOrnaments();
masks_flag = 0;
}
CaptureThread::CaptureThread(QString videoPath, QMutex *lock):
running(false), cameraID(-1), videoPath(videoPath), data_lock(lock)
{
frame_width = frame_height = 0;
taking_photo = false;
loadOrnaments();
masks_flag = 0;
}
CaptureThread::~CaptureThread() {
}
void CaptureThread::run() {
running = true;
cv::VideoCapture cap(cameraID);
// cv::VideoCapture cap("/home/kdr2/Videos/WIN_20190123_20_14_56_Pro.mp4");
cv::Mat tmp_frame;
frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
//classifier_data必须是const cv::String
const cv::String classifier_data = "haarcascades/haarcascade_frontalface_default.xml";
classifier = new cv::CascadeClassifier(classifier_data);
mark_detector = cv::face::createFacemarkLBF();
QString model_data = QApplication::instance()->applicationDirPath() + "/data/lbfmodel.yaml";
qDebug()<<"model_data"<<model_data;
mark_detector->loadModel(model_data.toStdString());
while(running) {
cap >> tmp_frame;
if (tmp_frame.empty()) {
break;
}
if(masks_flag > 0)
detectFaces(tmp_frame);
if(taking_photo) {
takePhoto(tmp_frame);
#ifdef USERDEBUG
{
if(!tmp_frame.data) //判断是否有数据
{
qDebug()<<"run()--->tmp_frame null!";
}
qDebug()<<"run()--->tmp_frame exists data!";
}
#endif
}
cvtColor(tmp_frame, tmp_frame, cv::COLOR_BGR2RGB);
data_lock->lock();
frame = tmp_frame;
data_lock->unlock();
emit frameCaptured(&frame);
}
cap.release();
delete classifier;
classifier = nullptr;
running = false;
}
void CaptureThread::takePhoto(cv::Mat &frame)
{
QString photo_name = Utilities::newPhotoName();
QString photo_path = Utilities::getPhotoPath(photo_name, "jpg");
bool writeResult = cv::imwrite(photo_path.toStdString(), frame);
// bool writeResult = cv::imwrite("a:a.jpg", frame);//冒号是错误格式
#ifdef USERDEBUG
{
if(writeResult)
{
qDebug()<<"takePhoto--->writeResult === cv::imwrite success!";
}
if(!frame.data) //判断是否有数据
{
qDebug()<<"takePhoto--->frame null!";
}
qDebug()<<"takePhoto--->frame exists data!";
qDebug()<<"takePhoto--->photo_path.toStdString()"<< photo_path;
}
#endif
emit photoTaken(photo_name);
taking_photo = false;
}
void CaptureThread::detectFaces(cv::Mat &frame)
{
vector<cv::Rect> faces;
cv::Mat gray_frame;
cv::cvtColor(frame, gray_frame, cv::COLOR_BGR2GRAY);
classifier->detectMultiScale(gray_frame, faces, 1.3, 5);
cv::Scalar color = cv::Scalar(0, 0, 255); // red
// draw the circumscribe rectangles
if (isMaskOn(RECTANGLE)) {
for(size_t i = 0; i < faces.size(); i++) {
cv::rectangle(frame, faces[i], color, 1);
}
}
vector< vector<cv::Point2f> > shapes;
if (mark_detector->fit(frame, faces, shapes)) {
// draw facial land marks
for (unsigned long i=0; i<faces.size(); i++) {
if (isMaskOn(LANDMARKS)) {
for(unsigned long k=0; k<shapes[i].size(); k++) {
cv::circle(frame, shapes[i][k], 2, color, cv::FILLED);
QString index = QString("%1").arg(k);
cv::putText(frame, index.toStdString(), shapes[i][k], cv::FONT_HERSHEY_SIMPLEX, 0.4, color, 2);
}
}
if (isMaskOn(GLASSES))
drawGlasses(frame, shapes[i]);
if (isMaskOn(MUSTACHE))
drawMustache(frame, shapes[i]);
if (isMaskOn(MOUSE_NOSE))
drawMouseNose(frame, shapes[i]);
}
}
}
void CaptureThread::loadOrnaments()
{
QImage image;
image.load(":/images/glasses.jpg");
image = image.convertToFormat(QImage::Format_RGB888);
glasses = cv::Mat(
image.height(), image.width(), CV_8UC3,
image.bits(), image.bytesPerLine()).clone();
image.load(":/images/mustache.jpg");
image = image.convertToFormat(QImage::Format_RGB888);
mustache = cv::Mat(
image.height(), image.width(), CV_8UC3,
image.bits(), image.bytesPerLine()).clone();
image.load(":/images/mouse-nose.jpg");
image = image.convertToFormat(QImage::Format_RGB888);
mouse_nose = cv::Mat(
image.height(), image.width(), CV_8UC3,
image.bits(), image.bytesPerLine()).clone();
}
void CaptureThread::drawGlasses(cv::Mat &frame, vector<cv::Point2f> &marks)
{
// resize
cv::Mat ornament;
double distance = cv::norm(marks[45] - marks[36]) * 1.5;
cv::resize(glasses, ornament, cv::Size(0, 0), distance / glasses.cols, distance / glasses.cols, cv::INTER_NEAREST);
// rotate
double angle = -atan((marks[45].y - marks[36].y) / (marks[45].x - marks[36].x));
cv::Point2f center = cv::Point(ornament.cols/2, ornament.rows/2);
cv::Mat rotateMatrix = cv::getRotationMatrix2D(center, angle * 180 / 3.14, 1.0);
cv::Mat rotated;
cv::warpAffine(
ornament, rotated, rotateMatrix, ornament.size(),
cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(255, 255, 255));
// paint
center = cv::Point((marks[45].x + marks[36].x) / 2, (marks[45].y + marks[36].y) / 2);
// cv::Rect rec(center.x - rotated.cols / 2, center.y - rotated.rows / 2, rotated.cols, rotated.rows);
cv::Rect rec((center.x - rotated.cols / 2)>0?(center.x - rotated.cols / 2):0,
(center.y - rotated.rows / 2)>0?(center.y - rotated.rows / 2):0,
rotated.cols>0?rotated.cols:0,
rotated.rows>0?rotated.rows:0);
frame(rec) &= rotated;
}
void CaptureThread::drawMustache(cv::Mat &frame, vector<cv::Point2f> &marks)
{
// resize
cv::Mat ornament;
double distance = cv::norm(marks[54] - marks[48]) * 1.5;
cv::resize(mustache, ornament, cv::Size(0, 0), distance / mustache.cols, distance / mustache.cols, cv::INTER_NEAREST);
// rotate
double angle = -atan((marks[54].y - marks[48].y) / (marks[54].x - marks[48].x));
cv::Point2f center = cv::Point(ornament.cols/2, ornament.rows/2);
cv::Mat rotateMatrix = cv::getRotationMatrix2D(center, angle * 180 / 3.14, 1.0);
cv::Mat rotated;
cv::warpAffine(
ornament, rotated, rotateMatrix, ornament.size(),
cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(255, 255, 255));
// paint
center = cv::Point((marks[33].x + marks[51].x) / 2, (marks[33].y + marks[51].y) / 2);
// cv::Rect rec(center.x - rotated.cols / 2, center.y - rotated.rows / 2, rotated.cols, rotated.rows);
cv::Rect rec((center.x - rotated.cols / 2)>0?(center.x - rotated.cols / 2):0,
(center.y - rotated.rows / 2)>0?(center.y - rotated.rows / 2):0,
rotated.cols>0?rotated.cols:0,
rotated.rows>0?rotated.rows:0);
frame(rec) &= rotated;
}
void CaptureThread::drawMouseNose(cv::Mat &frame, vector<cv::Point2f> &marks)
{
// resize
cv::Mat ornament;
double distance = cv::norm(marks[13] - marks[3]);
cv::resize(mouse_nose, ornament, cv::Size(0, 0), distance / mouse_nose.cols, distance / mouse_nose.cols, cv::INTER_NEAREST);
// rotate
double angle = -atan((marks[16].y - marks[0].y) / (marks[16].x - marks[0].x));
cv::Point2f center = cv::Point(ornament.cols/2, ornament.rows/2);
cv::Mat rotateMatrix = cv::getRotationMatrix2D(center, angle * 180 / 3.14, 1.0);
cv::Mat rotated;
cv::warpAffine(
ornament, rotated, rotateMatrix, ornament.size(),
cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(255, 255, 255));
// paint
center = marks[30];
// cv::Rect rec(center.x - rotated.cols, center.y - rotated.rows / 2, rotated.cols, rotated.rows);
cv::Rect rec((center.x - rotated.cols / 2)>0?(center.x - rotated.cols / 2):0,
(center.y - rotated.rows / 2)>0?(center.y - rotated.rows / 2):0,
rotated.cols>0?rotated.cols:0,
rotated.rows>0?rotated.rows:0);
frame(rec) &= rotated;
}