faceLiveness.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. #include "faceLiveness.h"
  2. #include "ui_faceLiveness.h"
  3. #include "CAppInfo.h"
  4. #include <QDesktopWidget>
  5. #include <QFile>
  6. #include <QFileInfo>
  7. #include "awMsgBox.h"
  8. #include "logproc.h"
  9. #include "CCommonTools.h"
  10. #include "CFaceRecProc.h"
  11. faceLiveness::faceLiveness(FACE_LIVENESS_TYPE livenessType, QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::faceLiveness), m_livenessType(livenessType)
  14. {
  15. ui->setupUi(this);
  16. setStyleSheet(g_appInfoPtr->m_sQssStr);
  17. initUI();
  18. qRegisterMetaType<CBaseResponsePackage>("CBaseResponsePackage");
  19. qRegisterMetaType<CProcessUpload>("CProcessUpload");
  20. connect(g_httpBllPtr.get(), &CHttpBll::sgnProcessUpload, this, &faceLiveness::onProcessUpload);
  21. connect(g_httpBllPtr.get(), &CHttpBll::sgnSaveFaceLiveVerifyResult, this, &faceLiveness::onSaveFaceLiveVerifyResult);
  22. connect(g_httpBllPtr.get(), &CHttpBll::sgnSaveFaceCompareResult, this, &faceLiveness::onSaveFaceCompareResult);
  23. if(m_livenessType == FACE_LIVENESS_TYPE::flt_inprogress)
  24. {
  25. ui->label_fl_time->setVisible(true);
  26. }
  27. else
  28. {
  29. ui->label_fl_time->setVisible(false);
  30. }
  31. m_nMaxSeconds = g_appInfoPtr->m_oExamInfo.nAllActionDuration;
  32. QString sFileName = g_appInfoPtr->m_sStudentPhotoPath.right(g_appInfoPtr->m_sStudentPhotoPath.length() - g_appInfoPtr->m_sStudentPhotoPath.lastIndexOf("/") - 1);
  33. sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  34. if(!QFile::exists(sFileName))
  35. {
  36. CHttpRequestPackage hrp;
  37. hrp.sUri = g_appInfoPtr->m_sStudentPhotoPath;
  38. hrp.sCommonStr = sFileName;
  39. hrp.nRequestType = RequestType::rtDownLoadFile;
  40. hrp.nRetryCount = 3;
  41. g_httpBllPtr->downLoad(hrp);
  42. }
  43. m_pVideoTimer = std::make_shared<QTimer>();
  44. m_pVideoTimer->setInterval(200);
  45. connect(m_pVideoTimer.get(), &QTimer::timeout, this, [&](){
  46. cv::Mat frame;
  47. {
  48. std::scoped_lock sl(m_imageMutex);
  49. frame = m_nCurImage;
  50. }
  51. QImage img = CCommonTools::Mat2QImage(frame);
  52. ui->widget_fc_camera->setAutoFillBackground(true);
  53. QPalette palette;
  54. palette.setBrush(QPalette::Window, QBrush(img.scaled(ui->widget_fc_camera->width(), ui->widget_fc_camera->height())));
  55. ui->widget_fc_camera->setPalette(palette);
  56. if(m_bStartCompare)
  57. {
  58. std::scoped_lock sl(m_imgMutex);
  59. m_imgList.push_back(frame);
  60. }
  61. });
  62. g_clientVideoProcPtr->startTest(this);
  63. m_pVideoTimer->start();
  64. //生成动作列表
  65. QStringList sActionList = g_appInfoPtr->m_oExamInfo.sActionOptions.split(",");
  66. if(sActionList.count() != g_appInfoPtr->m_oExamInfo.nActionNum)
  67. {
  68. ShowMsg(QString::fromLocal8Bit("初始化活体动作失败"), this, MSG_ICON_TYPE::mit_error);
  69. emit faceLivenessFaild();
  70. return;
  71. }
  72. LivenessVerifyInfo lvif;
  73. lvif.sActionType = ACTION_TYPE::AT_FACE_DETECT;
  74. lvif.nActionLeftSceonds = m_nFaceDetectDuration;
  75. m_livenessList.push_back(lvif);
  76. m_currentVerifyInfo = lvif;
  77. if(g_appInfoPtr->m_oExamInfo.sActionOrder == "FIXED")
  78. {
  79. for(QString sAction : sActionList)
  80. {
  81. LivenessVerifyInfo lvi;
  82. lvi.sActionType = sAction;
  83. lvi.nActionLeftSceonds = g_appInfoPtr->m_oExamInfo.nActionDuration;
  84. m_livenessList.push_back(lvi);
  85. }
  86. }
  87. else
  88. {
  89. QList<int> list;
  90. CCommonTools::genRandomNumber(list, g_appInfoPtr->m_oExamInfo.nActionNum);
  91. for(int i = 0; i < g_appInfoPtr->m_oExamInfo.nActionNum; i++)
  92. {
  93. LivenessVerifyInfo lvi;
  94. lvi.sActionType = sActionList[list[i]-1];
  95. lvi.nActionLeftSceonds = g_appInfoPtr->m_oExamInfo.nActionDuration;
  96. m_livenessList.push_back(lvi);
  97. }
  98. }
  99. m_nCurIndex = 0;
  100. m_ActionTimer = std::make_shared<QTimer>();
  101. m_ActionTimer->setInterval(1000);
  102. connect(m_ActionTimer.get(), &QTimer::timeout, this, &faceLiveness::actionTimer);
  103. m_countdownTimer = std::make_shared<QTimer>();
  104. m_countdownTimer->setInterval(1000);
  105. connect(m_countdownTimer.get(), &QTimer::timeout, this, &faceLiveness::countdownTimer);
  106. m_countdownTimer->start();
  107. m_bIsRun = true;
  108. m_thread = std::thread(std::bind(&faceLiveness::threadProc, this));
  109. m_initTimer = std::make_shared<QTimer>();
  110. m_initTimer->setInterval(100);
  111. connect(m_initTimer.get(), &QTimer::timeout, this, [&](){
  112. m_initTimer->stop();
  113. if(g_faceRecProcPtr == nullptr)
  114. {
  115. g_faceRecProcPtr = std::make_shared<CFaceRecProc>();
  116. if (!g_appInfoPtr->m_sStudentPhotoPath.isEmpty())
  117. {
  118. if(!g_faceRecProcPtr->hasBaseImage())
  119. {
  120. if(!setBaseImage())
  121. {
  122. return;
  123. }
  124. }
  125. }
  126. else
  127. {
  128. ShowMsg(QString::fromLocal8Bit("当前考试未底照"), this, MSG_ICON_TYPE::mit_error);
  129. return;
  130. }
  131. }
  132. if(!g_faceRecProcPtr->hasBaseImage())
  133. {
  134. if(!setBaseImage())
  135. {
  136. return;
  137. }
  138. }
  139. m_bStartCompare = true;
  140. m_fMaxYaw = 0;
  141. m_fMinYaw = 0;
  142. m_sLivenessStatus = STATUS_TYPE::ST_SUCCESS;
  143. {
  144. std::scoped_lock sl(m_livenessListMutex);
  145. m_currentVerifyInfo = m_livenessList[m_nCurIndex];
  146. }
  147. initAcionIcon();
  148. m_ActionTimer->start();
  149. });
  150. m_initTimer->start();
  151. }
  152. faceLiveness::~faceLiveness()
  153. {
  154. m_pVideoTimer->stop();
  155. m_ActionTimer->stop();
  156. g_clientVideoProcPtr->stopTest();
  157. m_bIsRun = false;
  158. m_thread.join();
  159. awMsgBox::clear(this);
  160. delete ui;
  161. }
  162. void faceLiveness::onRenderVideoFrame(const char* userId, TRTCVideoStreamType streamType, TRTCVideoFrame* frame)
  163. {
  164. if(g_clientVideoProcPtr->isCameraTest())
  165. {
  166. __int64 nServerTime = g_appInfoPtr->serverMTime();
  167. if(nServerTime - m_lastFaceTime >= 100)
  168. {
  169. m_lastFaceTime = nServerTime;
  170. cv::Mat matImg;
  171. cv::cvtColor(cv::Mat(frame->height, frame->width, CV_8UC4, frame->data), matImg, CV_RGBA2RGB);
  172. if(matImg.empty())
  173. {
  174. qDebug()<<"widgetCameraTest frame is empty";
  175. return;
  176. }
  177. std::scoped_lock sl(m_imageMutex);
  178. m_nCurImage = matImg;
  179. }
  180. }
  181. }
  182. void faceLiveness::initUI()
  183. {
  184. QDesktopWidget *dekwiget = QApplication::desktop();
  185. setGeometry(0, 0, dekwiget->width(), dekwiget->height());
  186. ui->widget_mask->setGeometry(0, 0, dekwiget->width(), dekwiget->height());
  187. ui->widget_fc_BG->setGeometry((width() - g_appInfoPtr->m_fRate*800)/2, (height() - g_appInfoPtr->m_fRate*536)/2,
  188. g_appInfoPtr->m_fRate*800, g_appInfoPtr->m_fRate*536);
  189. ui->label_fl_title->adjustSize();
  190. ui->label_fl_title->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*20,
  191. ui->label_fl_title->width(), ui->label_fl_title->height());
  192. ui->label_fl_time->setGeometry(ui->label_fl_title->x() + ui->label_fl_title->width(), ui->label_fl_title->y(),
  193. g_appInfoPtr->m_fRate*200, ui->label_fl_title->height());
  194. ui->btn_fc_close->setGeometry(ui->widget_fc_BG->width() - g_appInfoPtr->m_fRate*(20 + 16),
  195. g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
  196. ui->btn_fc_close->setVisible(false);
  197. ui->widget_fc_camera->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*75,
  198. g_appInfoPtr->m_fRate*740, g_appInfoPtr->m_fRate*320);
  199. ui->btn_fl_startVerify->setGeometry((ui->widget_fc_camera->width() - g_appInfoPtr->m_fRate*160)/2,
  200. ui->widget_fc_camera->y() + ui->widget_fc_camera->height() + g_appInfoPtr->m_fRate*30,
  201. g_appInfoPtr->m_fRate*160, g_appInfoPtr->m_fRate*40);
  202. ui->label_fl_tips->adjustSize();
  203. ui->label_fl_tips->setGeometry((ui->widget_fc_BG->width() - ui->label_fl_tips->width())/2, ui->btn_fl_startVerify->y() + ui->btn_fl_startVerify->height() + g_appInfoPtr->m_fRate*20,
  204. ui->label_fl_tips->width(), ui->label_fl_tips ->height());
  205. ui->label_fl_actionMoive->setGeometry(0, 0, g_appInfoPtr->m_fRate*64, g_appInfoPtr->m_fRate*64);
  206. ui->label_fl_actionTips->adjustSize();
  207. int nActionWidth = ui->label_fl_actionMoive->width() + g_appInfoPtr->m_fRate*20 + ui->label_fl_actionTips->width() +
  208. g_appInfoPtr->m_fRate*20 + g_appInfoPtr->m_fRate*44;
  209. ui->widget_fl_action->setGeometry((ui->widget_fc_camera->width() - nActionWidth)/2, g_appInfoPtr->m_fRate*10,
  210. nActionWidth, g_appInfoPtr->m_fRate*64) ;
  211. ui->label_fl_actionTips->setGeometry(ui->label_fl_actionMoive->x() + ui->label_fl_actionMoive->width() + g_appInfoPtr->m_fRate*20,
  212. (ui->widget_fl_action->height() - ui->label_fl_actionTips->height())/2,
  213. ui->label_fl_actionTips->width(), ui->label_fl_actionTips->height());
  214. ui->btn_fl_time->setGeometry(ui->label_fl_actionTips->x() + ui->label_fl_actionTips->width() + g_appInfoPtr->m_fRate*20,
  215. (ui->widget_fl_action->height() - g_appInfoPtr->m_fRate*40)/2, g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*40);
  216. ui->widget_fl_hint->setVisible(false);
  217. ui->widget_fl_hint->setGeometry((width() - g_appInfoPtr->m_fRate*400)/2, (height() - g_appInfoPtr->m_fRate*180)/2,
  218. g_appInfoPtr->m_fRate*400, g_appInfoPtr->m_fRate*180);
  219. ui->label_fl_icon->setGeometry(g_appInfoPtr->m_fRate*75, g_appInfoPtr->m_fRate*70, g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*40);
  220. ui->label_fl_hint->adjustSize();
  221. ui->label_fl_hint->setGeometry(ui->label_fl_icon->x() + ui->label_fl_icon->width() + g_appInfoPtr->m_fRate*20,
  222. ui->label_fl_icon->y() + (ui->label_fl_icon->height() - ui->label_fl_hint->height())/2,
  223. ui->label_fl_hint->width(), ui->label_fl_hint->height());
  224. ui->widget_fl_action->setVisible(false);
  225. ui->btn_fl_startVerify->setVisible(false);
  226. ui->label_fl_tips->setVisible(false);
  227. ui->widget_fc_camera->setFixedHeight(g_appInfoPtr->m_fRate*431);
  228. ui->widget_fl_action->setVisible(true);
  229. }
  230. void faceLiveness::threadProc()
  231. {
  232. try
  233. {
  234. while (m_bIsRun)
  235. {
  236. if (m_bStartCompare)
  237. {
  238. if (m_imgList.begin() != m_imgList.end())
  239. {
  240. myDebug() << "m_imgList:" << m_imgList.size();
  241. cv::Mat matImage;
  242. {
  243. std::scoped_lock lock(m_imgMutex);
  244. if (m_imgList.begin() != m_imgList.end())
  245. {
  246. matImage = (*m_imgList.begin()).clone();
  247. m_imgList.erase(m_imgList.begin());
  248. }
  249. else
  250. {
  251. continue;
  252. }
  253. }
  254. if (matImage.empty())
  255. {
  256. myDebug() << "matImage.empty";
  257. continue;
  258. }
  259. if (!m_currentVerifyInfo.bIsVerify)
  260. {
  261. verifyAction(matImage);
  262. }
  263. }
  264. }
  265. else
  266. {
  267. Sleep(100);
  268. }
  269. }
  270. }
  271. catch (const std::exception &e)
  272. {
  273. myDebug()<<QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  274. }
  275. }
  276. void faceLiveness::verifyAction(cv::Mat matImage)
  277. {
  278. try
  279. {
  280. bool bHasStatus = false;
  281. int nFaceCount = 0;
  282. float fScore = 0;
  283. if (m_currentVerifyInfo.bIsVerify)
  284. {
  285. return;
  286. }
  287. m_currentVerifyInfo.matImage = matImage;
  288. if (m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_FACE_DETECT)
  289. {
  290. SeetaRect rt;
  291. if (!g_faceRecProcPtr->compareWithBase(matImage, nFaceCount, fScore, rt))
  292. {
  293. m_currentVerifyInfo.sErrorMsg = g_faceRecProcPtr->errorMsg();
  294. myServerLog() << g_faceRecProcPtr->errorMsg();
  295. return;
  296. }
  297. m_currentVerifyInfo.nFaceCount = nFaceCount;
  298. m_currentVerifyInfo.fSimilarity = fScore;
  299. if (nFaceCount != 1)
  300. {
  301. myServerLog() << QString::fromLocal8Bit("活体检测人脸数量异常,") << nFaceCount;
  302. m_currentVerifyInfo.sErrorMsg = QString::fromLocal8Bit("活体检测人脸数量异常,").arg(nFaceCount);
  303. return;
  304. }
  305. if (fScore * 100 > g_appInfoPtr->m_oExamInfo.nWarnThreshold)
  306. {
  307. bool bRealness = false;
  308. if (!g_faceRecProcPtr->faceRealness(matImage, bRealness))
  309. {
  310. myServerLog() << g_faceRecProcPtr->errorMsg();
  311. m_currentVerifyInfo.sErrorMsg = g_faceRecProcPtr->errorMsg();
  312. return;
  313. }
  314. //验证成功
  315. if (bRealness)
  316. {
  317. m_currentVerifyInfo.nEndTime = g_appInfoPtr->serverMTime();
  318. m_currentVerifyInfo.nRealness = 1;
  319. m_currentVerifyInfo.bPass = true;
  320. m_currentVerifyInfo.bIsVerify = true;
  321. myServerLog() << m_currentVerifyInfo.sActionType << ":" << fScore;
  322. return;
  323. }
  324. else
  325. {
  326. m_currentVerifyInfo.nRealness = 0;
  327. }
  328. }
  329. }
  330. if (m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_BLINK ||
  331. m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_NOD)
  332. {
  333. int nFaceStatus = 0;
  334. if (m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_BLINK)
  335. {
  336. nFaceStatus = SL_EYE_CLOSE;
  337. }
  338. else
  339. {
  340. nFaceStatus = SL_HEAD_DOWN;
  341. }
  342. if (!g_faceRecProcPtr->getFaceAttribute(matImage, nFaceStatus, bHasStatus, nFaceCount))
  343. {
  344. myServerLog() << g_faceRecProcPtr->errorMsg();
  345. m_currentVerifyInfo.sErrorMsg = g_faceRecProcPtr->errorMsg();
  346. return;
  347. }
  348. m_currentVerifyInfo.nFaceCount = nFaceCount;
  349. if (nFaceCount != 1)
  350. {
  351. myServerLog() << QString::fromLocal8Bit("活体检测人脸数量异常,") << nFaceCount;
  352. m_currentVerifyInfo.sErrorMsg = QString::fromLocal8Bit("活体检测人脸数量异常,").arg(nFaceCount);
  353. return;
  354. }
  355. if (bHasStatus)
  356. {
  357. //验证成功
  358. m_currentVerifyInfo.nEndTime = g_appInfoPtr->serverMTime();
  359. m_currentVerifyInfo.nRealness = 1;
  360. m_currentVerifyInfo.bPass = true;
  361. m_currentVerifyInfo.bIsVerify = true;
  362. }
  363. }
  364. else if (m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_SHAKE)
  365. {
  366. float fYaw = 0;
  367. float fPitch = 0;
  368. float fRoll = 0;
  369. int nFaceCount = 0;
  370. if (!g_faceRecProcPtr->getFaceAttribute(matImage, fYaw, fPitch, fRoll, nFaceCount))
  371. {
  372. myServerLog() << g_faceRecProcPtr->errorMsg();
  373. m_currentVerifyInfo.sErrorMsg = g_faceRecProcPtr->errorMsg();
  374. return;
  375. }
  376. m_currentVerifyInfo.nFaceCount = nFaceCount;
  377. if (m_fMinYaw > fYaw)
  378. {
  379. m_fMinYaw = fYaw;
  380. }
  381. if (m_fMaxYaw < fYaw)
  382. {
  383. m_fMaxYaw = fYaw;
  384. }
  385. if (m_fMaxYaw - m_fMinYaw > 30)
  386. {
  387. //验证成功
  388. m_currentVerifyInfo.nEndTime = g_appInfoPtr->serverMTime();
  389. m_currentVerifyInfo.nRealness = 1;
  390. m_currentVerifyInfo.bPass = true;
  391. m_currentVerifyInfo.bIsVerify = true;
  392. }
  393. }
  394. }
  395. catch (const std::exception &e)
  396. {
  397. myDebug() << QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  398. }
  399. }
  400. void faceLiveness::on_btn_fc_close_clicked()
  401. {
  402. }
  403. bool faceLiveness::setBaseImage()
  404. {
  405. QString sFileName = g_appInfoPtr->m_sStudentPhotoPath.right(g_appInfoPtr->m_sStudentPhotoPath.length() - g_appInfoPtr->m_sStudentPhotoPath.lastIndexOf("/") - 1);
  406. sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  407. if(!QFile::exists(sFileName))
  408. {
  409. ShowMsg(QString::fromLocal8Bit("底照下载失败,请检查网络"), this, MSG_ICON_TYPE::mit_error);
  410. return false;
  411. }
  412. QFileInfo filePath(sFileName);
  413. if(!g_faceRecProcPtr->setBaseImage(filePath.absoluteFilePath()))
  414. {
  415. //g_faceRecProcPtr->errorMsg()
  416. ShowMsg(QString::fromLocal8Bit("底照不符合要求,请更换照片"), this, MSG_ICON_TYPE::mit_error);
  417. return false;
  418. }
  419. return true;
  420. }
  421. void faceLiveness::on_btn_fl_startVerify_clicked()
  422. {
  423. if(g_faceRecProcPtr == nullptr)
  424. {
  425. g_faceRecProcPtr = std::make_shared<CFaceRecProc>();
  426. if (!g_appInfoPtr->m_sStudentPhotoPath.isEmpty())
  427. {
  428. if(!g_faceRecProcPtr->hasBaseImage())
  429. {
  430. if(!setBaseImage())
  431. {
  432. return;
  433. }
  434. }
  435. // QString sFileName = g_appInfoPtr->m_sStudentPhotoPath.right(g_appInfoPtr->m_sStudentPhotoPath.length() - g_appInfoPtr->m_sStudentPhotoPath.lastIndexOf("/") - 1);
  436. // sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;
  437. // if(!QFile::exists(sFileName))
  438. // {
  439. // ShowMsg(QString::fromLocal8Bit("底照下载失败,请检查网络"), this, MSG_ICON_TYPE::mit_error);
  440. // return;
  441. // }
  442. // if(!g_faceRecProcPtr->setBaseImage(sFileName))
  443. // {
  444. // ShowMsg(g_faceRecProcPtr->errorMsg(), this, MSG_ICON_TYPE::mit_error);
  445. // return;
  446. // }
  447. }
  448. else
  449. {
  450. ShowMsg(QString::fromLocal8Bit("当前考试未底照"), this, MSG_ICON_TYPE::mit_error);
  451. return;
  452. }
  453. }
  454. if(!g_faceRecProcPtr->hasBaseImage())
  455. {
  456. if(!setBaseImage())
  457. {
  458. return;
  459. }
  460. }
  461. m_bStartCompare = true;
  462. m_fMaxYaw = 0;
  463. m_fMinYaw = 0;
  464. m_sLivenessStatus = STATUS_TYPE::ST_SUCCESS;
  465. ui->btn_fl_startVerify->setVisible(false);
  466. ui->label_fl_tips->setVisible(false);
  467. ui->widget_fc_camera->setFixedHeight(g_appInfoPtr->m_fRate*431);
  468. ui->widget_fl_action->setVisible(true);
  469. {
  470. std::scoped_lock sl(m_livenessListMutex);
  471. m_currentVerifyInfo = m_livenessList[m_nCurIndex];
  472. }
  473. initAcionIcon();
  474. m_ActionTimer->start();
  475. }
  476. void faceLiveness::initAcionIcon()
  477. {
  478. QMovie *movie;
  479. ui->btn_fl_time->setVisible(true);
  480. ui->btn_fl_time->setText(QString("%1s").arg(m_currentVerifyInfo.nActionLeftSceonds));
  481. if(m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_FACE_DETECT)
  482. {
  483. movie = new QMovie(":/images/img-fl-face.png");
  484. ui->label_fl_actionTips->setText(QString::fromLocal8Bit("请让我看到您的正脸"));
  485. //ui->btn_fl_time->setVisible(false);
  486. }
  487. else if(m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_BLINK)
  488. {
  489. movie = new QMovie(":/images/gif-close-eyes.gif");
  490. ui->label_fl_actionTips->setText(QString::fromLocal8Bit("请闭眼"));
  491. }
  492. else if(m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_SHAKE)
  493. {
  494. movie = new QMovie(":/images/gif-turn-head.gif");
  495. ui->label_fl_actionTips->setText(QString::fromLocal8Bit("请摇头"));
  496. }
  497. else if(m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_NOD)
  498. {
  499. movie = new QMovie(":/images/gif-head-down.gif");
  500. ui->label_fl_actionTips->setText(QString::fromLocal8Bit("请点头"));
  501. }
  502. ui->label_fl_actionMoive->setMovie(movie);
  503. movie->start();
  504. }
  505. void faceLiveness::saveLivenessResult()
  506. {
  507. try
  508. {
  509. bool bSubmit = true;
  510. for (int i = 0; i < m_livenessList.count(); ++i)
  511. {
  512. if (!m_livenessList[i].matImage.empty() &&
  513. m_livenessList[i].sUrl.isEmpty())
  514. {
  515. bSubmit = false;
  516. m_nCurIndex = i;
  517. m_currentVerifyInfo = m_livenessList[i];
  518. QString sImageFile = QString("temp/photo/%1.png").arg(CCommonTools::getUuid());
  519. QImage img = CCommonTools::Mat2QImage(m_currentVerifyInfo.matImage);
  520. img.save(sImageFile, "PNG");
  521. //上传图片
  522. CHttpRequestPackage hrp;
  523. hrp.sUri = "/api/ecs_oe_student/client/exam/process/upload";
  524. hrp.nRequestType = RequestType::rtProcessUpload;
  525. hrp.sCommonStr = __FILE__;
  526. hrp.sParamList.push_back(QString("formdataFileType,file,%1").arg(sImageFile));
  527. hrp.sParamList.push_back(QString("md5,%1").arg(CCommonTools::fileMd5(sImageFile)));
  528. hrp.eParamType = HttpParamType::hptFormdata;
  529. g_httpBllPtr->post(hrp);
  530. break;
  531. }
  532. }
  533. if (bSubmit)
  534. {
  535. bool bSucceed = true;
  536. Json::Value jLiveness = Json::Value::null;
  537. jLiveness["status"] = m_sLivenessStatus.toStdString();
  538. jLiveness["faceLiveVerifyId"] = g_appInfoPtr->m_oExamInfo.nFaceLiveVerifyId;
  539. jLiveness["processTime"] = m_nEndTime - m_nStartTime;
  540. for (int i = 0; i < m_livenessList.count(); ++i)
  541. {
  542. if (m_livenessList[i].sActionType == ACTION_TYPE::AT_FACE_DETECT)
  543. {
  544. jLiveness["examRecordDataId"] = g_appInfoPtr->m_oExamInfo.nExamRecordDataId;
  545. jLiveness["faceCount"] = m_livenessList[i].nFaceCount;
  546. jLiveness["realness"] = m_livenessList[i].nRealness;
  547. jLiveness["similarity"] = m_livenessList[i].fSimilarity;
  548. if (m_livenessList[i].nFaceCount == 0)
  549. {
  550. jLiveness["status"] = STATUS_TYPE::ST_NOT_ONESELF.toStdString();
  551. }
  552. }
  553. else
  554. {
  555. Json::Value jAction = Json::Value::null;
  556. if (!m_livenessList[i].bPass)
  557. {
  558. bSucceed = false;
  559. jAction["errorMsg"] = m_livenessList[i].sErrorMsg.toStdString();
  560. }
  561. jAction["fileUrl"] = m_livenessList[i].sUrl.toStdString();
  562. jAction["pass"] = m_livenessList[i].bPass;
  563. jAction["processTime"] = m_livenessList[i].nEndTime - m_livenessList[i].nStartTime;
  564. jAction["retry"] = 0;
  565. jAction["type"] = m_livenessList[i].sActionType.toStdString();
  566. jLiveness["actions"].append(jAction);
  567. }
  568. }
  569. qDebug() << jLiveness.toStyledString().c_str();
  570. CHttpRequestPackage hrp;
  571. hrp.sUri = QString("/api/ecs_oe_student/client/exam/process/saveFaceLiveVerifyResult");
  572. hrp.nRequestType = RequestType::rtSaveFaceLiveVerifyResult;
  573. hrp.eParamType = HttpParamType::hptCustomBody;
  574. hrp.sParamList.push_back(QString("CustomBody,%1").arg(jLiveness.toStyledString().c_str()));
  575. g_httpBllPtr->post(hrp);
  576. }
  577. }
  578. catch (const std::exception &e)
  579. {
  580. myDebug() << QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  581. }
  582. }
  583. void faceLiveness::countdownTimer()
  584. {
  585. try
  586. {
  587. --m_nMaxSeconds;
  588. ui->label_fl_time->setText(QString("(%1)").arg(m_nMaxSeconds));
  589. if(m_livenessType == FACE_LIVENESS_TYPE::flt_inprogress && m_nMaxSeconds <= 0)
  590. {
  591. //整体超时
  592. m_bStartCompare = false;
  593. m_ActionTimer->stop();
  594. m_countdownTimer->stop();
  595. {
  596. std::scoped_lock sl(m_livenessListMutex);
  597. m_livenessList[m_nCurIndex] = m_currentVerifyInfo;
  598. }
  599. m_sLivenessStatus = STATUS_TYPE::ST_TIME_OUT;
  600. saveLivenessResult();
  601. }
  602. }
  603. catch (const std::exception &e)
  604. {
  605. myDebug() << QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  606. }
  607. }
  608. void faceLiveness::actionTimer()
  609. {
  610. try
  611. {
  612. if (m_currentVerifyInfo.bIsVerify)
  613. {
  614. //当前比对完成
  615. {
  616. std::scoped_lock sl(m_livenessListMutex);
  617. m_livenessList[m_nCurIndex] = m_currentVerifyInfo;
  618. }
  619. //下一个动作
  620. ++m_nCurIndex;
  621. if (m_nCurIndex < m_livenessList.count())
  622. {
  623. std::scoped_lock sl(m_livenessListMutex);
  624. m_currentVerifyInfo = m_livenessList[m_nCurIndex];
  625. m_currentVerifyInfo.reset(m_livenessList[m_nCurIndex].sActionType == ACTION_TYPE ::AT_FACE_DETECT ? m_nFaceDetectDuration : g_appInfoPtr->m_oExamInfo.nActionDuration);
  626. initAcionIcon();
  627. {
  628. std::scoped_lock sl(m_imgMutex);
  629. m_imgList.clear();
  630. }
  631. }
  632. else
  633. {
  634. //整体比对完成
  635. m_sLivenessStatus = STATUS_TYPE::ST_SUCCESS;
  636. m_bStartCompare = false;
  637. m_ActionTimer->stop();
  638. m_countdownTimer->stop();
  639. saveLivenessResult();
  640. return;
  641. }
  642. }
  643. else
  644. {
  645. if (m_currentVerifyInfo.nActionLeftSceonds <= 0)
  646. {
  647. ++m_nFaceRetryCount;
  648. {
  649. std::scoped_lock sl(m_imgMutex);
  650. m_imgList.clear();
  651. }
  652. if(m_nFaceRetryCount >= m_nMaxFaceRetryCount)
  653. {
  654. if(m_nFaceWholeRetryCount >= m_nMaxFaceWholeRetryCount)
  655. {
  656. //整体失败
  657. m_bStartCompare = false;
  658. m_currentVerifyInfo.sErrorMsg = QString::fromLocal8Bit("action timeout");
  659. m_ActionTimer->stop();
  660. m_countdownTimer->stop();
  661. {
  662. std::scoped_lock sl(m_livenessListMutex);
  663. m_livenessList[m_nCurIndex] = m_currentVerifyInfo;
  664. }
  665. m_sLivenessStatus = STATUS_TYPE::ST_TIME_OUT;
  666. if(m_livenessType == FACE_LIVENESS_TYPE::flt_inprogress)
  667. {
  668. saveLivenessResult();
  669. }
  670. else
  671. {
  672. showVerifyResultUI();
  673. QTimer::singleShot(2000, this, [&]() {
  674. emit faceLivenessFaild();
  675. });
  676. }
  677. return;
  678. }
  679. ++m_nFaceWholeRetryCount;
  680. {
  681. std::scoped_lock sl(m_imgMutex);
  682. m_imgList.clear();
  683. }
  684. m_nFaceRetryCount = 0;
  685. m_nCurIndex = 0;
  686. if (m_nCurIndex < m_livenessList.count())
  687. {
  688. std::scoped_lock sl(m_livenessListMutex);
  689. m_currentVerifyInfo = m_livenessList[m_nCurIndex];
  690. m_currentVerifyInfo.reset(m_livenessList[m_nCurIndex].sActionType == ACTION_TYPE::AT_FACE_DETECT ? m_nFaceDetectDuration : g_appInfoPtr->m_oExamInfo.nActionDuration);
  691. initAcionIcon();
  692. }
  693. return;
  694. }
  695. ++m_nFaceRetryCount;
  696. m_currentVerifyInfo.reset(m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_FACE_DETECT ? m_nFaceDetectDuration : g_appInfoPtr->m_oExamInfo.nActionDuration);
  697. initAcionIcon();
  698. }
  699. else
  700. {
  701. --m_currentVerifyInfo.nActionLeftSceonds;
  702. ui->btn_fl_time->setText(QString("%1s").arg(m_currentVerifyInfo.nActionLeftSceonds));
  703. if(m_currentVerifyInfo.sActionType == ACTION_TYPE::AT_FACE_DETECT)
  704. {
  705. if(m_currentVerifyInfo.nActionLeftSceonds == 50)
  706. {
  707. ShowMsg(QString::fromLocal8Bit("人脸检测不通过(请调整角度和光线进行尝试,不要戴眼镜)"), (QWidget*)this->parent());
  708. }
  709. else if(m_currentVerifyInfo.nActionLeftSceonds == 30)
  710. {
  711. ShowMsg(QString::fromLocal8Bit("人脸检测不通过(请调整角度和光线进行尝试,不要戴眼镜)"), (QWidget*)this->parent());
  712. }
  713. else if(m_currentVerifyInfo.nActionLeftSceonds == 1)
  714. {
  715. ShowMsg(QString::fromLocal8Bit("若多次尝试不通过,请联系老师更换照片后重试"), (QWidget*)this->parent());
  716. }
  717. }
  718. }
  719. }
  720. }
  721. catch (const std::exception &e)
  722. {
  723. myDebug() << QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  724. }
  725. }
  726. //文件上传
  727. void faceLiveness::onProcessUpload(CProcessUpload processUpload)
  728. {
  729. try
  730. {
  731. if(processUpload.sCommonStr == __FILE__)
  732. {
  733. if (processUpload.nCode == 200)
  734. {
  735. m_nRetryCount = 0;
  736. m_livenessList[m_nCurIndex].sUrl = processUpload.sFileUrl;
  737. if(m_livenessType == FACE_LIVENESS_TYPE::flt_entry_exam &&
  738. m_livenessList[m_nCurIndex].sActionType == ACTION_TYPE::AT_FACE_DETECT)
  739. {
  740. saveFaceCampareResult();
  741. }
  742. else
  743. {
  744. saveLivenessResult();
  745. }
  746. }
  747. else
  748. {
  749. if (processUpload.sMessage.isEmpty())
  750. {
  751. ShowMsg(QString::fromLocal8Bit("上传照片失败"), this, MSG_ICON_TYPE::mit_error);
  752. }
  753. else
  754. {
  755. ShowMsg(processUpload.sMessage, this, MSG_ICON_TYPE::mit_error);
  756. }
  757. if(m_nRetryCount < 4)
  758. {
  759. saveLivenessResult();
  760. m_nRetryCount++;
  761. }
  762. else
  763. {
  764. emit faceLivenessFaild();
  765. }
  766. }
  767. }
  768. }
  769. catch (const std::exception &e)
  770. {
  771. myDebug() << QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  772. }
  773. }
  774. void faceLiveness::saveFaceCampareResult()
  775. {
  776. CHttpRequestPackage hrp;
  777. hrp.sUri = "/api/ecs_oe_student/client/exam/process/saveFaceCompareResult";
  778. hrp.nRequestType = RequestType::rtSaveFaceCompareResult;
  779. Json::Value jBody = Json::Value::null;
  780. jBody["faceCompareResult"] = QString::number(m_livenessList[m_nCurIndex].fSimilarity).toStdString();
  781. jBody["fileUrl"] = m_livenessList[m_nCurIndex].sUrl.toStdString();
  782. jBody["pass"] = true;
  783. jBody["processTime"] = m_livenessList[m_nCurIndex].nEndTime - m_livenessList[m_nCurIndex].nStartTime;
  784. jBody["stranger"] = m_livenessList[m_nCurIndex].nFaceCount > 1;
  785. hrp.sParamList.push_back(QString("CustomBody,%1").arg(jBody.toStyledString().c_str()));
  786. hrp.eParamType = HttpParamType::hptCustomBody;
  787. g_httpBllPtr->post(hrp);
  788. }
  789. void faceLiveness::onSaveFaceCompareResult(CBaseResponsePackage res)
  790. {
  791. try
  792. {
  793. if(res.nCode == 200)
  794. {
  795. m_nRetryCount = 0;
  796. saveLivenessResult();
  797. }
  798. else
  799. {
  800. if(m_nRetryCount < 4)
  801. {
  802. saveFaceCampareResult();
  803. m_nRetryCount++;
  804. }
  805. else
  806. {
  807. emit faceLivenessFaild();
  808. }
  809. }
  810. }
  811. catch (const std::exception &e)
  812. {
  813. ShowMsg(QString::fromLocal8Bit("保存人脸识别信息失败"), this, MSG_ICON_TYPE::mit_error);
  814. myServerLog()<<"exception error"<<e.what();
  815. }
  816. }
  817. void faceLiveness::showVerifyResultUI()
  818. {
  819. if (m_sLivenessStatus == STATUS_TYPE::ST_SUCCESS)
  820. {
  821. ui->label_fl_hint->setText(QString::fromLocal8Bit("恭喜您完成检测!"));
  822. ui->label_fl_icon->setPixmap(QPixmap(":/images/icon-welcom.png"));
  823. }
  824. else if(m_sLivenessStatus == STATUS_TYPE::ST_TIME_OUT)
  825. {
  826. ui->label_fl_hint->setText(QString::fromLocal8Bit("检测失败:活体检测超时"));
  827. ui->label_fl_icon->setPixmap(QPixmap(":/images/icon-liveness-faild.png"));
  828. }
  829. else
  830. {
  831. ui->label_fl_hint->setText(QString::fromLocal8Bit("检测失败:单个动作超时"));
  832. ui->label_fl_icon->setPixmap(QPixmap(":/images/icon-liveness-faild.png"));
  833. }
  834. ui->label_fl_hint->adjustSize();
  835. ui->widget_fc_BG->setVisible(false);
  836. ui->widget_fl_hint->setVisible(true);
  837. ui->widget_fl_hint->setGeometry((width() - ui->label_fl_hint->width() - g_appInfoPtr->m_fRate * 180) / 2, (height() - g_appInfoPtr->m_fRate * 180) / 2,
  838. ui->label_fl_hint->width() + g_appInfoPtr->m_fRate * 180, g_appInfoPtr->m_fRate * 180);
  839. ui->label_fl_icon->setGeometry(g_appInfoPtr->m_fRate * 75, g_appInfoPtr->m_fRate * 70, g_appInfoPtr->m_fRate * 40, g_appInfoPtr->m_fRate * 40);
  840. ui->label_fl_hint->setGeometry(ui->label_fl_icon->x() + ui->label_fl_icon->width() + g_appInfoPtr->m_fRate * 20,
  841. ui->label_fl_icon->y() + (ui->label_fl_icon->height() - ui->label_fl_hint->height()) / 2,
  842. ui->label_fl_hint->width(), ui->label_fl_hint->height());
  843. }
  844. //保存人脸活体验证结果
  845. void faceLiveness::onSaveFaceLiveVerifyResult(CBaseResponsePackage res)
  846. {
  847. try
  848. {
  849. if (res.nCode == 200)
  850. {
  851. if (m_pCloseTimer == nullptr)
  852. {
  853. m_pCloseTimer = std::make_shared<QTimer>();
  854. m_pCloseTimer->setInterval(2000);
  855. connect(m_pCloseTimer.get(), &QTimer::timeout, this, [&]() {
  856. if (m_sLivenessStatus == STATUS_TYPE::ST_SUCCESS)
  857. {
  858. emit faceLivenessSucceed();
  859. }
  860. else
  861. {
  862. emit faceLivenessFaild();
  863. }
  864. });
  865. }
  866. showVerifyResultUI();
  867. m_pCloseTimer->start();
  868. }
  869. else
  870. {
  871. if (res.sMessage.isEmpty())
  872. {
  873. ShowMsg(QString::fromLocal8Bit("活体验证结果失败"), this, MSG_ICON_TYPE::mit_error);
  874. }
  875. else
  876. {
  877. ShowMsg(res.sMessage, this, MSG_ICON_TYPE::mit_error);
  878. }
  879. if(m_nRetryCount < 4)
  880. {
  881. saveLivenessResult();
  882. m_nRetryCount++;
  883. }
  884. else
  885. {
  886. emit faceLivenessFaild();
  887. }
  888. }
  889. }
  890. catch (const std::exception &e)
  891. {
  892. myDebug() << QString::fromLocal8Bit("人脸比对失败,%1").arg(e.what());
  893. }
  894. }