diff --git a/Projects/img_mill/IM_Backend.cpp b/Projects/img_mill/IM_Backend.cpp new file mode 100644 index 00000000..68952f71 --- /dev/null +++ b/Projects/img_mill/IM_Backend.cpp @@ -0,0 +1,247 @@ +#include "IM_Backend.h" + +// Global Variables + +CSTR gsCFG_ADDRESS; +uint16_t guCFG_PORT = DEFAULT_SERVER_PORT; +CSTR gsCFG_LICENSE; +CSTR gsCFG_DB_SERVER; +CSTR gsCFG_DB_NAME; +CSTR gsCFG_DB_USER; +CSTR gsCFG_DB_PASSWORD; +CSTR gsCFG_PID_FILE = (CSTR) DEFAULT_SERVER_PID_FILE; +SOFTWARE_INFO gsSOFT_INFO; +bool gbCFG_MASTER_ROLE = true; + +bool gModule_GeoFence_Enabled = true; +bool gModule_SMS_Enabled = false; +bool gModule_Email_Enabled = false; +bool gSignal_Handler_Enabled = true; + +#define LOG( X ) m_pLog->Log( "main", 0, X ) +#define LOG_ERROR( X ) m_pLog->Log( "main", 0, _LOG_ERROR, X ) + +IM_Kernel * Kernel = NULL; +//LRF_HardwareManager * HWManager = NULL; +basicLogCollection * m_pLog = NULL; +networkServer * NetworkManager = NULL; + +void (*old_update_handler_2)(int); //SIGINT +void (*old_kill_handler_3)(int); //SIGQUIT +void (*old_kill_handler_9)(int); //SIGKILL +void (*old_alarm_handler_14)(int); //SIGALRM +void (*old_kill_handler_15)(int); //SIGTERM + + + + +void signal_SIGKILL_handler( int SIG ){ + if( Kernel!= NULL ){ + if( gSignal_Handler_Enabled ){ + m_pLog->Log("SIG", 0, _LOG_DEBUG, "Shutdown Service by Signal" ); + Kernel->setStatus( TH_ENDED ); // Closing all Connections; + } else { + m_pLog->Log("SIG", 0, _LOG_DEBUG, "Shutdown Signal Handler Disabled" ); + }; + }; +}; + +//#include + +int main (int argc, char *argv[]){ + //MySQL_Connector * DBC = NULL; + basicSettingsProvider * SETTINGS = NULL; + bool WBG; // Working Background + bool SSE; // Server Statistics Engine + bool NDC; // No Default Configuration + CSTR FLE_NAME; + CSTR DCF_NAME; + CSTR SSP_NAME; + objectFileLoggingService * FLE = NULL; + objectConsoleLoggingService * CLE = NULL; + // default init + WBG = true; + FLE_NAME = (CSTR) DEFAULT_SERVER_LOG_FILE; + SSE = true; + NDC = true; + DCF_NAME = (CSTR) DEFAULT_SERVER_CONFIG_PATH; + SSP_NAME = (CSTR) DEFAULT_SERVER_SOCKET_PATH; + gsSOFT_INFO.SW_BUILD_DATE = UNIX_TIMESTAMP; + gsSOFT_INFO.DATA_VALID = false; + m_pLog = basicLogCollection::getInstance( _LOG_TIME ); + gsSOFT_INFO.SW_MODE_FLAGS = 0 +#ifdef SKIP_TARGET_LOG + | SW_INFO_NO_TARGET_LOG +#endif +#ifdef HIBERNATE_ENABLE + | SW_INFO_HIBERNATE_ENA +#endif +#ifdef TGD + | SW_INFO_TGD_MOTORS + #ifdef TGD_VERSION_2 + | SW_INFO_TGD_VER_2 + #endif +#endif +#ifdef ENCODER_8192 + | SW_INFO_EU_PROJECT +#endif +#ifdef NO_BLACK_TARGETS + | SW_INFO_NO_BLACK_TARGETS +#endif +#ifndef NO_MOTORS + | SW_INFO_MOTOR_CTRL_ENA +#endif +#ifdef THREADED_FLIP + | SW_INFO_THREAD_FLIP +#endif +#ifndef PRU_SLAVE + | SW_INFO_FLAG_MASTER +#endif +#ifdef DEBUG + | SW_INFO_DEBUG_MODE +#endif + ; + + + // Reading Config + if( argc > 1 ){ + for( int i = 0; i < argc; i++ ){ + switch( argv[i][0] ){ + case '-': + case '\\': + case '/':{ + switch( argv[i][1] ){ + case 'h': case 'H': case 'v': case 'V': case '?':{ + printf( "Image Mill Neural Network Server\n"); + printf( "Software version <%s>\n", VERSION_NUMBER ); + printf( " -h -H This Message\n" ); + printf( " -i -I Interactive Mode\n" ); + return 0; + break; + }; + case 'i': case 'I':{ + WBG = false; + CLE = new objectConsoleLoggingService(); + if( CLE != NULL ) { + m_pLog->addHandler( CLE ); + }; + break; + }; + case 'c': case 'C':{ + DCF_NAME = (CSTR) malloc( MAX_PATH_LEN ); + if( DCF_NAME != NULL ) { + sprintf( DCF_NAME, "%s", argv[i+1] ); + } else { + printf("ERROR!\n\n"); + return -1; + }; + break; + }; + }; + break; + }; + }; + }; + }; + + FLE = new objectFileLoggingService( FLE_NAME ); + if( FLE != NULL ) { + m_pLog->addHandler( FLE ); + }; + LOG("Starting Log Engne!"); + if ( fileExist( DCF_NAME ) ){ + SETTINGS = basicSettingsProvider::getInstance(DCF_NAME); + } else { + SETTINGS = basicSettingsProvider::getInstance(); + }; + + // Demonizing or not + if( WBG ){ + printf( "Image Mill Neural Network Server ver. %s\n", VERSION_NUMBER ); + printf( "Configuration Applied:\n"); + printf( " Service Established %s at %d\n", gsCFG_ADDRESS, guCFG_PORT ); + printf( " PID File: %s\n", gsCFG_PID_FILE ); + printf( " Log File: %s\n", FLE_NAME ); + printf( " License: %s\n", gsCFG_LICENSE ); + printf( " Database Server: %s Database: %s\n", gsCFG_DB_SERVER, gsCFG_DB_NAME ); + if( NDC ){ + printf( " No configuration file (%s) supplied.\n", DCF_NAME); + } else { + printf( " Configuration read from: %s\n", DCF_NAME ); + }; + + //Demonizing + pid_t pid, sid; + sync(); + close( STDIN_FILENO ); + close( STDOUT_FILENO ); + close( STDERR_FILENO ); + + pid = fork(); + if( pid < 0 ){ + return pid; + }; + if( pid > 0 ){ + return 0; + }; + //umask(0); + sid = setsid(); + if (sid < 0) { + return sid; + }; + + pid = fork(); + if( pid < 0 ){ + return pid; + }; + if( pid > 0 ){ + FILE *pPIDF = fopen( gsCFG_PID_FILE, "w+" ); + if( pPIDF ){ + fprintf( pPIDF, "%d\n", pid ); + fclose( pPIDF ); + } + return 0; + }; + }; + + uint8_t m_bKernelExitCode = 0; + + do{ + //Loop till some Restart action + Kernel = IM_Kernel::getInstance(); + if( Kernel == NULL ) { + // Aborting Work + LOG_ERROR( "++++ Kernel failed to born ++++" ); + unlink( gsCFG_PID_FILE ); + LOG( "PID File Deleted" ); + delete( m_pLog ); + return -1; + } else { + LOG( "++++ Server Started ++++" ); + if ( Kernel->start() == false ){ + LOG_ERROR("Kernel create failure!"); + } else { + //Install Signal Listeners + old_kill_handler_15 = signal( 15, signal_SIGKILL_handler ); + old_kill_handler_9 = signal( 9, signal_SIGKILL_handler ); + // Waiting for Worker to Shut Down + Kernel->stop( false ); + m_bKernelExitCode = Kernel->getExitCause(); + signal( 9, old_kill_handler_9 ); + signal( 15, old_kill_handler_15 ); + }; + delete( Kernel ); + LOG( "++++ Server Ended ++++" ); + }; + } while( m_bKernelExitCode > 0 ); + + delete( NetworkManager ); + //LOG( "++++ CDC DEL ++++" ); + unlink( gsCFG_PID_FILE ); + LOG( "PID File Deleted" ); + if( FLE!= NULL ) delete( FLE ); + if( CLE!= NULL ) delete( CLE ); + delete( m_pLog ); + return(0); +} + diff --git a/Projects/img_mill/IM_Backend.h b/Projects/img_mill/IM_Backend.h new file mode 100644 index 00000000..4db45f83 --- /dev/null +++ b/Projects/img_mill/IM_Backend.h @@ -0,0 +1,48 @@ +/******************************************************************* +* +* DESCRIPTION: Iage Mill project main header file +* +* AUTHOR: Gagua +* +* HISTORY: +* +* DATE:5/29/2019 +* +*******************************************************************/ +#ifndef __IMG_MILL_BACKEND_MAIN_HEADER__ +#define __IMG_MILL_BACKEND_MAIN_HEADER__ + +/** include files **/ + +#include +#include +#include + +#include "../../lib/components/base/basicSettings.h" +#include "../../lib/components/base/basicLogCollection.h" +#include "../../lib/components/base/basicMemoryOperation.h" +#include "../../lib/components/object/objectFileLoggingService.h" +#include "../../lib/components/object/objectConsoleLoggingService.h" +#include "../../lib/data/net/netServer.h" +//#include "../../lib/data/mysql/database_class.h" +#include "IM_Kernel.h" + +/** local definitions **/ +//http://xmsecu.com:8080/ocx/NewActive.exe +/* default settings */ + +/** external functions **/ + +/** external data **/ + +/** internal functions **/ + +/** public data **/ + +/** private data **/ + +/** public functions **/ + +/** private functions **/ + +#endif diff --git a/Projects/img_mill/IM_ClientHandler.cpp b/Projects/img_mill/IM_ClientHandler.cpp new file mode 100644 index 00000000..96665efa --- /dev/null +++ b/Projects/img_mill/IM_ClientHandler.cpp @@ -0,0 +1,48 @@ +#include "IM_ClientHandler.h" +#include "../../lib/alg/generic/md5.h" +#include "../../lib/data/json/json_tool.h" + + +#define LOG( X ) m_pLog->Log( "NCH", 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( "NCH", this, _LOG_ERROR, _tBfrErrMSG234 );\ + } +#else + #define LOG_ERROR( X ) m_pLog->Log( "NCH", this, _LOG_ERROR, X ); +#endif + +// Constructor implementation +IM_ClientHandler::IM_ClientHandler( int SOCKET ) : networkClientHandler( SOCKET ), IM_ClientProtocol(){ + m_pLog = basicLogCollection::getInstance(); + if( m_pPort != NULL ){ + //LOG("server port is OK"); + if( create() ){ + setTransport( m_pPort ); + setConnectionStatus( CS_INIT ); + m_pPort->addRecipient(( protocolParser *)this ); + } else { + LOG_ERROR( "Protocol Parser Creation Failed!"); + }; + }; +}; + +// Destructor implementation +inline IM_ClientHandler::~IM_ClientHandler(){ + +}; + +bool IM_ClientHandler::getStatus( void ){ + if( m_pPort!= NULL ) { + return m_pPort->getStatus(); + }; + return false; +}; + + +#undef LOG +#undef LOG_ERROR + + diff --git a/Projects/img_mill/IM_ClientHandler.h b/Projects/img_mill/IM_ClientHandler.h new file mode 100644 index 00000000..01bfc02f --- /dev/null +++ b/Projects/img_mill/IM_ClientHandler.h @@ -0,0 +1,25 @@ +#ifndef __IM_CLIENTHANDLER_H_INCL__ +#define __IM_CLIENTHANDLER_H_INCL__ + +#include "../../lib/data/net/netClientHandler.h" +#include "IM_ClientProtocol.h" +/** + * TODO: Add class description + * + * @author Gagua + */ +class IM_ClientHandler : public networkClientHandler, public IM_ClientProtocol{ + private: + basicLogCollection * m_pLog; + public: + // Constructor + explicit IM_ClientHandler( int SOCKET ); + + // Destructor + virtual ~IM_ClientHandler(); + virtual bool getStatus( void ); +}; + + + +#endif // __IM_CLIENTHANDLER_H_INCL__ diff --git a/Projects/img_mill/IM_ClientProtocol.cpp b/Projects/img_mill/IM_ClientProtocol.cpp new file mode 100644 index 00000000..f6f9f2d1 --- /dev/null +++ b/Projects/img_mill/IM_ClientProtocol.cpp @@ -0,0 +1,18 @@ +#include "IM_ClientProtocol.h" + +#define LOG( X ) m_pLog->Log( "KRNL", 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( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\ + } +#else + #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X ); +#endif + + + +#undef LOG +#undef LOG_ERROR + diff --git a/Projects/img_mill/IM_ClientProtocol.h b/Projects/img_mill/IM_ClientProtocol.h new file mode 100644 index 00000000..93cac0bd --- /dev/null +++ b/Projects/img_mill/IM_ClientProtocol.h @@ -0,0 +1,56 @@ +#ifndef __IM_CLIENTPROTOCOL_H_INCL__ +#define __IM_CLIENTPROTOCOL_H_INCL__ + +#include "../../lib/data/protocol/protocol.h" + +/** + * TODO: Add class description + * + * @author Gagua + */ +class IM_ClientProtocol : public protocolParser { + public: + // Constructor + IM_ClientProtocol(); + + // Destructor + virtual ~IM_ClientProtocol(); + + // Copy constructor + // TODO: Uncomment the copy constructor when you need it. + //IM_ClientProtocol(const IM_ClientProtocol& src); + + // Assignment operator + // TODO: Uncomment the assignment operator when you need it. + //IM_ClientProtocol& operator=(const IM_ClientProtocol& src); +}; + +// Constructor implementation +inline IM_ClientProtocol::IM_ClientProtocol() : protocolParser() +{ +} + +// Destructor implementation +inline IM_ClientProtocol::~IM_ClientProtocol() +{ +} + +// TODO: Uncomment the copy constructor when you need it. +//inline IM_ClientProtocol::IM_ClientProtocol(const IM_ClientProtocol& src) +//{ +// // TODO: copy +//} + +// TODO: Uncomment the assignment operator when you need it. +//inline IM_ClientProtocol& IM_ClientProtocol::operator=(const IM_ClientProtocol& rhs) +//{ +// if (this == &rhs) { +// return *this; +// } +// +// // TODO: assignment +// +// return *this; +//} + +#endif // __IM_CLIENTPROTOCOL_H_INCL__ diff --git a/Projects/img_mill/IM_DataBaseAdapter.cpp b/Projects/img_mill/IM_DataBaseAdapter.cpp new file mode 100644 index 00000000..684038e7 --- /dev/null +++ b/Projects/img_mill/IM_DataBaseAdapter.cpp @@ -0,0 +1,96 @@ +#include "IM_DataBaseAdapter.h" +#include "../../lib/alg/generic/md5.h" + +#define LOG( X ) m_pLog->Log( "KRNL", 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( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\ + } +#else + #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X ); +#endif + +IM_DataBaseAdapter* IM_DataBaseAdapter::_instance = NULL; + +/** + * Returns the single instance of the object. + */ +IM_DataBaseAdapter* IM_DataBaseAdapter::getInstance( config_setting_t * Settings ) { + // check if the instance has been created yet + if( NULL == _instance ){ + // if not, then create it + _instance = new IM_DataBaseAdapter( Settings ); + }; + // return the single instance + return _instance; +}; + +// Constructor implementation +inline IM_DataBaseAdapter::IM_DataBaseAdapter( config_setting_t * Settings ){ + m_pLog = basicLogCollection::getInstance(); + m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" ); + m_bIsConnected = false; + if( m_pSettings!= NULL ) { + config_setting_t * tSetting; + char tDBHost[128]; + char tDBName[128]; + char tDBUser[128]; + char tDBPass[128]; + uint16_t tDBPort = 3306; + tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" ); + if( tSetting == NULL ){ + tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING ); + config_setting_set_string( tSetting, "localhost" ); + basicSettingsProvider::getInstance()->compromise(); + LOG("No DB Host Setting! Using default!"); + }; + sprintf(tDBHost, "%s", config_setting_get_string(tSetting)); + tSetting = config_setting_get_member( m_pSettings, "DATABASE_NAME" ); + if( tSetting == NULL ){ + tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING ); + config_setting_set_string( tSetting, "dfsl_gunter" ); + basicSettingsProvider::getInstance()->compromise(); + LOG("No DB Database Setting! Using default!"); + }; + sprintf(tDBName, "%s", config_setting_get_string(tSetting)); + tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" ); + if( tSetting == NULL ){ + tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING ); + config_setting_set_string( tSetting, "dbroot" ); + basicSettingsProvider::getInstance()->compromise(); + LOG("No DB User Setting! Using default!"); + }; + sprintf(tDBUser, "%s", config_setting_get_string(tSetting)); + tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" ); + if( tSetting == NULL ){ + tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING ); + config_setting_set_string( tSetting, "123567" ); + basicSettingsProvider::getInstance()->compromise(); + LOG("No DB Password Setting! Using default!"); + }; + sprintf(tDBPass, "%s", config_setting_get_string(tSetting)); + tSetting = config_setting_get_member( m_pSettings, "DATABASE_PORT" ); + if( tSetting == NULL ){ + tSetting = config_setting_add(m_pSettings, "DATABASE_PORT", CONFIG_TYPE_INT ); + config_setting_set_int( tSetting, 3306 ); + basicSettingsProvider::getInstance()->compromise(); + LOG("No DB Port Setting! Using default!"); + }; + tDBPort = config_setting_get_int( tSetting ); + basicSettingsProvider::getInstance()->validate(); + m_pConnectorClass = new MySQL_Connector( tDBName, tDBHost, tDBUser, tDBPass, tDBPort ); + m_bIsConnected = ( m_pConnectorClass != NULL ); + } else { + LOG_ERROR("No Settings Provided!"); + }; +}; + +// Destructor implementation +inline IM_DataBaseAdapter::~IM_DataBaseAdapter(){ +}; + +#undef LOG +#undef LOG_ERROR + diff --git a/Projects/img_mill/IM_DataBaseAdapter.h b/Projects/img_mill/IM_DataBaseAdapter.h new file mode 100644 index 00000000..d8fea53d --- /dev/null +++ b/Projects/img_mill/IM_DataBaseAdapter.h @@ -0,0 +1,47 @@ +#ifndef __IM_DATABASEADAPTER_H_INCL__ +#define __IM_DATABASEADAPTER_H_INCL__ + +#include "../../lib/data/mysql/database_class.h" +#include "../../lib/components/base/basicSettingsProvider.h" + +/** + * TODO: Add class description + * + * @author Gagua + */ +class IM_DataBaseAdapter { + private: + basicLogCollection * m_pLog; + config_setting_t * m_pSettings; + bool m_bIsConnected; + MySQL_Connector * m_pConnectorClass; + public: + // Method to fetch singleton instance. + static IM_DataBaseAdapter* getInstance( config_setting_t * Settings = NULL ); + + // Destructor + virtual ~IM_DataBaseAdapter(); + + protected: + // Constructor - protected so users cannot call it. + explicit IM_DataBaseAdapter( config_setting_t * Settings ); + + private: + // private static member referencing the single instance of the object + // TODO: Replace IM_DataBaseAdapter with the type of object you want to + // return the single instance of + static IM_DataBaseAdapter* _instance; + + // Copy constructor + // Declared but not defined to prevent auto-generated + // copy constructor. Refer to "Effective C++" by Meyers + IM_DataBaseAdapter(const IM_DataBaseAdapter& src); + + // Assignment operator + // Declared but not defined to prevent auto-generated + // assignment operator. Refer to "Effective C++" by Meyers + IM_DataBaseAdapter& operator=(const IM_DataBaseAdapter& src); + +}; + +#endif // __IM_DATABASEADAPTER_H_INCL__ diff --git a/Projects/img_mill/IM_DnnWorker.cpp b/Projects/img_mill/IM_DnnWorker.cpp new file mode 100644 index 00000000..b1986296 --- /dev/null +++ b/Projects/img_mill/IM_DnnWorker.cpp @@ -0,0 +1,156 @@ +#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_(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 + diff --git a/Projects/img_mill/IM_DnnWorker.h b/Projects/img_mill/IM_DnnWorker.h new file mode 100644 index 00000000..bfd98244 --- /dev/null +++ b/Projects/img_mill/IM_DnnWorker.h @@ -0,0 +1,33 @@ +#ifndef __IM_DNNWORKER_H_INCL__ +#define __IM_DNNWORKER_H_INCL__ + +#include "../../lib/components/base/basicVideoAnalyzer.h" +#include "../../lib/components/base/basicSettingsProvider.h" +#include +/** + * TODO: Add class description + * + * @author Gagua + */ +class IM_DnnWorker : public basicVideoAnalyzer { + private: + basicLogCollection * m_pLog; + config_setting_t * m_pSettings; + cv::dnn::Backend m_eBackendEngine; + cv::dnn::Target m_eTargetDevice; + cv::dnn::Net m_net; + char m_pConfigPath[1024]; + char m_pWeightsPath[1024]; + protected: + //virtual void m_fThreadProc( void ); + virtual bool m_fImageAnalyzer( Mat &Picture ); + public: + // Constructor + explicit IM_DnnWorker( config_setting_t * Settings = NULL ); + // Destructor + virtual ~IM_DnnWorker(); + bool create( void ); +}; + + +#endif // __IM_DNNWORKER_H_INCL__ diff --git a/Projects/img_mill/IM_Kernel.cpp b/Projects/img_mill/IM_Kernel.cpp new file mode 100644 index 00000000..618ad64d --- /dev/null +++ b/Projects/img_mill/IM_Kernel.cpp @@ -0,0 +1,77 @@ +#include "IM_Kernel.h" +#include "IM_ClientHandler.h" + +#define LOG( X ) m_pLog->Log( "KRNL", 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( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\ + } +#else + #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X ); +#endif + +IM_Kernel *IM_Kernel::_instance = NULL; + +IM_Kernel *IM_Kernel::getInstance(){ + if( _instance == NULL ) { + _instance = new IM_Kernel; + }; + return _instance; +}; + +// Constructor implementation +inline IM_Kernel::IM_Kernel( config_setting_t *Settings ) : basicEngine(), basicThread(){ + m_pLog = basicLogCollection::getInstance(); + m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" ); + m_pWorker = NULL; +}; + +// Destructor implementation +inline IM_Kernel::~IM_Kernel(){ + waitThreadEnd( true ); +}; + +bool IM_Kernel::start(){ + m_pWorker = new IM_DnnWorker( m_pSettings ); + if( m_pWorker ) { + if( m_pWorker->create() ){ + return init(); + } else LOG_ERROR( "DNN Worker Create Failure!" ); + }; + return false; +}; + +bool IM_Kernel::stop( bool Force ){ + delete( m_pWorker ); + m_pWorker = NULL; + waitThreadEnd( Force ); + return true; +}; + +networkClientHandler *IM_Kernel::attachNewClient( int SocketID ){ + IM_ClientHandler *tVal; + tVal = new IM_ClientHandler( SocketID ); + if( tVal ) { + tVal->attachEngine( this ); + }; + return (networkClientHandler *) tVal; +}; + +bool IM_Kernel::detachClient( networkClientHandler *ptr ){ + delete( (IM_ClientHandler *) ptr ); + return true; +}; + + +void IM_Kernel::m_fThreadProc( void ){ + m_bThreadStatus = TH_WORKING; + while( m_bThreadStatus < TH_ENDED ) { + SLEEP( DEFAULT_THREAD_SLEEP_TIME ); + }; +}; + +#undef LOG +#undef LOG_ERROR + diff --git a/Projects/img_mill/IM_Kernel.h b/Projects/img_mill/IM_Kernel.h new file mode 100644 index 00000000..d9353866 --- /dev/null +++ b/Projects/img_mill/IM_Kernel.h @@ -0,0 +1,42 @@ +#ifndef __IM_KERNEL_H_INCL__ +#define __IM_KERNEL_H_INCL__ + +#include "../../lib/components/base/basicEngine.h" +#include "../../lib/components/base/basicThread.h" +#include "../../lib/components/base/basicSettingsProvider.h" +#include "IM_DnnWorker.h" + + +class networkClientHandler; +/** + * TODO: Add class description + * + * @author Gagua + */ +class IM_Kernel : public basicEngine, public basicThread { + private: + basicLogCollection * m_pLog; + config_setting_t * m_pSettings; + uint8_t m_bExitCause; + IM_DnnWorker * m_pWorker; + protected: + virtual void m_fThreadProc( void ); + public: + + static IM_Kernel * getInstance(); + // Destructor + virtual ~IM_Kernel(); + bool signalEvent( void ); + virtual bool start(); + virtual bool stop( bool Force = false ); + uint8_t getExitCause( void ){ return m_bExitCause;}; + virtual networkClientHandler * attachNewClient( int SocketID ); + virtual bool detachClient( networkClientHandler *ptr ); + private: + static IM_Kernel * _instance; + // Constructor + explicit IM_Kernel( config_setting_t * Settings = NULL ); +}; + + +#endif // __IM_KERNEL_H_INCL__