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.

239 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "IM_DnnWorker.h"
  2. #include "../../lib/components/base/basicFunctionality.h"
  3. #define LOG( X ) m_pLog->Log( "WDNN", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "WDNN", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "WDNN", this, _LOG_ERROR, X );
  12. #endif
  13. // Constructor implementation
  14. IM_DnnWorker::IM_DnnWorker( config_setting_t * Settings ) : basicThread(), m_mBufferProtectorMutex(){
  15. m_pLog = basicLogCollection::getInstance();
  16. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DNN" );
  17. m_pEvtHeap = basicEventHeap::getInstance();
  18. m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  19. m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  20. m_pActiveJob = NULL;
  21. #ifdef DARKNET
  22. m_pDarknetDetector = NULL;
  23. #endif
  24. m_bDataIsActive = false;
  25. };
  26. // Destructor implementation
  27. IM_DnnWorker::~IM_DnnWorker(){};
  28. bool IM_DnnWorker::create( void ){
  29. char tBuff[256];
  30. if( m_pSettings == NULL ){
  31. return false;
  32. } else {
  33. config_setting_t * tSetting;
  34. tSetting = config_setting_get_member( m_pSettings, "PICTURES_BASE" );
  35. if( tSetting == NULL ){
  36. tSetting = config_setting_add(m_pSettings, "PICTURES_BASE", CONFIG_TYPE_STRING );
  37. config_setting_set_string( tSetting, "/srv/vault" );
  38. basicSettingsProvider::getInstance()->compromise();
  39. LOG("No Pictures Base Path! Using default!");
  40. };
  41. sprintf( m_pPicturesBase, "%s", config_setting_get_string(tSetting));
  42. tSetting = config_setting_get_member( m_pSettings, "NET_CONFIG_FILE" );
  43. if( tSetting == NULL ){
  44. tSetting = config_setting_add(m_pSettings, "NET_CONFIG_FILE", CONFIG_TYPE_STRING );
  45. config_setting_set_string( tSetting, "conf/yolo9000.cfg" );
  46. basicSettingsProvider::getInstance()->compromise();
  47. LOG("No Net Config File! Using default!");
  48. };
  49. sprintf( m_pConfigPath, "%s", config_setting_get_string(tSetting));
  50. tSetting = config_setting_get_member( m_pSettings, "NET_WEIGHTS_FILE" );
  51. if( tSetting == NULL ){
  52. tSetting = config_setting_add(m_pSettings, "NET_WEIGHTS_FILE", CONFIG_TYPE_STRING );
  53. config_setting_set_string( tSetting, "data/yolo9000.weights" );
  54. basicSettingsProvider::getInstance()->compromise();
  55. LOG("No Net Weights File! Using default!");
  56. };
  57. sprintf( m_pWeightsPath, "%s", config_setting_get_string(tSetting));
  58. tSetting = config_setting_get_member( m_pSettings, "NET_TARGET_DEVICE" );
  59. if( tSetting == NULL ){
  60. tSetting = config_setting_add(m_pSettings, "NET_TARGET_DEVICE", CONFIG_TYPE_STRING );
  61. config_setting_set_string( tSetting, "CPU" );
  62. basicSettingsProvider::getInstance()->compromise();
  63. LOG("No Net Target Device! Using default!");
  64. };
  65. if( strstr( config_setting_get_string(tSetting), "CPU" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  66. if( strstr( config_setting_get_string(tSetting), "OPENCL_FP16" )!=NULL ){
  67. m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL_FP16;
  68. } else {
  69. if( strstr( config_setting_get_string(tSetting), "OPENCL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL;
  70. };
  71. if( strstr( config_setting_get_string(tSetting), "MYRIAD" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_MYRIAD;
  72. if( strstr( config_setting_get_string(tSetting), "VULKAN" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_VULKAN;
  73. if( strstr( config_setting_get_string(tSetting), "FPGA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_FPGA;
  74. if( strstr( config_setting_get_string(tSetting), "CUDA_FP16" )!=NULL ){
  75. m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA_FP16;
  76. } else {
  77. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA;
  78. };
  79. if( strstr( config_setting_get_string(tSetting), "HDDL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_HDDL;
  80. sprintf( tBuff, "Device: %s", config_setting_get_string(tSetting) );
  81. LOG( tBuff );
  82. tSetting = config_setting_get_member( m_pSettings, "NET_BACKEND_ENGINE" );
  83. if( tSetting == NULL ){
  84. tSetting = config_setting_add(m_pSettings, "NET_BACKEND_ENGINE", CONFIG_TYPE_STRING );
  85. config_setting_set_string( tSetting, "DEFAULT" );
  86. basicSettingsProvider::getInstance()->compromise();
  87. LOG("No Net Backend Engine! Using default!");
  88. };
  89. if( strstr( config_setting_get_string(tSetting), "DEFAULT" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  90. if( strstr( config_setting_get_string(tSetting), "HALIDE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_HALIDE;
  91. if( strstr( config_setting_get_string(tSetting), "INFERENCE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE;
  92. if( strstr( config_setting_get_string(tSetting), "OPENCV" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_OPENCV;
  93. if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM;
  94. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA;
  95. sprintf( tBuff, "Engine: %s", config_setting_get_string(tSetting) );
  96. LOG( tBuff );
  97. basicSettingsProvider::getInstance()->validate();
  98. };
  99. sprintf( tBuff, "loading DNN from disk... CONF: %s WEIGHT: %s", m_pConfigPath, m_pWeightsPath );
  100. LOG( tBuff );
  101. if(!fileExist( m_pConfigPath )) {
  102. LOG_ERROR("No Config file exists!");
  103. return false;
  104. };
  105. if(!fileExist( m_pWeightsPath )) {
  106. LOG_ERROR("No Weight file exists!");
  107. return false;
  108. };
  109. #ifdef DARKNET
  110. try {
  111. m_pDarknetDetector = new Detector( m_pConfigPath, m_pWeightsPath );
  112. if( m_pDarknetDetector ) {
  113. LOG("DarkNet Detector Created");
  114. } else {
  115. LOG_ERROR("Failure during DarkNet Detector Creation!");
  116. };
  117. } catch ( exception e ) {
  118. LOG_ERROR( e.what() );
  119. };
  120. #else
  121. m_net = cv::dnn::readNetFromDarknet( m_pConfigPath, m_pWeightsPath );
  122. m_net.setPreferableBackend( m_eBackendEngine );
  123. m_net.setPreferableTarget( m_eTargetDevice );
  124. std::vector<String> outNames = m_net.getUnconnectedOutLayersNames();
  125. LOG("OutNames");
  126. for( std::vector<String>::iterator idx = outNames.begin(); idx < outNames.end(); idx++ ) {
  127. LOG( (*idx).c_str() );
  128. };
  129. #endif
  130. return init();
  131. };
  132. bool IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
  133. Size inpSize;
  134. bool swapRB = true; //BGR->RGB
  135. double scale = 1.0;///255.0;
  136. Scalar mean;
  137. inpSize.width = Picture.cols;
  138. inpSize.height = Picture.rows;
  139. try {
  140. LOG("Create a 4D blob from a frame");
  141. Mat blob = cv::dnn::blobFromImage( Picture, scale, Size(416, 416), mean, swapRB, false, CV_8U );
  142. if( blob.empty() ) {
  143. LOG_ERROR("Failure Creating Blob!");
  144. return false;
  145. };
  146. // construct a blob from the input frame and then perform a forward
  147. // pass of the YOLO object detector, giving us our bounding boxes
  148. // and associated probabilities
  149. //blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False )
  150. //net.setInput(blob)
  151. //layerOutputs = net.forward(ln)
  152. // Run a model.
  153. LOG("Set BLOB to NET");
  154. m_net.setInput( blob );
  155. //std::vector<Mat> outs;
  156. //std::vector<String> outNames = m_net.getUnconnectedOutLayersNames();
  157. Mat outMat = m_net.forward( String( "detection_out" ));
  158. LOG("DONE!");
  159. /*
  160. if( m_net.getLayer(0)->outputNameToIndex("im_info") != -1 ){
  161. // Faster-RCNN or R-FCN
  162. //resize( Picture, Picture, inpSize );
  163. cv::resize( Picture, Picture, cv::Size(416, 416));
  164. Mat imInfo = ( Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
  165. m_net.setInput( imInfo, "im_info" );
  166. };
  167. */
  168. } catch ( cv::Exception& e ) {
  169. LOG_ERROR("Catched Exception");
  170. LOG_ERROR( e.what() );
  171. };
  172. return false;
  173. };
  174. void IM_DnnWorker::m_fThreadProc( void ){
  175. m_bThreadStatus = TH_WORKING;
  176. while( m_bThreadStatus < TH_ENDED ) {
  177. while (!m_bDataIsActive && m_bThreadStatus < TH_ENDED ) SLEEP( 20 );
  178. if( m_mBufferProtectorMutex.try_lock() ){
  179. if( m_bDataIsActive ){
  180. LOG( "Pushing Picure to DNN" );
  181. m_fImageAnalyzer( m_oWorkingMat );
  182. m_bDataIsActive = false;
  183. };
  184. m_mBufferProtectorMutex.unlock();
  185. };
  186. // Create Finish EVT;
  187. //basicEvent *tEvt;
  188. //tEvt = createVAEvent(true);// EVT_CLASS_VIDEO, EVT_ALARM, "Motion Detected");
  189. //if (tEvt!=NULL)m_pEventHeap->broadcast(tEvt);^M
  190. };
  191. };
  192. bool IM_DnnWorker::enqueueJob( IM_JOB *ptr ){
  193. if( ptr ) {
  194. if( m_mBufferProtectorMutex.try_lock() ) {
  195. char tPath[1024];
  196. char p1[3];
  197. char p2[3];
  198. char p3[3];
  199. LOG( ptr->md5 );
  200. memcpy( p1, ptr->md5 , 2); p1[2]=(char)0;
  201. memcpy( p2, ptr->md5+2, 2); p2[2]=(char)0;
  202. memcpy( p3, ptr->md5+4, 2); p3[2]=(char)0;
  203. switch( ptr->type ) {
  204. case IM_ITEM_TYPE_JPEG: sprintf( tPath, "%s/%s/%s/%s/%s.jpg", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  205. case IM_ITEM_TYPE_PNG: sprintf( tPath, "%s/%s/%s/%s/%s.png", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  206. case IM_ITEM_TYPE_GIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  207. case IM_ITEM_TYPE_AGIF: sprintf( tPath, "%s/%s/%s/%s/%s.gif", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  208. case IM_ITEM_TYPE_TIFF: sprintf( tPath, "%s/%s/%s/%s/%s.tiff", m_pPicturesBase, p1, p2, p3, ptr->md5 ); break;
  209. };
  210. LOG( tPath );
  211. Mat Frame = cv::imread( tPath, IMREAD_COLOR );
  212. if( !Frame.empty() ){
  213. Frame.copyTo( m_oWorkingMat );
  214. cv::resize( m_oWorkingMat, m_oWorkingMat, cv::Size(416, 416));
  215. cv::imwrite("cur-test-img.jpg", m_oWorkingMat );
  216. m_bDataIsActive = true;
  217. m_mBufferProtectorMutex.unlock();
  218. } else LOG_ERROR( "Failure during Image Read!" );
  219. return true;
  220. };
  221. };
  222. return false;
  223. };
  224. #undef LOG
  225. #undef LOG_ERROR