mod_facetracking.cpp 12 KB

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