|
|
|
@ -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::milliseconds>(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<int>(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 ); |
|
|
|
} |
|
|
|
} |
|
|
|
|