forked from linuxdeepin/deepin-face
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivermanger.cpp
406 lines (360 loc) · 12.1 KB
/
drivermanger.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "drivermanger.h"
#include "modelmanger.h"
#include <sys/socket.h>
#include <sys/types.h>
DriverManger::DriverManger()
: m_spCharaDataManger(new CharaDataManger)
, m_eroll(new QThread(this))
, m_verify(new QThread(this))
{
}
DriverManger::~DriverManger()
{
m_eroll->quit();
m_eroll->wait(1000);
m_verify->quit();
m_verify->wait(1000);
}
void DriverManger::init()
{
m_spFileWatch = QSharedPointer<QFileSystemWatcher>(new QFileSystemWatcher(this));
m_spErollthread = QSharedPointer<ErollThread>(new ErollThread());
m_spVerifyThread = QSharedPointer<VerifyThread>(new VerifyThread());
m_spCharaDataManger->loadCharaData();
m_bClaim = false;
m_charalist = this->m_spCharaDataManger->getCharaList();
m_charaType = FACECHARATYPE;
m_spFileWatch->addPath("/dev/");
// 暂不支持热插拔
connect(m_spFileWatch.get(),
&QFileSystemWatcher::directoryChanged,
this,
&DriverManger::onDirectoryChanged);
onDirectoryChanged("/dev/");
m_spErollthread->moveToThread(m_eroll);
m_spVerifyThread->moveToThread(m_verify);
connect(m_spErollthread.data(), &ErollThread::processStatus, this, &DriverManger::processStatus);
connect(m_spVerifyThread.data(), &VerifyThread::processStatus, this, &DriverManger::processStatus);
m_eroll->start();
m_verify->start();
qDebug() << "DriverManger::init thread:" << QThread::currentThreadId();
}
QDBusUnixFileDescriptor DriverManger::enrollStart(QString chara,
qint32 charaType,
QString actionId,
ErrMsgInfo &errMsgInfo)
{
if (m_bClaim) {
errMsgInfo.errType = QDBusError::Failed;
errMsgInfo.msg = "face claim";
return QDBusUnixFileDescriptor(-1);
}
if (this->m_charaType != charaType) {
errMsgInfo.errType = QDBusError::InvalidArgs;
errMsgInfo.msg = "charaType not match";
return QDBusUnixFileDescriptor(-1);
}
auto iter = std::find(m_charalist.begin(), m_charalist.end(), chara);
if (iter != m_charalist.end()) {
errMsgInfo.errType = QDBusError::Failed;
errMsgInfo.msg = "face had same chara";
return QDBusUnixFileDescriptor(-1);
}
int fd[2];
int r = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fd);
if (r < 0) {
errMsgInfo.errType = QDBusError::Failed;
errMsgInfo.msg = "create socketpair err";
return QDBusUnixFileDescriptor(-1);
}
ModelManger::getSingleInstanceModel().load();
m_bClaim = true;
ActionInfo info;
info.chara = chara;
info.actionType = Enroll;
m_actionMap[actionId] = info;
QMetaObject::invokeMethod(m_spErollthread.data(),
"Start",
Qt::QueuedConnection,
QGenericReturnArgument(),
Q_ARG(QString, actionId),
Q_ARG(int, fd[1]));
return QDBusUnixFileDescriptor(fd[0]);
}
void DriverManger::enrollStop(QString actionId, ErrMsgInfo &errMsgInfo)
{
if (m_actionMap.count(actionId) != 1) {
errMsgInfo.errType = QDBusError::InvalidArgs;
errMsgInfo.msg = "not found action";
return;
}
m_bClaim = false;
auto actionInfo = m_actionMap[actionId];
if (actionInfo.status == FaceEnrollSuccess) {
m_spCharaDataManger->insertCharaDate(actionInfo.chara,
actionInfo.faceChara,
actionInfo.size);
if (actionInfo.faceChara != nullptr) {
free(actionInfo.faceChara);
}
}
ModelManger::getSingleInstanceModel().unLoad();
QMetaObject::invokeMethod(m_spErollthread.data(),
"Stop",
Qt::BlockingQueuedConnection);
m_actionMap.remove(actionId);
auto charaList = m_spCharaDataManger->getCharaList();
setCharaList(charaList);
qDebug() << "EnrollStop finish";
}
QDBusUnixFileDescriptor DriverManger::verifyStart(QStringList charas,
QString actionId,
ErrMsgInfo &errMsgInfo)
{
if (m_bClaim) {
errMsgInfo.errType = QDBusError::Failed;
errMsgInfo.msg = "face claim";
return QDBusUnixFileDescriptor(-1);
}
QVector<float*> faceCharas = m_spCharaDataManger->getCharaData(charas);
m_bClaim = true;
ActionInfo info;
info.actionType = Verify;
m_actionMap[actionId] = info;
qDebug() << "claim=true;";
ModelManger::getSingleInstanceModel().load();
qRegisterMetaType<QVector<float*>>("QVector<float*>");
QMetaObject::invokeMethod(m_spVerifyThread.data(),
"Start",
Qt::QueuedConnection,
QGenericReturnArgument(),
Q_ARG(QString, actionId),
Q_ARG(QVector<float*>, faceCharas));
return QDBusUnixFileDescriptor(0);
}
void DriverManger::verifyStop(QString actionId, ErrMsgInfo &errMsgInfo)
{
qDebug() << __LINE__ << "DriverManger::verifyStop";
if (m_actionMap.count(actionId) != 1) {
errMsgInfo.errType = QDBusError::InvalidArgs;
errMsgInfo.msg = "not found action";
return;
}
m_bClaim = false;
// 线程结束后才释放资源
QMetaObject::invokeMethod(m_spVerifyThread.data(),
"Stop",
Qt::BlockingQueuedConnection);
ModelManger::getSingleInstanceModel().unLoad();
m_actionMap.remove(actionId);
}
void DriverManger::deleteChara(QString chara, ErrMsgInfo &errMsgInfo)
{
bool bSuccess = m_spCharaDataManger->deleteCharaData(chara);
if (!bSuccess) {
errMsgInfo.errType = QDBusError::Failed;
errMsgInfo.msg = "delete error";
return;
}
auto charaList = m_spCharaDataManger->getCharaList();
setCharaList(charaList);
}
bool DriverManger::getClaim()
{
return this->m_bClaim;
}
void DriverManger::setClaim(bool bClaim)
{
this->m_bClaim = bClaim;
}
qint32 DriverManger::getCharaType()
{
return this->m_charaType;
}
void DriverManger::setCharaType(qint32 qCharaType)
{
if (this->m_charaType != qCharaType) {
qDebug() << "set chara Type" << qCharaType;
this->m_charaType = qCharaType;
QVariantMap qChangedProps;
qChangedProps.insert("CharaType", this->m_charaType);
emitPropertiesChanged(qChangedProps);
}
}
QStringList DriverManger::getCharaList()
{
return this->m_charalist;
}
void DriverManger::setCharaList(QStringList lCharaList)
{
if (this->m_charalist.size() != lCharaList.size()) {
this->m_charalist = lCharaList;
QVariantMap qChangedProps;
qChangedProps.insert("List", this->m_charalist);
emitPropertiesChanged(qChangedProps);
}
}
void DriverManger::processStatus(QString actionId, qint32 status, float *faceChara, int size)
{
if (m_actionMap.count(actionId) != 1) {
return;
}
auto &actionInfo = m_actionMap[actionId];
actionInfo.status = status;
if (faceChara != nullptr) {
actionInfo.faceChara = static_cast<float *>(
malloc(sizeof(float) * static_cast<unsigned long>(size)));
for (int i = 0; i < size; i++) {
actionInfo.faceChara[i] = faceChara[i];
}
actionInfo.size = size;
}
QString msg = getStatusMsg(actionInfo.actionType, status);
emitStatus(actionInfo.actionType, actionId, status, msg);
}
void DriverManger::emitStatus(ActionType action, QString actionId, qint32 status, QString msg)
{
qDebug() << "emitStatus action" << actionId << "status" << status;
QString emitSignal;
if (action == Enroll) {
emitSignal = "EnrollStatus";
} else {
emitSignal = "VerifyStatus";
}
QDBusMessage signal = QDBusMessage::createSignal(SERVERPATH, SERVERINTERFACE, emitSignal);
signal << actionId;
signal << status;
signal << msg;
QDBusConnection::systemBus().send(signal);
}
void DriverManger::emitPropertiesChanged(QVariantMap &qChangedProps)
{
qDebug() << "emitPropertiesChanged";
QDBusMessage signal = QDBusMessage::createSignal(SERVERPATH,
"org.freedesktop.DBus.Properties",
"PropertiesChanged");
signal << SERVERINTERFACE;
signal << qChangedProps;
QStringList invalidatedProps;
signal << invalidatedProps;
QDBusConnection::systemBus().send(signal);
}
void DriverManger::onDirectoryChanged(const QString &path)
{
qDebug() << "HandleFileChanged" << path;
QDir dir(path);
if (!dir.exists()) {
qDebug() << "dir " << path << "not exist";
return;
}
//获取filePath下所有系统文件
dir.setFilter(QDir::System);
QFileInfoList filelist = dir.entryInfoList();
bool bFound = false;
for (int i = 0; i < filelist.size(); i++) {
if (filelist[i].fileName().contains("video")) {
bFound = true;
break;
}
}
if (bFound) {
setCharaType(FACECHARATYPE);
} else {
setCharaType(EMPTYCHARATYPE);
}
}
QString DriverManger::getStatusMsg(ActionType actionType, qint32 status)
{
QString retMsg;
if (actionType == Enroll) {
EnrollStatus tempStatus = EnrollStatus(status);
switch (tempStatus) {
case FaceEnrollSuccess:
retMsg = "Enroll Success";
break;
case FaceEnrollNotRealHuman:
retMsg = "Enroll Not Real Human";
break;
case FaceEnrollFaceNotCenter:
retMsg = "Enroll Not Center";
break;
case FaceEnrollFaceTooBig:
retMsg = "Enroll Too Big";
break;
case FaceEnrollFaceTooSmall:
retMsg = "Enroll Too Small";
break;
case FaceEnrollNoFace:
retMsg = "Enroll No Face";
break;
case FaceEnrollTooManyFace:
retMsg = "Enroll Too Many Face";
break;
case FaceEnrollFaceNotClear:
retMsg = "Enroll Not Clear";
break;
case FaceEnrollBrightness:
retMsg = "Enroll Brightness";
break;
case FaceEnrollFaceCovered:
retMsg = "Enroll Covered";
break;
case FaceEnrollCancel:
retMsg = "Enroll Cancel";
break;
case FaceEnrollError:
retMsg = "Enroll Error";
break;
case FaceEnrollException:
retMsg = "Enroll Exception";
break;
}
} else if (actionType == Verify) {
VerifyStatus tempStatus = VerifyStatus(status);
switch (tempStatus) {
case FaceVerifySuccess:
retMsg = "Verify Success";
break;
case FaceVerifyNotRealHuman:
retMsg = "Verify Not Real Human";
break;
case FaceVerifyFaceNotCenter:
retMsg = "Verify Not Center";
break;
case FaceVerifyFaceTooBig:
retMsg = "Verify Too Big";
break;
case FaceVerifyFaceTooSmall:
retMsg = "Verify Too Small";
break;
case FaceVerifyNoFace:
retMsg = "Verify No Face";
break;
case FaceVerifyTooManyFace:
retMsg = "Verify Too Many Face";
break;
case FaceVerifyFaceNotClear:
retMsg = "Verify Not Clear";
break;
case FaceVerifyBrightness:
retMsg = "Verify Brightness";
break;
case FaceVerifyFaceCovered:
retMsg = "Verify Covered";
break;
case FaceVerifyCancel:
retMsg = "Verify Cancel";
break;
case FaceVerifyError:
retMsg = "Verify Error";
break;
case FaceVerifyException:
retMsg = "Verify Exception";
break;
}
}
return retMsg;
}