1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027 |
- #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)
- {
- //发送告警信息
- LogError(Severity_High,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());
- LogError(Severity_High,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();
- }
- std::string RvcFaceCapturer::formatTime(SYSTEMTIME time)
- {
- char tBuf[1024] = "";
- sprintf(tBuf, "%04u-%02u-%02u %02u:%02u:%02u:%03u", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
- return tBuf;
- }
- CSimpleString RvcFaceCapturer::generateAlarmJson(CSimpleString entityName, CSimpleString startTime, int cost)
- {
- return CSimpleString::Format("[{\"name\":\"%s\",\"time\":\"%s\",\"cost\":%d}]", entityName.GetData(), startTime.GetData(), cost);
- }
- }
|