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.

352 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #error "This source file seems to be unused. 2026-02-20"
  2. #include "IM_DnnWorker.h"
  3. #include "../../lib/components/base/basicFunctionality.h"
  4. #define LOG( X ) m_pLog->Log( "WDNN", this, X )
  5. #ifdef DEBUG
  6. #define LOG_ERROR( X ) {\
  7. char _tBfrErrMSG234[256];\
  8. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  9. m_pLog->Log( "WDNN", this, _LOG_ERROR, _tBfrErrMSG234 );\
  10. }
  11. #else
  12. #define LOG_ERROR( X ) m_pLog->Log( "WDNN", this, _LOG_ERROR, X );
  13. #endif
  14. // Constructor implementation
  15. IM_DnnWorker::IM_DnnWorker( config_setting_t * Settings ) : basicThread(), m_mBufferProtectorMutex(){
  16. m_pLog = basicLogCollection::getInstance();
  17. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DNN" );
  18. m_pEvtHeap = basicEventHeap::getInstance();
  19. m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  20. m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  21. m_pActiveJob = NULL;
  22. #ifdef DARKNET
  23. m_pDarknetDetector = NULL;
  24. #endif
  25. m_bDataIsActive = false;
  26. };
  27. // Destructor implementation
  28. IM_DnnWorker::~IM_DnnWorker(){
  29. #ifdef DARKNET
  30. if( m_pDarknetDetector ) delete( m_pDarknetDetector );
  31. #endif
  32. };
  33. bool IM_DnnWorker::create( void ){
  34. char tBuff[256];
  35. if( m_pSettings == NULL ){
  36. return false;
  37. } else {
  38. config_setting_t * tSetting;
  39. tSetting = config_setting_get_member( m_pSettings, "PICTURES_BASE" );
  40. if( tSetting == NULL ){
  41. tSetting = config_setting_add(m_pSettings, "PICTURES_BASE", CONFIG_TYPE_STRING );
  42. config_setting_set_string( tSetting, "/srv/vault" );
  43. basicSettingsProvider::getInstance()->compromise();
  44. LOG("No Pictures Base Path! Using default!");
  45. };
  46. sprintf( m_pPicturesBase, "%s", config_setting_get_string(tSetting));
  47. tSetting = config_setting_get_member( m_pSettings, "NET_CONFIG_FILE" );
  48. if( tSetting == NULL ){
  49. tSetting = config_setting_add(m_pSettings, "NET_CONFIG_FILE", CONFIG_TYPE_STRING );
  50. config_setting_set_string( tSetting, "conf/yolo9000.cfg" );
  51. basicSettingsProvider::getInstance()->compromise();
  52. LOG("No Net Config File! Using default!");
  53. };
  54. sprintf( m_pConfigPath, "%s", config_setting_get_string(tSetting));
  55. tSetting = config_setting_get_member( m_pSettings, "NET_WEIGHTS_FILE" );
  56. if( tSetting == NULL ){
  57. tSetting = config_setting_add(m_pSettings, "NET_WEIGHTS_FILE", CONFIG_TYPE_STRING );
  58. config_setting_set_string( tSetting, "data/yolo9000.weights" );
  59. basicSettingsProvider::getInstance()->compromise();
  60. LOG("No Net Weights File! Using default!");
  61. };
  62. sprintf( m_pWeightsPath, "%s", config_setting_get_string(tSetting));
  63. tSetting = config_setting_get_member( m_pSettings, "DETECTION_PROBABILITY" );
  64. if( tSetting == NULL ){
  65. tSetting = config_setting_add(m_pSettings, "DETECTION_PROBABILITY", CONFIG_TYPE_FLOAT );
  66. config_setting_set_float( tSetting, 0.5f );
  67. basicSettingsProvider::getInstance()->compromise();
  68. LOG("No Probability Set! Using default!");
  69. };
  70. m_fDetectionProbability = config_setting_get_float(tSetting);
  71. tSetting = config_setting_get_member( m_pSettings, "USE_CLASS_MAP" );
  72. if( tSetting == NULL ){
  73. tSetting = config_setting_add(m_pSettings, "USE_CLASS_MAP", CONFIG_TYPE_BOOL );
  74. config_setting_set_bool( tSetting, 0 );
  75. basicSettingsProvider::getInstance()->compromise();
  76. LOG("No Class Map Usage Set! Using default!");
  77. };
  78. m_bUseClassMapping = (bool) config_setting_get_bool(tSetting);
  79. if( m_bUseClassMapping ) {
  80. char tMapFileName[1024];
  81. FILE *fd;
  82. tSetting = config_setting_get_member( m_pSettings, "CLASS_MAP_FILE" );
  83. if( tSetting == NULL ){
  84. tSetting = config_setting_add(m_pSettings, "CLASS_MAP_FILE", CONFIG_TYPE_STRING );
  85. config_setting_set_string( tSetting, "data/coco9k.map" );
  86. basicSettingsProvider::getInstance()->compromise();
  87. LOG("No Class Map File Set! Using default!");
  88. };
  89. sprintf( tMapFileName, "%s", config_setting_get_string(tSetting) );
  90. fd = fopen( tMapFileName, "r" );
  91. if( fd ) {
  92. uint32_t tV;
  93. uint32_t idx = 0;
  94. char tB[128];
  95. while( !feof(fd) ) {
  96. if( fgets( tB, 128, fd ) ){
  97. tV = atoi( tB )+1;
  98. m_vClassesMap.push_back( tV );
  99. idx++;
  100. };
  101. };
  102. fclose( fd );
  103. sprintf( tB, "Loadded %d classes map", idx );
  104. LOG( tB );
  105. } else {
  106. m_bUseClassMapping = false;
  107. };
  108. };
  109. tSetting = config_setting_get_member( m_pSettings, "NET_TARGET_DEVICE" );
  110. if( tSetting == NULL ){
  111. tSetting = config_setting_add(m_pSettings, "NET_TARGET_DEVICE", CONFIG_TYPE_STRING );
  112. config_setting_set_string( tSetting, "CPU" );
  113. basicSettingsProvider::getInstance()->compromise();
  114. LOG("No Net Target Device! Using default!");
  115. };
  116. if( strstr( config_setting_get_string(tSetting), "CPU" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  117. if( strstr( config_setting_get_string(tSetting), "OPENCL_FP16" )!=NULL ){
  118. m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL_FP16;
  119. } else {
  120. if( strstr( config_setting_get_string(tSetting), "OPENCL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL;
  121. };
  122. if( strstr( config_setting_get_string(tSetting), "MYRIAD" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_MYRIAD;
  123. if( strstr( config_setting_get_string(tSetting), "VULKAN" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_VULKAN;
  124. if( strstr( config_setting_get_string(tSetting), "FPGA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_FPGA;
  125. if( strstr( config_setting_get_string(tSetting), "CUDA_FP16" )!=NULL ){
  126. m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA_FP16;
  127. } else {
  128. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA;
  129. };
  130. if( strstr( config_setting_get_string(tSetting), "HDDL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_HDDL;
  131. sprintf( tBuff, "Device: %s", config_setting_get_string(tSetting) );
  132. LOG( tBuff );
  133. tSetting = config_setting_get_member( m_pSettings, "NET_BACKEND_ENGINE" );
  134. if( tSetting == NULL ){
  135. tSetting = config_setting_add(m_pSettings, "NET_BACKEND_ENGINE", CONFIG_TYPE_STRING );
  136. config_setting_set_string( tSetting, "DEFAULT" );
  137. basicSettingsProvider::getInstance()->compromise();
  138. LOG("No Net Backend Engine! Using default!");
  139. };
  140. if( strstr( config_setting_get_string(tSetting), "DEFAULT" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  141. if( strstr( config_setting_get_string(tSetting), "HALIDE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_HALIDE;
  142. if( strstr( config_setting_get_string(tSetting), "INFERENCE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE;
  143. if( strstr( config_setting_get_string(tSetting), "OPENCV" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_OPENCV;
  144. if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM;
  145. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA;
  146. sprintf( tBuff, "Engine: %s", config_setting_get_string(tSetting) );
  147. LOG( tBuff );
  148. basicSettingsProvider::getInstance()->validate();
  149. };
  150. sprintf( tBuff, "loading DNN from disk... CONF: %s WEIGHT: %s", m_pConfigPath, m_pWeightsPath );
  151. LOG( tBuff );
  152. if(!fileExist( m_pConfigPath )) {
  153. LOG_ERROR("No Config file exists!");
  154. return false;
  155. };
  156. if(!fileExist( m_pWeightsPath )) {
  157. LOG_ERROR("No Weight file exists!");
  158. return false;
  159. };
  160. #ifdef DARKNET
  161. try {
  162. m_pDarknetDetector = new Detector( m_pConfigPath, m_pWeightsPath );
  163. if( m_pDarknetDetector ) {
  164. LOG("DarkNet Detector Created");
  165. } else {
  166. LOG_ERROR("Failure during DarkNet Detector Creation!");
  167. };
  168. } catch ( exception e ) {
  169. LOG_ERROR( e.what() );
  170. };
  171. #else
  172. m_net = cv::dnn::readNetFromDarknet( m_pConfigPath, m_pWeightsPath );
  173. m_net.setPreferableBackend( m_eBackendEngine );
  174. m_net.setPreferableTarget( m_eTargetDevice );
  175. std::vector<String> outNames = m_net.getUnconnectedOutLayersNames();
  176. LOG("OutNames");
  177. for( std::vector<String>::iterator idx = outNames.begin(); idx < outNames.end(); idx++ ) {
  178. LOG( (*idx).c_str() );
  179. };
  180. #endif
  181. return init();
  182. };
  183. IM_RESULT *IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
  184. Size inpSize;
  185. bool swapRB = true; //BGR->RGB
  186. double scale = 1.0;///255.0;
  187. Scalar mean;
  188. IM_RESULT * retVal = NULL;
  189. inpSize.width = Picture.cols;
  190. inpSize.height = Picture.rows;
  191. #ifndef DARKNET
  192. try {
  193. LOG("Create a 4D blob from a frame");
  194. //cv::resize( m_oWorkingMat, m_oWorkingMat, cv::Size(416, 416));
  195. Mat blob = cv::dnn::blobFromImage( Picture, scale, Size(416, 416), mean, swapRB, false, CV_8U );
  196. if( blob.empty() ) {
  197. LOG_ERROR("Failure Creating Blob!");
  198. return retVal;
  199. };
  200. // construct a blob from the input frame and then perform a forward
  201. // pass of the YOLO object detector, giving us our bounding boxes
  202. // and associated probabilities
  203. //blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False )
  204. //net.setInput(blob)
  205. //layerOutputs = net.forward(ln)
  206. // Run a model.
  207. LOG("Set BLOB to NET");
  208. m_net.setInput( blob );
  209. //std::vector<Mat> outs;
  210. //std::vector<String> outNames = m_net.getUnconnectedOutLayersNames();
  211. Mat outMat = m_net.forward( String( "detection_out" ));
  212. LOG("DONE!");
  213. /*
  214. if( m_net.getLayer(0)->outputNameToIndex("im_info") != -1 ){
  215. // Faster-RCNN or R-FCN
  216. //resize( Picture, Picture, inpSize );
  217. cv::resize( Picture, Picture, cv::Size(416, 416));
  218. Mat imInfo = ( Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
  219. m_net.setInput( imInfo, "im_info" );
  220. };
  221. */
  222. } catch ( cv::Exception& e ) {
  223. LOG_ERROR("Catched Exception");
  224. LOG_ERROR( e.what() );
  225. };
  226. #else
  227. std::shared_ptr<image_t> img = m_pDarknetDetector->mat_to_image_resize( Picture );
  228. std::vector<bbox_t> result_vec;
  229. //result_vec = detector.detect_resized( * img, inpSize.width, inpSize.height, thresh, true);
  230. result_vec = m_pDarknetDetector->detect_resized( *img, inpSize.width, inpSize.height, m_fDetectionProbability, true);
  231. if( result_vec.size() ) {
  232. retVal = (IM_RESULT *)malloc(sizeof(IM_RESULT));
  233. if( retVal ) {
  234. retVal->pJOB = m_pActiveJob;
  235. retVal->nBoxCount = result_vec.size();
  236. retVal->boxes = (IM_CLASS_BOUNDARY_BOX *) calloc( retVal->nBoxCount,sizeof(IM_CLASS_BOUNDARY_BOX));
  237. if( retVal->boxes ) {
  238. for( int i=0; i< result_vec.size(); i++ ) {
  239. bbox_t * b = &(result_vec.at(i));
  240. retVal->boxes[i].weight = b->prob;
  241. retVal->boxes[i].x = b->x;
  242. retVal->boxes[i].y = b->y;
  243. retVal->boxes[i].width = b->w;
  244. retVal->boxes[i].height = b->h;
  245. if( m_bUseClassMapping ){
  246. retVal->boxes[i].classID = m_vClassesMap[b->obj_id];
  247. } else {
  248. retVal->boxes[i].classID = b->obj_id;
  249. };
  250. #ifdef DEBUG
  251. char msg[256];
  252. sprintf( msg, "[%d] Class: %d Prob: %.2f X:%d Y:%d W:%d H:%d", i, retVal->boxes[i].classID, retVal->boxes[i].weight, b->x, b->y, b->w, b->h );
  253. LOG( msg );
  254. #endif
  255. };
  256. } else {
  257. free(retVal);
  258. retVal = NULL;
  259. };
  260. };
  261. };
  262. #endif
  263. return retVal;
  264. };
  265. void IM_DnnWorker::m_fThreadProc( void ){
  266. m_bThreadStatus = TH_WORKING;
  267. EVENT_CONTAINER *EVT_PTR = NULL;
  268. while( m_bThreadStatus < TH_ENDED ) {
  269. while (!m_bDataIsActive && m_bThreadStatus < TH_ENDED ) SLEEP( 20 );
  270. if( m_mBufferProtectorMutex.try_lock() ){
  271. if( m_bDataIsActive ){
  272. EVT_PTR =(EVENT_CONTAINER *) malloc(sizeof(EVENT_CONTAINER));
  273. if( EVT_PTR ) {
  274. memset( EVT_PTR, 0, sizeof(EVENT_CONTAINER));
  275. EVT_PTR->m_eClass = EVT_CLASS_IMAGE;
  276. EVT_PTR->m_eType = EVT_READY;
  277. if( IM_RESULT *tData = m_fImageAnalyzer( m_oWorkingMat ) ){
  278. //generate detection event
  279. EVT_PTR->m_pEvtData = tData;
  280. } else {
  281. //generate NOT detection event
  282. EVT_PTR->m_pEvtData = NULL;
  283. };
  284. };
  285. m_pActiveJob = NULL;
  286. m_bDataIsActive = false;
  287. };
  288. m_mBufferProtectorMutex.unlock();
  289. };
  290. if( EVT_PTR != NULL ) {
  291. m_pEvtHeap->broadcast( EVT_PTR );
  292. EVT_PTR = NULL;
  293. };
  294. };
  295. };
  296. bool IM_DnnWorker::enqueueJob( IM_JOB *ptr ){
  297. if( ptr ) {
  298. char tPath[1024];
  299. char p1[3];
  300. char p2[3];
  301. char p3[3];
  302. LOG( ptr->md5 );
  303. memcpy( p1, ptr->md5 , 2); p1[2]=(char)0;
  304. memcpy( p2, ptr->md5+2, 2); p2[2]=(char)0;
  305. memcpy( p3, ptr->md5+4, 2); p3[2]=(char)0;
  306. switch( ptr->image_type ) {
  307. case IM_ITEM_TYPE_JPEG: sprintf( tPath, "%s/%s/%s/%s/%s.jpg", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  308. case IM_ITEM_TYPE_PNG: sprintf( tPath, "%s/%s/%s/%s/%s.png", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  309. case IM_ITEM_TYPE_GIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  310. case IM_ITEM_TYPE_AGIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  311. case IM_ITEM_TYPE_TIFF: sprintf( tPath, "%s/%s/%s/%s/%s.tiff", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  312. };
  313. #ifdef DEBUG
  314. LOG( tPath );
  315. #endif
  316. Mat Frame = cv::imread( tPath, IMREAD_COLOR );
  317. if( !Frame.empty() ){
  318. while( m_bThreadStatus < TH_ENDED ) {
  319. if( m_mBufferProtectorMutex.try_lock() ) {
  320. Frame.copyTo( m_oWorkingMat );
  321. #ifdef DEBUG
  322. cv::imwrite("cur-test-img.jpg", m_oWorkingMat );
  323. #endif
  324. m_bDataIsActive = true;
  325. m_pActiveJob = ptr;
  326. m_mBufferProtectorMutex.unlock();
  327. return true;
  328. } else SLEEP(5);
  329. };
  330. } else {
  331. LOG_ERROR( "Failure during Image Read!" );
  332. };
  333. };
  334. return false;
  335. };
  336. #undef LOG
  337. #undef LOG_ERROR