浏览代码

Z991239-1621 #comment fea 解决人脸跟踪实体存在启动异常问题

陈礼鹏80274480 4 年之前
父节点
当前提交
a52b10e9fb
共有 2 个文件被更改,包括 100 次插入10 次删除
  1. 95 7
      Other/libfacecapture/libfacecapture.cpp
  2. 5 3
      Other/libfacecapture/libfacecapture.h

+ 95 - 7
Other/libfacecapture/libfacecapture.cpp

@@ -9,6 +9,8 @@
 #else
 #include "SpBase.h"
 #include <winpr/thread.h>
+#include <pthread.h>
+#include <semaphore.h>
 #endif
 
 #include <math.h>
@@ -199,16 +201,21 @@ public:
 		m_bCapFaceCompleted = FALSE;
 		m_pHostEvent = NULL;
 		m_eMonitorState = NoBody;
+
+#ifdef _RVC_OS_WIN
 		InitializeCriticalSection(&CS);
 
-		m_hEventWait= ::CreateEventA(NULL, TRUE, 0, 0);
-		if (!m_hEventWait) 
+		m_hEventWait = ::CreateEventA(NULL, TRUE, 0, 0);
+		if (!m_hEventWait)
 		{
 			*pResult = FALSE;
 			pHostAPI->Debug("create hEventWait failed!");
 			return;
 		}
-
+#else
+		pthread_mutex_init(&cs_mutex, NULL);
+		sem_init(&m_semt, 0, 0);
+#endif
 		memset(&m_stFaceConfig,0,sizeof(CFaceCaptureConfig));
 		memset(&m_stFaceRstStorage,0,sizeof(CFaceCaptureConfig));
 
