| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718 |
- #include "stdafx.h"
- #include "SpHelper.h"
- #include "LivenessDetectionFSM.h"
- #include "Event.h"
- #include "Common.h"
- #include "..\..\Other\libvideoframework\videoutil.h"
- #include "YituLiveSDK.h"
- int nCaptureType=CAPTURE_TYPE_PASSIVE;
- // 当前活体检测实体的引用
- CLivenessDetectionEntity *pDetection = NULL;
- // 当前活体检测状态机的引用
- CLivenessDetectionFSM *pFsm = NULL;
- // 心跳计时器ID号
- UINT_PTR TimerID = 88;
- // 心跳计时器间隔
- UINT Interval = 5000;
- // 活体检测窗口句柄
- HWND hDetectionWnd = NULL;
- // 活体检测窗口线程
- HANDLE hWndThread = NULL;
- // 活体检测窗口线程id
- UINT hWndThreadID = 0;
- // 回显摄像头编号
- int nEchoCamera = -1;
- // 当前回显提示消息
- CSimpleStringA strStatus="";
- // 历史回显提示消息
- CSimpleStringA strLastStatus="";
- CLivenessDetectionFSM::CLivenessDetectionFSM( int fps,LPCTSTR videoqueue0name,LPCTSTR videoqueue1name,int videowidth,int videoheight ): m_bStopCapture(FALSE)
- {
- m_nFps = fps;
- if (videoqueue0name)
- {
- m_VideoQueue0Name = videoqueue0name;
- }
- if (videoqueue1name)
- {
- m_VideoQueue1Name = videoqueue1name;
- }
- m_nTime = 0;
- m_hDetectThread = 0;
- m_hCameraThread0 = 0;
- m_hCameraThread1 = 0;
- m_bThreadRun = false;
- m_bUpdateScreen = false;
- m_nCaptureType = -1;
- m_bHasLiveTask = false;
- m_bActiveStop = false;
- m_bLookLowerScreen = false;
- m_nPassiveTimeLimit = TIME_LIMIT_PASSIVE;
- m_nActiveTimeLimit = TIME_LIMIT_ACTIVE;
- m_nLeastActiveShowTime = TIME_LEAST_ACTIVE_SHOW;
- m_bLoosePassiveStrategy = FALSE;
- m_bTimeout = false;
- m_StartCtx = NULL;
- m_StopCtx = NULL;
- m_bSaveLivenessResult = FALSE;
- m_Bitmap0.bmType = 0;
- m_Bitmap0.bmWidth = videowidth;
- m_Bitmap0.bmHeight = videoheight;
- m_Bitmap0.bmPlanes = 1;
- m_Bitmap0.bmWidthBytes = 3*m_Bitmap0.bmWidth;
- m_Bitmap0.bmBitsPixel = 24;
- m_Bitmap0.bmBits = NULL;
- m_Bitmap1.bmType = 0;
- m_Bitmap1.bmWidth = videoheight;
- m_Bitmap1.bmHeight = videowidth;
- m_Bitmap1.bmPlanes = 1;
- m_Bitmap1.bmWidthBytes = 3*m_Bitmap1.bmWidth;
- m_Bitmap1.bmBitsPixel = 24;
- m_Bitmap1.bmBits = NULL;
- m_Bitmap0Disp = m_Bitmap0;
- m_Bitmap1Disp = m_Bitmap1;
- m_nImageSize = videowidth*videoheight*3;
- m_Bitmap0.bmBits = new unsigned char[m_nImageSize];
- m_Bitmap1.bmBits = new unsigned char[m_nImageSize];
- m_Bitmap0Disp.bmBits = new unsigned char[m_nImageSize];
- m_Bitmap1Disp.bmBits = new unsigned char[m_nImageSize];
- InitializeCriticalSection(&m_lock1);
- InitializeCriticalSection(&m_lock2);
- }
- CLivenessDetectionFSM::~CLivenessDetectionFSM()
- {
- if(m_Bitmap0.bmBits != NULL)
- {
- delete[] m_Bitmap0.bmBits;
- m_Bitmap0.bmBits = NULL;
- }
- if (m_Bitmap0Disp.bmBits != NULL)
- {
- delete[] m_Bitmap0Disp.bmBits;
- m_Bitmap0Disp.bmBits = NULL;
- }
- if(m_Bitmap1.bmBits != NULL)
- {
- delete[] m_Bitmap1.bmBits;
- m_Bitmap1.bmBits = NULL;
- }
- if (m_Bitmap1Disp.bmBits != NULL)
- {
- delete[] m_Bitmap1Disp.bmBits;
- m_Bitmap1Disp.bmBits = NULL;
- }
- DeleteCriticalSection(&m_lock1);
- DeleteCriticalSection(&m_lock2);
- }
- ErrorCodeEnum CLivenessDetectionFSM::OnInit()
- {
- CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum eErrDev;
- eErrDev = spEntityFunction->OpenConfig(Config_CenterSetting, spConfig);
- if (eErrDev != Error_Succeed)
- {
- Dbg("open centersetting file failed!");
- return eErrDev;
- }
- SpIniMappingTable table;
- table.AddEntryInt("LivenessDetection","SaveLivenessResult",m_bSaveLivenessResult,FALSE);
- table.AddEntryInt("LivenessDetection","PassiveTimeLimit",m_nPassiveTimeLimit,TIME_LIMIT_PASSIVE);
- table.AddEntryInt("LivenessDetection","ActiveTimeLimit",m_nActiveTimeLimit,TIME_LIMIT_ACTIVE);
- table.AddEntryInt("LivenessDetection","LeastActiveShowTime",m_nLeastActiveShowTime,TIME_LEAST_ACTIVE_SHOW);
- table.AddEntryInt("LivenessDetection","LoosePassiveStrategy",m_bLoosePassiveStrategy,FALSE);
- eErrDev = table.Load(spConfig);
- if (eErrDev != Error_Succeed)
- {
- Dbg("fail to load centersettings!");
- m_bSaveLivenessResult = FALSE;
- m_nPassiveTimeLimit = TIME_LIMIT_PASSIVE;
- m_nActiveTimeLimit = TIME_LIMIT_ACTIVE;
- m_nLeastActiveShowTime = TIME_LEAST_ACTIVE_SHOW;
- m_bLoosePassiveStrategy = FALSE;
- }
- if (m_nPassiveTimeLimit<=0||m_nPassiveTimeLimit>7200)
- {
- m_nPassiveTimeLimit = TIME_LIMIT_PASSIVE;
- }
- if (m_nLeastActiveShowTime<=0||m_nLeastActiveShowTime>TIME_LIMIT_ACTIVE)
- {
- m_nLeastActiveShowTime = TIME_LEAST_ACTIVE_SHOW;
- }
- if (m_nActiveTimeLimit<m_nLeastActiveShowTime||m_nActiveTimeLimit>TIME_LIMIT_ACTIVE)
- {
- m_nActiveTimeLimit = TIME_LIMIT_ACTIVE;
- }
- if (m_bLoosePassiveStrategy != 0 && m_bLoosePassiveStrategy != 1)
- {
- m_bLoosePassiveStrategy = FALSE;
- }
- if (m_bSaveLivenessResult)
- {
- eErrDev = m_pEntity->GetFunction()->GetPath("UploadPhoto", m_LivenessResultPath);
- if (eErrDev != Error_Succeed)
- {
- Dbg("fail to get UploadPhotoPath!");
- m_LivenessResultPath = "";
- }
- else
- {
- int len = m_LivenessResultPath.GetLength();
- if (m_LivenessResultPath.GetLength() > 0 && m_LivenessResultPath[len-1] != '\\') {
- m_LivenessResultPath += "\\";
- }
- }
- }
- VideoInit(m_VideoQueue0Name.c_str(),m_VideoQueue1Name.c_str());
- StartMsgHwnd(5000);
- // TODO: Add extra initialization here
-
- return Error_Succeed;
- }
- ErrorCodeEnum CLivenessDetectionFSM::OnExit()
- {
- if (videoframe0->data != NULL)
- {
- delete videoframe0->data;
- videoframe0->data = NULL;
- }
- if (videoframe0 != NULL)
- {
- delete videoframe0;
- videoframe0 = NULL;
- }
- if (videoframe1->data != NULL)
- {
- delete videoframe1->data;
- videoframe1->data = NULL;
- }
- if (videoframe1 != NULL)
- {
- delete videoframe1;
- videoframe1 = NULL;
- }
- if (m_videoqueue0 != NULL)
- {
- delete m_videoqueue0;
- m_videoqueue0 = NULL;
- }
- if (m_videoqueue1 != NULL)
- {
- delete m_videoqueue1;
- m_videoqueue1 = NULL;
- }
- if (m_hEventWait)
- {
- CloseHandle(m_hEventWait);
- m_hEventWait = NULL;
- }
- if (m_hEventTask)
- {
- CloseHandle(m_hEventTask);
- m_hEventTask = NULL;
- }
- StopMsgHwnd();
- return Error_Succeed;
- }
- void CLivenessDetectionFSM::OnStateTrans( int iSrcState, int iDstState )
- {
- Dbg("[LivenessDetectionFSM] State trans from %d to %d!", iSrcState, iDstState);
- }
- void CLivenessDetectionFSM::s0_on_entry()
- {
- LOG_FUNCTION();
- if (m_bLookLowerScreen)
- {
- ScheduleTimer(1,4000);
- }
- else
- {
- SetEvent(m_hEventTask);
- }
- }
- void CLivenessDetectionFSM::s0_on_exit()
- {
- LOG_FUNCTION();
- CancelTimer(1);
- }
- unsigned int CLivenessDetectionFSM::s0_on_event( FSMEvent* e )
- {
- switch(e->iEvt)
- {
- case EVT_TIMER:
- if (m_bLookLowerScreen)
- {
- m_bLookLowerScreen=false;
- SetEvent(m_hEventTask);
- }
- break;
- case USER_EVT_STOPCAPTURE:
- StopLivenessDetection();
- m_nCaptureType=-1;
- m_bHasLiveTask=false;
- if (m_StopCtx!=NULL)
- {
- m_StopCtx->Answer(Error_Succeed);
- m_StopCtx=NULL;
- }
- e->SetHandled();
- break;
- case USER_EVT_STARTCAPTURE:
- SaveLiveConfig(m_StartCtx);
- m_bHasLiveTask=true;
- e->SetHandled();
- break;
- case USER_EVT_QUIT:
- e->SetHandled();
- LOG_TRACE("s0 on event quit");
- break;
- default:
- break;
- }
- return 0;
- }
- void CLivenessDetectionFSM::s1_on_entry()
- {
- LOG_FUNCTION();
- StartCaptureTask *task = new StartCaptureTask(this);
- //m_pEntity->GetFunction()->PostThreadPoolTask(task);
- m_pEntity->GetFunction()->PostEntityTaskFIFO(task);
- }
- void CLivenessDetectionFSM::s1_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CLivenessDetectionFSM::s1_on_event( FSMEvent* e )
- {
- int ret = 0;
- switch(e->iEvt)
- {
- case USER_EVT_STOPCAPTURE:
- StopLivenessDetection();
- m_nCaptureType=-1;
- m_bHasLiveTask=false;
- if (m_StopCtx!=NULL)
- {
- m_StopCtx->Answer(Error_Succeed);
- m_StopCtx=NULL;
- }
- e->SetHandled();
- break;
- case USER_EVT_STARTCAPTURE_FINISHED:
- e->SetHandled();
- ret = e->param1;
- break;
- default:
- break;
- }
- return ret;
- }
- void CLivenessDetectionFSM::s2_on_entry()
- {
- LOG_FUNCTION();
- }
- void CLivenessDetectionFSM::s2_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CLivenessDetectionFSM::s2_on_event( FSMEvent* e )
- {
- int ret = 0;
- switch(e->iEvt)
- {
- case USER_EVT_STOPCAPTURE:
- {
- StopCaptureTask *task = new StopCaptureTask(this);
- //m_pEntity->GetFunction()->PostThreadPoolTask(task);
- m_pEntity->GetFunction()->PostEntityTaskLIFO(task);
- e->SetHandled();
- }
- break;
- case USER_EVT_CAPTURE_FINISHED:
- m_bHasLiveTask=false;
- if (m_bTimeout)
- {
- m_bTimeout=false;
- DetectionStopUnExpected evt;
- evt.IsActive=(m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS||m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITH_LIVENESS?true:false);
- evt.ActionID=CSimpleStringA2W(m_uid.ToString());
- evt.ErrorCode=CSimpleStringA2W("Error_TimeOut");
- if (evt.IsActive)
- {
- evt.ErrorMsg = CSimpleStringA2W("主动捕获超时,已终止!");
- }
- else
- {
- evt.ErrorMsg = CSimpleStringA2W("被动捕获超时,已终止!");
- }
- SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(DetectionStopUnExpected), SP_MSG_SIG_OF(DetectionStopUnExpected), evt);
- Dbg("[dbg]: DetectionStopUnExpected has broadcasted.");
- }
- if (m_StopCtx!=NULL)
- {
- m_StopCtx->Answer(Error_Succeed);
- m_StopCtx=NULL;
- }
- m_nCaptureType=-1;
- e->SetHandled();
- break;
- default:
- break;
- }
- return ret;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_LIVENESS_DONE:
- if (pFsm->m_bHasLiveTask)
- {
- Dbg("主动捕获成功");
- pFsm->m_bHasLiveTask=false; // 防止重复广播
- // send to rvcweb
- ActiveDetectionDone evt;
- int nLiveType = (int)wParam;
- char *LiveTypes[]={"N","Y","X"};
- evt.VerifyResult=CSimpleStringA2W(LiveTypes[nLiveType]);
- Dbg("ActiveLiveType:%s",LiveTypes[nLiveType]);
- evt.ActionID=CSimpleStringA2W(pFsm->m_uid.ToString());
- BITMAP bitmap;
- YituLiveSDK::YituGetImage(&bitmap);
- if (bitmap.bmBits!=NULL)
- {
- if (pFsm->m_bSaveLivenessResult&&pFsm->m_LivenessResultPath.GetLength()>0)
- {
- // save liveness result
- char szPhoto[MAX_PATH]={0};
- sprintf(szPhoto,"%s%s_Active_%d_0.jpg",(LPCTSTR)pFsm->m_LivenessResultPath,
- (LPCTSTR)pFsm->m_uid.ToString(),nLiveType);
- SaveLivenessPhoto(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,szPhoto);
- }
- int nJpgSize=0;
- int size = 3*bitmap.bmHeight*bitmap.bmWidth;
- char *pJpg=new char[size];
- RGB2JPG(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,&nJpgSize,pJpg,pDetection);
- if (nJpgSize)
- {
- evt.SnapShotPhotoLength = nJpgSize;
- evt.SnapShotPhotoData.Alloc(nJpgSize);
- memmove(evt.SnapShotPhotoData.m_pData, pJpg, nJpgSize);
- evt.SnapShotPhotoData.Resize(nJpgSize);
- }
- else
- {
- evt.SnapShotPhotoLength=0;
- }
- delete []pJpg; pJpg=NULL;
- }
- else
- {
- evt.SnapShotPhotoLength=0;
- }
- int nTimeUsed=pFsm->m_nSeconds;
- int nLeastActiveShowTime=pFsm->m_nLeastActiveShowTime;
- if (nTimeUsed<nLeastActiveShowTime)
- {
- ::Sleep((nLeastActiveShowTime-nTimeUsed)*1000); // 保证至少进行nLeastActiveShowTime才结束并通知
- }
- pFsm->StopLivenessDetection();
- pFsm->PostEventFIFO(new FSMEvent(USER_EVT_CAPTURE_FINISHED));
- SpSendBroadcast(pDetection->GetFunction(), SP_MSG_OF(ActiveDetectionDone), SP_MSG_SIG_OF(ActiveDetectionDone), evt);
- Dbg("[dbg]: ActiveDetectionDone has broadcasted.");
- }
- break;
- case WM_IMAGE_INFO:
- if (pFsm->m_bHasLiveTask) // 如果还未报活体结束
- {
- strStatus="";
- switch(lParam) {
- case STATUS_HeadLeft:
- strStatus="请左转一些";
- break;
- case STATUS_HeadRight:
- strStatus="请右转一些";
- break;
- case STATUS_HeadUp:
- strStatus="请不要抬头太高";
- break;
- case STATUS_HeadDown:
- strStatus="请不要低头";
- break;
- case STATUS_HalfFace:
- strStatus="请不要遮挡面部";
- break;
- case STATUS_HasSunGlasses:
- strStatus="请不要遮挡眼睛";
- break;
- case STATUS_HasMask:
- strStatus="请不要遮挡嘴部";
- break;
- case STATUS_HasHat:
- strStatus="请取下帽子";
- break;
- case STATUS_NoFace:
- strStatus="请对准摄像头";
- break;
- case STATUS_Normal:
- strStatus="请保持";
- break;
- default:
- Dbg("receive status code: %d",lParam);
- break;
- }
- //Dbg("strStatus:%s(%d)",(LPCTSTR)strStatus,lParam);
- if (strStatus.GetLength() > 0 && strStatus != strLastStatus)
- {
- AutoSnapshotRemind evt;
- evt.ActionID=CSimpleStringA2W(pFsm->m_uid.ToString());
- evt.RemindInfo=CSimpleStringA2W(strStatus);
-
- LogEvent(Severity_Middle,LOG_EVT_SHOWACTIVECAPTUREMSG,(LPCTSTR)strStatus); // notify iebrowser
- SpSendBroadcast(pDetection->GetFunction(), SP_MSG_OF(AutoSnapshotRemind), SP_MSG_SIG_OF(AutoSnapshotRemind), evt);
- Dbg("[dbg]: AutoSnapshotRemind has broadcasted.");
- strLastStatus=strStatus;
- }
- }
- break;
- case WM_CAMERA_ECHO:
- {
- nEchoCamera=(int)wParam;
- char strEchoCamera[2]={0};
- sprintf(strEchoCamera,"%d",nEchoCamera);
- LogEvent(Severity_Middle,LOG_EVT_ECHOACTIVECAPTURECAM,strEchoCamera); // notify chh(sipphone)
- }
- break;
- case WM_TIME_UPDATE:
- {
- //Dbg("[dbg] WM_TIME_UPDATE received.");
- //CLivenessDetectionFSM *pFSM = (CLivenessDetectionFSM *)lParam;
- //pFSM->UpdateTime((int)wParam);
- }
- break;
- case WM_CAMERA_FAULT:
- {
- Dbg("On WM_CAMERA_FAULT!");
- CLivenessDetectionFSM *pFSM = (CLivenessDetectionFSM *)lParam;
- pFSM->NotifyCameraFault();
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- /*case WM_TIMER:
- printf("[dbg] got Timer Message.\n");
- break;*/
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
- {
- if (NULL == pDetection)
- {
- Dbg("[dbg]: heartbeat cann't be sent.");
- return;
- }
- LivenessDetectionHeartBeat evt;
- evt.Status = 0;
- SpSendBroadcast(pDetection->GetFunction(), SP_MSG_OF(LivenessDetectionHeartBeat), SP_MSG_SIG_OF(LivenessDetectionHeartBeat), evt);
- Dbg("[dbg]: heartbeat has sent.");
- }
- UINT CALLBACK HWndThreadProc( LPVOID param )
- {
- pDetection = dynamic_cast<CLivenessDetectionEntity *>((CEntityBase*)param);
- TCHAR szClassName[256] = {'l', 'd', 0};
- SYSTEMTIME st;
- GetLocalTime(&st);
- srand(GetTickCount());
- sprintf_s(szClassName+2, 250, "%d%d%d%d%d%d", rand(), rand(), st.wMinute, st.wSecond, st.wMilliseconds, GetTickCount());
- Dbg("[dbg] szClassName: %s.", szClassName);
- WNDCLASSEX wcex = {0};
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.lpszClassName = szClassName;
- if (!RegisterClassEx(&wcex))
- {
- Dbg("[dbg] RegisterClassEx Failed!\r\n");
- return 1;
- }
- hDetectionWnd = CreateWindow(szClassName, "", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
- HideCaret(hDetectionWnd);
- MSG msg;
- assert(hDetectionWnd!=NULL);
- CSimpleStringA csBinPath;
- ErrorCodeEnum errCode=pDetection->GetFunction()->GetPath("Bin",csBinPath);
- CSimpleStringA dllName;
- if (errCode!=Error_Succeed)
- {
- return 2;
- }
- DeviceTypeEnum deviceType = pDetection->GetDeviceType();
- if (!(ePadtype==deviceType||eMobilePadType==deviceType||eDesk2SType==deviceType))
- {
- dllName=csBinPath.Append("\\Liveness.0.12.dll");
- }
- else
- {
- dllName=csBinPath.Append("\\Liveness4Pad\\Liveness.0.12.dll");
- }
- Dbg("[dbg] dllName:%s",(LPCTSTR)dllName);
- YituLiveSDK::Yitu_InitParam Iniparam;
- Iniparam.szPath=".";
- Iniparam.hwnd=hDetectionWnd;
- Iniparam.uMsgLivenessDone=WM_LIVENESS_DONE;
- Iniparam.uMsgImageInfo=WM_IMAGE_INFO;
- Iniparam.uMsgEchoCamera=WM_CAMERA_ECHO;
- Dbg("[dbg] begin to call YituInit.");
- int rtn=YituLiveSDK::YituInit((LPCTSTR)dllName,&Iniparam);
- Dbg("[dbg] end to call YituInit.");
- assert(rtn==FUNCTION_RTN_SUCC);
- //SetTimer(hDetectionWnd,TimerID,Interval,TimerProc);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- BOOL CLivenessDetectionFSM::StartMsgHwnd( UINT nInterval )
- {
- if (hWndThread)
- {
- Dbg("[dbg] DetectionWndThread has started.");
- return TRUE;
- }
- Interval = nInterval;
- pFsm = this;
- hWndThread = (HANDLE)_beginthreadex(NULL, 0, HWndThreadProc, this->GetEntityBase(), 0, &hWndThreadID);
- return hWndThread ? TRUE : FALSE;
- }
- BOOL CLivenessDetectionFSM::StopMsgHwnd()
- {
- if (hDetectionWnd)
- {
- //KillTimer(hDetectionWnd, TimerID);
- SendMessage(hDetectionWnd,WM_CLOSE,0,0);
- if (hDetectionWnd)
- {
- WaitForSingleObject(hWndThread,-1);
- CloseHandle(hWndThread);
- }
- hWndThread = NULL;
- hDetectionWnd = NULL;
- int rtn=YituLiveSDK::YituUninit();
- assert(rtn==FUNCTION_RTN_SUCC);
- }
- return TRUE;
- }
- void CLivenessDetectionFSM::VideoInit( LPCTSTR videoqueue0name,LPCTSTR videoqueue1name )
- {
- videoframe0 = new videoq_frame;
- memset(videoframe0,0,sizeof(videoq_frame));
- videoframe1 = new videoq_frame;
- memset(videoframe1,0,sizeof(videoq_frame));
- m_hEventWait= ::CreateEventA(NULL, TRUE, 0, 0);
- if (!m_hEventWait)
- {
- Dbg("create hEventWait failed!");
- return;
- }
- m_hEventTask = ::CreateEventA(NULL, TRUE, TRUE, "TaskFinishEvent");
- if (!m_hEventTask)
- {
- Dbg("create hEventTask failed!");
- }
- if (videoqueue0name&&strlen(videoqueue0name)>0)
- {
- m_videoqueue0 = new Clibvideoqueue(videoqueue0name);
- }
- else
- {
- m_videoqueue0 = NULL;
- }
- if (videoqueue1name&&strlen(videoqueue1name)>0)
- {
- m_videoqueue1 = new Clibvideoqueue(videoqueue1name);
- }
- else
- {
- m_videoqueue1 = NULL;
- }
- }
- int CLivenessDetectionFSM::GetVideoFrameSize( int&nWidth,int&nHeight )
- {
- int size = 0;
- if (m_videoqueue0 == NULL)
- {
- Dbg("videoqueue not initialized,not exsit!");
- size = 0;
- }
- if (m_videoqueue1)
- {
- int width1 = 0;
- int height1 = 0;
- int width2 = 0;
- int height2 = 0;
- size = m_videoqueue0->GetFrameSize(width1,height1);
- if (size)
- {
- (width1>height1)?(nWidth=nHeight=width1):(nWidth=nHeight=height1);
- }
- else
- {
- size = m_videoqueue1->GetFrameSize(width2,height2);
- (width2>height2)?(nWidth=nHeight=width2):(nWidth=nHeight=height2);
- }
- size = nWidth*nHeight*3;
- }
- else
- {
- size = m_videoqueue0->GetFrameSize(nWidth,nHeight);
- int width1 = nWidth;
- int height1 = nHeight;
- (width1>height1)?(nWidth=nHeight=width1):(nWidth=nHeight=height1);
- size = nWidth*nHeight*3;
- }
- return size;
- }
- BOOL CLivenessDetectionFSM::GetVideoFrameByCameraID( int CameraID, videoq_frame* Video, int flags )
- {
- assert(CameraID == 0 || CameraID == 1);
- if (CameraID == 0) // For StandVTM or Pad
- {
- BOOL bRslt = FALSE;
- int width = 0;
- int height = 0;
- m_videoqueue0->GetFrameSize(width,height);
- memset(Video->data,0,Video->framesize);
- videoq_frame*tmp_frm = new videoq_frame;
- //tmp_frm->data = Video->data+ Video->width*(width-height)/2*3;
- tmp_frm->data = Video->data;
- bRslt = m_videoqueue0->GetVideo(tmp_frm, flags);
- if (!bRslt)
- {
- delete tmp_frm;
- Dbg("get video from videoqueue0 fail!");
- return FALSE;
- }
- delete tmp_frm;
- return TRUE;
- }
- else // For StandVTM
- {
- BOOL bRslt = FALSE;
- int width = 0;
- int height = 0;
- m_videoqueue1->GetFrameSize(width,height);
- memset(Video->data,0,Video->framesize);
- videoq_frame*tmp_frm = new videoq_frame;
- //tmp_frm->data = Video->data + (height-width)/2*3;
- tmp_frm->data = Video->data;
- //横向摄像头图像,拼接
- bRslt = m_videoqueue1->GetVideo(tmp_frm, flags);
- if (!bRslt)
- {
- delete tmp_frm;
- Dbg("get video from videoqueue1 fail!");
- return FALSE;
- }
- delete tmp_frm;
- return TRUE;
- }
- }
- void CLivenessDetectionFSM::FSleep( int ms )
- {
- DWORD dwRet = WaitForSingleObject(m_hEventWait, ms);
- if (dwRet == WAIT_OBJECT_0) // 如果等到信号
- {
- throw std::exception(); // 抛个异常表明是外界停止捕获
- }
- }
- UINT CLivenessDetectionFSM::VideoCapture0()
- {
- int nWidth = 0;
- int nHeight = 0;
- int nVideoFrameSize = 0; //视频帧的大小
- BOOL bInitVideoParam = FALSE;
- unsigned char* pData0=NULL;
- unsigned char* pData1=NULL;
- bool bHasOneMoreData=false;
- int nVideoSizeFailedTimes = 0; // add by ly 20160716
- while (m_bCameraThreadRun)
- {
- try
- {
- if (!bInitVideoParam)
- {
- //获取视频帧大小
- if (nVideoFrameSize <= 0)
- {
- if (nVideoSizeFailedTimes >= 400)
- {
- Dbg("[dbg] camera0 fault,nVideoSizeFailedTimes=%d!",nVideoSizeFailedTimes);
- m_bCameraThreadRun=false;
- ::PostMessage(hDetectionWnd,WM_CAMERA_FAULT,0,(LONG_PTR)this);
- continue;
- }
- //nVideoFrameSize = GetVideoFrameSize(nWidth,nHeight);
- nWidth = m_Bitmap0.bmWidth;
- nHeight = m_Bitmap0.bmHeight;
- nVideoFrameSize = 3*nWidth*nHeight;
- if(nVideoFrameSize <= 0)
- {
- ++nVideoSizeFailedTimes;
- Dbg("[Capture0] nVideoFrameSize = 0");
- FSleep(5);
- continue;
- }
- pData1=new unsigned char[nVideoFrameSize];
- //初始化视频buffer
- if (videoframe0->data == NULL)
- {
- videoframe0->data = new unsigned char[nVideoFrameSize];
- videoframe0->framesize = nVideoFrameSize;
- videoframe0->height = nHeight;
- videoframe0->width = nWidth;
- videoframe0->format = VIDEO_FORMAT_RGB24;
- }
- }
- bInitVideoParam = TRUE;
- continue;
- }
- BOOL bVideoLens = (m_videoqueue0->GetVideoLens()>0);
- if (bVideoLens)
- {
- if(m_bHasData0==false)
- {
- if (bHasOneMoreData)
- {
- memcpy(m_Bitmap0.bmBits,pData1,nVideoFrameSize);
- //FlipImage(&m_Bitmap0);
- bHasOneMoreData=false;
- EnterCriticalSection(&m_lock2);
- m_bHasData0=true;
- LeaveCriticalSection(&m_lock2);
- //Dbg("[dbg] VideoCapture0 got frame at 1.");
- }
- else
- {
- BOOL bGetvideo = GetVideoFrameByCameraID(0,videoframe0,0);
- if (!bGetvideo)
- {
- FSleep(2);
- continue;
- }
- pData0=videoframe0->data;
- if (pData0!=NULL)
- {
- memcpy(m_Bitmap0.bmBits,pData0,nVideoFrameSize);
- //FlipImage(&m_Bitmap0);
- EnterCriticalSection(&m_lock2);
- m_bHasData0=true;
- LeaveCriticalSection(&m_lock2);
- //Dbg("[dbg] VideoCapture0 got frame at 2.");
- }
- }
- }
- else if (bHasOneMoreData==false)
- {
- BOOL bGetvideo = GetVideoFrameByCameraID(0,videoframe0,0);
- if (!bGetvideo)
- {
- FSleep(2);
- continue;
- }
- pData0=videoframe0->data;
- if (pData0!=NULL)
- {
- memcpy(pData1,pData0,nVideoFrameSize);
- bHasOneMoreData=true;
- //Dbg("[dbg] VideoCapture0 put frame to buffer.");
- }
- }
- }
- else
- {
- //Dbg("[dbg] VideoCapture1 got no frame.");
- // 摄像头启动失败无法采集,填充黑色
- if (m_bHasData0 == false)
- {
- if (bHasOneMoreData)
- {
- memcpy(m_Bitmap0.bmBits,pData1,nVideoFrameSize);
- //FlipImage(&m_Bitmap0);
- bHasOneMoreData=false;
- EnterCriticalSection(&m_lock2);
- m_bHasData0=true;
- LeaveCriticalSection(&m_lock2);
- }
- else
- {
- memset(m_Bitmap0.bmBits,0,nVideoFrameSize);
- EnterCriticalSection(&m_lock2);
- m_bHasData0=true;
- LeaveCriticalSection(&m_lock2);
- }
- }
- else if (bHasOneMoreData == false)
- {
- memset(pData1,0,nVideoFrameSize);
- bHasOneMoreData=true;
- }
- }
- FSleep(5);
- }
- catch (...)
- {
- Dbg("[Captrue0] exception occurred or stop capture.");
- Sleep(50);
- continue;
- }
- }
- if (pData1)
- {
- delete pData1;
- pData1=NULL;
- }
- m_hCameraThread0=0;
- return 0;
- }
- UINT CLivenessDetectionFSM::VideoCapture1()
- {
- int nWidth = 0;
- int nHeight = 0;
- int nVideoFrameSize = 0; //视频帧的大小
- BOOL bInitVideoParam = FALSE;
- unsigned char* pData0=NULL;
- unsigned char* pData1=NULL;
- bool bHasOneMoreData=false;
- int nVideoSizeFailedTimes = 0; // add by ly 20160716
- while (m_bCameraThreadRun)
- {
- try
- {
- if (!bInitVideoParam)
- {
- //获取视频帧大小
- if (nVideoFrameSize <= 0)
- {
- if (nVideoSizeFailedTimes >= 400)
- {
- Dbg("[dbg] camera1 fault,nVideoSizeFailedTimes=%d!",nVideoSizeFailedTimes);
- m_bCameraThreadRun=false;
- ::PostMessage(hDetectionWnd,WM_CAMERA_FAULT,0,(LONG_PTR)this);
- continue;
- }
- //nVideoFrameSize = GetVideoFrameSize(nWidth,nHeight);
- nWidth = m_Bitmap1.bmWidth;
- nHeight = m_Bitmap1.bmHeight;
- nVideoFrameSize = 3*nWidth*nHeight;
- if(nVideoFrameSize <= 0)
- {
- ++nVideoSizeFailedTimes;
- Dbg("[Capture1] nVideoFrameSize = 0");
- FSleep(5);
- continue;
- }
- pData1=new unsigned char[nVideoFrameSize];
- //初始化视频buffer
- if (videoframe1->data == NULL)
- {
- videoframe1->data = new unsigned char[nVideoFrameSize];
- videoframe1->framesize = nVideoFrameSize;
- videoframe1->height = nHeight;
- videoframe1->width = nWidth;
- videoframe1->format = VIDEO_FORMAT_RGB24;
- }
- }
- bInitVideoParam = TRUE;
- continue;
- }
- BOOL bVideoLens = (m_videoqueue1->GetVideoLens()>0);
- if (bVideoLens)
- {
- if(m_bHasData1==false)
- {
- if (bHasOneMoreData)
- {
- memcpy(m_Bitmap1.bmBits,pData1,nVideoFrameSize);
- //FlipImage(&m_Bitmap1);
- bHasOneMoreData=false;
- EnterCriticalSection(&m_lock2);
- m_bHasData1=true;
- LeaveCriticalSection(&m_lock2);
- //Dbg("[dbg] VideoCapture1 got frame at 1.");
- }
- else
- {
- BOOL bGetvideo = GetVideoFrameByCameraID(1,videoframe1,0);
- if (!bGetvideo)
- {
- FSleep(2);
- continue;
- }
- pData0=videoframe1->data;
- if (pData0!=NULL)
- {
- memcpy(m_Bitmap1.bmBits,pData0,nVideoFrameSize);
- //FlipImage(&m_Bitmap1);
- EnterCriticalSection(&m_lock2);
- m_bHasData1=true;
- LeaveCriticalSection(&m_lock2);
- //Dbg("[dbg] VideoCapture1 got frame at 2.");
- }
- }
- }
- else if (bHasOneMoreData==false)
- {
- BOOL bGetvideo = GetVideoFrameByCameraID(1,videoframe1,0);
- if (!bGetvideo)
- {
- FSleep(2);
- continue;
- }
- pData0=videoframe1->data;
- if (pData0!=NULL)
- {
- memcpy(pData1,pData0,nVideoFrameSize);
- bHasOneMoreData=true;
- //Dbg("[dbg] VideoCapture1 put frame to buffer.");
- }
- }
- }
- else
- {
- //Dbg("[dbg] VideoCapture1 got no frame.");
- // 摄像头启动失败无法采集,用黑色填充
- if (m_bHasData1 == false)
- {
- if (bHasOneMoreData)
- {
- memcpy(m_Bitmap1.bmBits,pData1,nVideoFrameSize);
- //FlipImage(&m_Bitmap1);
- bHasOneMoreData=false;
- EnterCriticalSection(&m_lock2);
- m_bHasData1=true;
- LeaveCriticalSection(&m_lock2);
- }
- else
- {
- memset(m_Bitmap1.bmBits,0,nVideoFrameSize);
- EnterCriticalSection(&m_lock2);
- m_bHasData1=true;
- LeaveCriticalSection(&m_lock2);
- }
- }
- else if (bHasOneMoreData == false)
- {
- memset(pData1,0,nVideoFrameSize);
- bHasOneMoreData=true;
- }
- }
- FSleep(5);
- }
- catch (...)
- {
- Dbg("[Captrue1] exception occurred or stop capture.");
- Sleep(50);
- continue;
- }
- }
- if (pData1)
- {
- delete pData1;
- pData1=NULL;
- }
- m_hCameraThread1=0;
- return 0;
- }
- UINT CLivenessDetectionFSM::SingleCameraDetect()
- {
- int nFrameID = 0;
- DWORD dwStart = GetTickCount();
- while (m_bThreadRun)
- {
- if (m_bHasData0 == false)
- {
- Sleep(5);
- continue;
- }
- DWORD dwTime = GetTickCount();
- YituLiveSDK::Yitu_ImageData data;
- data.pImage = &m_Bitmap0;
- data.nCameraID = 0;
- data.nFrameID = nFrameID;
- data.dTimeStamp = GetTickCount() / 1000.0;
- YituLiveSDK::YituAppendFrame(&data);
- if (m_nCaptureType == CAPTURE_TYPE_PASSIVE)
- {
- data.pImage = NULL;
- data.nCameraID = 1;
- YituLiveSDK::YituAppendFrame(&data);
- }
- nFrameID++;
- if (GetTickCount() - dwStart > 1000)
- {
- dwStart += 1000;
- //::PostMessage(hDetectionWnd, WM_TIME_UPDATE, nFrameID, 0);
- m_nSeconds++;
- if (IsTimeOut())
- {
- m_bTimeout=true;
- m_bThreadRun=false;
- PostEventFIFO(new FSMEvent(USER_EVT_STOPCAPTURE));
- continue;
- }
- }
- else
- {
- EnterCriticalSection(&m_lock1);
- memcpy(m_Bitmap0Disp.bmBits, m_Bitmap0.bmBits, m_nImageSize);
- LeaveCriticalSection(&m_lock1);
- }
- m_nTime += GetTickCount() - dwTime;
- EnterCriticalSection(&m_lock2);
- m_bHasData0 = false;
- LeaveCriticalSection(&m_lock2);
- }
- m_hDetectThread = 0;
- return 0;
- }
- UINT CLivenessDetectionFSM::LiveDetect()
- {
- int nFrameID=0;
- DWORD dwStart=GetTickCount();
- while (m_bThreadRun)
- {
- if (m_bHasData0==false||m_bHasData1==false)
- {
- Sleep(5);
- continue;
- }
- //Dbg("[dbg] do detect.");
- DWORD dwTime=GetTickCount();
- YituLiveSDK::Yitu_ImageData data;
- // 目前只能先传0后传1
- data.pImage=&m_Bitmap0;
- data.nCameraID=0;
- data.nFrameID=nFrameID;
- data.dTimeStamp=GetTickCount()/1000.0;
- YituLiveSDK::YituAppendFrame(&data);
- data.pImage=&m_Bitmap1;
- data.nCameraID=1;
- YituLiveSDK::YituAppendFrame(&data);
- nFrameID++;
- if (GetTickCount()-dwStart>1000)
- {
- dwStart+=1000;
- //::PostMessage(hDetectionWnd,WM_TIME_UPDATE,nFrameID,(LONG_PTR)this);
- m_nSeconds++;
- if (IsTimeOut())
- {
- m_bTimeout=true;
- m_bThreadRun=false;
- PostEventFIFO(new FSMEvent(USER_EVT_STOPCAPTURE));
- continue;
- }
- }
- else
- {
- EnterCriticalSection(&m_lock1);
- memcpy(m_Bitmap0Disp.bmBits,m_Bitmap0.bmBits,m_nImageSize);
- memcpy(m_Bitmap1Disp.bmBits,m_Bitmap1.bmBits,m_nImageSize);
- LeaveCriticalSection(&m_lock1);
- // show image msg
- }
- m_nTime+=GetTickCount()-dwTime;
- EnterCriticalSection(&m_lock2);
- m_bHasData0=false;
- m_bHasData1=false;
- LeaveCriticalSection(&m_lock2);
- }
- m_hDetectThread=0;
- return 0;
- }
- UINT WINAPI CameraThread0(LPVOID pM)
- {
- CLivenessDetectionFSM* pFSM=(CLivenessDetectionFSM*)pM;
- return pFSM->VideoCapture0();
- }
- UINT WINAPI CameraThread1(LPVOID pM)
- {
- CLivenessDetectionFSM* pFSM=(CLivenessDetectionFSM*)pM;
- return pFSM->VideoCapture1();
- }
- UINT WINAPI SingleCameraDetectThread(LPVOID pM)
- {
- CLivenessDetectionFSM* pFSM=(CLivenessDetectionFSM*)pM;
- return pFSM->SingleCameraDetect();
- }
- UINT WINAPI DetectThread(LPVOID pM)
- {
- CLivenessDetectionFSM* pFSM=(CLivenessDetectionFSM*)pM;
- return pFSM->LiveDetect();
- }
- bool CLivenessDetectionFSM::IsTimeOut()
- {
- if (m_nCaptureType==-1)
- {
- return true;
- }
-
- int nTimeLimit=(m_nCaptureType==CAPTURE_TYPE_PASSIVE?m_nPassiveTimeLimit:m_nActiveTimeLimit);
- return m_nSeconds>=nTimeLimit?true:false;
- }
- void CLivenessDetectionFSM::UpdateTime( int nFrame )
- {
- m_nSeconds++;
- Dbg("Second=%d , FPS=%d, algorithm time=%.3fs",m_nSeconds,nFrame-m_nLastFrame,m_nTime/1000.0);
- m_nTime=0;
- m_nLastFrame=nFrame;
- // 显示人脸检测框
- int nCount=0,i;
- YituLiveSDK::Yitu_TrackData data;
- int rtn=YituLiveSDK::YituGetTrackCount(0,&nCount);
- for (i=0;i<nCount;i++)
- {
- if (0==YituLiveSDK::YituGetTrackData(0,i,&data))
- {
- EnterCriticalSection(&m_lock1);
- DrawRect(data.rectFace,&m_Bitmap0);
- memcpy(m_Bitmap0Disp.bmBits,m_Bitmap0.bmBits,m_nImageSize);
- BMP_SaveFile("d:\\rvc\\face_0.jpg",m_Bitmap0Disp.bmBits,m_Bitmap0Disp.bmWidth,m_Bitmap0Disp.bmHeight,m_Bitmap0Disp.bmBitsPixel);
- LeaveCriticalSection(&m_lock1);
- }
- }
- rtn=YituLiveSDK::YituGetTrackCount(1,&nCount);
- for (i=0;i<nCount;i++)
- {
- if (0==YituLiveSDK::YituGetTrackData(1,i,&data))
- {
- EnterCriticalSection(&m_lock1);
- DrawRect(data.rectFace,&m_Bitmap1);
- memcpy(m_Bitmap1Disp.bmBits,m_Bitmap1.bmBits,m_nImageSize);
- BMP_SaveFile("d:\\rvc\\face_1.jpg",m_Bitmap1Disp.bmBits,m_Bitmap1Disp.bmWidth,m_Bitmap1Disp.bmHeight,m_Bitmap1Disp.bmBitsPixel);
- LeaveCriticalSection(&m_lock1);
- }
- }
- }
- void CLivenessDetectionFSM::DrawRect( RECT rect,BITMAP* pPic )
- {
- if (rect.right<0 || rect.left<0 || rect.top<0 || rect.bottom<0)
- {
- return;
- }
- if (rect.right>=pPic->bmWidth || rect.left>=pPic->bmWidth ||
- rect.top>=pPic->bmHeight || rect.bottom>=pPic->bmHeight)
- {
- return;
- }
- int i;
- unsigned char* p1=(unsigned char*)pPic->bmBits;
- for (i=rect.left;i<rect.right;i++)
- {
- p1[rect.top*pPic->bmWidthBytes+i*3]=0;
- p1[rect.top*pPic->bmWidthBytes+i*3+1]=0;
- p1[rect.top*pPic->bmWidthBytes+i*3+2]=255;
- p1[rect.bottom*pPic->bmWidthBytes+i*3]=0;
- p1[rect.bottom*pPic->bmWidthBytes+i*3+1]=0;
- p1[rect.bottom*pPic->bmWidthBytes+i*3+2]=255;
- }
- for (i=rect.top;i<rect.bottom;i++)
- {
- p1[i*pPic->bmWidthBytes+rect.left*3]=0;
- p1[i*pPic->bmWidthBytes+rect.left*3+1]=0;
- p1[i*pPic->bmWidthBytes+rect.left*3+2]=255;
- p1[i*pPic->bmWidthBytes+rect.right*3]=0;
- p1[i*pPic->bmWidthBytes+rect.right*3+1]=0;
- p1[i*pPic->bmWidthBytes+rect.right*3+2]=255;
- }
- int leftborder=rect.left<1?0:rect.left-1;
- int rightborder=rect.right>pPic->bmWidth-1?pPic->bmWidth:rect.right+1;
- int topborder=rect.top<1?0:rect.top-1;
- int bottomborder=rect.bottom>pPic->bmHeight-1?pPic->bmHeight:rect.bottom+1;
- for (i=leftborder;i<rightborder;i++)
- {
- p1[topborder*pPic->bmWidthBytes+i*3]=0;
- p1[topborder*pPic->bmWidthBytes+i*3+1]=0;
- p1[topborder*pPic->bmWidthBytes+i*3+2]=255;
- p1[bottomborder*pPic->bmWidthBytes+i*3]=0;
- p1[bottomborder*pPic->bmWidthBytes+i*3+1]=0;
- p1[bottomborder*pPic->bmWidthBytes+i*3+2]=255;
- }
- for (i=topborder;i<bottomborder;i++)
- {
- p1[i*pPic->bmWidthBytes+leftborder*3]=0;
- p1[i*pPic->bmWidthBytes+leftborder*3+1]=0;
- p1[i*pPic->bmWidthBytes+leftborder*3+2]=255;
- p1[i*pPic->bmWidthBytes+rightborder*3]=0;
- p1[i*pPic->bmWidthBytes+rightborder*3+1]=0;
- p1[i*pPic->bmWidthBytes+rightborder*3+2]=255;
- }
- }
- int CLivenessDetectionFSM::StartLivenessDetection()
- {
- if (m_nCaptureType!=-1 /*&& m_nCaptureType!=nCaptureType*/)
- {
- StopLivenessDetection();
- m_nCaptureType=-1;
- }
- m_nCaptureType=nCaptureType;
- if (m_nCaptureType == CAPTURE_TYPE_ACTIVE_WITH_LIVENESS ||
- m_nCaptureType == CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS)
- {
- Dbg("主动人像捕获中...");
- }
- else
- {
- Dbg("人像捕获中...");
- }
- int rtn=YituLiveSDK::YituStartCapture(m_nCaptureType);
- if (rtn!=FUNCTION_RTN_SUCC)
- {
- Dbg("StartCapture(CAPTURE_TYPE_%d) failed!(%d)",m_nCaptureType,rtn);
- m_bHasLiveTask=false;
- return 1;
- }
- else
- {
- if (m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITH_LIVENESS ||
- m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS)
- {
- //LogEvent(Severity_Middle,LOG_EVT_STARTACTIVECAPTURE,"0@0@0@0@1792@40@896@896"); // for standvtm notify iebrowser and chh(sipphone)
- ActiveDetectionStarted evt;
- CLivenessDetectionEntity *pDetection = dynamic_cast<CLivenessDetectionEntity *>(m_pEntity);
- DeviceTypeEnum deviceType = pDetection->GetDeviceType();
- if (!(ePadtype==deviceType||eMobilePadType==deviceType||eDesk2SType==deviceType))
- {
- //evt.Param="0@0@0@0@1792@40@896@896";
- evt.Param="0@0@0@0@1838@290@600@600";
- }
- else
- {
- evt.Param="0@0@0@0@711@275@500@500"; // pad或低柜双屏回显位置
- }
- SpSendBroadcast(pDetection->GetFunction(),SP_MSG_OF(ActiveDetectionStarted),SP_MSG_SIG_OF(ActiveDetectionStarted),evt);
- Dbg("[dbg]: ActiveDetectionStarted has broadcasted.");
- }
- else if (m_nCaptureType==CAPTURE_TYPE_PASSIVE)
- {
- m_uid=CUUID::Create(m_uid);
- Dbg("NEW Liveness SessionID=%s",m_uid.ToString());
- }
- m_bCameraThreadRun=true;
- m_bThreadRun=true;
- m_bUpdateScreen=true;
- m_nLastFrame=0;
- m_nSeconds=0;
- m_bHasData0=false;
- m_bHasData1=false;
- strLastStatus="";
- strStatus="";
- ResetEvent(m_hEventWait); // add by ly
- ResetEvent(m_hEventTask);
- m_StartCtx=NULL;
- if (!m_videoqueue1)
- {
- m_hCameraThread0=(HANDLE)_beginthreadex(NULL,0,CameraThread0,this,0,NULL);
- m_hCameraThread1=NULL;
- m_hDetectThread=(HANDLE)_beginthreadex(NULL,0,SingleCameraDetectThread,this,0,NULL);
- }
- else
- {
- m_hCameraThread0=(HANDLE)_beginthreadex(NULL,0,CameraThread0,this,0,NULL);
- m_hCameraThread1=(HANDLE)_beginthreadex(NULL,0,CameraThread1,this,0,NULL);
- m_hDetectThread=(HANDLE)_beginthreadex(NULL,0,DetectThread,this,0,NULL);
- }
- }
- return 0;
- }
- void CLivenessDetectionFSM::StopLivenessDetection()
- {
- SetEvent(m_hEventWait); // add by ly at 20160630
- if (m_hDetectThread!=0)
- {
- m_bThreadRun=false;
- WaitForSingleObject(m_hDetectThread,2000);
- }
- if (m_hCameraThread0!=0 ||m_hCameraThread1!=0)
- {
- m_bCameraThreadRun=false;
- WaitForSingleObject(m_hCameraThread0,2000);
- WaitForSingleObject(m_hCameraThread1,2000);
- }
- if (m_nCaptureType==CAPTURE_TYPE_PASSIVE)
- {
- int nCount=0;
- Dbg("[dbg] begin to call YituGetLiveImageCount.");
- int rtn=YituLiveSDK::YituGetLiveImageCount(&nCount);
- Dbg("[dbg] end to call YituGetLiveImageCount.");
- if (rtn==0)
- {
- Dbg("捕获到%d个活体人像",nCount);
- int n = (nCount < 10 ? nCount : 10);
- if (n > 0)
- {
- char szPhoto[MAX_PATH]={0};
- BYTE *pBuf = new BYTE[n*m_nImageSize];
- int lens[10] = {0};
- int offset = 0;
- int nLive = 0, nHack = 0, nNoSure = 0;
- int nImageIndex = 0;
- for (int i=0;i<n;i++)
- {
- BITMAP bitmap;
- bitmap.bmBits=NULL;
- int nLiveType=LIVENESSTYPE_NOT_SURE;
- Dbg("[dbg] begin to call YituGetLiveImage.");
- rtn=YituLiveSDK::YituGetLiveImage(i,&bitmap,&nLiveType);
- Dbg("[dbg] end to call YituGetLiveImage.");
- if (rtn==0 && bitmap.bmBits!=NULL)
- {
- if (m_bSaveLivenessResult&&m_LivenessResultPath.GetLength()>0)
- {
- sprintf(szPhoto,"%s%s_Passive_%d_%d.jpg",(LPCTSTR)pFsm->m_LivenessResultPath,
- (LPCTSTR)pFsm->m_uid.ToString(),nLiveType,nImageIndex++);
- SaveLivenessPhoto(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,szPhoto);
- }
- if (nLiveType==LIVENESSTYPE_LIVE)
- {
- ++nLive;
- int size=3*bitmap.bmWidth*bitmap.bmHeight;
- char *pJpg=new char[size];
- int nJpgSize=0;
- RGB2JPG(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,&nJpgSize,pJpg,pDetection);
- if (nJpgSize)
- {
- lens[i] = nJpgSize;
- memcpy(pBuf+offset,pJpg,lens[i]);
- }
- delete []pJpg; pJpg=NULL;
- }
- else if (nLiveType==LIVENESSTYPE_HACK)
- {
- ++nHack;
- lens[i] = 0;
- }
- else if (nLiveType==LIVENESSTYPE_NOT_SURE)
- {
- ++nNoSure;
- lens[i] = 0;
- }
- }
- else
- {
- Dbg("[YituGetLiveImage] rtn!=0 or bitmap.bmBits==NULL!");
- lens[i] = 0;
- }
- offset += lens[i];
- }
- Dbg("[dbg] nLive=%d,nHack=%d,nNoSure=%d!",nLive,nHack,nNoSure);
- char lenstr[MAX_PATH] = "", tmpstr[32] = "";
- int size = 0;
- for (int i=0;i<nLive;i++)
- {
- if (lens[i]!=0)
- {
- sprintf(tmpstr,"%d",lens[i]);
- strcat(lenstr,tmpstr);
- }
- if (i<nLive-1)
- strcat(lenstr,"|");
- size += lens[i];
- }
-
- // send to rvcweb
- if (m_StopCtx!=NULL) // edit by ly @2018/04/12
- {
- m_StopCtx->Ans.ActionID=CSimpleStringA2W(m_uid.ToString());
- if (!m_bLoosePassiveStrategy)
- {
- if (nHack>0) // 有攻击就不过
- {
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("N");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- else if (nLive>0&&nNoSure==0) // 全活体则过
- {
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("Y");
- Dbg("LivePhotosLength=%s",lenstr);
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W(lenstr);
- CBlob *pImagesData=&m_StopCtx->Ans.LivePhotosData;
- pImagesData->Alloc(size);
- memmove(pImagesData->m_pData,pBuf,size);
- pImagesData->Resize(size);
- }
- else // 不确定
- {
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("X");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- }
- else
- {
- if (nLive>0) // 有活体就通过
- {
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("Y");
- Dbg("LivePhotosLength=%s",lenstr);
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W(lenstr);
- CBlob *pImagesData=&m_StopCtx->Ans.LivePhotosData;
- pImagesData->Alloc(size);
- memmove(pImagesData->m_pData,pBuf,size);
- pImagesData->Resize(size);
- }
- else if (nHack>0&&nNoSure==0) // 全攻击则不过
- {
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("N");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- else // 不确定
- {
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("X");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- }
- }
- delete[] pBuf; pBuf = NULL;
- }
- else
- {
- // send to rvcweb
- if (m_StopCtx!=NULL)
- {
- m_StopCtx->Ans.ActionID=CSimpleStringA2W(m_uid.ToString());
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("X");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- }
- }
- else
- {
- // send to rvcweb
- if (m_StopCtx!=NULL)
- {
- m_StopCtx->Ans.ActionID=CSimpleStringA2W(m_uid.ToString());
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("X");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- }
- }
- else if (m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITH_LIVENESS ||
- m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS)
- {
- //do nothing here
- }
- else // multiple stop
- {
- // send to rvcweb
- if (m_StopCtx!=NULL)
- {
- m_StopCtx->Ans.ActionID=CSimpleStringA2W(m_uid.ToString());
- m_StopCtx->Ans.VerifyResult=CSimpleStringA2W("X");
- m_StopCtx->Ans.LivePhotosLength=CSimpleStringA2W("");
- }
- }
- Dbg("[dbg] begin to call YituStopCapture.");
- YituLiveSDK::YituStopCapture();
- Dbg("[dbg] end to call YituStopCapture.");
-
- if (m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITH_LIVENESS ||
- m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS)
- {
- if (m_StartCtx==NULL||!m_StartCtx->Req.IsActive)
- {
- ::Sleep(50); // 50ms后发出主动活体结束广播
- //LogEvent(Severity_Middle,LOG_EVT_STOPACTIVECAPTURE,"looklowerscreen.jpg"); // notify iebrowser and chh(sipphone)
- ActiveDetectionStopped evt;
- CLivenessDetectionEntity *pDetection = dynamic_cast<CLivenessDetectionEntity *>(m_pEntity);
- DeviceTypeEnum deviceType = pDetection->GetDeviceType();
- if (!(ePadtype==deviceType||eMobilePadType==deviceType||eDesk2SType==deviceType))
- {
- evt.Param="looklowerscreen.jpg";
- m_bLookLowerScreen=true;
- }
- SpSendBroadcast(pDetection->GetFunction(),SP_MSG_OF(ActiveDetectionStopped),SP_MSG_SIG_OF(ActiveDetectionStopped),evt);
- Dbg("[dbg]: ActiveDetectionStopped has broadcasted.");
- }
- else
- {
- Dbg("[dbg]: A new active task has come in!");
- }
- }
- /*m_nCaptureType=-1;*/
- }
- BOOL CLivenessDetectionFSM::SaveLiveConfig(SpReqAnsContext<LivenessDetectionService_StartLivenessDetection_Req, LivenessDetectionService_StartLivenessDetection_Ans>::Pointer ctx)
- {
- // 是否是主动捕获,主动捕获是否做活体检测
- bool bActiveCapture=false, bDoActiveDetect=false;
- bActiveCapture=ctx->Req.IsActive;
- bDoActiveDetect=CSimpleStringW2A(ctx->Req.Reserved)=="Y"?true:false;
- Dbg("[dbg] bActiveCapture=%d, bDoActiveDetect=%d.",bActiveCapture,bDoActiveDetect);
- nCaptureType=CAPTURE_TYPE_PASSIVE;
- if (bActiveCapture)
- {
- nCaptureType=bDoActiveDetect?CAPTURE_TYPE_ACTIVE_WITH_LIVENESS:CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS;
- }
-
- /*m_ActionID=(LPCTSTR)CSimpleStringW2A(ctx->Req.ActionID);*/
- return TRUE;
- }
- void CLivenessDetectionFSM::StartLiveDetect( SpReqAnsContext<LivenessDetectionService_StartLivenessDetection_Req, LivenessDetectionService_StartLivenessDetection_Ans>::Pointer ctx )
- {
- m_StartCtx = ctx;
- if (m_bHasLiveTask || strcmp(GetCurrStateName(),"Normal") || m_bLookLowerScreen)
- {
- Dbg("GetCurrStateName:%s",GetCurrStateName());
- Dbg("[dbg]: Waiting for the last live task terminate...");
- WaitCaptureFinishTask *task = new WaitCaptureFinishTask(this);
- m_pEntity->GetFunction()->PostThreadPoolTask(task);
- }
- else
- {
- ctx->Answer(Error_Succeed);
- PostEventFIFO(new FSMEvent(USER_EVT_STARTCAPTURE));
- }
- }
- void CLivenessDetectionFSM::SetActiveStopLiveDetect( SpReqAnsContext<LivenessDetectionService_StopLivenessDetection_Req, LivenessDetectionService_StopLivenessDetection_Ans>::Pointer ctx )
- {
- m_StopCtx = ctx;
- //m_bActiveStop = true;
- PostEventFIFO(new FSMEvent(USER_EVT_STOPCAPTURE));
- }
- void CLivenessDetectionFSM::NotifyCameraFault()
- {
- if (m_bHasLiveTask)
- {
- StopLivenessDetection(); // 停止活体 抛出异常
- DetectionStopUnExpected evt;
- evt.ActionID=CSimpleStringA2W(m_uid.ToString());
- evt.IsActive=(m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITHOUT_LIVENESS||m_nCaptureType==CAPTURE_TYPE_ACTIVE_WITH_LIVENESS?true:false);
- evt.ErrorCode=CSimpleStringA2W("Error_CameraFault");
- evt.ErrorMsg=CSimpleStringA2W("摄像头故障,捕获已终止!");
- PostEventFIFO(new FSMEvent(USER_EVT_CAPTURE_FINISHED));
- SpSendBroadcast(pDetection->GetFunction(),SP_MSG_OF(DetectionStopUnExpected),SP_MSG_SIG_OF(DetectionStopUnExpected),evt);
- Dbg("[dbg]: DetectionStopUnExpected has broadcasted.");
- }
- }
|