diff --git a/Projects/la_mojo/la_mojo.vpj b/Projects/la_mojo/la_mojo.vpj index 25499dee..ab3d34a3 100644 --- a/Projects/la_mojo/la_mojo.vpj +++ b/Projects/la_mojo/la_mojo.vpj @@ -176,7 +176,7 @@ RunFromDir="%rw"> + OtherOptions="-s"/> - + + + + + + + + + + + + + + + + + + + - + + m_eType){ case EVT_ROI_ACTIVE:{ - TARGET_DATA * tDATA = (TARGET_DATA *) E->m_pEvtData; - if( tDATA ) lookAt(E->m_db_id, tDATA->system_target); + if ( !m_bCameraAIControled ) { + TARGET_DATA *tDATA = (TARGET_DATA *)E->m_pEvtData; + if( tDATA ) lookAt(E->m_db_id, tDATA->system_target); + } break; }; + case EVT_CAMERA_AI_MODE_ON:{ + if( E->m_nDestinationNodeID == m_sNodeData.node_id ) m_bCameraAIControled = true; + break; + }; + case EVT_CAMERA_AI_MODE_OFF:{ + if( E->m_nDestinationNodeID == m_sNodeData.node_id ) m_bCameraAIControled = false; + break; + }; case EVT_CAMERA_MOVE_TO:{ - TARGET_DATA * tDATA = (TARGET_DATA *) E->m_pEvtData; - if (tDATA) lookAt( E->m_db_id, tDATA->system_target ); + //Event used by AI Analytics to move camera + TARGET_DATA *tDATA = (TARGET_DATA *)E->m_pEvtData; + if (tDATA) { + lookAt( E->m_db_id, tDATA->system_target ); + }; break; }; case EVT_TARGET_1D: @@ -66,18 +79,20 @@ bool deviceNodePTZCamControl::dispatchEvent(EVENT_CONTAINER * E) case EVT_TARGET_2D_WHITE: case EVT_TARGET_3D_BLACK: case EVT_TARGET_3D_WHITE:{ - TARGET_DATA * tDATA = (TARGET_DATA *) E->m_pEvtData; - if (tDATA){ -#ifdef DEBUG_EXTRA - LOG("CHECK CAMERA FOV"); -#endif - if (testFOV(tDATA->system_target)){ -#ifdef DEBUG_EXTRA - LOG("TARGET IN CAMERA FOV"); -#endif - lookAt(E->m_db_id, tDATA->system_target); - }; - }; + if ( !m_bCameraAIControled ) { + TARGET_DATA *tDATA = (TARGET_DATA *)E->m_pEvtData; + if (tDATA){ + #ifdef DEBUG_EXTRA + LOG("CHECK CAMERA FOV"); + #endif + if (testFOV(tDATA->system_target)){ + #ifdef DEBUG_EXTRA + LOG("TARGET IN CAMERA FOV"); + #endif + lookAt(E->m_db_id, tDATA->system_target); + }; + }; + } break; }; default: break; diff --git a/lib/App/common/kernelEventFilterYOLO.cpp b/lib/App/common/kernelEventFilterYOLO.cpp index f30975df..01ce81be 100644 --- a/lib/App/common/kernelEventFilterYOLO.cpp +++ b/lib/App/common/kernelEventFilterYOLO.cpp @@ -43,6 +43,9 @@ bool kernelEventFilterYOLO::validate() m_mDataProtector.lock(); memcpy(&m_sYOLOFilterData, tData, sizeof(YOLO_FILTER_DATA)); m_mDataProtector.unlock(); + + //ToDO: Make Re Init filter (stop the thread and start it again) + }; free(tData); return true; @@ -63,6 +66,11 @@ bool kernelEventFilterYOLO::initFilter(){ LOG_ERROR("No YOLO filter settings (getYOLOFilterData returned nothing)"); return false; }; + if ( m_sYOLOFilterData.gpu_index > -1 ) { + Darknet::set_gpu_index( m_sYOLOFilterData.gpu_index ); + } else { + LOG("Run On CPU Backend"); + }; char tB[160]; // LOG_ERROR formats into a 256-byte buffer (with file and function name): keep it short if ( !fileExist( m_sYOLOFilterData.config_path ) ) { snprintf( tB, sizeof(tB), "YOLO config not found: %.120s", m_sYOLOFilterData.config_path ); @@ -74,28 +82,45 @@ bool kernelEventFilterYOLO::initFilter(){ LOG_ERROR( tB ); return false; }; - if ( m_pNET ) { - free_network_ptr( m_pNET ); // initFilter() called again - m_pNET = NULL; + if ( !fileExist( m_sYOLOFilterData.classes_path ) ) { + snprintf( tB, sizeof(tB), "YOLO classes not found: %.120s", m_sYOLOFilterData.classes_path ); + LOG_ERROR( tB ); + return false; }; - if ( m_sYOLOFilterData.batch ) { - m_pNET = load_network_custom( m_sYOLOFilterData.config_path, m_sYOLOFilterData.weights_path, m_sYOLOFilterData.clear, m_sYOLOFilterData.batch ); - } else { - m_pNET = load_network( m_sYOLOFilterData.config_path, m_sYOLOFilterData.weights_path, m_sYOLOFilterData.clear ); + try { + if (m_pNET) { + //free_network_ptr( m_pNET ); // initFilter() called again + Darknet::free_neural_network( m_pNET ); + m_pNET = NULL; + }; + + m_pNET = Darknet::load_neural_network( m_sYOLOFilterData.config_path, m_sYOLOFilterData.classes_path, m_sYOLOFilterData.weights_path ); + + //if ( m_sYOLOFilterData.batch ) { + // m_pNET = load_network_custom( m_sYOLOFilterData.config_path, m_sYOLOFilterData.weights_path, m_sYOLOFilterData.clear, m_sYOLOFilterData.batch ); + //} else { + // m_pNET = load_network( m_sYOLOFilterData.config_path, m_sYOLOFilterData.weights_path, m_sYOLOFilterData.clear ); + //}; + if ( m_pNET ){ + Darknet::network_dimensions( m_pNET, m_nW, m_nH, m_nC ); + snprintf( tB, sizeof(tB), "YOLO network loaded: %dx%dx%d", m_nW, m_nH, m_nC ); + LOG( tB ); + Darknet::set_detection_threshold( m_pNET, m_sYOLOFilterData.threshold ); // minimum confidence (default 0.25) + Darknet::set_non_maximal_suppression_threshold( m_pNET, m_sYOLOFilterData.overlap_boxes ); // overlap for merging boxes (default 0.45) + return init(); + }; + } catch ( const std::exception & e ) { + LOG_ERROR( e.what() ); + if( m_pNET ) Darknet::free_neural_network( m_pNET ); }; - if ( m_pNET ){ - darknet_network_dimensions( m_pNET, &m_nW, &m_nH, &m_nC ); - snprintf( tB, sizeof(tB), "YOLO network loaded: %dx%dx%d", m_nW, m_nH, m_nC ); - LOG( tB ); - return true; - } LOG_ERROR("Darknet could not load the YOLO network"); return false; } bool kernelEventFilterYOLO::stopFilter(){ - return false; + return uninit(); } + bool kernelEventFilterYOLO::dispatchEvent(EVENT_CONTAINER * Evt){ (void) Evt; // ToDo: YOLO event handling return false; @@ -112,3 +137,63 @@ void kernelEventFilterYOLO::m_fThreadProc(void) SLEEP(DEFAULT_THREAD_SLEEP_TIME); }; } + + +bool kernelEventFilterYOLO::detectYOLO( cv::Mat &Picture ){ + //DarknetNetworkPtr tNET = ( DarknetNetworkPtr ) m_pNET; + if( m_pNET == NULL ) return false; + //int nW, nH, nC; + //darknet_network_dimensions( m_pNET, &nW, &nH, &nC ); + //Darknet::Image im = mat_to_image( Picture ); + + // Resize image to network dimensions + //Darknet::Image sized = letterbox_image( im, m_nW, m_nH ); + + // 4. Run the prediction + // The results are stored internally in the network structure + //network_predict_ptr( m_pNET, sized.data ); + + // 5. Get and process results (example for a single image) + //int nms_t = 0.45; // NMS threshold + //float thresh = 0.5; // Confidence threshold + //detection *dets = get_network_boxes( tNET, Picture.cols, Picture.rows, thresh, thresh, 0, 1, &nms_t, 0); + + // You would then iterate through 'dets' to draw boxes using OpenCV calls + // The bounding boxes (x, y, w, h) in `dets` are relative to the original image dimensions + + //LOG("Detections found. Processing results..."); + // Example loop to print detections + //for (int i = 0; i < tNET->layers[ tNET->n-1 ].outputs; ++i) { + // if( dets[i].prob[0] > thresh ){ // Check if confidence > threshold + // // Process the detection + // }; + //}; + + + // 6. Free allocated memory + //free_detections( dets, m_pNET->layers[ m_pNET->n-1 ].outputs ); + //free_image( im ); + //free_image( sized ); + + + /*const*/ Darknet::Predictions predictions; + if ( m_sYOLOFilterData.debug_mode ) { + const auto t0 = std::chrono::steady_clock::now(); + predictions = Darknet::predict( m_pNET, Picture ); + const auto ms = std::chrono::duration_cast(std::chrono::steady_clock::now() - t0).count(); + const Darknet::VStr &names = Darknet::get_class_names(m_pNET); + for ( const auto & p : predictions ){ + if( p.best_class < 0 ){ + continue; // nothing above the threshold + }; + const float confidence = p.prob.count(p.best_class) ? p.prob.at(p.best_class) : 0.0f; + //std::cout << " " << names.at( p.best_class ) + // << " " << static_cast(confidence * 100.0f + 0.5f) << "%" + // << " x=" << p.rect.x << " y=" << p.rect.y + // << " w=" << p.rect.width << " h=" << p.rect.height << std::endl; + }; + } else { + predictions = Darknet::predict( m_pNET, Picture ); + } +} + diff --git a/lib/App/common/kernelEventFilterYOLO.h b/lib/App/common/kernelEventFilterYOLO.h index 6af5ec13..882fd270 100644 --- a/lib/App/common/kernelEventFilterYOLO.h +++ b/lib/App/common/kernelEventFilterYOLO.h @@ -6,6 +6,8 @@ #include "kernelEventFilter.h" #include "kernelPrototype.h" +#include "../../components/base/basicLIBAV.h" + //#include "../../data/darknet/darknetWraper.h" //#include @@ -13,14 +15,23 @@ // when DARKNET_INCLUDE_ORIGINAL_API is defined before darknet.h ("was not declared in this scope"). #define DARKNET_INCLUDE_ORIGINAL_API // Darknet's CMake installs the header as /include/darknet.h (no darknet/ subdirectory) -#if __has_include() - #include -#elif __has_include() - #include +#if __has_include() + //#include + #include +#elif __has_include() + //#include + #include #else - #error "darknet.h not found: install Darknet (libdarknet.so + darknet.h, e.g. the darknet .deb)" + #error "darknet.hpp not found: install Darknet (libdarknet.so + darknet.hpp, e.g. the darknet .deb)" #endif + +#include +#include +#include +#include +#include + class kernelEventFilterYOLO : public kernelEventFilter, public basicEventHandler, public basicThread { private: basicLogCollection * m_pLog; @@ -28,10 +39,11 @@ class kernelEventFilterYOLO : public kernelEventFilter, public basicEventHandler basicMutex m_mStateProtector; basicMutex m_mDataProtector; //darknetWraper * m_pDarkNet; - DarknetNetworkPtr m_pNET; // DarknetNetworkPtr is already a pointer (void *) + Darknet::NetworkPtr m_pNET; // DarknetNetworkPtr is already a pointer (void *) int m_nW; int m_nH; int m_nC; + bool detectYOLO( cv::Mat &Picture ); protected: virtual void m_fThreadProc(void); diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index 63adc285..1e70334e 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -2418,6 +2418,11 @@ bool kernelModuleEmbedVideo::unregisterStream(uint32_t streamID) return false; } +cv::Mat *kernelModuleEmbedVideo::getCurrentFrame( uint32_t streamID ){ + cv::Mat *retVal = NULL; + return retVal; +} + #undef TLOG #undef TLOG_ERROR diff --git a/lib/App/common/kernelModuleEmbedVideo.h b/lib/App/common/kernelModuleEmbedVideo.h index aa719902..e4492fe9 100644 --- a/lib/App/common/kernelModuleEmbedVideo.h +++ b/lib/App/common/kernelModuleEmbedVideo.h @@ -139,10 +139,11 @@ class kernelModuleEmbedVideo : public kernelModule */ int getWidh(void); int getHeight(void); - bool registerStream(uint32_t streamID, uint8_t flip_mode = 0); - bool unregisterStream(uint32_t streamID); + bool registerStream( uint32_t streamID, uint8_t flip_mode = 0 ); + bool unregisterStream( uint32_t streamID ); uint8_t getMediaMode(void); bool isProcessingVideoStream(); + cv::Mat * getCurrentFrame( uint32_t streamID ); friend class streamHandler; }; #else //LRF_PROXY_ONLY diff --git a/lib/components/base/basicEvent.h b/lib/components/base/basicEvent.h index 4f78cdce..bffd14aa 100644 --- a/lib/components/base/basicEvent.h +++ b/lib/components/base/basicEvent.h @@ -132,8 +132,10 @@ typedef enum E_basicEventTypeID : EVT_SRV_MODBUS_CLEAR_P = 97, // Triggering SCADA Input Clear ALL Platform Signals and Modbus Blockers EVT_SRV_MODBUS_S_POSTBOX = 98, // Snooze one Modbus Blocker EVT_SRV_MODBUS_D_POSTBOX = 99, // Dissmis one Modbus Blocker - EVT_SRV_MODBUS_CLEAR_S = 100, // Dissmiss all Platform alarm signals only - EVT_COUNT = 101 + EVT_SRV_MODBUS_CLEAR_S = 100,// Dissmiss all Platform alarm signals only + EVT_CAMERA_AI_MODE_ON = 101,// Set Camera Movement by AI Only -> Only Targeted Event + EVT_CAMERA_AI_MODE_OFF = 102,// Enables all Camera Movements (Auto && Manual) -> Only Targeted Event + EVT_COUNT = 103 } basicEventTypeID; typedef enum E_basicEventClassID :