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
156 lines
6.9 KiB
#include "IM_DnnWorker.h"
|
|
#include "../../lib/components/base/basicFunctionality.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "WDNN", this, X )
|
|
#ifdef DEBUG
|
|
#define LOG_ERROR( X ) {\
|
|
char _tBfrErrMSG234[256];\
|
|
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
|
|
m_pLog->Log( "WDNN", this, _LOG_ERROR, _tBfrErrMSG234 );\
|
|
}
|
|
#else
|
|
#define LOG_ERROR( X ) m_pLog->Log( "WDNN", this, _LOG_ERROR, X );
|
|
#endif
|
|
|
|
// Constructor implementation
|
|
IM_DnnWorker::IM_DnnWorker( config_setting_t * Settings ) : basicVideoAnalyzer(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DNN" );
|
|
|
|
type = VA_DNN_ANAL;
|
|
m_bDataIsActive = false;
|
|
m_bDetectionStatus = false;
|
|
|
|
m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
|
|
m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
|
|
|
|
}
|
|
|
|
// Destructor implementation
|
|
inline IM_DnnWorker::~IM_DnnWorker(){
|
|
|
|
}
|
|
|
|
bool IM_DnnWorker::create( void ){
|
|
if( m_pSettings == NULL ){
|
|
return false;
|
|
} else {
|
|
config_setting_t * tSetting;
|
|
tSetting = config_setting_get_member( m_pSettings, "NET_CONFIG_FILE" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "NET_CONFIG_FILE", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "conf/yolo9000.cfg" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No Net Config File! Using default!");
|
|
};
|
|
sprintf( m_pConfigPath, "%s", config_setting_get_string(tSetting));
|
|
tSetting = config_setting_get_member( m_pSettings, "NET_WEIGHTS_FILE" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "NET_WEIGHTS_FILE", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "data/yolo9000.weights" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No Net Weights File! Using default!");
|
|
};
|
|
sprintf( m_pWeightsPath, "%s", config_setting_get_string(tSetting));
|
|
tSetting = config_setting_get_member( m_pSettings, "NET_TARGET_DEVICE" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "NET_TARGET_DEVICE", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "CPU" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No Net Target Device! Using default!");
|
|
};
|
|
if( strstr( config_setting_get_string(tSetting), "CPU" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CPU;
|
|
if( strstr( config_setting_get_string(tSetting), "OPENCL_FP16" )!=NULL ){
|
|
m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL_FP16;
|
|
} else {
|
|
if( strstr( config_setting_get_string(tSetting), "OPENCL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_OPENCL;
|
|
};
|
|
if( strstr( config_setting_get_string(tSetting), "MYRIAD" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_MYRIAD;
|
|
if( strstr( config_setting_get_string(tSetting), "VULKAN" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_VULKAN;
|
|
if( strstr( config_setting_get_string(tSetting), "FPGA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_FPGA;
|
|
if( strstr( config_setting_get_string(tSetting), "CUDA_FP16" )!=NULL ){
|
|
m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA_FP16;
|
|
} else {
|
|
if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_CUDA;
|
|
};
|
|
if( strstr( config_setting_get_string(tSetting), "HDDL" )!=NULL ) m_eTargetDevice = cv::dnn::DNN_TARGET_HDDL;
|
|
|
|
tSetting = config_setting_get_member( m_pSettings, "NET_BACKEND_ENGINE" );
|
|
if( tSetting == NULL ){
|
|
tSetting = config_setting_add(m_pSettings, "NET_BACKEND_ENGINE", CONFIG_TYPE_STRING );
|
|
config_setting_set_string( tSetting, "DEFAULT" );
|
|
basicSettingsProvider::getInstance()->compromise();
|
|
LOG("No Net Backend Engine! Using default!");
|
|
};
|
|
if( strstr( config_setting_get_string(tSetting), "DEFAULT" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_DEFAULT;
|
|
if( strstr( config_setting_get_string(tSetting), "HALIDE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_HALIDE;
|
|
if( strstr( config_setting_get_string(tSetting), "INFERENCE" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE;
|
|
if( strstr( config_setting_get_string(tSetting), "OPENCV" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_OPENCV;
|
|
if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM;
|
|
if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA;
|
|
basicSettingsProvider::getInstance()->validate();
|
|
};
|
|
LOG( "[INFO] loading YOLO from disk..." );
|
|
if(!fileExist( m_pConfigPath )) {
|
|
LOG_ERROR("No Config file exists!");
|
|
return false;
|
|
};
|
|
if(!fileExist( m_pWeightsPath )) {
|
|
LOG_ERROR("No Weight file exists!");
|
|
return false;
|
|
};
|
|
m_net = cv::dnn::readNetFromDarknet( m_pConfigPath, m_pWeightsPath );
|
|
m_net.setPreferableBackend( m_eBackendEngine );
|
|
m_net.setPreferableTarget( m_eTargetDevice );
|
|
return false;
|
|
};
|
|
|
|
bool IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){
|
|
static Mat blob;
|
|
Size inpSize;
|
|
bool swapRB = true;
|
|
float scale = 1.f;
|
|
const Scalar mean;
|
|
|
|
// Create a 4D blob from a frame.
|
|
inpSize.width = Picture.cols;
|
|
inpSize.height = Picture.rows;
|
|
cv::dnn::blobFromImage( Picture, blob, 1 / 255.0, Size(416, 416), mean, swapRB, false, CV_8U );
|
|
|
|
// construct a blob from the input frame and then perform a forward
|
|
// pass of the YOLO object detector, giving us our bounding boxes
|
|
// and associated probabilities
|
|
//blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False )
|
|
//net.setInput(blob)
|
|
//layerOutputs = net.forward(ln)
|
|
|
|
|
|
// Run a model.
|
|
m_net.setInput( blob, "", scale, mean );
|
|
if( m_net.getLayer(0)->outputNameToIndex("im_info") != -1 ){
|
|
// Faster-RCNN or R-FCN
|
|
resize( Picture, Picture, inpSize);
|
|
Mat imInfo = (Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
|
|
m_net.setInput(imInfo, "im_info");
|
|
};
|
|
return false;
|
|
};
|
|
|
|
/*
|
|
void IM_DnnWorker::m_fThreadProc( void ){
|
|
m_bThreadStatus = TH_WORKING;
|
|
while( m_bThreadStatus < TH_ENDED ) {
|
|
while (!m_bDataIsActive && m_bThreadStatus < TH_ENDED ) SLEEP(1);
|
|
if (m_bThreadStatus < TH_ENDED){
|
|
m_mBufferProtectorMutex.lock();
|
|
m_bDetectionStatus = m_fImageAnalyzer( m_oWorkingMat );
|
|
m_bDataIsActive = false;
|
|
m_mBufferProtectorMutex.unlock();
|
|
};
|
|
};
|
|
};
|
|
*/
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
|