libfacecapture.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  1. // libfacecapture.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "libfacecapture.h"
  4. #include <string.h>
  5. #ifdef _WIN32
  6. #include <atlstr.h>
  7. #include <windows.h>
  8. #else
  9. #include <pthread.h>
  10. #include <semaphore.h>
  11. #include <errno.h>
  12. #endif
  13. #include <math.h>
  14. #include <time.h>
  15. #include "cv.h"
  16. #include "highgui.h"
  17. #include "libvideoqueue.h"
  18. #define SLASH '/'
  19. #define BACK_SLASH '\\'
  20. #define MB_SLASH_STR "/"
  21. #define MB_BACK_SLASH_STR "\\"
  22. #define W_SLASH_STR L"/"
  23. #define W_BACK_SLASH_STR L"\\"
  24. #if defined(UNICODE) || defined(_UNICODE)
  25. #define SLASH_STR W_SLASH_STR
  26. #define BACK_SLASH_STR W_BACK_SLASH_STR
  27. #else
  28. #define SLASH_STR MB_SLASH_STR
  29. #define BACK_SLASH_STR MB_BACK_SLASH_STR
  30. #endif
  31. #ifdef _WIN32
  32. #define SPLIT_SLASH BACK_SLASH
  33. #define SPLIT_SLASH_STR BACK_SLASH_STR
  34. #define MB_SPLIT_SLASH_STR MB_BACK_SLASH_STR
  35. #define W_SPLIT_SLASH_STR W_BACK_SLASH_STR
  36. #define LINE_BREAK_STR "\r\n"
  37. #else
  38. #define SPLIT_SLASH SLASH
  39. #define SPLIT_SLASH_STR SLASH_STR
  40. #define MB_SPLIT_SLASH_STR MB_SLASH_STR
  41. #define W_SPLIT_SLASH_STR W_SLASH_STR
  42. #define LINE_BREAK_STR "\n"
  43. #endif
  44. #define CASCADE_FACE "haarcascade_frontalface_alt.xml"
  45. #define CASCADE_EYE "haarcascade_mcs_eyepair_small.xml"
  46. #define CASCADE_NOSE "haarcascade_mcs_nose.xml"
  47. #define CASCADE_MOUTH "haarcascade_mcs_mouth.xml"
  48. #define MAX_FACE_NUM 6 //最多保留6张人脸
  49. #define RGB_IMG 1 //RGB格式的图像
  50. //将图像区域分成9宫格,此枚举用来表示人脸所处9宫格的区域
  51. enum FaceRegionEnum
  52. {
  53. FaceError = 0,
  54. FaceRegionLeftup, //左上角
  55. FaceRegionUp, //正上方
  56. FaceRegionRightUP, //右上角
  57. FaceRegionLeft, //左边
  58. FaceRegionCenter, //中央
  59. FaceRegionRight, //右边
  60. FaceRegionLeftDn, //左下角
  61. FaceRegionDn, //正下方
  62. FaceRegionRightDn, //右下角
  63. };
  64. //运动跟踪结果
  65. struct CMotionTrackRslt
  66. {
  67. int nObjNum; //当前图像中运动物体的数量
  68. CvRect ObjRect[MAX_FACE_NUM]; //存放运动物体
  69. };
  70. //客户位置
  71. struct CCustomerRegion
  72. {
  73. FaceRegionEnum eRegion; //人脸中心点在图像9宮格中属于哪个宮格
  74. CvRect stFaceRect; //脸部矩形,使用crect表示人脸在图像中精确位置
  75. CvRect stUpperBodyRect; //脸部剪辑矩形,使用crect表示人脸剪辑区域
  76. };
  77. //客户信息,程序内部逻辑使用
  78. struct CCustomerInfo
  79. {
  80. uint64_t FaceID; //人脸ID号
  81. CameraEnum eCamera; //1、2号摄像头
  82. CCustomerRegion stRegion; //客户位置,客户脸部的矩形区域
  83. SceneEnum eScene; //0不确定,1开始锁定、2失去锁定、3晃动、4向前、5向后
  84. DistanceEnum eDistance; //0不确定,1操作距离,2近端,3远端
  85. PoseEnum ePose; //0不确定,1-站立,2-坐立
  86. CCover stCustomerCover; //覆盖属性
  87. BrightnessEnum eBrightness; //人脸亮度
  88. SexEnum eSex; //0不知道,1女,2男
  89. YoungOldEnum eYoungOld; //0不知道,1小孩,2年轻人,3中年人,4老人
  90. HeightEnunm eHeight; //0不知道,1矮,2中等,3高
  91. };
  92. //计算过程中用于存储人脸的缓存
  93. struct CAllFaceInfo
  94. {
  95. int nTatolFaceNum; //当前图像帧中人脸数量
  96. CCustomerInfo astFaceInfo[MAX_FACE_NUM]; //所有用户的人脸,astFaceInfo[0]为主用户的人脸
  97. };
  98. //图像亮度调整参数
  99. struct CBrightAdjParam
  100. {
  101. double fLow; //映射前低值
  102. double fHigh; //映射前高值
  103. double fBottom; //映射后低值
  104. double fTop; //映射后高值
  105. double fGamma; //gamma变换值
  106. };
  107. //用于本地保存人脸的结构体
  108. struct CLocalFaceInfo
  109. {
  110. CCover stFaceCover; //人脸覆盖
  111. CCustomerRegion stCustomerRegion; //人脸位置
  112. IplImage*img; //人脸图像
  113. };
  114. enum MonitorStateEnum
  115. {
  116. NoBody = 0, //当前没有人
  117. SomebodyFar, //远距离有人
  118. SomebodyClose, //有人在靠近
  119. SomebodyOperate //客户进入操作区域
  120. };
  121. class Clibfacecapture_impl
  122. {
  123. public:
  124. Clibfacecapture_impl(bool *pResult, CHostApi *pHostAPI, CVideoMonitorEvent*pHost, const char* EnvironVideoName, const char* OperateVideoName = NULL, MonitorEnum eMonitorType = MotionTarckAndFaceDetect)
  125. {
  126. m_pResult = pResult;
  127. m_pHostApi = pHostAPI;
  128. m_pHostEvent = pHost;
  129. m_eType = eMonitorType;
  130. m_pFaceCascade = NULL; //脸部分类器
  131. m_pEyeCascade = NULL; //眼睛分类器
  132. m_pMouthCascade = NULL; //嘴巴分类器
  133. m_pNoseCascade = NULL; //鼻子分类器
  134. m_pFaceStorage = NULL;
  135. pfaceSeq = NULL;
  136. m_pProcessImg = NULL;
  137. #ifdef _WIN32
  138. m_hVieoMonitorThread = NULL;
  139. m_nVieoMonitorThreadId = 0;
  140. #else
  141. m_videomonitorthreadid = 0;
  142. #endif
  143. m_bStopVieoMonitor = true;
  144. m_pEnvironVideoQueue = NULL;
  145. m_pOperatorVideoQueue = NULL;
  146. m_nImgWidth = 0;
  147. m_nImgHeight = 0;
  148. m_pMhImg = NULL;
  149. m_pMhBuf = NULL;
  150. m_bLightChange = false;
  151. m_nCameraState = 0;
  152. m_nLastBuf = 0;
  153. m_eMonitorState = NoBody;
  154. memset(m_envqueuename, 0, MAX_PATH);
  155. memset(m_optqueuename, 0, MAX_PATH);
  156. if (NULL != EnvironVideoName){
  157. strncpy(m_envqueuename, EnvironVideoName, (strlen(EnvironVideoName) > MAX_PATH) ? (MAX_PATH - 1) : strlen(EnvironVideoName));
  158. }
  159. if (NULL != OperateVideoName) {
  160. strncpy(m_optqueuename, OperateVideoName, (strlen(OperateVideoName) > MAX_PATH) ? (MAX_PATH - 1) : strlen(OperateVideoName));
  161. }
  162. #ifdef _WIN32
  163. InitializeCriticalSection(&CS);
  164. m_hStopEventWait= ::CreateEventA(NULL, TRUE, 0, 0);
  165. if (!m_hStopEventWait) {
  166. *pResult = false;
  167. pHostAPI->Debug(FACECAP_INFO, "create hEventWait failed!");
  168. return;
  169. }
  170. #else
  171. pthread_mutex_init(&cs_mutex, NULL);
  172. sem_init(&m_semt, 0, 0);
  173. #endif
  174. memset(&m_stFaceConfig,0,sizeof(CFaceCaptureConfig));
  175. memset(&m_stFaceRstStorage,0,sizeof(CFaceCaptureConfig));
  176. //读取配置文件
  177. *pResult = pHostAPI->LoadConfig(m_stFaceConfig);
  178. if (!*pResult){
  179. pHostAPI->Debug(FACECAP_INFO, "Load Configuration failed!");
  180. return;
  181. }
  182. *pResult = InitFaceCapture(m_stFaceConfig.strFaceDataDirPath);
  183. if (!*pResult){
  184. return;
  185. }
  186. for (int i = 0; i < MAX_FACE_NUM; i++) {
  187. m_PreFaceRect[i] = cvRect(0, 0, 0, 0);
  188. }
  189. memset(&m_stAllFaceInfo, 0, sizeof(CAllFaceInfo));
  190. //初始化剪辑区域为屏幕中央区域
  191. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect = cvRect(160, 60, 320, 240);
  192. memset(&m_stObjTrackRslt, 0, sizeof(CMotionTrackRslt));
  193. memset(&m_stBrightAdjParam, 0, sizeof(CBrightAdjParam));
  194. *pResult = WriteAllFaceInfo(m_stAllFaceInfo);
  195. if (!*pResult) {
  196. return;
  197. }
  198. }
  199. bool StartFaceCapture()
  200. {
  201. if (!m_bStopVieoMonitor) {
  202. m_pHostApi->Debug(FACECAP_INFO, "facetracking thread is already start.");
  203. return true;
  204. }
  205. m_pEnvironVideoQueue = new Clibvideoqueue(m_envqueuename);
  206. if (strlen(m_optqueuename)) {
  207. m_pOperatorVideoQueue = new Clibvideoqueue(m_optqueuename);
  208. }
  209. else {
  210. m_pOperatorVideoQueue = NULL;
  211. }
  212. //开始视频监控线程
  213. return StartVideoMonitor(m_pHostApi, m_pHostEvent, m_eType);
  214. }
  215. bool StopFaceCapture()
  216. {
  217. if (m_bStopVieoMonitor) {
  218. m_pHostApi->Debug(FACECAP_INFO, "facetracking thread is already stop.");
  219. return true;
  220. }
  221. if (true == StopVideoMonitor()){
  222. m_pHostApi->Debug(FACECAP_DEBUG, "stop video monitor thread success!");
  223. }
  224. if (NULL != m_pEnvironVideoQueue){
  225. delete m_pEnvironVideoQueue;
  226. m_pEnvironVideoQueue = NULL;
  227. }
  228. if (NULL != m_pOperatorVideoQueue){
  229. delete m_pOperatorVideoQueue;
  230. m_pOperatorVideoQueue = NULL;
  231. }
  232. return true;
  233. }
  234. ~Clibfacecapture_impl()
  235. {
  236. #ifdef _WIN32
  237. DeleteCriticalSection(&CS);
  238. if (!m_hStopEventWait) {
  239. CloseHandle(m_hStopEventWait);
  240. m_hStopEventWait = NULL;
  241. }
  242. #else
  243. pthread_mutex_destroy(&cs_mutex);
  244. #endif
  245. if (m_pMhImg){
  246. cvReleaseImage(&m_pMhImg);
  247. m_pMhImg = NULL;
  248. }
  249. if(m_pMhBuf){
  250. for (int i=0;i<3;i++){
  251. if (m_pMhBuf[i] != NULL){
  252. cvReleaseImage(&m_pMhBuf[i]);
  253. m_pMhBuf[i] = NULL;
  254. }
  255. }
  256. free(m_pMhBuf);
  257. }
  258. if (m_pProcessImg){
  259. cvReleaseImage(&m_pProcessImg);
  260. m_pProcessImg = NULL;
  261. }
  262. if (m_pFaceCascade){
  263. cvReleaseHaarClassifierCascade(&m_pFaceCascade);
  264. m_pFaceCascade = NULL;
  265. }
  266. if (m_pEyeCascade){
  267. cvReleaseHaarClassifierCascade(&m_pEyeCascade);
  268. m_pEyeCascade = NULL;
  269. }
  270. if (m_pMouthCascade){
  271. cvReleaseHaarClassifierCascade(&m_pMouthCascade);
  272. m_pMouthCascade = NULL;
  273. }
  274. if (m_pNoseCascade){
  275. cvReleaseHaarClassifierCascade(&m_pNoseCascade);
  276. m_pNoseCascade = NULL;
  277. }
  278. }
  279. public:
  280. bool m_bLightChange;
  281. int m_nCameraState; //0:无故障1:env故障,2:opt故障,3:全部故障
  282. private:
  283. #ifdef _WIN32
  284. HANDLE m_hStopEventWait; // CreateEvent
  285. CRITICAL_SECTION CS;
  286. #else
  287. pthread_mutex_t cs_mutex;
  288. sem_t m_semt;
  289. #endif
  290. bool* m_pResult;
  291. MonitorEnum m_eType;
  292. CvHaarClassifierCascade* m_pFaceCascade; //脸部分类器
  293. CvHaarClassifierCascade* m_pEyeCascade; //眼睛分类器
  294. CvHaarClassifierCascade* m_pMouthCascade; //嘴巴分类器
  295. CvHaarClassifierCascade* m_pNoseCascade; //鼻子分类器
  296. CMotionTrackRslt m_stObjTrackRslt; //运动跟踪的结果
  297. IplImage *m_pMhImg; //MHI: motion history image
  298. IplImage **m_pMhBuf;
  299. int m_nLastBuf;
  300. CFaceCaptureConfig m_stFaceConfig;
  301. int m_nImgWidth; //当前摄像头图像宽
  302. int m_nImgHeight; //当前摄像头图像高
  303. CvSeq* pfaceSeq; //人脸序列
  304. CvMemStorage* m_pFaceStorage; //人脸内部存储空间
  305. IplImage* m_pProcessImg; //用于处理的图像
  306. //存储前一帧的人脸矩形(不包括人脸)
  307. CvRect m_PreFaceRect[MAX_FACE_NUM];
  308. #ifdef _WIN32
  309. HANDLE m_hVieoMonitorThread;
  310. unsigned int m_nVieoMonitorThreadId;
  311. #else
  312. pthread_t m_videomonitorthreadid;
  313. #endif
  314. bool m_bStopVieoMonitor;
  315. Clibvideoqueue* m_pEnvironVideoQueue; //环境摄像头小分辨率视频队列
  316. Clibvideoqueue* m_pOperatorVideoQueue; //操作摄像头视频队列
  317. CAllFaceInfo m_stAllFaceInfo;
  318. CAllFaceInfo m_stFaceRstStorage;
  319. CBrightAdjParam m_stBrightAdjParam; //亮度调节参数
  320. volatile MonitorStateEnum m_eMonitorState;
  321. CVideoMonitorEvent* m_pHostEvent;
  322. CHostApi* m_pHostApi;
  323. CameraEnum m_eCamera;
  324. char m_envqueuename[MAX_PATH];
  325. char m_optqueuename[MAX_PATH];
  326. //初始化
  327. bool InitFaceCapture(const char *base_dir)
  328. {
  329. char dir[MAX_PATH] = {0};
  330. FIGetDir(CASCADE_FACE, base_dir, dir);
  331. m_pFaceCascade=(CvHaarClassifierCascade*)cvLoad(dir,NULL,NULL,NULL);
  332. if (!m_pFaceCascade){
  333. m_pHostApi->Debug(FACECAP_INFO, "加载脸部分类器失败!");
  334. return false;
  335. }
  336. FIGetDir(CASCADE_EYE, base_dir, dir);
  337. m_pEyeCascade=(CvHaarClassifierCascade*)cvLoad(dir,NULL,NULL,NULL);
  338. if (!m_pEyeCascade){
  339. m_pHostApi->Debug(FACECAP_INFO, "加载眼睛分类器失败!");
  340. return false;
  341. }
  342. FIGetDir(CASCADE_MOUTH, base_dir, dir);
  343. m_pMouthCascade=(CvHaarClassifierCascade*)cvLoad(dir,NULL,NULL,NULL);
  344. if (!m_pMouthCascade){
  345. m_pHostApi->Debug(FACECAP_INFO, "加载嘴巴分类器失败!");
  346. return false;
  347. }
  348. FIGetDir(CASCADE_NOSE, base_dir, dir);
  349. m_pNoseCascade=(CvHaarClassifierCascade*)cvLoad(dir,NULL,NULL,NULL);
  350. if (!m_pMouthCascade){
  351. m_pHostApi->Debug(FACECAP_INFO, "加载鼻子分类器失败!");
  352. return false;
  353. }
  354. return true;
  355. }
  356. //获取分类器路径
  357. void FIGetDir(const char*CascadeType,const char *base_dir,char*dir)
  358. {
  359. strcpy(dir, m_stFaceConfig.strFaceDataDirPath);
  360. strcat(dir, SPLIT_SLASH_STR);
  361. strcat(dir,CascadeType);
  362. }
  363. //写人脸检测数据
  364. bool WriteAllFaceInfo(CAllFaceInfo faceInfo)
  365. {
  366. #ifdef _WIN32
  367. EnterCriticalSection(&CS);
  368. #else
  369. pthread_mutex_lock(&cs_mutex);
  370. #endif
  371. m_stFaceRstStorage = faceInfo;
  372. #ifdef _WIN32
  373. LeaveCriticalSection(&CS);
  374. #else
  375. pthread_mutex_unlock(&cs_mutex);
  376. #endif
  377. return true;
  378. }
  379. //计算人脸所处的区域
  380. FaceRegionEnum CalcFaceRegion(CvRect FaceRect, int nImgWidth, int nImgHeight)
  381. {
  382. //计算人脸的中心坐标
  383. CvPoint FaceCenter;
  384. FaceCenter.x = FaceRect.x + (int)FaceRect.width/2;
  385. FaceCenter.y = FaceRect.y + (int)FaceRect.height/2;
  386. if(FaceCenter.y < (int)nImgHeight/3)
  387. {
  388. if(FaceCenter.x < (int)nImgWidth/3)
  389. {
  390. return FaceRegionLeftup;
  391. }
  392. else if((FaceCenter.x > (int)nImgWidth/3)&&(FaceCenter.x < (int)nImgWidth*2/3))
  393. {
  394. return FaceRegionUp;
  395. }
  396. else if (FaceCenter.x > (int)nImgWidth*2/3)
  397. {
  398. return FaceRegionRightUP;
  399. }
  400. }
  401. else if((FaceCenter.y > (int)nImgHeight/3)&&(FaceCenter.y < (int)nImgHeight*2/3))
  402. {
  403. if(FaceCenter.x < (int)nImgWidth/3)
  404. {
  405. return FaceRegionLeft;
  406. }
  407. else if((FaceCenter.x > (int)nImgWidth/3)&&(FaceCenter.x < (int)nImgWidth*2/3))
  408. {
  409. return FaceRegionCenter;
  410. }
  411. else if (FaceCenter.x > (int)nImgWidth*2/3)
  412. {
  413. return FaceRegionRight;
  414. }
  415. }
  416. else if(FaceCenter.y > (int)nImgHeight*2/3)
  417. {
  418. if(FaceCenter.x < (int)nImgWidth/3)
  419. {
  420. return FaceRegionLeftDn;
  421. }
  422. else if((FaceCenter.x > (int)nImgWidth/3)&&(FaceCenter.x < (int)nImgWidth*2/3))
  423. {
  424. return FaceRegionDn;
  425. }
  426. else if (FaceCenter.x > (int)nImgWidth*2/3)
  427. {
  428. return FaceRegionRightDn;
  429. }
  430. }
  431. return FaceError;
  432. }
  433. void SwitchDetectCamera()
  434. {
  435. if (NULL != m_pOperatorVideoQueue) {
  436. (m_eCamera == EnvironCamera) ? (m_eCamera = OperatorCamera) : (m_eCamera = EnvironCamera);
  437. }
  438. }
  439. //计算人脸到机具的距离
  440. DistanceEnum CalcFaceDistance(CvRect FaceRect)
  441. {
  442. if(FaceRect.height>(int)(m_nImgHeight/m_stFaceConfig.fOperateFaceSize))
  443. {
  444. return OperateDistance; //可操作距离0.5m
  445. }
  446. else if((FaceRect.height<(int)(m_nImgHeight/m_stFaceConfig.fOperateFaceSize))&&(FaceRect.height>(int)(m_nImgHeight/m_stFaceConfig.fCloseFaceSize)))
  447. {
  448. return CloseDistance; //近端1.5m
  449. }
  450. else
  451. {
  452. return FarDistance; //远端2.5m
  453. }
  454. }
  455. //从共享队列获取图像
  456. bool GetImgFromVideoQueue(Clibvideoqueue* videoqueue)
  457. {
  458. if (videoqueue->GetVideoLens() <= 0){
  459. if (videoqueue == m_pEnvironVideoQueue){
  460. m_pHostApi->Debug(FACECAP_DEBUG, "环境相机图像队列为空");
  461. }
  462. else if (videoqueue == m_pOperatorVideoQueue){
  463. m_pHostApi->Debug(FACECAP_DEBUG, "操作相机图像队列为空");
  464. }
  465. return false;
  466. }
  467. int iSize = videoqueue->GetFrameSize(m_nImgWidth, m_nImgHeight);
  468. if (0 == iSize) {
  469. return false;
  470. }
  471. videoq_frame* videoframe = new videoq_frame;
  472. if (m_pProcessImg == NULL){
  473. m_pProcessImg = cvCreateImage(cvSize(m_nImgWidth, m_nImgHeight), 8, 3); //创建图像
  474. }
  475. else{
  476. if((m_pProcessImg->height != m_nImgHeight)||(m_pProcessImg->width != m_nImgWidth)){
  477. cvReleaseImage(&m_pProcessImg);
  478. m_pProcessImg = NULL;
  479. m_pProcessImg = cvCreateImage(cvSize(m_nImgWidth,m_nImgHeight), 8, 3); //创建图像
  480. }
  481. }
  482. videoframe->data = (unsigned char*)m_pProcessImg->imageData;
  483. bool bGetvideo = videoqueue->GetVideo(videoframe, 0);
  484. if (!bGetvideo){
  485. delete videoframe;
  486. m_pHostApi->Debug(FACECAP_DEBUG, "从队列获取图像失败");
  487. return false;
  488. }
  489. m_nImgWidth = videoframe->width;
  490. m_nImgHeight = videoframe->height;
  491. delete videoframe;
  492. return true;
  493. }
  494. //从指定摄像头取图像数据
  495. //成功时返回true,图像保存在m_pProcessImg结构,同时保存m_nImgWidth, m_nImgHeight值
  496. //失败时返回false
  497. bool GetSpecifiedCameraImage(CameraEnum eCamera)
  498. {
  499. if (eCamera == EnvironCamera){
  500. if (m_pEnvironVideoQueue == NULL){
  501. return false;
  502. }
  503. bool bRslt = GetImgFromVideoQueue(m_pEnvironVideoQueue);
  504. if (!bRslt){
  505. return false;
  506. }
  507. }
  508. else if(eCamera == OperatorCamera){
  509. if (m_pOperatorVideoQueue == NULL){
  510. return false;
  511. }
  512. bool bRslt = GetImgFromVideoQueue(m_pOperatorVideoQueue);
  513. if (!bRslt){
  514. return false;
  515. }
  516. }
  517. return true;
  518. }
  519. //清空上次检测结果
  520. void ClearInspectResult()
  521. {
  522. //清除上一次的记录
  523. m_stAllFaceInfo.nTatolFaceNum = 0;
  524. m_stAllFaceInfo.astFaceInfo[0].stCustomerCover.bIsClearFace = false;
  525. m_stAllFaceInfo.astFaceInfo[0].stCustomerCover.bShowEyes = false;
  526. m_stAllFaceInfo.astFaceInfo[0].stCustomerCover.bShowMouth = false;
  527. m_stAllFaceInfo.astFaceInfo[0].stCustomerCover.bShowNose = false;
  528. m_stAllFaceInfo.astFaceInfo[0].eDistance = UncertainDistance;
  529. m_stAllFaceInfo.astFaceInfo[0].eHeight = UncertainHeight;
  530. m_stAllFaceInfo.astFaceInfo[0].ePose = UncertainPose;
  531. m_stAllFaceInfo.astFaceInfo[0].eSex = UncertainSex;
  532. m_stAllFaceInfo.astFaceInfo[0].eYoungOld = UncertainAge;
  533. m_stAllFaceInfo.astFaceInfo[0].eCamera = OperatorCamera;
  534. for (int i = 1; i < MAX_FACE_NUM; i++){
  535. memset(&m_stAllFaceInfo.astFaceInfo[i], 0, sizeof(CCustomerInfo));
  536. }
  537. }
  538. //在检测到正方形人脸图像中继续查找眼睛,鼻子,嘴巴
  539. //检测到时返回true
  540. //未检测到时返回false
  541. bool FacialFeatureDetect(CvHaarClassifierCascade *pCascade, CvRect *pFaceRect, CvMemStorage* pFaceStorage)
  542. {
  543. //搜索区域图像
  544. CvRect RegionRect;
  545. if (pCascade == m_pEyeCascade)
  546. {
  547. //眼睛
  548. RegionRect.x = pFaceRect->x;
  549. RegionRect.y = pFaceRect->y;
  550. RegionRect.width = pFaceRect->width;
  551. RegionRect.height = (int)pFaceRect->height*1/2;
  552. }
  553. else if (pCascade == m_pNoseCascade)
  554. {
  555. //鼻子
  556. RegionRect.x = (int)pFaceRect->x+(pFaceRect->width)*1/6;
  557. RegionRect.y = (int)pFaceRect->y+pFaceRect->height*1/6;
  558. RegionRect.width = (int)pFaceRect->width*2/3;
  559. RegionRect.height = (int)pFaceRect->height*2/3;
  560. }
  561. else if (pCascade == m_pMouthCascade)
  562. {
  563. //嘴巴
  564. RegionRect.x = (int)pFaceRect->x;
  565. RegionRect.y = (int)pFaceRect->y+pFaceRect->height*1/2;
  566. RegionRect.width = (int)pFaceRect->width;
  567. RegionRect.height = (int)pFaceRect->height*1/2;
  568. }
  569. CvRect RectTemp = cvRect(0,0,0,0);
  570. bool bSetRio = false;
  571. //设置roi
  572. if(m_pProcessImg->roi){
  573. RectTemp.x = m_pProcessImg->roi->xOffset;
  574. RectTemp.y = m_pProcessImg->roi->yOffset;
  575. RectTemp.width = m_pProcessImg->roi->width;
  576. RectTemp.height = m_pProcessImg->roi->height;
  577. RegionRect.x += RectTemp.x;
  578. RegionRect.y += RectTemp.y;
  579. bSetRio = true;
  580. }
  581. cvResetImageROI(m_pProcessImg);
  582. cvSetImageROI(m_pProcessImg, RegionRect);
  583. //脸部特征搜索
  584. CvSeq* pFacialFeatureSeq = cvHaarDetectObjects(m_pProcessImg, pCascade, pFaceStorage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(10,10));
  585. cvResetImageROI(m_pProcessImg);
  586. if(bSetRio){
  587. cvSetImageROI(m_pProcessImg,RectTemp);
  588. }
  589. if (pFacialFeatureSeq ? pFacialFeatureSeq->total : 0){
  590. return true;
  591. }
  592. else{
  593. return false;
  594. }
  595. }
  596. //计算两个人脸数据的距离
  597. int CalcTwoFaceDistance(CvRect *pSrcRect,CvRect *pDstRect)
  598. {
  599. CvPoint p1;
  600. CvPoint p2;
  601. p1.x = (int)pSrcRect->x+pSrcRect->width/2;
  602. p1.y = (int)pSrcRect->y+pSrcRect->height/2;
  603. p2.x = (int)pDstRect->x+pDstRect->width/2;
  604. p2.y = (int)pDstRect->y+pDstRect->height/2;
  605. return (int)sqrt((double)(p1.x-p2.x)*(p1.x-p2.x) + (double)(p1.y-p2.y)*(p1.y-p2.y));
  606. }
  607. //计算人脸亮度,同时计算人脸亮度调节参数
  608. BrightnessEnum CalcFaceBrightness(IplImage*img, CvRect* FaceRect)
  609. {
  610. cvResetImageROI(img);
  611. cvSetImageROI(img,*FaceRect);
  612. IplImage* FaceImgTmp = cvCreateImage(cvSize(FaceRect->width,FaceRect->height),IPL_DEPTH_8U,3);
  613. cvCopy(img,FaceImgTmp,NULL);
  614. cvResetImageROI(img);
  615. IplImage* FaceImg = cvCreateImage(cvSize(FaceRect->width,FaceRect->height),IPL_DEPTH_8U,1);
  616. cvCvtColor(FaceImgTmp, FaceImg, CV_BGR2GRAY);
  617. cvReleaseImage(&FaceImgTmp);
  618. int aver=0;
  619. unsigned char *data=(uchar*)FaceImg->imageData;
  620. for(int i=0;i<FaceRect->height;i++)
  621. {
  622. for(int j=0;j<FaceRect->width;j++)
  623. {
  624. aver+=data[i*FaceImg->widthStep+j];
  625. }
  626. }
  627. aver=aver/FaceRect->width/FaceRect->height;
  628. cvReleaseImage(&FaceImg);
  629. if (aver>230)
  630. {
  631. m_stBrightAdjParam.fLow = 0.8;
  632. m_stBrightAdjParam.fHigh = 1.0;
  633. m_stBrightAdjParam.fBottom = 0.45;
  634. m_stBrightAdjParam.fTop = 0.75;
  635. m_stBrightAdjParam.fGamma = 1;
  636. return VeryHigh;
  637. }
  638. else if((aver<230)&&(aver>205))
  639. {
  640. m_stBrightAdjParam.fLow = 0.75;
  641. m_stBrightAdjParam.fHigh = 1.0;
  642. m_stBrightAdjParam.fBottom = 0.5;
  643. m_stBrightAdjParam.fTop = 0.8;
  644. m_stBrightAdjParam.fGamma = 1;
  645. return Higher;
  646. }
  647. else if((aver<205)&&(aver>180))
  648. {
  649. m_stBrightAdjParam.fLow = 0.7;
  650. m_stBrightAdjParam.fHigh = 1.0;
  651. m_stBrightAdjParam.fBottom = 0.5;
  652. m_stBrightAdjParam.fTop = 0.8;
  653. m_stBrightAdjParam.fGamma = 1;
  654. return NorMal;
  655. }
  656. else if((aver<180)&&(aver>70))
  657. {
  658. m_stBrightAdjParam.fLow = 0.0;
  659. m_stBrightAdjParam.fHigh = 0.0;
  660. m_stBrightAdjParam.fBottom = 0.0;
  661. m_stBrightAdjParam.fTop = 0.0;
  662. m_stBrightAdjParam.fGamma = 0.0;
  663. return NorMal;
  664. }
  665. else if((aver<70)&&(aver>50))
  666. {
  667. m_stBrightAdjParam.fLow = 0;
  668. m_stBrightAdjParam.fHigh = 0.3;
  669. m_stBrightAdjParam.fBottom = 0.15;
  670. m_stBrightAdjParam.fTop = 0.55;
  671. m_stBrightAdjParam.fGamma = 1;
  672. return NorMal;
  673. }
  674. else if((aver<50)&&(aver>30))
  675. {
  676. m_stBrightAdjParam.fLow = 0;
  677. m_stBrightAdjParam.fHigh = 0.25;
  678. m_stBrightAdjParam.fBottom = 0.25;
  679. m_stBrightAdjParam.fTop = 0.65;
  680. m_stBrightAdjParam.fGamma = 1;
  681. return Lower;
  682. }
  683. else
  684. {
  685. m_stBrightAdjParam.fLow = 0;
  686. m_stBrightAdjParam.fHigh = 0.2;
  687. m_stBrightAdjParam.fBottom = 0.25;
  688. m_stBrightAdjParam.fTop = 0.65;
  689. m_stBrightAdjParam.fGamma = 1;
  690. return VeryLow;
  691. }
  692. }
  693. //计算人脸亮度调节参数
  694. bool CalcBrightAdjParam(IplImage*img, CvRect* FaceRect)
  695. {
  696. cvResetImageROI(img);
  697. cvSetImageROI(img,*FaceRect);
  698. IplImage* FaceImgTmp = cvCreateImage(cvSize(FaceRect->width,FaceRect->height),IPL_DEPTH_8U,3);
  699. cvCopy(img,FaceImgTmp,NULL);
  700. cvResetImageROI(img);
  701. IplImage* FaceImg = cvCreateImage(cvSize(FaceRect->width,FaceRect->height),IPL_DEPTH_8U,1);
  702. cvCvtColor(FaceImgTmp, FaceImg, CV_BGR2GRAY);
  703. cvReleaseImage(&FaceImgTmp);
  704. int aver=0;
  705. unsigned char *data=(uchar*)FaceImg->imageData;
  706. for(int i=0;i<FaceRect->height;i++)
  707. {
  708. for(int j=0;j<FaceRect->width;j++)
  709. {
  710. aver+=data[i*FaceImg->widthStep+j];
  711. }
  712. }
  713. aver=aver/FaceRect->width/FaceRect->height;
  714. if (aver>230)
  715. {
  716. m_stBrightAdjParam.fLow = 0.8;
  717. m_stBrightAdjParam.fHigh = 1.0;
  718. m_stBrightAdjParam.fBottom = 0.45;
  719. m_stBrightAdjParam.fTop = 0.75;
  720. m_stBrightAdjParam.fGamma = 1;
  721. }
  722. else if((aver<230)&&(aver>205))
  723. {
  724. m_stBrightAdjParam.fLow = 0.75;
  725. m_stBrightAdjParam.fHigh = 1.0;
  726. m_stBrightAdjParam.fBottom = 0.5;
  727. m_stBrightAdjParam.fTop = 0.8;
  728. m_stBrightAdjParam.fGamma = 1;
  729. }
  730. else if((aver<205)&&(aver>180))
  731. {
  732. m_stBrightAdjParam.fLow = 0.7;
  733. m_stBrightAdjParam.fHigh = 1.0;
  734. m_stBrightAdjParam.fBottom = 0.5;
  735. m_stBrightAdjParam.fTop = 0.8;
  736. m_stBrightAdjParam.fGamma = 1;
  737. }
  738. else if((aver<70)&&(aver>60))
  739. {
  740. m_stBrightAdjParam.fLow = 0;
  741. m_stBrightAdjParam.fHigh = 0.3;
  742. m_stBrightAdjParam.fBottom = 0.15;
  743. m_stBrightAdjParam.fTop = 0.55;
  744. m_stBrightAdjParam.fGamma = 1;
  745. }
  746. else if((aver<60)&&(aver>30))
  747. {
  748. m_stBrightAdjParam.fLow = 0;
  749. m_stBrightAdjParam.fHigh = 0.25;
  750. m_stBrightAdjParam.fBottom = 0.25;
  751. m_stBrightAdjParam.fTop = 0.65;
  752. m_stBrightAdjParam.fGamma = 1;
  753. }
  754. else
  755. {
  756. m_stBrightAdjParam.fLow = 0;
  757. m_stBrightAdjParam.fHigh = 0.2;
  758. m_stBrightAdjParam.fBottom = 0.25;
  759. m_stBrightAdjParam.fTop = 0.65;
  760. m_stBrightAdjParam.fGamma = 1;
  761. }
  762. cvReleaseImage(&FaceImg);
  763. return true;
  764. }
  765. //人脸检测,获取指定摄像头的图像进行全局检测,比较当前人脸和主人脸信息,将更居中人脸信息保存为主人脸。
  766. //检测到有人时,返回ture
  767. //没检测到有人时,返回false
  768. bool FaceDetectWithSpecifiedCamera(CameraEnum eCamera)
  769. {
  770. if (!GetSpecifiedCameraImage(eCamera)){
  771. m_pHostApi->Debug(FACECAP_DEBUG, "获取图像失败");
  772. return false;
  773. }
  774. CCustomerInfo UserFaceTemp;
  775. int nFaceSerial = 1;
  776. memset(&UserFaceTemp, 0, sizeof(CCustomerInfo));
  777. CvRect* pFaceRect = NULL; //人脸矩形
  778. int nPreFaceNum = 0;
  779. cvResetImageROI(m_pProcessImg);
  780. //内存初始化
  781. if (m_pFaceStorage){
  782. m_pFaceStorage = cvCreateMemStorage(0);
  783. cvClearMemStorage(m_pFaceStorage);
  784. }
  785. else{
  786. m_pFaceStorage = cvCreateMemStorage(0);
  787. }
  788. //人脸搜索
  789. if (m_pFaceCascade == NULL){
  790. ClearInspectResult();
  791. return false;
  792. }
  793. int isize = (int)(m_nImgHeight/m_stFaceConfig.fDetectFaceSize);
  794. pfaceSeq = cvHaarDetectObjects(m_pProcessImg, m_pFaceCascade, m_pFaceStorage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(isize, isize));
  795. if (!pfaceSeq || !pfaceSeq->total){
  796. cvReleaseMemStorage(&pfaceSeq->storage);
  797. pfaceSeq = NULL;
  798. ClearInspectResult();
  799. return false;
  800. }
  801. else
  802. {
  803. //保存上次人脸矩形,对当前人脸位置进行微调防止跳动
  804. nPreFaceNum = m_stAllFaceInfo.nTatolFaceNum;
  805. for(int num = 0; num < m_stAllFaceInfo.nTatolFaceNum; num++){
  806. m_PreFaceRect[num] = m_stAllFaceInfo.astFaceInfo[num].stRegion.stFaceRect;
  807. }
  808. //清除上一次的记录
  809. ClearInspectResult();
  810. }
  811. for(int i = 0; i < (pfaceSeq ? pfaceSeq->total : 0)&&(i < MAX_FACE_NUM); i++)
  812. {
  813. m_stAllFaceInfo.nTatolFaceNum++;
  814. //获取脸部矩形
  815. pFaceRect = (CvRect*)cvGetSeqElem(pfaceSeq, i);
  816. //如果是前端检测则需要特征检测,如果是坐席则不需检测
  817. if (m_stFaceConfig.nServersType == 0)
  818. {
  819. //找眼睛
  820. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowEyes = FacialFeatureDetect(m_pEyeCascade, pFaceRect, m_pFaceStorage);
  821. //找鼻子
  822. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowNose = FacialFeatureDetect(m_pNoseCascade, pFaceRect, m_pFaceStorage);
  823. //找嘴巴
  824. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowMouth = FacialFeatureDetect(m_pMouthCascade, pFaceRect, m_pFaceStorage);
  825. }
  826. else
  827. {
  828. //找眼睛
  829. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowEyes = false;
  830. //找鼻子
  831. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowNose = false;
  832. //找嘴巴
  833. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowMouth = false;
  834. }
  835. if(!(m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowEyes||
  836. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowNose||
  837. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bShowMouth))
  838. {
  839. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bIsClearFace = false;
  840. }
  841. else
  842. {
  843. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stCustomerCover.bIsClearFace = true;
  844. }
  845. //计算当前人脸位置,如果当前人脸位置与上次人脸位置小于偏移量则取上次人脸位置,防止人脸图像跳变
  846. for (int num = 0; num < nPreFaceNum; num++)
  847. {
  848. if ((abs(pFaceRect->x - m_PreFaceRect[num].x) < m_stFaceConfig.nFaceSizeOffset)
  849. && (abs(pFaceRect->y - m_PreFaceRect[num].y) < m_stFaceConfig.nFaceSizeOffset)
  850. && (abs(pFaceRect->width - m_PreFaceRect[num].width) < (m_stFaceConfig.nFaceSizeOffset*2))
  851. && (abs(pFaceRect->height - m_PreFaceRect[num].height) < (m_stFaceConfig.nFaceSizeOffset*2)))
  852. {
  853. pFaceRect->x = m_PreFaceRect[num].x;
  854. pFaceRect->y = m_PreFaceRect[num].y;
  855. pFaceRect->width = m_PreFaceRect[num].width;
  856. pFaceRect->height = m_PreFaceRect[num].height;
  857. }
  858. }
  859. //搜索到人脸,计算人脸数据
  860. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stRegion.stFaceRect = *pFaceRect;
  861. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stRegion.eRegion = CalcFaceRegion(*pFaceRect,m_nImgWidth,m_nImgHeight);
  862. m_stAllFaceInfo.astFaceInfo[nFaceSerial].eDistance = CalcFaceDistance(*pFaceRect);
  863. m_stAllFaceInfo.astFaceInfo[nFaceSerial].eCamera = eCamera;
  864. //获取UUID
  865. if (m_stAllFaceInfo.astFaceInfo[nFaceSerial].FaceID == 0)
  866. {
  867. m_stAllFaceInfo.astFaceInfo[nFaceSerial].FaceID = m_pHostApi->GenerateUUID();
  868. }
  869. //截取人脸图像,放大截取区域,截取半身头像
  870. CalcUpbodyRegion(pFaceRect);
  871. m_stAllFaceInfo.astFaceInfo[nFaceSerial].stRegion.stUpperBodyRect = *pFaceRect;
  872. //当前数据的区域和距离,temp数据的区域和距离
  873. FaceRegionEnum CurRegion = m_stAllFaceInfo.astFaceInfo[nFaceSerial].stRegion.eRegion;
  874. DistanceEnum CurDistance = m_stAllFaceInfo.astFaceInfo[nFaceSerial].eDistance;
  875. FaceRegionEnum TempRegion = UserFaceTemp.stRegion.eRegion;
  876. DistanceEnum TempDistance = UserFaceTemp.eDistance;
  877. int TempPointDistance = CalcTwoFaceDistance(&UserFaceTemp.stRegion.stFaceRect, &m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect);
  878. int CurPointDistance = CalcTwoFaceDistance(&m_stAllFaceInfo.astFaceInfo[nFaceSerial].stRegion.stFaceRect, &m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect);
  879. //计算当前操作者的人脸区域
  880. if(//如果temp数据为0,则更新temp
  881. ((TempRegion == FaceError)&&(TempDistance == UncertainDistance)) ||
  882. //如果当前数据在中心区域,temp数据也在中心区域,且当前距离比temp数据更近,则更新temp
  883. ((CurRegion == FaceRegionCenter)&&(TempRegion == FaceRegionCenter)&&(CurDistance < TempDistance))||
  884. //如果当前数据不在中心区域,temp数据也不在中心区域,且当前数据比temp数据更近。则更新temp
  885. ((TempRegion != FaceRegionCenter)&&(CurRegion != FaceRegionCenter)&&(CurDistance < TempDistance))||
  886. //如果当前数据在中心区域,temp数据不在中心区域,且当前距离更近,则更新temp
  887. ((CurRegion == FaceRegionCenter)&&(TempRegion != FaceRegionCenter)&&(CurDistance < TempDistance))||
  888. //如果当前数据不在中心区域,temp数据在中心区域,且当前数据距离更近,则更新temp
  889. ((CurRegion != FaceRegionCenter)&&(TempRegion == FaceRegionCenter)&&(CurDistance < TempDistance))||
  890. //如果当前数据不在中心区域,temp数据也不在中心区域,且距离一样,若当前数据更靠近历史数据,则当前数据作为temp
  891. ((TempRegion != FaceRegionCenter)&&(CurRegion != FaceRegionCenter)&&(CurDistance == TempDistance)&&(CurPointDistance < TempPointDistance)))
  892. {
  893. if((TempRegion != FaceError)&&(TempDistance != UncertainDistance))
  894. {
  895. CCustomerInfo Temp = UserFaceTemp;
  896. UserFaceTemp = m_stAllFaceInfo.astFaceInfo[nFaceSerial];
  897. m_stAllFaceInfo.astFaceInfo[nFaceSerial] = Temp;
  898. nFaceSerial++;
  899. }
  900. else
  901. {
  902. UserFaceTemp = m_stAllFaceInfo.astFaceInfo[nFaceSerial];
  903. memset(&m_stAllFaceInfo.astFaceInfo[nFaceSerial],0,sizeof(CCustomerInfo));
  904. }
  905. }
  906. else
  907. {
  908. nFaceSerial++;
  909. }
  910. }
  911. if((UserFaceTemp.stRegion.eRegion != FaceError)&&(UserFaceTemp.eDistance != UncertainDistance))
  912. {
  913. m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect = UserFaceTemp.stRegion.stFaceRect;
  914. m_stAllFaceInfo.astFaceInfo[0].eDistance = UserFaceTemp.eDistance;
  915. m_stAllFaceInfo.astFaceInfo[0].stRegion.eRegion = UserFaceTemp.stRegion.eRegion;
  916. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect = UserFaceTemp.stRegion.stUpperBodyRect;
  917. m_stAllFaceInfo.astFaceInfo[0].eCamera = UserFaceTemp.eCamera;
  918. m_stAllFaceInfo.astFaceInfo[0].stCustomerCover = UserFaceTemp.stCustomerCover;
  919. //计算人脸亮度
  920. //if (m_stFaceConfig.nServersType == 0)
  921. //{
  922. // m_pHostApi->Debug(FACECAP_DEBUG, "%s:%d eCamera = %d.", __FUNCTION__, __LINE__, eCamera);
  923. // m_stAllFaceInfo.astFaceInfo[0].eBrightness = CalcFaceBrightness(m_pProcessImg,&m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect);
  924. // m_pHostApi->Debug(FACECAP_DEBUG, "%s:%d m_stAllFaceInfo.astFaceInfo[0].eBrightness = %d.", __FUNCTION__, __LINE__, m_stAllFaceInfo.astFaceInfo[0].eBrightness);
  925. //}
  926. }
  927. //清空存储区
  928. cvResetImageROI(m_pProcessImg);
  929. if (pfaceSeq != NULL){
  930. cvReleaseMemStorage(&pfaceSeq->storage);
  931. pfaceSeq = NULL;
  932. }
  933. for(int i = 0; i < MAX_FACE_NUM; i++){
  934. m_PreFaceRect[i] = cvRect(0,0,0,0);
  935. }
  936. if(m_stAllFaceInfo.nTatolFaceNum == 0){
  937. ClearInspectResult();
  938. return false;
  939. }
  940. else{
  941. return true;
  942. }
  943. }
  944. bool bIsRectValid(int iWidth, int iHeight, CvRect rect)
  945. {
  946. bool bRet = false;
  947. if ((0 != rect.width) && (0 != rect.height) && (iWidth >= rect.x + rect.width) && (iHeight >= rect.y + rect.height)) {
  948. bRet = true;
  949. }
  950. return bRet;
  951. }
  952. //人脸检测,对指定摄像头的指定区域进行全局检测,比较当前人脸和主人脸信息,将更居中人脸信息保存为主人脸。
  953. //检测到有人时,返回ture
  954. //没检测到有人时,返回false
  955. bool FaceDetectWithSpecifiedCameraRegion(CameraEnum eCamera, CvRect rect)
  956. {
  957. if (!GetSpecifiedCameraImage(eCamera)){
  958. return false;
  959. }
  960. CvMemStorage* pFaceStorage = NULL;
  961. if (eCamera == EnvironCamera){
  962. pFaceStorage = m_pFaceStorage;
  963. }
  964. else{
  965. pFaceStorage = m_pFaceStorage;
  966. }
  967. CCustomerInfo UserFaceTemp;
  968. CCustomerInfo astFaceTemp;
  969. memset(&astFaceTemp,0,sizeof(CCustomerInfo));
  970. memset(&UserFaceTemp,0,sizeof(CCustomerInfo));
  971. CvRect* pFaceRect = NULL; //人脸矩形
  972. cvResetImageROI(m_pProcessImg);
  973. if (bIsRectValid(m_pProcessImg->width, m_pProcessImg->height, rect)) {
  974. cvSetImageROI(m_pProcessImg, rect);
  975. }
  976. //内存初始化
  977. if (pFaceStorage){
  978. pFaceStorage = cvCreateMemStorage(0);
  979. cvClearMemStorage(pFaceStorage);
  980. }
  981. else{
  982. pFaceStorage = cvCreateMemStorage(0);
  983. }
  984. //人脸搜索
  985. if (m_pFaceCascade == NULL){
  986. return false;
  987. }
  988. int size = (int)m_nImgHeight/m_stFaceConfig.fDetectFaceSize;
  989. pfaceSeq = cvHaarDetectObjects(m_pProcessImg, m_pFaceCascade, pFaceStorage, 1.2, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(size,size));
  990. if (!pfaceSeq || !pfaceSeq->total)
  991. {
  992. cvReleaseMemStorage(&pfaceSeq->storage);
  993. pfaceSeq = NULL;
  994. return false;
  995. }
  996. for(int i = 0; i < (pfaceSeq?pfaceSeq->total:0)&&(i<MAX_FACE_NUM); i++)
  997. {
  998. //获取脸部矩形
  999. pFaceRect = (CvRect*)cvGetSeqElem(pfaceSeq, i);
  1000. //如果是前端检测则需要特征检测,如果是坐席则不需检测
  1001. if (m_stFaceConfig.nServersType == 0)
  1002. {
  1003. //找眼睛
  1004. astFaceTemp.stCustomerCover.bShowEyes = FacialFeatureDetect(m_pEyeCascade, pFaceRect, pFaceStorage);
  1005. //找鼻子
  1006. astFaceTemp.stCustomerCover.bShowNose = FacialFeatureDetect(m_pNoseCascade, pFaceRect, pFaceStorage);
  1007. //找嘴巴
  1008. astFaceTemp.stCustomerCover.bShowMouth = FacialFeatureDetect(m_pMouthCascade, pFaceRect, pFaceStorage);
  1009. }
  1010. else
  1011. {
  1012. //找眼睛
  1013. astFaceTemp.stCustomerCover.bShowEyes = false;
  1014. //找鼻子
  1015. astFaceTemp.stCustomerCover.bShowNose = false;
  1016. //找嘴巴
  1017. astFaceTemp.stCustomerCover.bShowMouth = false;
  1018. }
  1019. if(!(astFaceTemp.stCustomerCover.bShowEyes||astFaceTemp.stCustomerCover.bShowNose||astFaceTemp.stCustomerCover.bShowMouth))
  1020. {
  1021. astFaceTemp.stCustomerCover.bIsClearFace = false;
  1022. }
  1023. else
  1024. {
  1025. astFaceTemp.stCustomerCover.bIsClearFace = true;
  1026. }
  1027. //计算当前人脸位置如果当前人脸位置与上次人脸位置小于偏移量则取上次人脸位置,防止人脸图像跳变
  1028. //坐标变换
  1029. pFaceRect->x = pFaceRect->x+rect.x;
  1030. pFaceRect->y = pFaceRect->y+rect.y;
  1031. if ((abs(pFaceRect->x - m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.x) < m_stFaceConfig.nFaceSizeOffset)
  1032. &&(abs(pFaceRect->y-m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.y) < m_stFaceConfig.nFaceSizeOffset)
  1033. &&(abs(pFaceRect->width-m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.width) < (m_stFaceConfig.nFaceSizeOffset*2))
  1034. &&(abs(pFaceRect->height-m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.height) < (m_stFaceConfig.nFaceSizeOffset*2)))
  1035. {
  1036. pFaceRect->x = m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.x;
  1037. pFaceRect->y = m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.y;
  1038. pFaceRect->width = m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.width;
  1039. pFaceRect->height = m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.height;
  1040. }
  1041. //搜索到人脸,计算人脸数据
  1042. astFaceTemp.stRegion.stFaceRect = *pFaceRect;
  1043. astFaceTemp.stRegion.eRegion = CalcFaceRegion(*pFaceRect,m_nImgWidth,m_nImgHeight);
  1044. astFaceTemp.eDistance = CalcFaceDistance(*pFaceRect);
  1045. astFaceTemp.eCamera = eCamera;
  1046. //截取人脸图像,放大截取区域,截取半身头像
  1047. CalcUpbodyRegion(pFaceRect);
  1048. astFaceTemp.stRegion.stUpperBodyRect = *pFaceRect;
  1049. //当前数据的区域和距离,temp数据的区域和距离
  1050. FaceRegionEnum CurRegion = astFaceTemp.stRegion.eRegion;
  1051. DistanceEnum CurDistance = astFaceTemp.eDistance;
  1052. FaceRegionEnum TempRegion = UserFaceTemp.stRegion.eRegion;
  1053. DistanceEnum TempDistance = UserFaceTemp.eDistance;
  1054. int TempPointDistance = CalcTwoFaceDistance(&UserFaceTemp.stRegion.stFaceRect, &m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect);
  1055. int CurPointDistance = CalcTwoFaceDistance(&astFaceTemp.stRegion.stFaceRect, &m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect);
  1056. //计算当前操作者的人脸区域
  1057. if(//如果temp数据为0,则更新temp
  1058. ((TempRegion == FaceError)&&(TempDistance == UncertainDistance)) ||
  1059. //如果当前数据在中心区域,temp数据也在中心区域,且当前距离比temp数据更近,则更新temp
  1060. ((CurRegion == FaceRegionCenter)&&(TempRegion == FaceRegionCenter)&&(CurDistance < TempDistance))||
  1061. //如果当前数据不在中心区域,temp数据也不在中心区域,且当前数据比temp数据更近。则更新temp
  1062. ((TempRegion != FaceRegionCenter)&&(CurRegion != FaceRegionCenter)&&(CurDistance < TempDistance))||
  1063. //如果当前数据在中心区域,temp数据不在中心区域,且当前距离更近,则更新temp
  1064. ((CurRegion == FaceRegionCenter)&&(TempRegion != FaceRegionCenter)&&(CurDistance < TempDistance))||
  1065. //如果当前数据不在中心区域,temp数据在中心区域,且当前数据距离更近,则更新temp
  1066. ((CurRegion != FaceRegionCenter)&&(TempRegion == FaceRegionCenter)&&(CurDistance < TempDistance))||
  1067. //如果当前数据不在中心区域,temp数据也不在中心区域,且距离一样,若当前数据更靠近历史数据,则当前数据作为temp
  1068. ((TempRegion != FaceRegionCenter)&&(CurRegion != FaceRegionCenter)&&(CurDistance == TempDistance)&&(CurPointDistance < TempPointDistance)))
  1069. {
  1070. UserFaceTemp = astFaceTemp;
  1071. memset(&astFaceTemp, 0, sizeof(CCustomerInfo));
  1072. }
  1073. }
  1074. if((UserFaceTemp.stRegion.eRegion != FaceError)&&(UserFaceTemp.eDistance != UncertainDistance))
  1075. {
  1076. m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect = UserFaceTemp.stRegion.stFaceRect;
  1077. m_stAllFaceInfo.astFaceInfo[0].eDistance = UserFaceTemp.eDistance;
  1078. m_stAllFaceInfo.astFaceInfo[0].stRegion.eRegion = UserFaceTemp.stRegion.eRegion;
  1079. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect = UserFaceTemp.stRegion.stUpperBodyRect;
  1080. m_stAllFaceInfo.astFaceInfo[0].eCamera = UserFaceTemp.eCamera;
  1081. m_stAllFaceInfo.astFaceInfo[0].stCustomerCover = UserFaceTemp.stCustomerCover;
  1082. //计算人脸亮度
  1083. //if (m_stFaceConfig.nServersType == 0){
  1084. // m_stAllFaceInfo.astFaceInfo[0].eBrightness = CalcFaceBrightness(m_pProcessImg,&m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect);
  1085. //}
  1086. }
  1087. //清空存储区
  1088. if (pfaceSeq != NULL){
  1089. cvReleaseMemStorage(&pfaceSeq->storage);
  1090. pfaceSeq = NULL;
  1091. }
  1092. cvResetImageROI(m_pProcessImg);
  1093. return true;
  1094. }
  1095. //调整图像亮度
  1096. bool AdjustImgBrightness(char* src,int width,int height,CBrightAdjParam stAdjParam,int ImgColorType = RGB_IMG)
  1097. {
  1098. if(stAdjParam.fLow<0&&stAdjParam.fLow>1&&stAdjParam.fHigh<0&&stAdjParam.fHigh>1
  1099. &&stAdjParam.fBottom<0&&stAdjParam.fBottom>1&&stAdjParam.fTop<0&&stAdjParam.fTop>1&& stAdjParam.fLow>stAdjParam.fHigh)
  1100. return false;
  1101. if (((stAdjParam.fHigh-0.0)<0.001)&&((stAdjParam.fTop-0.0)<0.001))
  1102. return false;
  1103. double low2 = stAdjParam.fLow*255;
  1104. double high2 = stAdjParam.fHigh*255;
  1105. double bottom2 = stAdjParam.fBottom*255;
  1106. double top2 = stAdjParam.fTop*255;
  1107. double err_in = high2 - low2;
  1108. double err_out = top2 - bottom2;
  1109. int x,y;
  1110. double val;
  1111. int nChannels = 0;
  1112. if (ImgColorType = RGB_IMG)
  1113. {
  1114. nChannels = 3;
  1115. }
  1116. else
  1117. {
  1118. nChannels = 1;
  1119. }
  1120. // intensity transform
  1121. for( y = 0; y < height; y++)
  1122. {
  1123. for (x = 0; x < width*3; x++)
  1124. {
  1125. val = ((uchar*)(src + width*nChannels*y))[x];
  1126. val = pow((val - low2)/err_in, stAdjParam.fGamma) * err_out + bottom2;
  1127. if(val>255)
  1128. val=255;
  1129. if(val<0)
  1130. val=0; // Make sure src is in the range [low,high]
  1131. ((uchar*)(src + width*nChannels*y))[x] = (uchar) val;
  1132. }
  1133. }
  1134. return true;
  1135. }
  1136. //形态学结构元素的映射
  1137. IplConvKernel* IpStructuringElementMap(IplConvKernel* se)
  1138. {
  1139. CvMat *mat = cvCreateMat( se->nRows, se->nCols, CV_32SC1);
  1140. memcpy(mat->data.i, se->values, sizeof(int) * se->nRows * se->nCols );
  1141. cvFlip(mat, NULL, -1);
  1142. IplConvKernel* semap = cvCreateStructuringElementEx( se->nCols, se->nRows,
  1143. se->nCols-se->anchorX-1, se->nRows-se->anchorY-1, 0, NULL );
  1144. semap->nShiftR = se->nShiftR;
  1145. memcpy( semap->values, mat->data.i, sizeof(int) * se->nRows * se->nCols );
  1146. cvReleaseMat(&mat);
  1147. return semap;
  1148. }
  1149. //形态学闭运算
  1150. void IpMorpClose(const IplImage*src,IplImage* dst,IplConvKernel* se=NULL, int iterations=1)
  1151. {
  1152. cvDilate(src,dst,se,iterations );
  1153. IplConvKernel* semap = IpStructuringElementMap(se);
  1154. cvErode(src,dst,semap,iterations );
  1155. cvReleaseStructuringElement(&semap);
  1156. }
  1157. //计算半身头像区域,按4:3比例剪辑,防止超出图像界限
  1158. void CalcUpbodyRegion(CvRect* pFaceRect)
  1159. {
  1160. if (m_nImgWidth > m_nImgHeight)
  1161. {
  1162. pFaceRect->y = ((pFaceRect->y-pFaceRect->height*1/3)<0)?0:(int)(pFaceRect->y-pFaceRect->height*1/3);
  1163. pFaceRect->height = ((pFaceRect->height*19/12+pFaceRect->y)>m_nImgHeight)?(int)(m_nImgHeight-pFaceRect->y):(int)(pFaceRect->height*19/12);
  1164. if ((int)(pFaceRect->x+pFaceRect->width/2-pFaceRect->height*4/6)<0)
  1165. {
  1166. pFaceRect->x = 0;
  1167. }
  1168. else if ((pFaceRect->x+pFaceRect->width/2+pFaceRect->height*4/6)>m_nImgWidth)
  1169. {
  1170. pFaceRect->x = (int)(m_nImgWidth-pFaceRect->height*4/3);
  1171. }
  1172. else
  1173. {
  1174. pFaceRect->x=(int)(pFaceRect->x+pFaceRect->width/2-pFaceRect->height*4/6);
  1175. }
  1176. pFaceRect->width = pFaceRect->height*4/3;
  1177. }
  1178. else
  1179. {
  1180. pFaceRect->x = ((pFaceRect->x-pFaceRect->width*7/24)<0)?0:(int)(pFaceRect->x-pFaceRect->width*7/24);
  1181. pFaceRect->width = ((pFaceRect->width*19/12+pFaceRect->x)>m_nImgWidth)?(int)(m_nImgWidth-pFaceRect->x):(int)(pFaceRect->width*19/12);
  1182. if ((int)(pFaceRect->y+pFaceRect->height/2-pFaceRect->width*3/8)<0)
  1183. {
  1184. pFaceRect->y = 0;
  1185. }
  1186. else if ((pFaceRect->y+pFaceRect->height/2+pFaceRect->width*3/8)>m_nImgHeight)
  1187. {
  1188. pFaceRect->y = (int)(m_nImgHeight-pFaceRect->width*3/8);
  1189. }
  1190. else
  1191. {
  1192. pFaceRect->y = (int)(pFaceRect->y+pFaceRect->height/2-pFaceRect->width*3/8);
  1193. }
  1194. pFaceRect->height = pFaceRect->width*3/4;
  1195. }
  1196. }
  1197. //按指定尺寸对图像进行全局搜索
  1198. //检测到有人时,返回ture,更新人脸状态,同时发送人脸状态信息,保存检测到的人脸数,最靠近机器那人的人脸位置和大小信息,当前摄像头信息,用于其他分类器做进一步检测
  1199. //没检测到有人时,返回false
  1200. bool FaceSearchWithSpecifiedSize(uint32_t nFaceLimit)
  1201. {
  1202. int nFaceNum = 0;
  1203. int nCurafaceSerial = 0;
  1204. //人脸搜索
  1205. if (NULL == m_pFaceCascade){
  1206. return false;
  1207. }
  1208. cvResetImageROI(m_pProcessImg);
  1209. CvRect* pFaceRect = NULL; //人脸矩形
  1210. //内存初始化
  1211. if (m_pFaceStorage){
  1212. m_pFaceStorage = cvCreateMemStorage(0);
  1213. cvClearMemStorage(m_pFaceStorage);
  1214. }
  1215. else{
  1216. m_pFaceStorage = cvCreateMemStorage(0);
  1217. }
  1218. pfaceSeq = cvHaarDetectObjects(m_pProcessImg, m_pFaceCascade, m_pFaceStorage, 1.2, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(nFaceLimit, nFaceLimit));
  1219. if (pfaceSeq && pfaceSeq->total)
  1220. {
  1221. for(int i = 0; i < (pfaceSeq?pfaceSeq->total:0); i++)
  1222. {
  1223. nFaceNum++;
  1224. //获取脸部矩形
  1225. CvRect* pFaceRect = (CvRect*)cvGetSeqElem(pfaceSeq, i);
  1226. //如果发现有人脸到达靠近区域,置有人靠近状态
  1227. if((pFaceRect->height >= (int)(m_nImgHeight/m_stFaceConfig.fCloseFaceSize))&&(pFaceRect->height <= (int)(m_nImgHeight/m_stFaceConfig.fOperateFaceSize)))
  1228. {
  1229. if((m_eMonitorState == NoBody) || (m_eMonitorState == SomebodyFar))
  1230. {
  1231. nCurafaceSerial = i;
  1232. m_eMonitorState = SomebodyClose; //进入有人靠近状态
  1233. }
  1234. m_pHostEvent->GenerateMonitorEvent(Close); //产生有人靠近事件
  1235. }
  1236. else if (pFaceRect->height >= (int)(m_nImgHeight/m_stFaceConfig.fOperateFaceSize))
  1237. {
  1238. if(m_eMonitorState != SomebodyOperate)
  1239. {
  1240. nCurafaceSerial = i;
  1241. m_eMonitorState = SomebodyOperate; //进入有人操作状态
  1242. }
  1243. m_pHostEvent->GenerateMonitorEvent(EnterOperate); //产生有人进入操作距离事件
  1244. }
  1245. else
  1246. {
  1247. if(m_eMonitorState == NoBody)
  1248. {
  1249. nCurafaceSerial = i;
  1250. m_eMonitorState = SomebodyFar; //进入远距离有人状态
  1251. }
  1252. else
  1253. {
  1254. m_eMonitorState = SomebodyFar; //进入远距离有人状态
  1255. }
  1256. m_pHostEvent->GenerateMonitorEvent(Appear); //产生有人出现事件
  1257. }
  1258. }
  1259. CvRect* pFaceRect = (CvRect*)cvGetSeqElem(pfaceSeq, nCurafaceSerial);
  1260. //截取人脸图像,放大截取区域,截取半身头像
  1261. CalcUpbodyRegion(pFaceRect);
  1262. //保存半身头像坐标
  1263. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect = *pFaceRect;
  1264. m_stAllFaceInfo.astFaceInfo[0].eCamera = m_eCamera;
  1265. }
  1266. else {
  1267. #ifdef DEVOPS_ON_PRD
  1268. #else
  1269. if (EnvironCamera == m_eCamera) {
  1270. m_pHostEvent->GenerateMonitorEvent(video_monitor_event_type::NoDetectBody, "上摄像头未检测到人脸"); //上摄像头未检测到人脸
  1271. }
  1272. else if(OperatorCamera == m_eCamera){
  1273. m_pHostEvent->GenerateMonitorEvent(video_monitor_event_type::NoDetectBody, "下摄像头未检测到人脸"); //下摄像头未检测到人脸
  1274. }
  1275. #endif
  1276. }
  1277. if (pfaceSeq != NULL){
  1278. cvReleaseMemStorage(&pfaceSeq->storage);
  1279. pfaceSeq = NULL;
  1280. }
  1281. cvResetImageROI(m_pProcessImg);
  1282. if(nFaceNum > 0){
  1283. //发现有人
  1284. m_stAllFaceInfo.nTatolFaceNum = nFaceNum;
  1285. return true;
  1286. }
  1287. else{
  1288. m_eMonitorState = NoBody;
  1289. return false;
  1290. }
  1291. }
  1292. //运动检测
  1293. bool MotionTrack(CameraEnum eCamera, int diff_threshold = 10)
  1294. {
  1295. if (!GetSpecifiedCameraImage(eCamera))
  1296. {
  1297. m_pHostApi->Debug(FACECAP_DEBUG, "运动跟踪获取图像失败");
  1298. return false;
  1299. }
  1300. cvResetImageROI(m_pProcessImg);
  1301. memset(&m_stObjTrackRslt,0,sizeof(CMotionTrackRslt));
  1302. const double MHI_DURATION = 0.5;
  1303. const double MAX_TIME_DELTA = 0.5;
  1304. const double MIN_TIME_DELTA = 0.05;
  1305. const int N = 3;
  1306. //ring image buffer
  1307. // temporary images
  1308. IplImage*dst = cvCreateImage( cvSize(m_pProcessImg->width,m_pProcessImg->height), 8, 1 );
  1309. cvZero( dst);
  1310. dst->origin = m_pProcessImg->origin;
  1311. //get current time in seconds
  1312. double timestamp = clock()/100.;
  1313. CvSize size = cvSize(m_pProcessImg->width,m_pProcessImg->height);
  1314. int i, idx1, idx2;
  1315. IplImage* silh;
  1316. IplImage* pyr = cvCreateImage( cvSize((size.width & -2)/2, (size.height & -2)/2), 8, 1 );
  1317. CvMemStorage *stor;
  1318. CvSeq *cont;
  1319. if( !m_pMhImg || m_pMhImg->width != size.width || m_pMhImg->height != size.height )
  1320. {
  1321. if( m_pMhBuf == 0 )
  1322. {
  1323. m_pMhBuf = (IplImage**)malloc(N*sizeof(m_pMhBuf[0]));
  1324. memset( m_pMhBuf, 0, N*sizeof(m_pMhBuf[0]));
  1325. }
  1326. for( i = 0; i < N; i++ )
  1327. {
  1328. cvReleaseImage( &m_pMhBuf[i] );
  1329. m_pMhBuf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
  1330. cvZero(m_pMhBuf[i]);
  1331. }
  1332. cvReleaseImage( &m_pMhImg );
  1333. m_pMhImg = cvCreateImage( size, IPL_DEPTH_32F, 1 );
  1334. //clear MHI at the beginning
  1335. cvZero( m_pMhImg );
  1336. }
  1337. //convert frame to grayscale
  1338. cvCvtColor( m_pProcessImg, m_pMhBuf[m_nLastBuf], CV_BGR2GRAY );
  1339. //cvSaveImage("0.bmp",m_pProcessImg);
  1340. idx1 = m_nLastBuf;
  1341. //index of (last - (N-1))th frame
  1342. idx2 = (m_nLastBuf + 1) % N;
  1343. m_nLastBuf = idx2;
  1344. // 做帧差
  1345. silh = m_pMhBuf[idx2];
  1346. //get difference between frames
  1347. cvAbsDiff( m_pMhBuf[idx1], m_pMhBuf[idx2], silh );
  1348. //cvSaveImage("Diff.bmp",silh);
  1349. //对差图像做二值化
  1350. cvThreshold( silh, silh, 10, 255, CV_THRESH_BINARY );
  1351. //cvSaveImage("Threshold.bmp",silh);
  1352. //更新运动历史图像
  1353. cvUpdateMotionHistory( silh, m_pMhImg, timestamp, MHI_DURATION );
  1354. cvCvtScale( m_pMhImg, dst, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION );
  1355. //cvCvtScale( m_pMhImg, dst, 255./MHI_DURATION, 0 );
  1356. // 中值滤波,消除小的噪声
  1357. cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 );
  1358. // 向下采样,去掉噪声
  1359. cvPyrDown( dst, pyr, 7 );
  1360. //闭运算,消除目标的不连续空洞
  1361. //进行闭运算
  1362. //IplConvKernel* element = cvCreateStructuringElementEx(20,20,1,1,CV_SHAPE_ELLIPSE);
  1363. //cvMorphologyEx(pyr,pyr,NULL,element,CV_MOP_CLOSE,1);
  1364. //膨胀
  1365. cvDilate( pyr, pyr, 0, 2 ); // 做膨胀操作,消除目标的不连续空洞
  1366. cvPyrUp( pyr, dst, 7 );
  1367. // 下面的程序段用来找到轮廓
  1368. stor = cvCreateMemStorage(0);
  1369. cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
  1370. // 找到所有轮廓
  1371. cvFindContours( dst, stor, &cont, sizeof(CvContour), CV_RETR_LIST , CV_LINK_RUNS, cvPoint(0,0));
  1372. cvReleaseImage(&dst);
  1373. //double fArea = 0.0;
  1374. // 直接使用CONTOUR中的矩形来画轮廓
  1375. for(;cont;cont = cont->h_next)
  1376. {
  1377. CvRect r = ((CvContour*)cont)->rect;
  1378. if((r.height * r.width > m_stFaceConfig.nContourMinAera)&&((r.height * r.width) != (m_nImgWidth*m_nImgHeight))) // 面积小的方形抛弃掉
  1379. //{
  1380. //fArea = fabs(cvContourArea( cont, CV_WHOLE_SEQ )); //获取当前轮廓面积
  1381. //if ((fArea > CONTOUR_MAX_AERA)&&(r.height * r.width != m_nImgWidth*m_nImgHeight))
  1382. {
  1383. m_stObjTrackRslt.ObjRect[m_stObjTrackRslt.nObjNum] = r;
  1384. m_stObjTrackRslt.nObjNum++;
  1385. }
  1386. //}
  1387. }
  1388. // free memory
  1389. cvReleaseMemStorage(&stor);
  1390. cvReleaseImage( &pyr );
  1391. cvResetImageROI(m_pProcessImg);
  1392. if (m_stObjTrackRslt.nObjNum >0)
  1393. {
  1394. return true;
  1395. }
  1396. else
  1397. {
  1398. return false;
  1399. }
  1400. }
  1401. //计算监控线程下一步
  1402. void VideoMonitorThreadNextStep()
  1403. {
  1404. if(m_stAllFaceInfo.astFaceInfo[0].eDistance == OperateDistance)
  1405. {
  1406. if (m_eMonitorState != SomebodyOperate)
  1407. {
  1408. m_eMonitorState = SomebodyOperate;
  1409. m_stAllFaceInfo.astFaceInfo[0].eScene = ForwardScened;
  1410. m_pHostEvent->GenerateMonitorEvent(EnterOperate); //产生有人进入操作距离事件
  1411. m_pHostApi->Debug(FACECAP_DEBUG, "有人进入操作距离!");
  1412. //获取UUID
  1413. if (m_stAllFaceInfo.astFaceInfo[0].FaceID == 0)
  1414. {
  1415. m_stAllFaceInfo.astFaceInfo[0].FaceID = m_pHostApi->GenerateUUID();
  1416. }
  1417. }
  1418. else
  1419. {
  1420. if(m_stAllFaceInfo.astFaceInfo[0].eScene != LockScene)
  1421. {
  1422. m_eMonitorState = SomebodyOperate;
  1423. m_stAllFaceInfo.astFaceInfo[0].eScene = LockScene;
  1424. }
  1425. m_pHostEvent->GenerateMonitorEvent(CaptureFace); //发送捕获人脸事件
  1426. m_pHostApi->Debug(FACECAP_DEBUG, "锁定人脸!");
  1427. }
  1428. }
  1429. else if(m_stAllFaceInfo.astFaceInfo[0].eDistance == FarDistance)
  1430. {
  1431. if((m_eMonitorState == SomebodyOperate)||(m_eMonitorState == SomebodyClose))
  1432. {
  1433. m_stAllFaceInfo.astFaceInfo[0].eScene = UnLockScene;
  1434. m_eMonitorState = SomebodyFar;
  1435. memset(&m_stAllFaceInfo,0,sizeof(CAllFaceInfo));
  1436. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.height = m_nImgHeight/2;
  1437. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.width = m_nImgWidth/2;
  1438. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.x = m_nImgHeight/4;
  1439. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.y = m_nImgWidth/4;
  1440. m_pHostEvent->GenerateMonitorEvent(Leave); //产生客户离开事件
  1441. m_pHostApi->Debug(FACECAP_DEBUG, "客户离开!");
  1442. //清除UUID
  1443. m_stAllFaceInfo.astFaceInfo[0].FaceID = 0;
  1444. }
  1445. }
  1446. else if (m_stAllFaceInfo.astFaceInfo[0].eDistance == CloseDistance)
  1447. {
  1448. if (SomebodyOperate == m_eMonitorState)
  1449. {
  1450. m_stAllFaceInfo.astFaceInfo[0].eScene = BackwardScened;
  1451. m_eMonitorState = SomebodyClose;
  1452. m_pHostEvent->GenerateMonitorEvent(BackToClose); //产生客户退回到接近距离事件
  1453. m_pHostApi->Debug(FACECAP_DEBUG, "客户退回到接近距离!");
  1454. }
  1455. else if(SomebodyFar == m_eMonitorState)
  1456. {
  1457. m_stAllFaceInfo.astFaceInfo[0].eScene = ForwardScened;
  1458. m_eMonitorState = SomebodyClose;
  1459. m_pHostApi->Debug(FACECAP_DEBUG, "有人靠近!");
  1460. //获取UUID
  1461. if (m_stAllFaceInfo.astFaceInfo[0].FaceID == 0)
  1462. {
  1463. m_stAllFaceInfo.astFaceInfo[0].FaceID = m_pHostApi->GenerateUUID();
  1464. }
  1465. }
  1466. m_pHostEvent->GenerateMonitorEvent(Close); //产生有人靠近事件
  1467. }
  1468. }
  1469. // 计算当前人脸是否进入另外一个相机的最佳视野中
  1470. bool IsFacePosOverStep(CameraEnum eCamera,CvRect FaceRect)
  1471. {
  1472. if(m_pOperatorVideoQueue == NULL)
  1473. {
  1474. return false;
  1475. }
  1476. if(EnvironCamera == eCamera)
  1477. {
  1478. if(((FaceRect.x > (m_nImgWidth/2-m_nImgHeight/2+m_nImgHeight/m_stFaceConfig.fDetectFaceSize/2))|| (FaceRect.x < (m_nImgWidth/2+m_nImgHeight/2-m_nImgHeight/m_stFaceConfig.fDetectFaceSize/2)))&&
  1479. (FaceRect.y > (m_nImgHeight-m_nImgHeight/m_stFaceConfig.fDetectFaceSize*4/m_stFaceConfig.nUpCameraEdgeLimit)))
  1480. {
  1481. return true;
  1482. }
  1483. else
  1484. {
  1485. return false;
  1486. }
  1487. }
  1488. else if(OperatorCamera == eCamera)
  1489. {
  1490. if(FaceRect.y < ((m_nImgHeight/m_stFaceConfig.fDetectFaceSize)*1/m_stFaceConfig.nDownCameraEdgeLimit))
  1491. {
  1492. return true;
  1493. }
  1494. else
  1495. {
  1496. return false;
  1497. }
  1498. }
  1499. else
  1500. {
  1501. return false;
  1502. }
  1503. }
  1504. //void SetUpperbodyToCenter(CameraEnum eCamera)
  1505. //{
  1506. // int nImgWidth,nImgHeight;
  1507. // m_stAllFaceInfo.astFaceInfo[0].eCamera = eCamera;
  1508. // m_pHostApi->Debug(FACECAP_DEBUG, "%s:%d set m_stAllFaceInfo.astFaceInfo[0].eCamera = %d.", __FUNCTION__, __LINE__, eCamera);
  1509. // if (m_stAllFaceInfo.astFaceInfo[0].eCamera == EnvironCamera)
  1510. // {
  1511. // if (m_pEnvironVideoQueue == NULL)
  1512. // {
  1513. // return;
  1514. // }
  1515. // if (m_pEnvironVideoQueue->GetVideoLens()<=0)
  1516. // {
  1517. // m_pHostApi->Debug(FACECAP_DEBUG, "获取环境相机图像队列长度错误!");
  1518. // return;
  1519. // }
  1520. // int size = m_pEnvironVideoQueue->GetFrameSize(nImgWidth,nImgHeight);
  1521. // }
  1522. // else if(m_stAllFaceInfo.astFaceInfo[0].eCamera == OperatorCamera)
  1523. // {
  1524. // if (m_pOperatorVideoQueue == NULL)
  1525. // {
  1526. // return;
  1527. // }
  1528. // if (m_pOperatorVideoQueue->GetVideoLens()<=0)
  1529. // {
  1530. // m_pHostApi->Debug(FACECAP_DEBUG, "获取操作相机图像队列长度错误!");
  1531. // return ;
  1532. // }
  1533. // int size = m_pOperatorVideoQueue->GetFrameSize(nImgWidth,nImgHeight);
  1534. // }
  1535. // m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.height = nImgHeight/2;
  1536. // m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.width = nImgWidth/2;
  1537. // m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.x = nImgWidth/4;
  1538. // m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.y = nImgHeight/4;
  1539. // WriteAllFaceInfo(m_stAllFaceInfo);
  1540. //}
  1541. //根据主用户设置人脸剪辑区域
  1542. void SetUpperbodyToCenter()
  1543. {
  1544. if(OperatorCamera == m_stFaceConfig.nPrimCamera)
  1545. {
  1546. if (m_pOperatorVideoQueue == NULL){
  1547. m_stAllFaceInfo.astFaceInfo[0].eCamera = EnvironCamera;
  1548. }
  1549. else{
  1550. m_stAllFaceInfo.astFaceInfo[0].eCamera = OperatorCamera;
  1551. }
  1552. }
  1553. else{
  1554. m_stAllFaceInfo.astFaceInfo[0].eCamera = EnvironCamera;
  1555. }
  1556. int nImgWidth = 0, nImgHeight = 0;
  1557. if (EnvironCamera == m_stAllFaceInfo.astFaceInfo[0].eCamera){
  1558. if (m_pEnvironVideoQueue == NULL){
  1559. return;
  1560. }
  1561. if (m_pEnvironVideoQueue->GetVideoLens() <= 0){
  1562. m_pHostApi->Debug(FACECAP_DEBUG, "环境相机图像队列为空");
  1563. return;
  1564. }
  1565. int isize = m_pEnvironVideoQueue->GetFrameSize(nImgWidth, nImgHeight);
  1566. if (0 == isize) {
  1567. return;
  1568. }
  1569. }
  1570. else if(OperatorCamera == m_stAllFaceInfo.astFaceInfo[0].eCamera){
  1571. if (m_pOperatorVideoQueue == NULL){
  1572. return;
  1573. }
  1574. if (m_pOperatorVideoQueue->GetVideoLens() <= 0){
  1575. m_pHostApi->Debug(FACECAP_DEBUG, "操作相机图像队列为空");
  1576. return ;
  1577. }
  1578. int isize = m_pOperatorVideoQueue->GetFrameSize(nImgWidth, nImgHeight);
  1579. if (0 == isize) {
  1580. return;
  1581. }
  1582. }
  1583. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.height = nImgHeight/2;
  1584. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.width = nImgWidth/2;
  1585. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.x = nImgWidth/4;
  1586. m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect.y = nImgHeight/4;
  1587. WriteAllFaceInfo(m_stAllFaceInfo);
  1588. }
  1589. bool RvcTimeout(int ims)
  1590. {
  1591. bool btimeout = true;
  1592. #ifdef _WIN32
  1593. DWORD dwRet = WaitForSingleObject(m_hStopEventWait, ims);
  1594. if (WAIT_TIMEOUT == dwRet) {
  1595. }
  1596. else if (WAIT_OBJECT_0 == dwRet) {
  1597. btimeout = false;
  1598. }
  1599. #else
  1600. struct timespec ts;
  1601. clock_gettime(CLOCK_REALTIME, &ts);
  1602. long unsec = ts.tv_nsec + (1000 * 1000 * ims);
  1603. ts.tv_sec += (unsec / 1000000000);
  1604. ts.tv_nsec = (unsec % 1000000000);
  1605. int itimeout = sem_timedwait(&m_semt, &ts);
  1606. if (0 != itimeout && (ETIMEDOUT == errno)) {
  1607. }
  1608. else if (0 == itimeout) {
  1609. btimeout = false;
  1610. }
  1611. #endif
  1612. return btimeout;
  1613. }
  1614. void ReleaseMotionTrackRst()
  1615. {
  1616. if (m_pMhImg)
  1617. {
  1618. cvReleaseImage(&m_pMhImg);
  1619. m_pMhImg = NULL;
  1620. }
  1621. if(m_pMhBuf)
  1622. {
  1623. for (int i=0;i<3;i++)
  1624. {
  1625. if (m_pMhBuf[i] != NULL)
  1626. {
  1627. cvReleaseImage(&m_pMhBuf[i]);
  1628. m_pMhBuf[i] = NULL;
  1629. }
  1630. }
  1631. free(m_pMhBuf);
  1632. m_pMhBuf = NULL;
  1633. }
  1634. m_nLastBuf=0;
  1635. }
  1636. CameraEnum GetInitPrimCamera()
  1637. {
  1638. CameraEnum eCamera = (CameraEnum)m_stFaceConfig.nPrimCamera;
  1639. if (NULL == m_pOperatorVideoQueue)
  1640. {
  1641. if ((m_nCameraState == 0) || (m_nCameraState == 2))
  1642. {
  1643. eCamera = EnvironCamera;
  1644. }
  1645. else if (m_nCameraState == 1)
  1646. {
  1647. eCamera = OperatorCamera;
  1648. }
  1649. else if (m_nCameraState == 3)
  1650. {
  1651. eCamera = ErrorCamera;
  1652. }
  1653. }
  1654. return eCamera;
  1655. }
  1656. int VideoMonitor()
  1657. {
  1658. bool bRst = false;
  1659. uint32_t nSearchFaceFailNum = 0; //搜索人脸失败次数
  1660. uint32_t nDetectFailNum = 0; //检测人脸失败的次数
  1661. uint32_t nMotionTrackNum = 0;
  1662. //select camera
  1663. m_eCamera = GetInitPrimCamera();
  1664. uint32_t iwaittime = m_stFaceConfig.nSleepShort;
  1665. while (!m_bStopVieoMonitor)
  1666. {
  1667. if(RvcTimeout(iwaittime))
  1668. {
  1669. //如果摄像头没启动或故障,停止扫描,休眠2s
  1670. if (3 == m_nCameraState){
  1671. iwaittime = 2000;
  1672. m_pHostApi->Debug(FACECAP_DEBUG, "camera stop or all camera error!");
  1673. continue;
  1674. }
  1675. //灯光变化时,清空历史记录,防止误判
  1676. if (m_bLightChange){
  1677. ReleaseMotionTrackRst();
  1678. nMotionTrackNum = 0;
  1679. m_bLightChange = false;
  1680. }
  1681. //if no people,start motion tracker
  1682. if(NoBody == m_eMonitorState){
  1683. m_pHostApi->Debug(FACECAP_DEBUG, "NoBody");
  1684. bRst = GetSpecifiedCameraImage(m_eCamera);
  1685. if (bRst){
  1686. int size = (int)m_nImgHeight/m_stFaceConfig.fSearchFaceSize;
  1687. bRst = FaceSearchWithSpecifiedSize(size);
  1688. if (!bRst){
  1689. SetUpperbodyToCenter();
  1690. iwaittime = m_stFaceConfig.nSleepLong;
  1691. SwitchDetectCamera();
  1692. }
  1693. else{
  1694. WriteAllFaceInfo(m_stAllFaceInfo); //保存人脸坐标信息
  1695. iwaittime = m_stFaceConfig.nSleepMiddle;
  1696. }
  1697. }
  1698. else{
  1699. iwaittime = m_stFaceConfig.nSleepMiddle;
  1700. SwitchDetectCamera();
  1701. }
  1702. }
  1703. else if (SomebodyFar == m_eMonitorState) //if somebody space 2.5m
  1704. {
  1705. m_pHostApi->Debug(FACECAP_DEBUG, "SomebodyFar");
  1706. bRst = GetSpecifiedCameraImage(m_eCamera);
  1707. if (bRst){
  1708. int size = (int)m_nImgHeight/m_stFaceConfig.fSearchFaceSize;
  1709. bRst = FaceSearchWithSpecifiedSize(size);
  1710. if (!bRst){
  1711. SetUpperbodyToCenter();
  1712. iwaittime = m_stFaceConfig.nSleepLong;
  1713. SwitchDetectCamera();
  1714. }
  1715. else{
  1716. WriteAllFaceInfo(m_stAllFaceInfo);
  1717. iwaittime = m_stFaceConfig.nSleepShort;
  1718. }
  1719. }
  1720. else{
  1721. iwaittime = m_stFaceConfig.nSleepMiddle;
  1722. SwitchDetectCamera();
  1723. }
  1724. }
  1725. else if (SomebodyClose == m_eMonitorState) //close distance,1.5m
  1726. {
  1727. m_pHostApi->Debug(FACECAP_DEBUG, "SomebodyClose");
  1728. bRst = FaceDetectWithSpecifiedCamera(m_eCamera);
  1729. if(!bRst){
  1730. if(NULL != m_pOperatorVideoQueue){
  1731. SwitchDetectCamera();
  1732. bRst = FaceDetectWithSpecifiedCamera(m_eCamera);
  1733. if (!bRst){
  1734. nSearchFaceFailNum++;
  1735. //if search face fail, return motiontracker
  1736. if(nSearchFaceFailNum > 2){
  1737. SetUpperbodyToCenter();
  1738. m_eMonitorState = NoBody;
  1739. m_pHostEvent->GenerateMonitorEvent(Leave);
  1740. m_pHostApi->Debug(FACECAP_DEBUG, "客户离开!");
  1741. nSearchFaceFailNum = 0;
  1742. iwaittime = m_stFaceConfig.nSleepLong;
  1743. }
  1744. else{
  1745. iwaittime = m_stFaceConfig.nSleepMiddle;
  1746. }
  1747. }
  1748. else{
  1749. nSearchFaceFailNum = 0;
  1750. VideoMonitorThreadNextStep();
  1751. WriteAllFaceInfo(m_stAllFaceInfo);
  1752. iwaittime = m_stFaceConfig.nSleepShort;
  1753. }
  1754. }
  1755. else //single camera mod
  1756. {
  1757. nSearchFaceFailNum++;
  1758. //if search face fail,return motiontracker
  1759. if(nSearchFaceFailNum > 2){
  1760. SetUpperbodyToCenter();
  1761. m_eMonitorState = NoBody;
  1762. m_pHostEvent->GenerateMonitorEvent(Leave);
  1763. m_pHostApi->Debug(FACECAP_DEBUG, "客户离开!");
  1764. nSearchFaceFailNum = 0;
  1765. iwaittime = m_stFaceConfig.nSleepLong;
  1766. }
  1767. else{
  1768. iwaittime = m_stFaceConfig.nSleepMiddle;
  1769. }
  1770. }
  1771. }
  1772. else
  1773. {
  1774. //if curcamera search face success, judge which camera result is the best
  1775. m_pHostApi->Debug(FACECAP_DEBUG, "Close distance, %s Search face Success", (m_eCamera == EnvironCamera) ? "EnvironCamera" : "OperatorCamera");
  1776. if((m_pOperatorVideoQueue != NULL)&&(m_pEnvironVideoQueue != NULL))
  1777. {
  1778. SwitchDetectCamera();
  1779. CAllFaceInfo stFaceInfoTmp = m_stAllFaceInfo;
  1780. if (FaceDetectWithSpecifiedCamera(m_eCamera))
  1781. {
  1782. int CurFaceArea = m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.width*m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.height;
  1783. int FrontFaceArea = stFaceInfoTmp.astFaceInfo[0].stRegion.stFaceRect.width*stFaceInfoTmp.astFaceInfo[0].stRegion.stFaceRect.height;
  1784. //if cur face area less than front face,select front Face
  1785. if (FrontFaceArea > CurFaceArea){
  1786. SwitchDetectCamera();
  1787. m_stAllFaceInfo = stFaceInfoTmp;
  1788. }
  1789. }
  1790. else{
  1791. SwitchDetectCamera();
  1792. m_stAllFaceInfo = stFaceInfoTmp;
  1793. }
  1794. }
  1795. nSearchFaceFailNum = 0;
  1796. VideoMonitorThreadNextStep();
  1797. WriteAllFaceInfo(m_stAllFaceInfo);
  1798. iwaittime = m_stFaceConfig.nSleepShort;
  1799. }
  1800. }
  1801. else if (SomebodyOperate == m_eMonitorState) //当有人进入操作距离时
  1802. {
  1803. m_pHostApi->Debug(FACECAP_DEBUG, "SomebodyOperate");
  1804. //如果是双相机模式
  1805. if (m_pOperatorVideoQueue != NULL){
  1806. m_eCamera = m_stAllFaceInfo.astFaceInfo[0].eCamera;
  1807. //先搜索上次搜索到人脸的区域
  1808. if(m_stAllFaceInfo.nTatolFaceNum > 0){
  1809. m_pHostApi->Debug(FACECAP_DEBUG, "Operator distance,CurCamera search last face Region");
  1810. bRst = FaceDetectWithSpecifiedCameraRegion(m_eCamera, m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect);
  1811. }
  1812. else{
  1813. bRst = false;
  1814. }
  1815. if(!bRst){
  1816. m_pHostApi->Debug(FACECAP_DEBUG, "Operator distance,search Face by CurCamera");
  1817. bRst = FaceDetectWithSpecifiedCamera(m_eCamera);
  1818. }
  1819. }
  1820. else //如果单相机模式
  1821. {
  1822. //先搜索上次搜索到人脸的区域
  1823. if(m_stAllFaceInfo.nTatolFaceNum > 0){
  1824. m_pHostApi->Debug(FACECAP_DEBUG, "Operator distance, %s Search Last face Region", (m_eCamera == EnvironCamera) ? "EnvironCamera" : "OperatorCamera");
  1825. bRst = FaceDetectWithSpecifiedCameraRegion(m_eCamera, m_stAllFaceInfo.astFaceInfo[0].stRegion.stUpperBodyRect);
  1826. }
  1827. else{
  1828. bRst = false;
  1829. }
  1830. if(!bRst){
  1831. bRst = FaceDetectWithSpecifiedCamera(m_eCamera);
  1832. }
  1833. }
  1834. if(bRst) //如果在当前相机中找到人脸
  1835. {
  1836. nDetectFailNum = 0;
  1837. //如果是双相机模式,
  1838. if(m_pOperatorVideoQueue!=NULL)
  1839. {
  1840. //找到人脸后,检测另一个相机中是否有更大的人脸
  1841. SwitchDetectCamera();
  1842. CAllFaceInfo stFaceInfoTmp = m_stAllFaceInfo;
  1843. if (FaceDetectWithSpecifiedCamera(m_eCamera))
  1844. {
  1845. int CurFaceArea = m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.width*m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect.height;
  1846. int FrontFaceArea = stFaceInfoTmp.astFaceInfo[0].stRegion.stFaceRect.width*stFaceInfoTmp.astFaceInfo[0].stRegion.stFaceRect.height;
  1847. //if cur face area less than front face,select front Face
  1848. if (FrontFaceArea > CurFaceArea){
  1849. SwitchDetectCamera();
  1850. m_stAllFaceInfo = stFaceInfoTmp;
  1851. }
  1852. }
  1853. else{
  1854. SwitchDetectCamera();
  1855. m_stAllFaceInfo = stFaceInfoTmp;
  1856. }
  1857. //判断用户是否进入另外一个相机的视野,如果进入另一个相机的最佳视野
  1858. if((m_pEnvironVideoQueue!=NULL)&&IsFacePosOverStep(m_eCamera,m_stAllFaceInfo.astFaceInfo[0].stRegion.stFaceRect))
  1859. {
  1860. stFaceInfoTmp = m_stAllFaceInfo; //暂存当前结果
  1861. memset(&m_stAllFaceInfo,0,sizeof(CAllFaceInfo));
  1862. SwitchDetectCamera();
  1863. //在另一个相机中重新搜索
  1864. m_pHostApi->Debug(FACECAP_DEBUG, "Operator distance, Change camera to:%s search Face", (m_eCamera == EnvironCamera) ? "EnvironCamera" : "OperatorCamera");
  1865. bRst = FaceDetectWithSpecifiedCamera(m_eCamera);
  1866. if(!bRst)
  1867. {
  1868. SwitchDetectCamera();
  1869. m_stAllFaceInfo = stFaceInfoTmp;
  1870. }
  1871. }
  1872. }
  1873. VideoMonitorThreadNextStep();
  1874. WriteAllFaceInfo(m_stAllFaceInfo);
  1875. iwaittime = m_stFaceConfig.nSleepLong;
  1876. }
  1877. else //如果在当前相机中未找到用户
  1878. {
  1879. //如果双相机模式
  1880. if(m_pOperatorVideoQueue!=NULL)
  1881. {
  1882. SwitchDetectCamera();
  1883. m_pHostApi->Debug(FACECAP_DEBUG, "Operator distance, Change camera to:%s search Face", (m_eCamera == EnvironCamera) ? "EnvironCamera" : "OperatorCamera");
  1884. bRst = FaceDetectWithSpecifiedCamera(m_eCamera);
  1885. if(bRst)
  1886. {
  1887. nDetectFailNum = 0;
  1888. VideoMonitorThreadNextStep();
  1889. WriteAllFaceInfo(m_stAllFaceInfo);
  1890. m_pHostApi->Debug(FACECAP_DEBUG, "Write Face info Success");
  1891. iwaittime = m_stFaceConfig.nSleepLong;
  1892. }
  1893. else
  1894. {
  1895. nDetectFailNum++;
  1896. if(nDetectFailNum >2) //如果连续2次未找到人脸,回到有人靠近状态
  1897. {
  1898. m_eMonitorState = SomebodyClose;
  1899. m_stAllFaceInfo.astFaceInfo[0].eScene = UnLockScene;
  1900. m_pHostEvent->GenerateMonitorEvent(LoseFace); //发送失去人脸事件
  1901. m_pHostApi->Debug(FACECAP_DEBUG, "人脸失去锁定!");
  1902. nDetectFailNum = 0;
  1903. }
  1904. iwaittime = m_stFaceConfig.nSleepShort;
  1905. }
  1906. }
  1907. else //如果单相机模式
  1908. {
  1909. nDetectFailNum++;
  1910. if(nDetectFailNum > 3) //如果连续3次未找到人脸,回到有人靠近状态
  1911. {
  1912. m_eMonitorState = SomebodyClose;
  1913. m_stAllFaceInfo.astFaceInfo[0].eScene = UnLockScene;
  1914. m_pHostEvent->GenerateMonitorEvent(LoseFace); //发送失去人脸事件
  1915. m_pHostApi->Debug(FACECAP_DEBUG, "人脸失去锁定!");
  1916. nDetectFailNum = 0;
  1917. }
  1918. iwaittime = m_stFaceConfig.nSleepShort;
  1919. }
  1920. }
  1921. }
  1922. }
  1923. else
  1924. {
  1925. //清空存储区
  1926. if (pfaceSeq != NULL){
  1927. cvReleaseMemStorage(&pfaceSeq->storage);
  1928. pfaceSeq = NULL;
  1929. }
  1930. if (NULL != m_pProcessImg){
  1931. cvResetImageROI(m_pProcessImg);
  1932. }
  1933. m_pHostApi->Debug(FACECAP_DEBUG, "stop face tracking..");
  1934. break;
  1935. }
  1936. }
  1937. return 0;
  1938. }
  1939. #ifdef _WIN32
  1940. static unsigned int __stdcall VideoMonitorThread(void* pParam)
  1941. {
  1942. Clibfacecapture_impl* VideoFaceMonitor = (Clibfacecapture_impl*)pParam;
  1943. return VideoFaceMonitor->VideoMonitor();
  1944. }
  1945. #else
  1946. static void* videomonitorfunc(void* pParam)
  1947. {
  1948. Clibfacecapture_impl* VideoFaceMonitor = (Clibfacecapture_impl*)pParam;
  1949. VideoFaceMonitor->VideoMonitor();
  1950. return ((void*)0);
  1951. }
  1952. #endif
  1953. public:
  1954. //开始视频监控,pHost:事件,nMonitorFreq:监控频率,单位ms,MonitorType:监控类型,有三种模式可供选择
  1955. bool StartVideoMonitor(CHostApi *pHostAPI, CVideoMonitorEvent*pHost, MonitorEnum eMonitorType)
  1956. {
  1957. #ifdef _WIN32
  1958. ResetEvent(m_hStopEventWait);
  1959. if (NULL == m_hVieoMonitorThread){
  1960. m_pHostApi->Debug(FACECAP_DEBUG, "create video monitor thread.");
  1961. m_hVieoMonitorThread = (HANDLE)_beginthreadex(NULL, 0, VideoMonitorThread, this, 0, (unsigned int*)&m_nVieoMonitorThreadId);
  1962. m_bStopVieoMonitor = false;
  1963. m_eMonitorState = MonitorStateEnum::NoBody;
  1964. }
  1965. #else
  1966. if (0 == pthread_create(&m_videomonitorthreadid, NULL, videomonitorfunc, (void*)this)){
  1967. m_pHostApi->Debug(FACECAP_DEBUG, "create video monitor thread and thread id is %u.", m_videomonitorthreadid);
  1968. m_bStopVieoMonitor = false;
  1969. m_eMonitorState = MonitorStateEnum::NoBody;
  1970. }
  1971. else {
  1972. m_pHostApi->Debug(FACECAP_INFO, "create video monitor thread failed.");
  1973. }
  1974. #endif
  1975. return true;
  1976. }
  1977. //停止视频监控
  1978. bool StopVideoMonitor()
  1979. {
  1980. m_bStopVieoMonitor = true;
  1981. #ifdef _WIN32
  1982. SetEvent(m_hStopEventWait);
  1983. DWORD ret = WaitForSingleObject(m_hVieoMonitorThread, INFINITE);
  1984. if (WAIT_OBJECT_0 == ret) {
  1985. m_pHostApi->Debug(FACECAP_DEBUG, "normal ....");
  1986. }
  1987. else if (WAIT_ABANDONED == ret) {
  1988. m_pHostApi->Debug(FACECAP_DEBUG, "abandoned ....");
  1989. }
  1990. CloseHandle(m_hVieoMonitorThread);
  1991. m_hVieoMonitorThread = NULL;
  1992. #else
  1993. sem_post(&m_semt);
  1994. if (0 == pthread_join(m_videomonitorthreadid, NULL)) {
  1995. m_pHostApi->Debug(FACECAP_DEBUG, "thread join video monitor thread %u success!", m_videomonitorthreadid);
  1996. m_videomonitorthreadid = 0;
  1997. }
  1998. else{
  1999. m_pHostApi->Debug(FACECAP_INFO, "thread join video monitor thread failed!");
  2000. }
  2001. #endif
  2002. return true;
  2003. }
  2004. //有多少其他用户
  2005. bool GetCustomerNum(uint32_t& num)
  2006. {
  2007. #ifdef _WIN32
  2008. EnterCriticalSection(&CS);
  2009. #else
  2010. pthread_mutex_lock(&cs_mutex);
  2011. #endif
  2012. num = m_stFaceRstStorage.nTatolFaceNum;
  2013. #ifdef _WIN32
  2014. LeaveCriticalSection(&CS);
  2015. #else
  2016. pthread_mutex_unlock(&cs_mutex);
  2017. #endif
  2018. return true;
  2019. }
  2020. //获取主用户的常规属性
  2021. bool GetMainCustomerStatus(uint64_t &FaceID, CCustomerStatus &Property)
  2022. {
  2023. #ifdef _WIN32
  2024. EnterCriticalSection(&CS);
  2025. #else
  2026. pthread_mutex_lock(&cs_mutex);
  2027. #endif
  2028. //Property.eBrightness = m_stFaceRstStorage.astFaceInfo[0].eBrightness;
  2029. Property.stCustomerCover = m_stFaceRstStorage.astFaceInfo[0].stCustomerCover;
  2030. Property.stCustomerPos.eCamera = m_stFaceRstStorage.astFaceInfo[0].eCamera;//1
  2031. Property.stCustomerPos.eDistance = m_stFaceRstStorage.astFaceInfo[0].eDistance;
  2032. Property.stCustomerPos.ePose = m_stFaceRstStorage.astFaceInfo[0].ePose;
  2033. Property.stCustomerPos.eScene = m_stFaceRstStorage.astFaceInfo[0].eScene;
  2034. Property.stCustomerPos.stRange.sX = m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.x + (short)m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.width/2 - (short)m_nImgWidth/2;
  2035. Property.stCustomerPos.stRange.sY = m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.y + (short)m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.height/2 - (short)m_nImgHeight/2;
  2036. Property.stCustomerPos.stRange.sH = (short)m_stFaceRstStorage.astFaceInfo[0].stRegion.stUpperBodyRect.height/2;
  2037. FaceID = m_stFaceRstStorage.astFaceInfo[0].FaceID;
  2038. #ifdef _WIN32
  2039. LeaveCriticalSection(&CS);
  2040. #else
  2041. pthread_mutex_unlock(&cs_mutex);
  2042. #endif
  2043. return true;
  2044. }
  2045. //获取用户的常规属性
  2046. bool GetCustomerStatus(uint64_t FaceID, CCustomerStatus &Property)
  2047. {
  2048. #ifdef _WIN32
  2049. EnterCriticalSection(&CS);
  2050. #else
  2051. pthread_mutex_lock(&cs_mutex);
  2052. #endif
  2053. bool bSearch = false;
  2054. for (int i =0;i<m_stFaceRstStorage.nTatolFaceNum&&!bSearch;i++)
  2055. {
  2056. if(m_stFaceRstStorage.astFaceInfo[i].FaceID == FaceID)
  2057. {
  2058. Property.eBrightness = m_stFaceRstStorage.astFaceInfo[i].eBrightness;
  2059. Property.stCustomerCover = m_stFaceRstStorage.astFaceInfo[i].stCustomerCover;
  2060. Property.stCustomerPos.eCamera = m_stFaceRstStorage.astFaceInfo[i].eCamera;
  2061. Property.stCustomerPos.eDistance = m_stFaceRstStorage.astFaceInfo[i].eDistance;
  2062. Property.stCustomerPos.ePose = m_stFaceRstStorage.astFaceInfo[i].ePose;
  2063. Property.stCustomerPos.eScene = m_stFaceRstStorage.astFaceInfo[i].eScene;
  2064. Property.stCustomerPos.stRange.sX = m_stFaceRstStorage.astFaceInfo[i].stRegion.stUpperBodyRect.x
  2065. +(short)m_stFaceRstStorage.astFaceInfo[i].stRegion.stUpperBodyRect.width/2-(short)m_nImgWidth/2;
  2066. Property.stCustomerPos.stRange.sY = m_stFaceRstStorage.astFaceInfo[i].stRegion.stUpperBodyRect.y
  2067. +(short)m_stFaceRstStorage.astFaceInfo[i].stRegion.stUpperBodyRect.height/2-(short)m_nImgHeight/2;
  2068. Property.stCustomerPos.stRange.sH = (short)m_stFaceRstStorage.astFaceInfo[i].stRegion.stUpperBodyRect.height/2;
  2069. bSearch = true;
  2070. }
  2071. }
  2072. #ifdef _WIN32
  2073. LeaveCriticalSection(&CS);
  2074. #else
  2075. pthread_mutex_unlock(&cs_mutex);
  2076. #endif
  2077. return true;
  2078. }
  2079. //获取所有用户FACEID
  2080. bool GetTrackFace(uint64_t*arrFaceIDs, int *size)
  2081. {
  2082. #ifdef _WIN32
  2083. EnterCriticalSection(&CS);
  2084. #else
  2085. pthread_mutex_lock(&cs_mutex);
  2086. #endif
  2087. for (int i = 0;i<m_stFaceRstStorage.nTatolFaceNum && i < *size;i++)
  2088. {
  2089. arrFaceIDs[i] = m_stFaceRstStorage.astFaceInfo[i].FaceID;
  2090. }
  2091. #ifdef _WIN32
  2092. LeaveCriticalSection(&CS);
  2093. #else
  2094. pthread_mutex_unlock(&cs_mutex);
  2095. #endif
  2096. return true;
  2097. }
  2098. //获取用户的交互属性
  2099. bool GetCustomerAppearance(uint64_t FaceID,CCustomerAppearance &Property)
  2100. {
  2101. #ifdef _WIN32
  2102. EnterCriticalSection(&CS);
  2103. #else
  2104. pthread_mutex_lock(&cs_mutex);
  2105. #endif
  2106. bool bSearch = false;
  2107. for (int i =0;i<m_stFaceRstStorage.nTatolFaceNum&&!bSearch;i++)
  2108. {
  2109. if(m_stFaceRstStorage.astFaceInfo[i].FaceID == FaceID)
  2110. {
  2111. Property.eHeight =m_stFaceRstStorage.astFaceInfo[i].eHeight;
  2112. Property.eSex = m_stFaceRstStorage.astFaceInfo[i].eSex;
  2113. Property.eYoungOld = m_stFaceRstStorage.astFaceInfo[i].eYoungOld;
  2114. bSearch = true;
  2115. }
  2116. }
  2117. #ifdef _WIN32
  2118. LeaveCriticalSection(&CS);
  2119. #else
  2120. pthread_mutex_unlock(&cs_mutex);
  2121. #endif
  2122. return true;
  2123. }
  2124. };
  2125. // 这是已导出类的构造函数。
  2126. // 有关类定义的信息,请参阅 libfacecapture.h
  2127. Clibfacecapture::Clibfacecapture(bool *pResult, CHostApi *pHostAPI, CVideoMonitorEvent*pHost, const char* EnvironVideoName, const char* OperateVideoName, MonitorEnum eMonitorType)
  2128. {
  2129. m_pImpl = new Clibfacecapture_impl(pResult, pHostAPI, pHost, EnvironVideoName, OperateVideoName, eMonitorType);
  2130. }
  2131. Clibfacecapture::~Clibfacecapture(void)
  2132. {
  2133. delete m_pImpl;
  2134. }
  2135. //有多少其他用户
  2136. bool Clibfacecapture::GetCustomerNum(uint32_t& num)
  2137. {
  2138. return m_pImpl->GetCustomerNum(num);
  2139. }
  2140. //获取主用户的常规属性
  2141. bool Clibfacecapture::GetMainCustomerStatus(uint64_t &FaceID,CCustomerStatus &Property)
  2142. {
  2143. return m_pImpl->GetMainCustomerStatus(FaceID,Property);
  2144. }
  2145. //获取其他用户的常规属性
  2146. bool Clibfacecapture::GetCustomerStatus (uint64_t FaceID,CCustomerStatus &Property)
  2147. {
  2148. return m_pImpl->GetCustomerStatus(FaceID,Property);
  2149. }
  2150. //获取主用户的交互属性
  2151. bool Clibfacecapture::GetCustomerAppearance(uint64_t FaceID,CCustomerAppearance &Property)
  2152. {
  2153. return m_pImpl->GetCustomerAppearance(FaceID,Property);
  2154. }
  2155. //获取所有用户FACEID
  2156. bool Clibfacecapture::GetTrackFace(uint64_t*arrFaceIDs, int *size)
  2157. {
  2158. return m_pImpl->GetTrackFace(arrFaceIDs, size);
  2159. }
  2160. //灯光变化
  2161. void Clibfacecapture::SetLightChange()
  2162. {
  2163. m_pImpl->m_bLightChange = true;
  2164. }
  2165. //相机状态
  2166. void Clibfacecapture::SetCameraState(int nState)
  2167. {
  2168. m_pImpl->m_nCameraState = nState;
  2169. }
  2170. bool Clibfacecapture::StartFaceCapture()
  2171. {
  2172. return m_pImpl->StartFaceCapture();
  2173. }
  2174. bool Clibfacecapture::StopFaceCapture()
  2175. {
  2176. return m_pImpl->StopFaceCapture();
  2177. }
  2178. bool SnapShot(CImageFrame*img)
  2179. {
  2180. if(img == NULL){
  2181. return false;
  2182. }
  2183. IplImage*cvimgtmp = cvCreateImage(cvSize(img->width,img->height),8,3);
  2184. IplImage*cvimg = cvCreateImage(cvSize(img->width,img->height),8,3);
  2185. char m_FileName[MAX_PATH] = {0};
  2186. #ifdef _WIN32
  2187. SYSTEMTIME nowTime; // 系统时间结构体
  2188. GetLocalTime(&nowTime);
  2189. _snprintf(m_FileName, MAX_PATH, "%4d%02d%02d%02d%02d%02d", nowTime.wYear, nowTime.wMonth, nowTime.wDay, nowTime.wHour, nowTime.wMinute, nowTime.wSecond);
  2190. #else
  2191. struct tm* pst = NULL;
  2192. time_t t = time(NULL);
  2193. pst = localtime(&t);
  2194. snprintf(m_FileName, MAX_PATH, "%4d%02d%02d%02d%02d%02d", pst->tm_year + 1900, pst->tm_mon + 1, pst->tm_mday, pst->tm_hour, pst->tm_min, pst->tm_sec);
  2195. #endif // _WIN32
  2196. CvFont fontEvent = cvFont(2,2);
  2197. memcpy(cvimg->imageData,img->data,img->framesize);
  2198. int nstartx = 0;
  2199. int nstarty = 0;
  2200. cvCopy(cvimg,cvimgtmp);
  2201. cvPutText(cvimgtmp, m_FileName,cvPoint(img->width-280,img->height-10), &fontEvent, CV_RGB( 255, 0, 0));
  2202. memcpy(img->data,cvimgtmp->imageData,img->framesize);
  2203. cvReleaseImage(&cvimg);
  2204. cvReleaseImage(&cvimgtmp);
  2205. return true;
  2206. }