-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainthread.cpp
56 lines (50 loc) · 1.53 KB
/
trainthread.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
#include "trainthread.h"
TrainThread::TrainThread(Recognizer *recognizer, int sid, QObject *parent) : QThread(parent)
{
this->sid = sid;
this->recognizer = recognizer;
}
void TrainThread::run() {
emit changeState("正在采集照片...");
emit changeProgress(0);
while (this->sampleCollected < SAMPLES)
;
emit changeState("训练模型中...");
recognizer->trainModel(this->faces, this->labels);
emit finished();
}
void TrainThread::onProbeFrameSlot(const QVideoFrame &frame) {
if (this->sampleCollected > SAMPLES) {
return;
}
QVideoFrame cloneFrame(frame);
int w = cloneFrame.width();
int h = cloneFrame.height();
if (cloneFrame.map(QAbstractVideoBuffer::ReadOnly) == false){
qDebug()<<"map error";
return ;
}
Mat grayImg;
Mat matTemp = Mat(h, w, CV_8UC2, cloneFrame.bits(), static_cast<size_t>(cloneFrame.bytesPerLine()));
cvtColor(matTemp, matTemp, COLOR_YUV2RGB_YUYV);
cvtColor(matTemp, grayImg, COLOR_BGR2GRAY);
Mat face = this->recognizer->faceDetect(grayImg);
if (face.empty()) {
face.release();
grayImg.release();
matTemp.release();
cloneFrame.unmap();
return;
}
this->faces.push_back(face);
this->labels.push_back(this->sid);
grayImg.release();
matTemp.release();
cloneFrame.unmap();
emit changeProgress(this->sampleCollected > 100 ? 100 : this->sampleCollected);
}
TrainThread::~TrainThread() {
for (Mat face : this->faces) {
face.release();
}
}