@@ -263,7 +270,11 @@ public:
 
 	~Clibfacecapture_impl()
 	{
+#ifdef RVC_OS_WIN
 		DeleteCriticalSection(&CS);
+#else
+		pthread_mutex_destroy(&cs_mutex);
+#endif
 		if (m_pEnvironMinVideoQueue)
 		{
 			delete m_pEnvironMinVideoQueue;
@@ -332,9 +343,15 @@ public:
 	int m_nCameraStae;//0:无故障1:env故障,2:opt故障,3:全部故障
 
 private:
+#ifdef RVC_OS_WIN
 	HANDLE m_hEventWait; // CreateEvent
-
 	CRITICAL_SECTION CS;
+#else
+	pthread_mutex_t cs_mutex;
+	sem_t m_semt;
+
+#endif
+
 	CvHaarClassifierCascade* m_pFaceCascade;	//脸部分类器
 	CvHaarClassifierCascade* m_pEyeCascade;		//眼睛分类器
 	CvHaarClassifierCascade* m_pMouthCascade;	//嘴巴分类器
@@ -420,9 +437,18 @@ private:
 	//写人脸检测数据
 	BOOL WriteAllFaceInfo(CAllFaceInfo faceInfo)
 	{
+#ifdef RVC_OS_WIN
 		EnterCriticalSection(&CS);
+#else
+		pthread_mutex_lock(&cs_mutex);
+#endif
 		m_stFaceRstStorage = faceInfo;
-		LeaveCriticalSection(&CS);
+
+#ifdef RVC_OS_WIN
+		LeaveCriticalSection(&CS);;
+#else
+		pthread_mutex_unlock(&cs_mutex);
+#endif
 		return TRUE;
 	}
 	//计算人脸所处的区域
@@ -1898,11 +1924,28 @@ private:
 
 	void FSleep(int ms)
 	{
+#ifdef RVC_OS_WIN
 		DWORD dwRet = WaitForSingleObject(m_hEventWait, ms);
-		if (dwRet == WAIT_OBJECT_0) 
+		if (dwRet == WAIT_OBJECT_0)
 		{
 			throw std::exception();
 		}
+#else
+		struct timespec ts;
+		int ivalue = -1;
+		clock_gettime(CLOCK_REALTIME, &ts);
+		ts.tv_nsec += 1000 * 1000 * ms;
+		sem_getvalue(&m_semt, &ivalue);
+		m_pHostApi->Debug("%s:%d current sem value is %d.", __FUNCTION__, __LINE__, ivalue);
+		if (-1 == sem_timedwait(&m_semt, &ts)) {
+			if (ETIMEDOUT == errno) {
+			
+			}
+		}
+		else {
+			throw std::exception();
+		}
+#endif // RVC_OS_WIN
 	}
 
 	void ReleaseMotionTrackRst()
@@ -2050,7 +2093,6 @@ private:
 						}
 						else
 						{
-
 							WriteAllFaceInfo(m_stAllFaceInfo);
 							FSleep(m_stFaceConfig.nSleepMiddle);
 							continue;
@@ -2375,6 +2417,7 @@ public:
 		m_pHostApi = pHostAPI;
 		if (m_hVieoMonitorThread == NULL)
 		{
+			m_pHostApi->Debug("create video monitor thread.");
 			m_hVieoMonitorThread = (HANDLE)_beginthreadex(NULL, 0, VideoMonitorThread, (LPVOID)this, 0, (unsigned int*)&m_nVieoMonitorThreadId );
 		}
 		return TRUE;
@@ -2383,7 +2426,11 @@ public:
 	BOOL StopVideoMonitor()
 	{
 		m_bStopVieoMonitor = TRUE;
+#ifdef RVC_OS_WIN
 		SetEvent(m_hEventWait);
+#else
+		sem_post(&m_semt);
+#endif
 		WaitForSingleObject(m_hVieoMonitorThread, INFINITE);
 		CloseHandle(m_hVieoMonitorThread);
 		m_hVieoMonitorThread = NULL;
@@ -2509,15 +2556,27 @@ public:
 	//有多少其他用户
 	BOOL GetCustomerNum(UINT&num)
 	{
+#ifdef RVC_OS_WIN
 		EnterCriticalSection(&CS);
+#else
+		pthread_mutex_lock(&cs_mutex);
+#endif
 		num = m_stFaceRstStorage.nTatolFaceNum;
+#ifdef RVC_OS_WIN
 		LeaveCriticalSection(&CS);
+#else
+		pthread_mutex_unlock(&cs_mutex);
+#endif
 		return TRUE;
 	}
 	//获取主用户的常规属性
 	BOOL GetMainCustomerStatus(CUUID&FaceID,CCustomerStatus &Property)
 	{
+#ifdef RVC_OS_WIN
 		EnterCriticalSection(&CS);
+#else
+		pthread_mutex_lock(&cs_mutex);
+#endif
 		//Property.eBrightness				= m_stFaceRstStorage.astFaceInfo[0].eBrightness;
 		Property.stCustomerCover			= m_stFaceRstStorage.astFaceInfo[0].stCustomerCover;
 		Property.stCustomerPos.eCamera		= m_stFaceRstStorage.astFaceInfo[0].eCamera;//1
@@ -2528,14 +2587,22 @@ public:
 		Property.stCustomerPos.stRange.sY	= m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.y			+(short)m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.height/2-(short)m_nImgHeight/2;
 		Property.stCustomerPos.stRange.sH	= (short)m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.height/2;
 		FaceID	= m_stFaceRstStorage.astFaceInfo[0].FaceID;
+#ifdef RVC_OS_WIN
 		LeaveCriticalSection(&CS);
+#else
+		pthread_mutex_unlock(&cs_mutex);
+#endif	
 		return TRUE;
 	}
 
 	//获取用户的常规属性
 	BOOL GetCustomerStatus(CUUID FaceID,CCustomerStatus &Property)
 	{
+#ifdef RVC_OS_WIN
 		EnterCriticalSection(&CS);
+#else
+		pthread_mutex_lock(&cs_mutex);
+#endif
 		BOOL bSearch = FALSE;
 		for (int i =0;i<m_stFaceRstStorage.nTatolFaceNum&&!bSearch;i++)
 		{
@@ -2555,25 +2622,41 @@ public:
 				bSearch = TRUE;
 			}
 		}
+#ifdef RVC_OS_WIN
 		LeaveCriticalSection(&CS);
+#else
+		pthread_mutex_unlock(&cs_mutex);
+#endif
 		return TRUE;
 	}
 
 	//获取所有用户FACEID
 	BOOL GetTrackFace(CUUID *arrFaceIDs, int *size)
 	{
+#ifdef RVC_OS_WIN
 		EnterCriticalSection(&CS);
+#else
+		pthread_mutex_lock(&cs_mutex);
+#endif
 		for (int i = 0;i<m_stFaceRstStorage.nTatolFaceNum && i < *size;i++)
 		{
 			arrFaceIDs[i] = m_stFaceRstStorage.astFaceInfo[i].FaceID;
 		}
+#ifdef RVC_OS_WIN
 		LeaveCriticalSection(&CS);
+#else
+		pthread_mutex_unlock(&cs_mutex);
+#endif
 		return TRUE;
 	}
 	//获取用户的交互属性
 	BOOL GetCustomerAppearance(CUUID FaceID,CCustomerAppearance &Property)
 	{
+#ifdef RVC_OS_WIN
 		EnterCriticalSection(&CS);
+#else
+		pthread_mutex_lock(&cs_mutex);
+#endif
 		BOOL bSearch = FALSE;
 		for (int i =0;i<m_stFaceRstStorage.nTatolFaceNum&&!bSearch;i++)
 		{
@@ -2585,7 +2668,12 @@ public:
 				bSearch = TRUE;
 			}
 		}
+#ifdef RVC_OS_WIN
 		LeaveCriticalSection(&CS);
+#else
+		pthread_mutex_unlock(&cs_mutex);
+#endif
+		
 		return TRUE;
 	}
 };

+ 5 - 3
Other/libfacecapture/libfacecapture.h

@@ -13,9 +13,11 @@
 #else
 #define LIBFACECAPTURE_API __declspec(dllimport)
 #endif
-#else
-#define LIBFACECAPTURE_API	 
-#endif
+# elif ( defined(__GNUC__) &&  __GNUC__ >= 4 )
+#define LIBFACECAPTURE_API __attribute__((visibility("default")))
+#else // RVC_OS_WIN
+#define LIBFACECAPTURE_API
+#endif // RVC_OS_WIN
 
 
 #ifndef RVC_OS_WIN