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.

476 lines
22 KiB

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