mod_facetracking.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpIni.h"
  4. #include "rvc_media_common.h"
  5. #include "libvideoqueue.h"
  6. #include "libfacecapture.h"
  7. #include "y2k_time.h"
  8. #include "sysvar.h"
  9. #include "Event.h"
  10. #include "fileutil.h"
  11. #include "EventCode.h"
  12. #include "../mod_mediacontroller/Event.h"
  13. #ifndef MAX_LOG_LEN
  14. #define MAX_LOG_LEN 512
  15. #endif
  16. #ifndef rvc_snprintf
  17. #ifdef RVC_OS_WIN
  18. #define rvc_snprintf _snprintf
  19. #else
  20. #define rvc_snprintf snprintf
  21. #endif // RVC_OS_WIN
  22. #endif // rvc_snprintf
  23. // 脸部跟踪&拍照 devel: 0x302
  24. static struct {
  25. int monitor_id;
  26. int evt_code;
  27. char* evt_msg;
  28. } monitor_id_evtmsg[] = {
  29. {0, EVENT_MOD_CUSTOMER_CLOSE, "客户接近 1.5m"},
  30. {1, EVENT_MOD_CUSTOMER_LEAVE, "客户消失或退出到接近距离1.5M之外"},
  31. {2, EVENT_MOD_CUSTOMER_ENTEROPERATE, "客户进入操作距离0.5m"},
  32. {3, EVENT_MOD_CUSTOMER_BACKTOCLOSE, "客户退回到接近距离"},
  33. {4, EVENT_MOD_CUSTOMER_APPEAR, "有人出现"},
  34. {5, EVENT_MOD_CUSTOMER_CHANGE, "客户已经换人"},
  35. {6, EVENT_MOD_CUSTOMER_CAPTUREFACE, "捕获脸部事件"},
  36. {7, EVENT_MOD_CUSTOMER_LOSEFACE, "失去脸部事件"},
  37. {8, EVENT_MOD_FACE_BREAKDOWN,"the opencv library breakdown"},
  38. {9, EVENT_MOD_NODETECT_BODY,"未检测到人脸"},
  39. };
  40. class CFaceTrackingEntity : public CEntityBase, public CHostApi , public CVideoMonitorEvent, public ISysVarListener, public ITimerListener,public ILogListener
  41. {
  42. public:
  43. CFaceTrackingEntity() : m_facecapture(NULL), bIsSessionChange(false),strCustomerID(false),strSessionID(false),bIsCustomerChange(false), m_bSingleCamera(false)
  44. {
  45. // note: this object initialize at DllMain, so it suggests keep your code simple here.
  46. // 1) do simple initializing here
  47. // 2) dont do complex operation, complex operation such as create new process(thread) and so on
  48. }
  49. virtual ~CFaceTrackingEntity() {}
  50. virtual const char *GetEntityName() const { return "FaceTracking"; }
  51. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  52. {
  53. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  54. ErrorCodeEnum Error;
  55. bool bRet = false;
  56. m_facecapture = new Clibfacecapture(&bRet, this, this, REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  57. if (!bRet) {
  58. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load libface capture failed!");
  59. pTransactionContext->SendAnswer(Error_Resource);
  60. return;
  61. }
  62. Error = GetFunction()->RegistSysVarEvent("SessionID", this);
  63. if (Error != Error_Succeed)
  64. {
  65. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "SessionID");
  66. }
  67. Error = GetFunction()->RegistSysVarEvent("CustomerID", this);
  68. if (Error != Error_Succeed)
  69. {
  70. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "CustomerID");
  71. }
  72. Error = GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE, this);
  73. if (Error != Error_Succeed)
  74. {
  75. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", SYSVAR_CAMERASTATE);
  76. }
  77. CSimpleStringA strValue;
  78. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  79. if (strValue[0] == 'N')
  80. {
  81. m_facecapture->SetCameraState(0);
  82. }
  83. else if (strValue[0] == 'E')
  84. {
  85. m_facecapture->SetCameraState(1);
  86. }
  87. else if (strValue[0] == 'O')
  88. {
  89. m_facecapture->SetCameraState(2);
  90. }
  91. else if (strValue[0] == 'B')
  92. {
  93. m_facecapture->SetCameraState(3);
  94. }
  95. //is Pad Version
  96. CSystemStaticInfo stStaticinfo;
  97. spFunction->GetSystemStaticInfo(stStaticinfo);
  98. if (stricmp(stStaticinfo.strMachineType,"RVC.Stand1SPlus")==0){
  99. m_bSingleCamera = true;
  100. }
  101. spFunction->SubscribeLog(m_UUid1, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_HEADLIGHT_GREEN_OFF,NULL,false);
  102. spFunction->SubscribeLog(m_UUid2, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED,NULL,false);
  103. spFunction->SubscribeLog(m_UUid3, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED,NULL,false);
  104. spFunction->SubscribeLog(m_UUid4, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_RESET_ACTIVECAMERA, NULL, false);
  105. pTransactionContext->SendAnswer(Error);
  106. }
  107. virtual void OnStarted()
  108. {
  109. LogEvent(Severity_Middle, LOG_EVT_MOD_FACETRACKING_STARTED_SUCCESS, "facetracking entity started successfully.");
  110. }
  111. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  112. {
  113. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  114. delete m_facecapture;
  115. m_facecapture = NULL;
  116. spFunction->UnsubscribeLog(m_UUid1);
  117. spFunction->UnsubscribeLog(m_UUid2);
  118. spFunction->UnsubscribeLog(m_UUid3);
  119. spFunction->UnsubscribeLog(m_UUid4);
  120. spFunction->UnregistSysVarEvent("SessionID");
  121. spFunction->UnregistSysVarEvent("CustomerID");
  122. spFunction->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
  123. pTransactionContext->SendAnswer(Error_Succeed);
  124. }
  125. int TransCameraStateToInt(char cData)
  126. {
  127. int iRet = 0;
  128. if ('E' == cData){
  129. iRet = 1;
  130. }
  131. else if('O' == cData){
  132. iRet = 2;
  133. }
  134. else if('B' == cData){
  135. iRet = 3;
  136. }
  137. return iRet;
  138. }
  139. void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  140. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  141. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo)
  142. {
  143. switch (dwUserCode)
  144. {
  145. case LOG_EVT_HEADLIGHT_GREEN_OFF:
  146. m_facecapture->SetLightChange();
  147. break;
  148. case LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED:
  149. {
  150. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED event");
  151. CSimpleStringA strValue;
  152. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  153. int iState = TransCameraStateToInt(strValue[0]);
  154. if (3 != iState) {
  155. m_facecapture->SetCameraState(iState);
  156. m_facecapture->StartFaceCapture();
  157. }
  158. else {
  159. m_facecapture->SetCameraState(3);
  160. }
  161. if (!m_bSingleCamera) {
  162. GetFunction()->SetTimer(1, this, 1000);
  163. }
  164. }
  165. break;
  166. case LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED:
  167. {
  168. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED event");
  169. m_facecapture->StopFaceCapture();
  170. CSimpleStringA strValue;
  171. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  172. int iState = TransCameraStateToInt(strValue[0]);
  173. m_facecapture->SetCameraState(iState);
  174. if (!m_bSingleCamera) {
  175. GetFunction()->KillTimer(1);
  176. }
  177. GetFunction()->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
  178. }
  179. break;
  180. case LOG_EVT_MEDIACONTROLLER_RESET_ACTIVECAMERA:
  181. {
  182. CSimpleStringA strValue;
  183. GetFunction()->GetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, strValue); // E or O
  184. if ('O' == strValue[0]) {
  185. GetFunction()->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
  186. }
  187. }
  188. break;
  189. default:
  190. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown event.");
  191. break;
  192. }
  193. }
  194. //////////////////////////////////////////////////////////////////////////////////////////////////////
  195. // ITimerListener implementation
  196. virtual void OnTimeout(DWORD dwTimerID)
  197. {
  198. uint64_t uid;
  199. CCustomerStatus status;
  200. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("before GetMainCustomerStatus");
  201. if (m_facecapture->GetMainCustomerStatus(uid, status))
  202. {
  203. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  204. CSimpleStringA strValue;
  205. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("OnTimeout, check value, eCamera = %d", status.stCustomerPos.eCamera);
  206. spFunction->GetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, strValue); // E or O
  207. if (status.stCustomerPos.eCamera == EnvironCamera)
  208. {
  209. if (strValue[0] == 'O')
  210. {
  211. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("ActiveTrackingCamera change from Opt->Env!");
  212. spFunction->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
  213. }
  214. }
  215. else
  216. {
  217. if (strValue[0] == 'E')
  218. {
  219. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("ActiveTrackingCamera change from Env->Opt!");
  220. spFunction->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_OPT); // from Environment -> Operation
  221. }
  222. }
  223. }
  224. else
  225. {
  226. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("GetMainCustomerStatus failed!");
  227. }
  228. }
  229. //////////////////////////////////////////////////////////////////////////////////////////////////////
  230. // CHostApi implementation
  231. virtual uint64_t GenerateUUID()
  232. {
  233. m_lastUUID = CUUID::Create(m_lastUUID);
  234. return m_lastUUID;
  235. }
  236. virtual bool IsCustomerChange()
  237. {
  238. bool bChange = bIsSessionChange||bIsCustomerChange;
  239. return bChange;
  240. }
  241. virtual void GetFaceImgName(char* FaceName, uint32_t uLen)
  242. {
  243. CSimpleStringA strPath;
  244. ErrorCodeEnum Error = GetFunction()->GetPath("UploadPhoto", strPath);
  245. if (Error == Error_Succeed)
  246. {
  247. if (((const char*)strCustomerID == NULL)||(_stricmp(strCustomerID,"N") == 0))
  248. {
  249. rvc_snprintf(FaceName, uLen,"%s%s%s_0", strPath.GetData(), SPLIT_SLASH_STR, strSessionID.GetData());
  250. }
  251. else
  252. {
  253. rvc_snprintf(FaceName, uLen, "%s%s%s_%s", strPath.GetData(), SPLIT_SLASH_STR, strSessionID.GetData(), strCustomerID.GetData());
  254. }
  255. bIsSessionChange = false;
  256. bIsCustomerChange = false;
  257. }
  258. else
  259. {
  260. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("getpath uploadphoto failed!");
  261. }
  262. }
  263. virtual bool LoadConfig(CFaceCaptureConfig &config)
  264. {
  265. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  266. CSmartPointer<IConfigInfo> spConfig;
  267. ErrorCodeEnum Error(Error_Succeed);
  268. /*
  269. PrimCamera=1
  270. UpCameraEdgeLimit=3
  271. DownCameraEdgeLimit=2
  272. ServersType=0
  273. ContourMinAera=14400
  274. FaceDataDirPath=facedata
  275. OperateFaceSize=4.2
  276. CloseFaceSize=6.6
  277. FarFaceSize=8.2
  278. SearchFaceSize=8.2
  279. DetectFaceSize=6.6
  280. FaceSizeOffset=18
  281. SleepLong=800
  282. SleepMiddle=600
  283. SleepShort=500
  284. ThresholdNum=10
  285. */
  286. CSimpleStringA strPath;
  287. config.nServersType = 0;
  288. GetFunction()->GetPath("cfg", strPath);
  289. strcpy(config.strFaceDataDirPath, strPath);
  290. strcat(config.strFaceDataDirPath, SPLIT_SLASH_STR "facedata");
  291. config.nPrimCamera = 1;
  292. config.nContourMinAera = 14400;
  293. config.nUpCameraEdgeLimit = 3;
  294. config.nDownCameraEdgeLimit = 2;
  295. config.fOperateFaceSize = 4.2f;
  296. config.fCloseFaceSize = 6.6f;
  297. config.fFarFaceSize = 8.2f;
  298. config.fSearchFaceSize = 8.2f;
  299. config.fDetectFaceSize = 6.6f;
  300. config.nFaceSizeOffset = 18;
  301. config.nSleepLong = 800;
  302. config.nSleepMiddle = 600;
  303. config.nSleepShort = 500;
  304. config.nThresholdNum = 10;
  305. Error = Error_Succeed;
  306. return Error == Error_Succeed;
  307. }
  308. virtual void Debug(facecap_loglevel elevel, const char *fmt, ...)
  309. {
  310. va_list arg;
  311. va_start(arg, fmt);
  312. int n = vsnprintf(NULL, 0, fmt, arg);
  313. if (n >= MAX_LOG_LEN) {
  314. char* buf = (char*)malloc((size_t)(n + 1));
  315. vsnprintf(buf, n + 1, fmt, arg);
  316. DbgWithLink((LOG_LEVEL_E)elevel, LOG_TYPE_SYSTEM)("%s", buf);
  317. free(buf);
  318. }
  319. else{
  320. char strlog[MAX_LOG_LEN] = {0};
  321. vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  322. DbgWithLink((LOG_LEVEL_E)elevel, LOG_TYPE_SYSTEM)("%s", strlog);
  323. }
  324. va_end(arg);
  325. }
  326. ////////////////////////////////////////////////////////////////////////////////////
  327. // CVideoMonitorEvent
  328. void GenerateMonitorEvent(video_monitor_event_type eType)
  329. {
  330. LogEvent(Severity_Middle, monitor_id_evtmsg[eType].evt_code, monitor_id_evtmsg[eType].evt_msg);
  331. }
  332. private:
  333. virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  334. {
  335. if (_stricmp(pszKey,"SessionID") == 0)
  336. {
  337. GetFunction()->GetSysVar("SessionID",strSessionID);
  338. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sessionID change to %s",strSessionID);
  339. bIsSessionChange = true;
  340. }
  341. if (_stricmp(pszKey,"CustomerID") == 0)
  342. {
  343. GetFunction()->GetSysVar("CustomerID",strCustomerID);
  344. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("customerID change to %s",strCustomerID);
  345. bIsCustomerChange = true;
  346. }
  347. if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
  348. {
  349. CSimpleStringA str;
  350. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE,str);
  351. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("customerID change to %s",strCustomerID);
  352. if (str == 'N')
  353. {
  354. m_facecapture->SetCameraState(0);
  355. }
  356. else if (str == 'E')
  357. {
  358. m_facecapture->SetCameraState(1);
  359. }
  360. else if (str == 'O')
  361. {
  362. m_facecapture->SetCameraState(2);
  363. }
  364. else if (str == 'B')
  365. {
  366. m_facecapture->SetCameraState(3);
  367. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set Camera State to 3");
  368. }
  369. }
  370. }
  371. private:
  372. CUUID m_UUid1,m_UUid2,m_UUid3,m_UUid4;
  373. bool bIsSessionChange;
  374. bool bIsCustomerChange;
  375. CSimpleStringA strCustomerID;
  376. CSimpleStringA strSessionID;
  377. CUUID m_lastUUID;
  378. Clibfacecapture *m_facecapture;
  379. bool m_bSingleCamera;
  380. };
  381. SP_BEGIN_ENTITY_MAP()
  382. SP_ENTITY(CFaceTrackingEntity)
  383. SP_END_ENTITY_MAP()