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.

156 lines
6.9 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 ) : basicVideoAnalyzer(){
  15. m_pLog = basicLogCollection::getInstance();
  16. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DNN" );
  17. type = VA_DNN_ANAL;
  18. m_bDataIsActive = false;
  19. m_bDetectionStatus = false;
  20. m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  21. m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  22. }
  23. // Destructor implementation
  24. inline IM_DnnWorker::~IM_DnnWorker(){
  25. }
  26. bool IM_DnnWorker::create( void ){
  27. if( m_pSettings == NULL ){
  28. return false;
  29. } else {
  30. config_setting_t * tSetting;
  31. tSetting = config_setting_get_member( m_pSettings, "NET_CONFIG_FILE" );
  32. if( tSetting == NULL ){
  33. tSetting = config_setting_add(m_pSettings, "NET_CONFIG_FILE", CONFIG_TYPE_STRING );
  34. config_setting_set_string( tSetting, "conf/yolo9000.cfg" );
  35. basicSettingsProvider::getInstance()->compromise();
  36. LOG("No Net Config File! Using default!");
  37. };
  38. sprintf( m_pConfigPath, "%s", config_setting_get_string(tSetting));
  39. tSetting = config_setting_get_member( m_pSettings, "NET_WEIGHTS_FILE" );
  40. if( tSetting == NULL ){
  41. tSetting = config_setting_add(m_pSettings, "NET_WEIGHTS_FILE", CONFIG_TYPE_STRING );
  42. config_setting_set_string( tSetting, "data/yolo9000.weights" );
  43. basicSettingsProvider::getInstance()->compromise();
  44. LOG("No Net Weights File! Using default!");
  45. };
  46. sprintf( m_pWeightsPath, "%s", config_setting_get_string(tSetting));
  47. tSetting = config_setting_get_member( m_pSettings, "NET_TARGET_DEVICE" );
  48. if( tSetting == NULL ){
  49. tSetting = config_setting_add(m_pSettings, "NET_TARGET_DEVICE", CONFIG_TYPE_STRING );
  50. config_setting_set_string( tSetting, "CPU" );
  51. basicSettingsProvider::getInstance()->compromise();
  52. LOG("No Net Target Device! Using default!");
  53. };
  54. if( strstr( config_setting_get_string(tSetting), "CPU" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
  55. if( strstr( config_setting_get_string(tSetting), "OPENCL_FP16" )!=NULL ){
  56. m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL_FP16;
  57. } else {
  58. if( strstr( config_setting_get_string(tSetting), "OPENCL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL;
  59. };
  60. if( strstr( config_setting_get_string(tSetting), "MYRIAD" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_MYRIAD;
  61. if( strstr( config_setting_get_string(tSetting), "VULKAN" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_VULKAN;
  62. if( strstr( config_setting_get_string(tSetting), "FPGA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_FPGA;
  63. if( strstr( config_setting_get_string(tSetting), "CUDA_FP16" )!=NULL ){
  64. m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA_FP16;
  65. } else {
  66. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA;
  67. };
  68. if( strstr( config_setting_get_string(tSetting), "HDDL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_HDDL;
  69. tSetting = config_setting_get_member( m_pSettings, "NET_BACKEND_ENGINE" );
  70. if( tSetting == NULL ){
  71. tSetting = config_setting_add(m_pSettings, "NET_BACKEND_ENGINE", CONFIG_TYPE_STRING );
  72. config_setting_set_string( tSetting, "DEFAULT" );
  73. basicSettingsProvider::getInstance()->compromise();
  74. LOG("No Net Backend Engine! Using default!");
  75. };
  76. if( strstr( config_setting_get_string(tSetting), "DEFAULT" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
  77. if( strstr( config_setting_get_string(tSetting), "HALIDE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_HALIDE;
  78. if( strstr( config_setting_get_string(tSetting), "INFERENCE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE;
  79. if( strstr( config_setting_get_string(tSetting), "OPENCV" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_OPENCV;
  80. if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM;
  81. if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA;
  82. basicSettingsProvider::getInstance()->validate();
  83. };
  84. LOG( "[INFO] loading YOLO from disk..." );
  85. if(!fileExist( m_pConfigPath )) {
  86. LOG_ERROR("No Config file exists!");
  87. return false;
  88. };
  89. if(!fileExist( m_pWeightsPath )) {
  90. LOG_ERROR("No Weight file exists!");
  91. return false;
  92. };
  93. m_net = cv::dnn::readNetFromDarknet( m_pConfigPath, m_pWeightsPath );
  94. m_net.setPreferableBackend( m_eBackendEngine );
  95. m_net.setPreferableTarget( m_eTargetDevice );
  96. return false;
  97. };
  98. bool IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
  99. static Mat blob;
  100. Size inpSize;
  101. bool swapRB = true;
  102. float scale = 1.f;
  103. const Scalar mean;
  104. // Create a 4D blob from a frame.
  105. inpSize.width = Picture.cols;
  106. inpSize.height = Picture.rows;
  107. cv::dnn::blobFromImage( Picture, blob, 1 / 255.0, Size(416, 416), mean, swapRB, false, CV_8U );
  108. // construct a blob from the input frame and then perform a forward
  109. // pass of the YOLO object detector, giving us our bounding boxes
  110. // and associated probabilities
  111. //blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False )
  112. //net.setInput(blob)
  113. //layerOutputs = net.forward(ln)
  114. // Run a model.
  115. m_net.setInput( blob, "", scale, mean );
  116. if( m_net.getLayer(0)->outputNameToIndex("im_info") != -1 ){
  117. // Faster-RCNN or R-FCN
  118. resize( Picture, Picture, inpSize);
  119. Mat imInfo = (Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
  120. m_net.setInput(imInfo, "im_info");
  121. };
  122. return false;
  123. };
  124. /*
  125. void IM_DnnWorker::m_fThreadProc( void ){
  126. m_bThreadStatus = TH_WORKING;
  127. while( m_bThreadStatus < TH_ENDED ) {
  128. while (!m_bDataIsActive && m_bThreadStatus < TH_ENDED ) SLEEP(1);
  129. if (m_bThreadStatus < TH_ENDED){
  130. m_mBufferProtectorMutex.lock();
  131. m_bDetectionStatus = m_fImageAnalyzer( m_oWorkingMat );
  132. m_bDataIsActive = false;
  133. m_mBufferProtectorMutex.unlock();
  134. };
  135. };
  136. };
  137. */
  138. #undef LOG
  139. #undef LOG_ERROR