mod_facetracking.cpp 12 KB

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