You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

479 lines
22 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #error "This source file seems to be unused. 2026-02-20"
  2. #include "IM_DLibWorker.h"
  3. #include <stdio.h>
  4. #include "../../lib/alg/generic/md5.h"
  5. #include "../../lib/components/base/basicFunctionality.h"
  6. #define LOG( X ) m_pLog->Log( "WDLIB", this, X )
  7. #ifdef DEBUG
  8. #define LOG_ERROR( X ) {\
  9. char _tBfrErrMSG234[256];\
  10. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  11. m_pLog->Log( "WDLIB", this, _LOG_ERROR, _tBfrErrMSG234 );\
  12. }
  13. #else
  14. #define LOG_ERROR( X ) m_pLog->Log( "WDLIB", this, _LOG_ERROR, X );
  15. #endif
  16. // Constructor implementation
  17. IM_DlibWorker::IM_DlibWorker( config_setting_t * Settings ) : basicThread(), m_mBufferProtectorMutex(){
  18. m_pLog = basicLogCollection::getInstance();
  19. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DLIB" );
  20. m_pEvtHeap = basicEventHeap::getInstance();
  21. m_pDBA = IM_DataBaseAdapter::getInstance();
  22. // m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  23. // m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  24. m_pActiveJob = NULL;
  25. #ifdef DARKNET
  26. m_pDarknetDetector = NULL;
  27. #endif
  28. m_fDetectionProbability = .7f;
  29. m_bUseShapePredictor = false;
  30. m_bUseFaceRecognition = false;
  31. m_bDataIsActive = false;
  32. };
  33. // Destructor implementation
  34. IM_DlibWorker::~IM_DlibWorker(){
  35. #ifdef DARKNET
  36. if( m_pDarknetDetector ) delete( m_pDarknetDetector );
  37. #endif
  38. };
  39. bool IM_DlibWorker::create( void ){
  40. char tBuff[256];
  41. if( m_pSettings == NULL ){
  42. return false;
  43. } else {
  44. config_setting_t * tSetting;
  45. tSetting = config_setting_get_member( m_pSettings, "PICTURES_BASE" );
  46. if( tSetting == NULL ){
  47. tSetting = config_setting_add(m_pSettings, "PICTURES_BASE", CONFIG_TYPE_STRING );
  48. config_setting_set_string( tSetting, "/srv/vault" );
  49. basicSettingsProvider::getInstance()->compromise();
  50. LOG("No Pictures Base Path! Using default!");
  51. };
  52. sprintf( m_pPicturesBase, "%s", config_setting_get_string(tSetting));
  53. tSetting = config_setting_get_member( m_pSettings, "FACEES_BASE" );
  54. if( tSetting == NULL ){
  55. tSetting = config_setting_add(m_pSettings, "FACEES_BASE", CONFIG_TYPE_STRING );
  56. config_setting_set_string( tSetting, "/srv/vault/faces" );
  57. basicSettingsProvider::getInstance()->compromise();
  58. LOG("No Face Pictures Base Path! Using default!");
  59. };
  60. sprintf( m_pFacesBase, "%s", config_setting_get_string(tSetting));
  61. tSetting = config_setting_get_member( m_pSettings, "USE_SHAPE_PREDICTOR" );
  62. if( tSetting == NULL ){
  63. tSetting = config_setting_add(m_pSettings, "USE_SHAPE_PREDICTOR", CONFIG_TYPE_BOOL );
  64. config_setting_set_bool( tSetting, 0 );
  65. basicSettingsProvider::getInstance()->compromise();
  66. LOG("No Class Map Usage Set! Using default!");
  67. };
  68. m_bUseShapePredictor = (bool) config_setting_get_bool(tSetting);
  69. if( m_bUseShapePredictor ) {
  70. tSetting = config_setting_get_member( m_pSettings, "USE_FACE_RECOGNITION" );
  71. if( tSetting == NULL ){
  72. tSetting = config_setting_add(m_pSettings, "USE_FACE_RECOGNITION", CONFIG_TYPE_BOOL );
  73. config_setting_set_bool( tSetting, 0 );
  74. basicSettingsProvider::getInstance()->compromise();
  75. LOG("No Class Map Usage Set! Using default!");
  76. };
  77. m_bUseFaceRecognition = (bool) config_setting_get_bool(tSetting);
  78. tSetting = config_setting_get_member( m_pSettings, "SHAPE_PREDICTOR_LANDMARKS" );
  79. if( tSetting == NULL ){
  80. tSetting = config_setting_add(m_pSettings, "SHAPE_PREDICTOR_LANDMARKS", CONFIG_TYPE_STRING );
  81. config_setting_set_string( tSetting, "data/shape_predictor_5_face_landmarks.dat" );
  82. basicSettingsProvider::getInstance()->compromise();
  83. LOG("No Net Config File! Using default!");
  84. };
  85. sprintf( m_pShapePredictorPath, "%s", config_setting_get_string(tSetting));
  86. if( m_bUseFaceRecognition ) {
  87. tSetting = config_setting_get_member( m_pSettings, "NET_CONFIG_FILE" );
  88. if( tSetting == NULL ){
  89. tSetting = config_setting_add(m_pSettings, "NET_CONFIG_FILE", CONFIG_TYPE_STRING );
  90. config_setting_set_string( tSetting, "data/dlib_face_recognition_resnet_model_v1.dat" );
  91. basicSettingsProvider::getInstance()->compromise();
  92. LOG("No Net Weights File! Using default!");
  93. };
  94. sprintf( m_pNetModelPath, "%s", config_setting_get_string(tSetting));
  95. };
  96. } else m_bUseFaceRecognition = false;
  97. tSetting = config_setting_get_member( m_pSettings, "DETECTION_PROBABILITY" );
  98. if( tSetting == NULL ){
  99. tSetting = config_setting_add(m_pSettings, "DETECTION_PROBABILITY", CONFIG_TYPE_FLOAT );
  100. config_setting_set_float( tSetting, 0.5f );
  101. basicSettingsProvider::getInstance()->compromise();
  102. LOG("No Probability Set! Using default!");
  103. };
  104. m_fDetectionProbability = config_setting_get_float(tSetting);
  105. tSetting = config_setting_get_member( m_pSettings, "OUTPUT_CHOWN_USER" );
  106. if( tSetting == NULL ){
  107. tSetting = config_setting_add(m_pSettings, "OUTPUT_CHOWN_USER", CONFIG_TYPE_STRING );
  108. config_setting_set_string( tSetting, "www-data" );
  109. basicSettingsProvider::getInstance()->compromise();
  110. LOG("No CHOWN USER! Using default!");
  111. };
  112. sprintf( m_sChownUser, "%s", config_setting_get_string(tSetting));
  113. tSetting = config_setting_get_member( m_pSettings, "OUTPUT_CHOWN_GROUP" );
  114. if( tSetting == NULL ){
  115. tSetting = config_setting_add(m_pSettings, "OUTPUT_CHOWN_GROUP", CONFIG_TYPE_STRING );
  116. config_setting_set_string( tSetting, "www-data" );
  117. basicSettingsProvider::getInstance()->compromise();
  118. LOG("No CHOWN GROUP! Using default!");
  119. };
  120. sprintf( m_sChownGroup, "%s", config_setting_get_string(tSetting));
  121. /*
  122. tSetting = config_setting_get_member( m_pSettings, "USE_CLASS_MAP" );
  123. if( tSetting == NULL ){
  124. tSetting = config_setting_add(m_pSettings, "USE_CLASS_MAP", CONFIG_TYPE_BOOL );
  125. config_setting_set_bool( tSetting, 0 );
  126. basicSettingsProvider::getInstance()->compromise();
  127. LOG("No Class Map Usage Set! Using default!");
  128. };
  129. m_bUseClassMapping = (bool) config_setting_get_bool(tSetting);
  130. if( m_bUseClassMapping ) {
  131. char tMapFileName[1024];
  132. FILE *fd;
  133. tSetting = config_setting_get_member( m_pSettings, "CLASS_MAP_FILE" );
  134. if( tSetting == NULL ){
  135. tSetting = config_setting_add(m_pSettings, "CLASS_MAP_FILE", CONFIG_TYPE_STRING );
  136. config_setting_set_string( tSetting, "data/coco9k.map" );
  137. basicSettingsProvider::getInstance()->compromise();
  138. LOG("No Class Map File Set! Using default!");
  139. };
  140. sprintf( tMapFileName, "%s", config_setting_get_string(tSetting) );
  141. fd = fopen( tMapFileName, "r" );
  142. if( fd ) {
  143. uint32_t tV;
  144. uint32_t idx = 0;
  145. char tB[128];
  146. while( !feof(fd) ) {
  147. if( fgets( tB, 128, fd ) ){
  148. tV = atoi( tB )+1;
  149. m_vClassesMap.push_back( tV );
  150. idx++;
  151. };
  152. };
  153. fclose( fd );
  154. sprintf( tB, "Loadded %d classes map", idx );
  155. LOG( tB );
  156. } else {
  157. m_bUseClassMapping = false;
  158. };
  159. };
  160. tSetting = config_setting_get_member( m_pSettings, "NET_TARGET_DEVICE" );
  161. if( tSetting == NULL ){
  162. tSetting = config_setting_add(m_pSettings, "NET_TARGET_DEVICE", CONFIG_TYPE_STRING );
  163. config_setting_set_string( tSetting, "CPU" );
  164. basicSettingsProvider::getInstance()->compromise();
  165. LOG("No Net Target Device! Using default!");
  166. };
  167. if( strstr( config_setting_get_string(tSetting), "CPU" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  168. if( strstr( config_setting_get_string(tSetting), "OPENCL_FP16" )!=NULL ){
  169. m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL_FP16;
  170. } else {
  171. if( strstr( config_setting_get_string(tSetting), "OPENCL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL;
  172. };
  173. if( strstr( config_setting_get_string(tSetting), "MYRIAD" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_MYRIAD;
  174. if( strstr( config_setting_get_string(tSetting), "VULKAN" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_VULKAN;
  175. if( strstr( config_setting_get_string(tSetting), "FPGA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_FPGA;
  176. if( strstr( config_setting_get_string(tSetting), "CUDA_FP16" )!=NULL ){
  177. m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA_FP16;
  178. } else {
  179. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA;
  180. };
  181. if( strstr( config_setting_get_string(tSetting), "HDDL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_HDDL;
  182. sprintf( tBuff, "Device: %s", config_setting_get_string(tSetting) );
  183. LOG( tBuff );
  184. tSetting = config_setting_get_member( m_pSettings, "NET_BACKEND_ENGINE" );
  185. if( tSetting == NULL ){
  186. tSetting = config_setting_add(m_pSettings, "NET_BACKEND_ENGINE", CONFIG_TYPE_STRING );
  187. config_setting_set_string( tSetting, "DEFAULT" );
  188. basicSettingsProvider::getInstance()->compromise();
  189. LOG("No Net Backend Engine! Using default!");
  190. };
  191. if( strstr( config_setting_get_string(tSetting), "DEFAULT" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  192. if( strstr( config_setting_get_string(tSetting), "HALIDE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_HALIDE;
  193. if( strstr( config_setting_get_string(tSetting), "INFERENCE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE;
  194. if( strstr( config_setting_get_string(tSetting), "OPENCV" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_OPENCV;
  195. if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM;
  196. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA;
  197. sprintf( tBuff, "Engine: %s", config_setting_get_string(tSetting) );
  198. LOG( tBuff );
  199. */
  200. basicSettingsProvider::getInstance()->validate();
  201. };
  202. if( m_bUseShapePredictor ) {
  203. if( m_bUseFaceRecognition ) {
  204. sprintf( tBuff, "loading Data from disk... Shape: %s Face: %s", m_pShapePredictorPath, m_pNetModelPath );
  205. } else {
  206. sprintf( tBuff, "loading Data from disk... Shape Predictor: %s ", m_pShapePredictorPath );
  207. };
  208. LOG( tBuff );
  209. if(!fileExist( m_pShapePredictorPath )) {
  210. LOG_ERROR("No Shape Predictor file exists!");
  211. return false;
  212. };
  213. if( m_bUseFaceRecognition ) {
  214. if(!fileExist( m_pNetModelPath )) {
  215. LOG_ERROR("No Net Model file exists!");
  216. return false;
  217. };
  218. }
  219. }
  220. m_oFaceDetector = get_frontal_face_detector();
  221. if( m_bUseShapePredictor ) {
  222. deserialize( m_pShapePredictorPath ) >> m_oShapePredictor;
  223. if( m_bUseFaceRecognition ) {
  224. deserialize( m_pNetModelPath ) >> m_oNet;
  225. };
  226. };
  227. return init();
  228. };
  229. std::vector<matrix<rgb_pixel>> jitter_image( const matrix<rgb_pixel>& img ){
  230. // All this function does is make 100 copies of img, all slightly jittered by being
  231. // zoomed, rotated, and translated a little bit differently. They are also randomly
  232. // mirrored left to right.
  233. thread_local dlib::rand rnd;
  234. std::vector<matrix<rgb_pixel>> crops;
  235. for (int i = 0; i < 100; ++i)
  236. crops.push_back(jitter_image(img,rnd));
  237. return crops;
  238. };
  239. CSTR IM_DlibWorker::m_fSaveFace( matrix<rgb_pixel> &Picture, CSTR md5, uint32_t index ){
  240. char tPathSrc[1024];
  241. char tPathDest[1024];
  242. CSTR nMd5;
  243. CSTR nPath;
  244. sprintf( tPathSrc, "%s/%s_%d.jpg", m_pFacesBase, md5, index );
  245. LOG("Save Face");
  246. LOG( tPathSrc );
  247. save_jpeg( Picture, tPathSrc, 75 );
  248. nMd5 = MD5File( tPathSrc );
  249. nPath = buildMd5Path( m_pFacesBase, nMd5, 0755, m_sChownUser, m_sChownGroup );
  250. sprintf( tPathDest, "%s/%s.jpg", nPath, nMd5 );
  251. free( nPath );
  252. //fileCopy( tPathSrc, tPathDest );
  253. //remove( tPathSrc );
  254. rename ( tPathSrc, tPathDest );
  255. ch_chown( tPathDest, m_sChownUser, m_sChownGroup );
  256. return nMd5;
  257. };
  258. IM_RESULT *IM_DlibWorker::m_fImageAnalyzer( cv::Mat &Pic ){
  259. IM_RESULT * retVal = NULL;
  260. cv_image<bgr_pixel> Picture(Pic);
  261. std::vector<matrix<rgb_pixel>> faces;
  262. std::vector<uint32_t> dbIDs;
  263. uint32_t idx = 0;
  264. CSTR tVAL = NULL;
  265. for( auto face : m_oFaceDetector( Picture ) ){
  266. //dlib::rectangle face
  267. if( m_bUseShapePredictor ) {
  268. auto shape = m_oShapePredictor( Picture, face );
  269. matrix<rgb_pixel> face_chip;
  270. extract_image_chip( Picture, get_face_chip_details( shape, 150, 0.25 ), face_chip );
  271. tVAL = m_fSaveFace( face_chip, m_pActiveJob->md5, idx );
  272. faces.push_back( move( face_chip ) );
  273. } else {
  274. matrix<rgb_pixel> face_chip;
  275. extract_image_chip( Picture, face, face_chip );
  276. tVAL = m_fSaveFace( face_chip, m_pActiveJob->md5, idx );
  277. };
  278. if( tVAL!= NULL ) {
  279. uint32_t tID;
  280. tID = m_pDBA->insertItemFace( m_pActiveJob->dbID, tVAL, face.left(), face.top(), face.width(), face.height() );
  281. dbIDs.push_back( tID );
  282. char tB[64];
  283. sprintf(tB,"ID:%d", tID);
  284. LOG(tB);
  285. free( tVAL );
  286. tVAL = NULL;
  287. };
  288. idx++;
  289. };
  290. if( m_bUseShapePredictor ) {
  291. //if (faces.size() == 0){
  292. // cout << "No faces found in image!" << endl;
  293. // return 1;
  294. //}
  295. if( m_bUseFaceRecognition ) {
  296. // This call asks the DNN to convert each face image in faces into a 128D vector.
  297. // In this 128D vector space, images from the same person will be close to each other
  298. // but vectors from different people will be far apart. So we can use these vectors to
  299. // identify if a pair of images are from the same person or from different people.
  300. if( faces.size() ) {
  301. //LOG("Have Detected Faces!");
  302. //std::vector<matrix<float,0,1>> face_descriptors = m_oNet( faces );
  303. try{
  304. for( int idx = 0; idx < faces.size(); idx++ ) {
  305. matrix<float,0,1> face_descriptor = mean( mat( m_oNet( jitter_image( faces[idx] ) ) ) );
  306. //matrix<float,0,1> face_descriptor = m_oNet( faces[0] );
  307. //LOG("Classification Done!");
  308. for( int i = 0; i < face_descriptor.size(); i++ ) m_pDBA->updateItemFace( dbIDs[idx], i, face_descriptor( i, 0 ) );
  309. };
  310. } catch ( std::exception& e ){
  311. LOG_ERROR( e.what() );
  312. };
  313. } else {
  314. LOG("No Faces Found!");
  315. };
  316. //std::vector<matrix<float,0,1>> face_descriptors = m_oNet( faces );
  317. // In particular, one simple thing we can do is face clustering. This next bit of code
  318. // creates a graph of connected faces and then uses the Chinese whispers graph clustering
  319. // algorithm to identify how many people there are and which faces belong to whom.
  320. /*
  321. std::vector<sample_pair> edges;
  322. for (size_t i = 0; i < face_descriptors.size(); ++i){
  323. for (size_t j = i; j < face_descriptors.size(); ++j){
  324. // Faces are connected in the graph if they are close enough. Here we check if
  325. // the distance between two face descriptors is less than 0.6, which is the
  326. // decision threshold the network was trained to use. Although you can
  327. // certainly use any other threshold you find useful.
  328. if (length(face_descriptors[i]-face_descriptors[j]) < 0.6)
  329. edges.push_back(sample_pair(i,j));
  330. };
  331. };
  332. std::vector<unsigned long> labels;
  333. const auto num_clusters = chinese_whispers(edges, labels);
  334. */
  335. // This will correctly indicate that there are 4 people in the image.
  336. //cout << "number of people found in the image: "<< num_clusters << endl;
  337. // Now let's display the face clustering results on the screen. You will see that it
  338. // correctly grouped all the faces.
  339. /*
  340. std::vector<image_window> win_clusters(num_clusters);
  341. for( size_t cluster_id = 0; cluster_id < num_clusters; ++cluster_id ){
  342. std::vector<matrix<rgb_pixel>> temp;
  343. for( size_t j = 0; j < labels.size(); ++j ){
  344. if (cluster_id == labels[j]) temp.push_back(faces[j]);
  345. };
  346. win_clusters[cluster_id].set_title("face cluster " + cast_to_string(cluster_id));
  347. win_clusters[cluster_id].set_image(tile_images(temp));
  348. };
  349. // Finally, let's print one of the face descriptors to the screen.
  350. cout << "face descriptor for one face: " << trans(face_descriptors[0]) << endl;
  351. // It should also be noted that face recognition accuracy can be improved if jittering
  352. // is used when creating face descriptors. In particular, to get 99.38% on the LFW
  353. // benchmark you need to use the jitter_image() routine to compute the descriptors,
  354. // like so:
  355. matrix<float,0,1> face_descriptor = mean(mat(net(jitter_image(faces[0]))));
  356. cout << "jittered face descriptor for one face: " << trans(face_descriptor) << endl;
  357. // If you use the model without jittering, as we did when clustering the bald guys, it
  358. // gets an accuracy of 99.13% on the LFW benchmark. So jittering makes the whole
  359. // procedure a little more accurate but makes face descriptor calculation slower.
  360. */
  361. };
  362. };
  363. return retVal;
  364. };
  365. void IM_DlibWorker::m_fThreadProc( void ){
  366. m_bThreadStatus = TH_WORKING;
  367. EVENT_CONTAINER *EVT_PTR = NULL;
  368. while( m_bThreadStatus < TH_ENDED ) {
  369. while (!m_bDataIsActive && m_bThreadStatus < TH_ENDED ) SLEEP( 20 );
  370. if( m_mBufferProtectorMutex.try_lock() ){
  371. if( m_bDataIsActive ){
  372. EVT_PTR =(EVENT_CONTAINER *) malloc(sizeof(EVENT_CONTAINER));
  373. if( EVT_PTR ) {
  374. memset( EVT_PTR, 0, sizeof(EVENT_CONTAINER));
  375. EVT_PTR->m_eClass = EVT_CLASS_IMAGE;
  376. EVT_PTR->m_eType = EVT_READY;
  377. if( IM_RESULT *tData = m_fImageAnalyzer( m_oWorkingMat ) ){
  378. //generate detection event
  379. EVT_PTR->m_pEvtData = tData;
  380. } else {
  381. //generate NOT detection event
  382. EVT_PTR->m_pEvtData = NULL;
  383. };
  384. };
  385. m_pActiveJob = NULL;
  386. m_bDataIsActive = false;
  387. };
  388. m_mBufferProtectorMutex.unlock();
  389. };
  390. if( EVT_PTR != NULL ) {
  391. m_pEvtHeap->broadcast( EVT_PTR );
  392. EVT_PTR = NULL;
  393. };
  394. };
  395. };
  396. bool IM_DlibWorker::enqueueJob( IM_JOB *ptr ){
  397. if( ptr ) {
  398. char tPath[1024];
  399. char p1[3];
  400. char p2[3];
  401. char p3[3];
  402. LOG( ptr->md5 );
  403. memcpy( p1, ptr->md5 , 2); p1[2]=(char)0;
  404. memcpy( p2, ptr->md5+2, 2); p2[2]=(char)0;
  405. memcpy( p3, ptr->md5+4, 2); p3[2]=(char)0;
  406. switch( ptr->image_type ) {
  407. case IM_ITEM_TYPE_JPEG: sprintf( tPath, "%s/%s/%s/%s/%s.jpg", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  408. case IM_ITEM_TYPE_PNG: sprintf( tPath, "%s/%s/%s/%s/%s.png", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  409. case IM_ITEM_TYPE_GIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  410. case IM_ITEM_TYPE_AGIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  411. case IM_ITEM_TYPE_TIFF: sprintf( tPath, "%s/%s/%s/%s/%s.tiff", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  412. };
  413. #ifdef DEBUG
  414. LOG( tPath );
  415. #endif
  416. Mat Frame = cv::imread( tPath, IMREAD_COLOR );
  417. try{
  418. while( m_bThreadStatus < TH_ENDED ) {
  419. if( m_mBufferProtectorMutex.try_lock() ) {
  420. Frame.copyTo( m_oWorkingMat );
  421. #ifdef DEBUG
  422. cv::imwrite("cur-test-img.jpg", m_oWorkingMat );
  423. //save_jpeg( m_oInputImg, "cur-test-img.jpg", 75 );
  424. #endif
  425. m_bDataIsActive = true;
  426. m_pActiveJob = ptr;
  427. m_mBufferProtectorMutex.unlock();
  428. return true;
  429. } else SLEEP(5);
  430. };
  431. } catch ( exception e ){
  432. LOG_ERROR( "Failure during Image Read!" );
  433. };
  434. };
  435. return false;
  436. };
  437. #undef LOG
  438. #undef LOG_ERROR