libfacecapture.cpp 88 KB

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