| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014 |
- #ifdef RVC_OS_WIN
- #include "stdafx.h"
- #endif // RVC_OS_WIN
- #include "RvcFaceCapturer.h"
- #include "MyEvent.h"
- #include "Common.h"
- #include "../../Other/libvideoframework/videoutil.h"
- #include <process.h>
- namespace RvcFaceCapture
- {
- #define MAX(a,b) ((a)<(b)?(b):(a))
- #define WNDCLS_NAME "facerect"
- // 人脸框窗口句柄
- HWND hFaceRectWnd = NULL;
- HDC hdcBuffer = NULL; //人脸回显框背景图缓冲
- RvcFaceCapturer::RvcFaceCapturer( LPCTSTR videoqueue0name,LPCTSTR videoqueue1name,int videowidth,int videoheight )
- {
- if (videoqueue0name)
- {
- m_VideoQueue0Name = videoqueue0name;
- }
- if (videoqueue1name)
- {
- m_VideoQueue1Name = videoqueue1name;
- }
- m_bCaptured = FALSE;
- m_hInstance = NULL;
- RvcFaceCapInit = NULL;
- RvcFaceCapStartCapture = NULL;
- RvcFaceCapFeedFrame = NULL;
- RvcFaceCapStopCapture = NULL;
- RvcFaceCapGetImage = NULL;
- RvcFaceCapUnInit = NULL;
- RvcFaceCapGetTrackData = NULL;
- m_nEchoCamera = 0;
- m_hEventWait = NULL;
- m_strStatus = "";
- m_strLastStatus= "";
- m_nLastEchoCamera = -1;
- m_bThreadRun = false;
- m_hCaptureThread = NULL;
- m_hFaceRectThread = NULL;
- videoframe0 = NULL;
- videoframe1 = NULL;
- m_videoqueue0 = NULL;
- m_videoqueue1 = NULL;
- m_bTimeout = false;
- m_nSeconds = 0;
- m_bitmap0.bmPlanes = 1;
- m_bitmap0.bmType = 0;
- m_bitmap0.bmBitsPixel = 24;
- m_bitmap0.bmWidth = videowidth;
- m_bitmap0.bmHeight = videoheight;
- m_bitmap0.bmWidthBytes = 3*m_bitmap0.bmWidth;
- m_nImageSize = m_bitmap0.bmWidthBytes*m_bitmap0.bmHeight;
- m_bitmap0.bmBits = new unsigned char[m_nImageSize];
- m_bitmap1.bmPlanes = 1;
- m_bitmap1.bmType = 0;
- m_bitmap1.bmBitsPixel = 24;
- m_bitmap1.bmWidth = videoheight;
- m_bitmap1.bmHeight = videowidth;
- m_bitmap1.bmWidthBytes = 3*m_bitmap1.bmWidth;
- m_nImageSize = m_bitmap1.bmWidthBytes*m_bitmap1.bmHeight;
- m_bitmap1.bmBits = new unsigned char[m_nImageSize];
- m_bSaveCaptureResult = FALSE;
- }
- RvcFaceCapturer::~RvcFaceCapturer()
- {
- if(m_bitmap0.bmBits)
- {
- delete[] m_bitmap0.bmBits;
- m_bitmap0.bmBits = NULL;
- }
- if(m_bitmap1.bmBits)
- {
- delete[] m_bitmap1.bmBits;
- m_bitmap1.bmBits = NULL;
- }
- RvcFaceCapInit = NULL;
- RvcFaceCapStartCapture = NULL;
- RvcFaceCapFeedFrame = NULL;
- RvcFaceCapStopCapture = NULL;
- RvcFaceCapGetImage = NULL;
- RvcFaceCapUnInit = NULL;
- RvcFaceCapGetTrackData = NULL;
- m_hCaptureThread = NULL;
- }
- void RvcFaceCapturer::OnFaceCapImgInfo( const char* msg )
- {
- if(m_timer.isRunning())
- {
- return;
- }
- else
- {
- m_timer.start();
- }
- m_strStatus=msg;
- if (m_strStatus.GetLength() > 0 && m_strStatus != m_strLastStatus)
- {
- //发送告警信息
- LogWarn(Severity_High,Error_Debug,LOG_EVT_SHOWACTIVECAPTUREMSG,m_strStatus.GetData());
- AutoSnapshotRemind evt;
- evt.ActionID=CSimpleStringA2W(m_ActionID);
- evt.RemindInfo=CSimpleStringA2W(m_strStatus);
- LogEvent(Severity_Middle,
- LOG_EVT_SHOWACTIVECAPTUREMSG,
- (LPCTSTR)m_strStatus); // notify iebrowser
- SpSendBroadcast(m_pDetection->GetFunction(),
- SP_MSG_OF(AutoSnapshotRemind),
- SP_MSG_SIG_OF(AutoSnapshotRemind),
- evt);
- Dbg("[RvcFaceCapturer]: AutoSnapshotRemind has broadcasted.");
- m_strLastStatus=m_strStatus;
- }
- }
- void RvcFaceCapturer::OnEchoCamera( int cameraID )
- {
- m_nEchoCamera = cameraID;
- if (m_nEchoCamera != m_nLastEchoCamera)
- {
- //发送告警信息
- CSimpleString msg = CSimpleString::Format("EchoCameraID:%d", m_nEchoCamera);
- LogWarn(Severity_High, Error_Debug, LOG_EVT_ECHOACTIVECAPTURECAM, msg);
- char strEchoCamera[2]={0};
- sprintf(strEchoCamera,"%d",m_nEchoCamera);
- LogEvent(Severity_Middle,
- LOG_EVT_ECHOACTIVECAPTURECAM,
- strEchoCamera); // notify chh(sipphone)
- m_nLastEchoCamera = m_nEchoCamera;
- }
- }
- void RvcFaceCapturer::OnFaceCapDone()
- {
- m_bCaptured = TRUE;
- Dbg("主动捕获成功");
- }
- //人脸捕获线程
- UINT WINAPI FaceCaptureThread(LPVOID pM)
- {
- Dbg("FaceCaptureThread entered!");
- RvcFaceCapturer* pFaceCapturer = (RvcFaceCapturer*)pM;
- return pFaceCapturer->StartCapture();
- }
- // 重画边框的具体代码
- void DrawBorder(HWND hWnd)
- {
- if (hWnd)
- {
- PAINTSTRUCT pt;
- HDC hdc;
- hdc = BeginPaint(hWnd,&pt); //开始重绘
-
- HBRUSH brush = CreateSolidBrush(RGB(0,255,0));
- HBRUSH oldBrush = (HBRUSH)SelectObject(hdc,brush);
- RECT rtWnd;
- GetWindowRect(hWnd,&rtWnd);
- int width = rtWnd.right-rtWnd.left;
- int height = rtWnd.bottom-rtWnd.top;
- POINT point;
- //填充顶部框架
- point.x = width;
- point.y = GetSystemMetrics(SM_CYFRAME)+1;
- PatBlt(hdc, 0, 0, point.x, point.y, PATCOPY);
- //填充左侧框架
- point.x = GetSystemMetrics(SM_CXFRAME);
- point.y = height;
- PatBlt(hdc, 0, 0, point.x, point.y, PATCOPY);
- //填充底部框架
- point.x = width;
- point.y = GetSystemMetrics(SM_CYFRAME) + 1;
- PatBlt(hdc, 0, height-point.y, point.x, point.y, PATCOPY);
- //填充右侧框架
- point.x = GetSystemMetrics(SM_CXFRAME);
- point.y = height;
- PatBlt(hdc, width-point.x, 0, point.x, point.y, PATCOPY);
- SelectObject(hdc,oldBrush);//恢复以前的画笔
- DeleteObject(brush);
- DeleteObject(oldBrush);
-
- EndPaint(hWnd, &pt); //停止重绘 不加这一句会造成死循环
- }
- }
- // 重画边框的具体代码
- void DrawBorder(HWND hWnd,int iWidth)
- {
- if (hWnd)
- {
- PAINTSTRUCT pt;
- HDC hdc;
- hdc = BeginPaint(hWnd,&pt); //开始重绘
-
- HBRUSH brush = CreateSolidBrush(RGB(0,255,0));
- HBRUSH oldBrush = (HBRUSH)SelectObject(hdc,brush);
- RECT rtWnd;
- GetWindowRect(hWnd,&rtWnd);
- int width = rtWnd.right-rtWnd.left;
- int height = rtWnd.bottom-rtWnd.top;
- //将位图复制到背景
- BitBlt(hdc, 0, 0, width, height, hdcBuffer, 0, 0, SRCCOPY);
- POINT point;
- //填充顶部框架
- point.x = width;
- point.y = iWidth;
- PatBlt(hdc, 0, 0, point.x, point.y, PATCOPY);
- //填充左侧框架
- point.x = iWidth;
- point.y = height;
- PatBlt(hdc, 0, 0, point.x, point.y, PATCOPY);
- //填充底部框架
- point.x = width;
- point.y = iWidth;
- PatBlt(hdc, 0, height-point.y, point.x, point.y, PATCOPY);
- //填充右侧框架
- point.x = iWidth;
- point.y = height;
- PatBlt(hdc, width-point.x, 0, point.x, point.y, PATCOPY);
- SelectObject(hdc,oldBrush);//恢复以前的画笔
- DeleteObject(brush);
- DeleteObject(oldBrush);
-
- EndPaint(hWnd, &pt); //停止重绘 不加这一句会造成死循环
- }
- }
- LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg) {
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- case WM_ACTIVATE:
- case WM_TOUCH:
- case WM_GESTURE:
- ReleaseCapture();
- return 0;
- case WM_WINDOWPOSCHANGED:
- {
- LPWINDOWPOS pPos = (LPWINDOWPOS)lParam;
- if (pPos->hwndInsertAfter != HWND_TOPMOST && pPos->hwndInsertAfter != HWND_TOP) {
- BringWindowToTop(hWnd);
- } else {
- return DefWindowProc(hWnd, msg, wParam, lParam);
- }
- }
- return 0;
- //return DefWindowProc(hWnd, msg, wParam, lParam);
- case WM_MBUTTONDOWN:
- case WM_MBUTTONUP:
- case WM_MBUTTONDBLCLK:
- case WM_LBUTTONDBLCLK:
- case WM_LBUTTONDOWN:
- case WM_LBUTTONUP:
- OutputDebugStringA("mouse clicked!");
- return 0;
- case WM_CLOSE:
- DestroyWindow(hWnd);
- return 0;
- case WM_PAINT:
- DrawBorder(hWnd, 2);
- return 0;
- default:
- return DefWindowProc(hWnd, msg, wParam, lParam);
- }
- }
- UINT WINAPI FaceRectThread(LPVOID pM)
- {
- WNDCLASSA wc = {0};
- ATOM a = 0;
- HWND hWnd = NULL;
- MSG msg;
- HINSTANCE hInst = ModuleBase::GetModuleBase()->GetInstance();
- CoInitialize(0);
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInst;
- //wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
- wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);//空画刷
- wc.hCursor = NULL;
- wc.hIcon = NULL;
- wc.lpfnWndProc = &WndProc;
- wc.lpszClassName = WNDCLS_NAME;
- wc.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
- a = RegisterClassA(&wc);
- if (a == 0)
- {
- Dbg("RegisterClassA is 0, error(%d)!", GetLastError());
- return 0;
- }
- hWnd = CreateWindowExA(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
- WNDCLS_NAME, NULL, WS_POPUP|WS_VISIBLE,
- 0, 0, 0, 0,
- NULL, NULL, hInst, NULL);
- hFaceRectWnd = hWnd;
- if (hWnd) {
- //设置背景透明
- SetWindowLongPtrA(hWnd,GWL_EXSTYLE,GetWindowLongPtrA(hWnd,GWL_EXSTYLE)^WS_EX_LAYERED);
- SetLayeredWindowAttributes(hWnd,RGB(0,0,0),0,LWA_COLORKEY); // 透明
- SetWindowPos(hWnd, HWND_TOP, 0, 0, 100, 100, SWP_HIDEWINDOW);
- //缓冲背景图
- HDC hdc = ::GetDC(hWnd);
- hdcBuffer = CreateCompatibleDC(hdc);
- RECT rtWnd;
- GetWindowRect(hWnd,&rtWnd);
- int width = rtWnd.right-rtWnd.left;
- int height = rtWnd.bottom-rtWnd.top;
- HBITMAP hBitMap = CreateCompatibleBitmap(hdc, width*2.5, height*2.5);
- ReleaseDC(hWnd, hdc);
- if(hBitMap != NULL)
- {
- SelectObject(hdcBuffer, hBitMap);
- }
- else
- {
- LOG_TRACE("hBitMap is NULL");
- }
- DrawBorder(hWnd,2);
- ShowCursor(FALSE);
- while (GetMessageA(&msg, NULL, NULL, NULL))
- {
- TranslateMessage(&msg);
- DispatchMessageA(&msg);
- }
- }
- else {
- Dbg("hWnd IS NULL!");
- }
- if (a) {
- UnregisterClassA(WNDCLS_NAME, hInst);
- }
- CoUninitialize();
- return 0;
- }
- BOOL RvcFaceCapturer::StartFaceCapture()
- {
- //发送告警信息
- LogWarn(Severity_High,Error_Debug,LOG_EVT_STARTACTIVECAPTURE,"FaceCapture Start");
- Dbg("[dbg] StartFaceCapture");
- if (m_hCaptureThread != NULL)
- {
- Dbg("[RvcFaceCapturer]: Waiting for the last capture task terminate...");
- StopFaceCapture();
- }
-
- ResetEvent(m_hEventWait);
- m_bCaptured = FALSE;
- m_bThreadRun = true;
- m_nSeconds = 0;
- m_strLastStatus = "";
- m_nLastEchoCamera = -1;
- m_nEchoCamera = 0;
- m_strStatus = "";
- m_hCaptureThread = (HANDLE)_beginthreadex(NULL,0,FaceCaptureThread,this,0,NULL);
- m_hFaceRectThread = (HANDLE)_beginthreadex(NULL, 0, &FaceRectThread, NULL, 0, NULL);
- ActiveDetectionStarted evt;
- CLivenessDetectionEntity *pDetection = dynamic_cast<CLivenessDetectionEntity *>(m_pDetection);
- DeviceTypeEnum deviceType = pDetection->GetDeviceType();
- if (ePadtype==deviceType||eMobilePadType==deviceType||eDesk2SType==deviceType)
- {
- //evt.Param="0@0@0@0@711@275@500@500"; // PAD或低柜双屏回显位置
- evt.Param="0@0@0@0@10@10@500@500"; // just for test
- m_EchoWinRect.left=10;
- m_EchoWinRect.top=10;
- m_EchoWinRect.right=m_EchoWinRect.left+500;
- m_EchoWinRect.bottom=m_EchoWinRect.top+500;
- }
- else
- {
- evt.Param="0@0@0@0@1838@290@600@600";
- m_EchoWinRect.left=1838;
- m_EchoWinRect.top=290;
- m_EchoWinRect.right=m_EchoWinRect.left+600;
- m_EchoWinRect.bottom=m_EchoWinRect.top+600;
- }
- SpSendBroadcast(pDetection->GetFunction(),SP_MSG_OF(ActiveDetectionStarted),SP_MSG_SIG_OF(ActiveDetectionStarted),evt);
- Dbg("[RvcFaceCapturer]: ActiveDetectionStarted has broadcasted.");
- return TRUE;
- }
- BOOL RvcFaceCapturer::StopFaceCapture()
- {
- //发送告警信息
- LogWarn(Severity_High,Error_Debug,LOG_EVT_STOPACTIVECAPTURE,"FaceCapture Stop");
- //关闭人脸框回显线程
- ::PostMessage(hFaceRectWnd,WM_CLOSE,NULL,NULL);//关掉人脸窗口
- WaitForSingleObject(m_hFaceRectThread, INFINITE);//等待当前线程执行完毕
- ReleaseDC(hFaceRectWnd, hdcBuffer);
- CloseHandle(m_hFaceRectThread);//关闭当前线程
- m_hFaceRectThread = NULL;
- hFaceRectWnd = NULL;
- hdcBuffer = NULL;
- SetEvent(m_hEventWait);
- m_bThreadRun = false;
- WaitForSingleObject(m_hCaptureThread, INFINITE);
- CloseHandle(m_hCaptureThread);
- m_hCaptureThread = NULL;
-
- ::Sleep(50); // 50ms后发出抓拍结束广播
- ActiveDetectionStopped evt;
- CLivenessDetectionEntity *pDetection = dynamic_cast<CLivenessDetectionEntity *>(m_pDetection);
- DeviceTypeEnum deviceType = pDetection->GetDeviceType();
- if (!(ePadtype==deviceType||eMobilePadType==deviceType||eDesk2SType==deviceType))
- {
- evt.Param="looklowerscreen.jpg";
- }
- SpSendBroadcast(pDetection->GetFunction(),SP_MSG_OF(ActiveDetectionStopped),SP_MSG_SIG_OF(ActiveDetectionStopped),evt);
- Dbg("[RvcFaceCapturer]: ActiveDetectionStopped has broadcasted.");
- return TRUE;
- }
- void RvcFaceCapturer::NotifyCameraFault()
- {
- if (m_hCaptureThread != NULL)
- {
- StopFaceCapture(); // 停止捕获 抛出异常
- DetectionStopUnExpected evt;
- evt.ActionID=CSimpleStringA2W(m_ActionID);
- evt.IsActive=true;
- evt.ErrorCode=CSimpleStringA2W("Error_CameraFault");
- evt.ErrorMsg=CSimpleStringA2W("摄像头故障,捕获已终止!");
- SpSendBroadcast(m_pDetection->GetFunction(),SP_MSG_OF(DetectionStopUnExpected),SP_MSG_SIG_OF(DetectionStopUnExpected),evt);
- Dbg("[RvcFaceCapturer]: DetectionStopUnExpected has broadcasted.");
- }
- }
- UINT RvcFaceCapturer::StartCapture()
- {
- Dbg("RvcFaceCapturer::StartCapture");
- RvcFaceCapStartCapture();
- int nFrameID=0;
- DWORD dwStart=GetTickCount();
- while (m_bThreadRun)
- {
- try
- {
- BOOL bGetVideo0 = FALSE;
- BOOL bGetVideo1 = FALSE;
- if (m_videoqueue0->GetVideoLens() > 0)
- {
- bGetVideo0 = GetVideoFrameByCameraID(0,videoframe0,0);
- if (bGetVideo0)
- {
- memcpy(m_bitmap0.bmBits,videoframe0->data,m_nImageSize);
- }
- else
- {
- Dbg("bGetvideo0=%d",bGetVideo0);
- }
- }
-
- if (m_videoqueue1)
- {
- if (m_videoqueue1->GetVideoLens() > 0)
- {
- bGetVideo1 = GetVideoFrameByCameraID(1,videoframe1,0);
- if (bGetVideo1)
- {
- memcpy(m_bitmap1.bmBits,videoframe1->data,m_nImageSize);
- }
- else
- {
- Dbg("bGetVideo1=%d",bGetVideo1);
- }
- }
- }
- if (bGetVideo0&&!bGetVideo1)
- {
- OnEchoCamera(0);
- }
- if (!bGetVideo0&&bGetVideo1)
- {
- OnEchoCamera(1);
- }
- if(!bGetVideo0&&!bGetVideo1)
- {
- //发送告警信息
- LogWarn(Severity_Middle, Error_Debug,LOG_EVT_ACTIVECAPTURENOVIDEO,"No VideoFrame in videoqueue");
- Dbg("No VideoFrame in videoqueue!!!!");
- }
- RVC_ImageData imageData;
- imageData.pImage = &m_bitmap0;
- imageData.nCameraID = 0;
- imageData.nFrameID = nFrameID;
- imageData.dTimeStamp = GetTickCount()/1000.0;
- // 摄像头采集成功,才送去捕获
- if (bGetVideo0)
- {
- //Dbg("[dbg] begin to call RvcFaceCapFeedFrame");
- RvcFaceCapFeedFrame(&imageData);
- //Dbg("[dbg] end to call RvcFaceCapFeedFrame");
- }
- if (m_videoqueue1)
- {
- imageData.pImage = &m_bitmap1;
- imageData.nCameraID = 1;
- if (bGetVideo1)
- {
- //Dbg("[dbg] begin to call RvcFaceCapFeedFrame");
- RvcFaceCapFeedFrame(&imageData);
- //Dbg("[dbg] end to call RvcFaceCapFeedFrame");
- }
- }
- RVC_TrackData trackdata;
- if (nFrameID >= 8 && 0 == RvcFaceCapGetTrackData(m_nEchoCamera,&trackdata))
- {
- CLivenessDetectionEntity *pDetection = dynamic_cast<CLivenessDetectionEntity *>(m_pDetection);
- DeviceTypeEnum deviceType = pDetection->GetDeviceType();
- if (ePadtype==deviceType||eMobilePadType==deviceType||eDesk2SType==deviceType)
- {
- // 镜像
- trackdata.rectFace.x = m_bitmap0.bmWidth - trackdata.rectFace.x - trackdata.rectFace.width;
- // 640x360->480x360->500x500
- trackdata.rectFace.x -= 80;
- if (trackdata.rectFace.x < 0)
- {
- trackdata.rectFace.width += trackdata.rectFace.x;
- trackdata.rectFace.x = 0;
- }
- if (trackdata.rectFace.x + trackdata.rectFace.width > m_bitmap0.bmWidth - 160)
- {
- trackdata.rectFace.width = m_bitmap0.bmWidth - 160 - trackdata.rectFace.x;
- }
- double x_ratio=1.0*(m_EchoWinRect.right-m_EchoWinRect.left)/480;
- double y_ratio=1.0*(m_EchoWinRect.bottom-m_EchoWinRect.top)/360;
- trackdata.rectFace.x*=x_ratio;
- trackdata.rectFace.y*=y_ratio;
- trackdata.rectFace.width*=x_ratio;
- trackdata.rectFace.height*=y_ratio;
- RECT rect;
- rect.left = m_EchoWinRect.left;
- rect.top = m_EchoWinRect.top;
- rect.left += trackdata.rectFace.x;
- rect.top += trackdata.rectFace.y;
- rect.right = rect.left+trackdata.rectFace.width;
- rect.bottom = rect.top+trackdata.rectFace.height;
- //DrawRectangle(GetDesktopWindow(), rect, 2, m_EchoWinRect);
- ::SetWindowPos(hFaceRectWnd, HWND_TOPMOST, rect.left,rect.top,
- rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW);
- }
- else
- {
- double ratio=1.0*(m_EchoWinRect.right-m_EchoWinRect.left)/m_bitmap0.bmWidth;
- trackdata.rectFace.x*=ratio;
- trackdata.rectFace.y*=ratio;
- trackdata.rectFace.width*=ratio;
- trackdata.rectFace.height*=ratio;
- int offset = (m_bitmap0.bmWidth-m_bitmap0.bmHeight)/2*ratio;
- RECT rect;
- if (0 == m_nEchoCamera)
- {
- rect.left = m_EchoWinRect.left;
- rect.top = m_EchoWinRect.top+offset;
- }
- else
- {
- rect.left = m_EchoWinRect.left+offset;
- rect.top = m_EchoWinRect.top;
- }
- rect.left += trackdata.rectFace.x;
- rect.top += trackdata.rectFace.y;
- rect.right = rect.left+trackdata.rectFace.width;
- rect.bottom = rect.top+trackdata.rectFace.height;
- //DrawRectangle(GetDesktopWindow(), rect, 2, m_EchoWinRect);
- ::SetWindowPos(hFaceRectWnd, HWND_TOPMOST, rect.left,rect.top,
- rect.right-rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW);
- }
- }
- else
- {
- SetWindowPos(hFaceRectWnd, HWND_TOP, 0, 0, 100, 100, SWP_HIDEWINDOW);
- }
- nFrameID++;
- if (m_bCaptured)
- {
- //抓拍成功
- SYSTEMTIME startTime;
- GetLocalTime(&startTime);
- LogWarn(Severity_High, Error_Debug, LOG_EVT_ACTIVECAPTURECOST,
- generateAlarmJson("LivenessDetection", formatTime(startTime).c_str(), GetTickCount()-dwStart).GetData());
- // send to rvcweb
- ActiveDetectionDone evt;
- evt.VerifyResult=CSimpleStringA2W("X");
- evt.ActionID=CSimpleStringA2W(m_ActionID);
- BITMAP bitmap;
- RVC_FaceRect face;
- RvcFaceCapGetImage(&bitmap,&face);
- Dbg("[OnFaceCapDone]:%d,%d,%d,%d",face.x,face.y,face.width,face.height);
- if (bitmap.bmBits!=NULL)
- {
- RECT roi={face.x,face.y,face.x+face.width,face.y+face.height};
- if (m_bSaveCaptureResult&&m_CaptureResultPath.GetLength()>0)
- {
- // save capture result
- char szPhoto[MAX_PATH]={0};
- sprintf(szPhoto,"%s%s_Capture.jpg",(LPCTSTR)m_CaptureResultPath,(LPCTSTR)m_ActionID);
- SaveLivenessPhoto(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,szPhoto);
- sprintf(szPhoto,"%s%s_Face.jpg",(LPCTSTR)m_CaptureResultPath,(LPCTSTR)m_ActionID);
- SaveLivenessPhoto(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,szPhoto,&roi);
- }
- int nJpgSize[2]={0};
- char *pJpg=new char[2*m_nImageSize];
- RGB2JPG(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,&nJpgSize[0],pJpg,m_pDetection,&roi);
- RGB2JPG(bitmap.bmBits,bitmap.bmWidth,bitmap.bmHeight,&nJpgSize[1],pJpg+nJpgSize[0],m_pDetection);
- if (nJpgSize[0]>0&&nJpgSize[1]>0)
- {
- int size = nJpgSize[0]+nJpgSize[1];
- evt.SnapShotPhotoLength = size;
- evt.SnapShotPhotoData.Alloc(size);
- memmove(evt.SnapShotPhotoData.m_pData, pJpg, size);
- evt.SnapShotPhotoData.Resize(size);
- evt.VerifyResult = CSimpleStringA2W(CSimpleStringA::Format("X|%d,%d,%d,%d,%d,%d,%d,%d,%d",
- nJpgSize[0],nJpgSize[1],MAX(m_nSeconds,1),bitmap.bmWidth,bitmap.bmHeight,roi.left,roi.top,roi.right-roi.left,roi.bottom-roi.top));
- }
- else
- {
- evt.SnapShotPhotoLength=0;
- }
-
- delete []pJpg; pJpg=NULL;
- }
- else
- {
- evt.SnapShotPhotoLength=0;
- }
- int nTimeUsed=m_nSeconds;
- int nLeastActiveShowTime=m_nLeastActiveShowTime;
- if (nTimeUsed<nLeastActiveShowTime)
- {
- ::PostMessage(hFaceRectWnd,WM_CLOSE,NULL,NULL);
- ::Sleep((nLeastActiveShowTime-nTimeUsed)*1000); // 保证至少进行nLeastActiveShowTime才结束并通知
- }
- SpSendBroadcast(m_pDetection->GetFunction(), SP_MSG_OF(ActiveDetectionDone), SP_MSG_SIG_OF(ActiveDetectionDone), evt);
- Dbg("[RvcFaceCapturer]: ActiveDetectionDone has broadcasted.");
- m_bThreadRun = false;
- StopFaceCaptureTask *task = new StopFaceCaptureTask(this);
- m_pDetection->GetFunction()->PostThreadPoolTask(task);
- continue;
- }
- if (GetTickCount()-dwStart>1000)
- {
- dwStart+=1000;
- m_nSeconds++;
- if (IsTimeOut())
- {
- m_bTimeout=true;
- m_bThreadRun=false;
- StopFaceCaptureTask *task = new StopFaceCaptureTask(this);
- m_pDetection->GetFunction()->PostThreadPoolTask(task);
- continue;
- }
- }
- }
- catch (...)
- {
- Dbg("[Captrue] exception occurred or stop capture.");
- FSleep(50);
- continue;
- }
- }
- m_hCaptureThread=0;
- RvcFaceCapStopCapture();
- if (m_bTimeout)
- {
- m_bTimeout = false;
- DetectionStopUnExpected evt;
- evt.IsActive=true;
- evt.ActionID=CSimpleStringA2W(m_ActionID);
- evt.ErrorCode=CSimpleStringA2W("Error_TimeOut");
- evt.ErrorMsg = CSimpleStringA2W("主动捕获超时,已终止!");
- SpSendBroadcast(m_pDetection->GetFunction(), SP_MSG_OF(DetectionStopUnExpected), SP_MSG_SIG_OF(DetectionStopUnExpected), evt);
- Dbg("[RvcFaceCapturer]: DetectionStopUnExpected has broadcasted.");
- }
-
- return 0;
- }
- ErrorCodeEnum RvcFaceCapturer::Init(CEntityBase* pDetection)
- {
- m_pDetection = pDetection;
- CSmartPointer<IEntityFunction> spEntityFunction = m_pDetection->GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum eErrDev;
- eErrDev = spEntityFunction->OpenConfig(Config_CenterSetting, spConfig);
- if (eErrDev == Error_Succeed)
- {
- SpIniMappingTable table;
- table.AddEntryInt("LivenessDetection","SaveLivenessResult",m_bSaveCaptureResult,FALSE);
- table.AddEntryInt("LivenessDetection","ActiveTimeLimit",m_nActiveTimeLimit,TIME_LIMIT_ACTIVE);
- table.AddEntryInt("LivenessDetection","LeastActiveShowTime",m_nLeastActiveShowTime,TIME_LEAST_ACTIVE_SHOW);
- table.AddEntryInt("LivenessDetection","SendInfoInterval",m_nSendInfoIntervalTime,TIME_INTERVAL_SENDINFO);
- eErrDev = table.Load(spConfig);
- if (eErrDev != Error_Succeed)
- {
- Dbg("fail to load centersettings!");
- m_bSaveCaptureResult = FALSE;
- m_nActiveTimeLimit = TIME_LIMIT_ACTIVE;
- m_nLeastActiveShowTime = TIME_LEAST_ACTIVE_SHOW;
- }
- 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;
- }
- }
- else
- {
- m_nActiveTimeLimit = TIME_LIMIT_ACTIVE;
- m_nLeastActiveShowTime = TIME_LEAST_ACTIVE_SHOW;
- m_nSendInfoIntervalTime = TIME_INTERVAL_SENDINFO;
- }
- if (m_bSaveCaptureResult)
- {
- eErrDev = m_pDetection->GetFunction()->GetPath("UploadPhoto", m_CaptureResultPath);
- if (eErrDev != Error_Succeed)
- {
- Dbg("fail to get UploadPhotoPath!");
- m_CaptureResultPath = "";
- }
- else
- {
- int len = m_CaptureResultPath.GetLength();
- if (m_CaptureResultPath.GetLength() > 0 && m_CaptureResultPath[len-1] != '\\') {
- m_CaptureResultPath += "\\";
- }
- }
- }
- 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 Error_Null;
- }
- if (m_VideoQueue0Name.length()>0)
- {
- m_videoqueue0 = new Clibvideoqueue(m_VideoQueue0Name.c_str());
- }
- else
- {
- m_videoqueue0 = NULL;
- }
- if (m_VideoQueue1Name.length()>0)
- {
- m_videoqueue1 = new Clibvideoqueue(m_VideoQueue1Name.c_str());
- }
- else
- {
- m_videoqueue1 = NULL;
- }
- //初始化视频buffer
- if (videoframe0->data == NULL)
- {
- videoframe0->data = new unsigned char[m_nImageSize];
- videoframe0->framesize = m_nImageSize;
- videoframe0->height = m_bitmap0.bmHeight;
- videoframe0->width = m_bitmap0.bmWidth;
- videoframe0->format = VIDEO_FORMAT_RGB24;
- }
- if (videoframe1->data == NULL)
- {
- videoframe1->data = new unsigned char[m_nImageSize];
- videoframe1->framesize = m_nImageSize;
- videoframe1->height = m_bitmap1.bmHeight;
- videoframe1->width = m_bitmap1.bmWidth;
- videoframe1->format = VIDEO_FORMAT_RGB24;
- }
- CSimpleStringA csBinPath;
- m_pDetection->GetFunction()->GetPath("Bin",csBinPath);
- CSimpleStringA dllName=csBinPath.Append("\\RvcFaceCapture.dll");
- Dbg("dllName:%s",(LPCTSTR)dllName);
- m_hInstance = ::LoadLibrary((LPCTSTR)dllName);
- if (m_hInstance)
- {
- RvcFaceCapInit = (tPfnInit)GetProcAddress(m_hInstance,"Init");
- RvcFaceCapStartCapture = (tPfnStartCapture)GetProcAddress(m_hInstance,"StartCapture");
- RvcFaceCapFeedFrame = (tPfnFeedFrame)GetProcAddress(m_hInstance,"FeedFrame");
- RvcFaceCapStopCapture = (tPfnStopCapture)GetProcAddress(m_hInstance,"StopCapture");
- RvcFaceCapGetImage = (tPfnGetImage)GetProcAddress(m_hInstance,"GetImage");
- RvcFaceCapUnInit = (tPfnUnInit)GetProcAddress(m_hInstance,"UnInit");
- RvcFaceCapGetTrackData = (tPfnGetTrackData)GetProcAddress(m_hInstance,"GetTrackData");
- if (!(RvcFaceCapInit&&RvcFaceCapStartCapture&&RvcFaceCapFeedFrame&&
- RvcFaceCapStopCapture&&RvcFaceCapGetImage&&RvcFaceCapUnInit&&RvcFaceCapGetTrackData))
- {
- Dbg("failed to get function from hinstance(%d)", GetLastError());
- return Error_Null;
- }
- else
- {
- Dbg("[dbg] begin to call RvcFaceCapInit");
- CSystemStaticInfo stStaticinfo;
- spEntityFunction->GetSystemStaticInfo(stStaticinfo);
- int rtn=RvcFaceCapInit(this, (LPCTSTR)stStaticinfo.strMachineType);
- Dbg("[dbg] end to call RvcFaceCapInit");
- if (rtn!=0)
- {
- return Error_Unexpect;
- }
- }
- }
- else
- {
- //发送告警信息
- CSimpleString msg = CSimpleString::Format("failed to load RvcFaceCapture dll(%d)", GetLastError());
- LogWarn(Severity_Middle, Error_Debug, LOG_EVT_ACTIVECAPTURELOADDEPFAIL, msg.GetData());
- Dbg(msg.GetData());
- return Error_Null;
- }
- //设置定时器超时时间
- m_timer.setWaitTime(m_nSendInfoIntervalTime);
- return Error_Succeed;
- }
- ErrorCodeEnum RvcFaceCapturer::UnInit()
- {
- 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 (RvcFaceCapUnInit)
- {
- Dbg("[dbg] begin to call RvcFaceCapUnInit");
- RvcFaceCapUnInit();
- Dbg("[dbg] end to call RvcFaceCapUnInit");
- }
- if (m_hInstance)
- {
- ::FreeLibrary(m_hInstance);
- m_hInstance = NULL;
- }
- return Error_Succeed;
- }
- BOOL RvcFaceCapturer::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;
- 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;
- 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 RvcFaceCapturer::FSleep( int ms )
- {
- DWORD dwRet = WaitForSingleObject(m_hEventWait, ms);
- if (dwRet == WAIT_OBJECT_0) // 如果等到信号
- {
- throw std::exception(); // 抛个异常表明是外界停止捕获
- }
- }
- bool RvcFaceCapturer::IsTimeOut()
- {
- return m_nSeconds >= m_nActiveTimeLimit;
- }
- void StopFaceCaptureTask::Process()
- {
- m_pCapture->StopFaceCapture();
- }
- }
|