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.

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