|
|
#include "darknetWraper.h"
#include <experimental/filesystem>
#define DARKNET_INCLUDE_ORIGINAL_API
#include <darknet/darknet.hpp>
//#include <darknet/darknet_image.hpp>
darknetWraper::darknetWraper(){ m_pNET = NULL;};
darknetWraper::~darknetWraper(){ if ( m_pNET ) free_network_ptr( ( DarknetNetworkPtr ) m_pNET );};
bool darknetWraper::l_n( CSTR conf, CSTR weights, uint8_t clear, uint8_t batch ){ if ( !fileExist( conf ) ) return false; if ( !fileExist( weights ) ) return false; if ( batch ) { m_pNET = (void *)load_network_custom( conf, weights, clear, batch ); } else { m_pNET = (void *)load_network( conf, weights, clear ); }; if ( m_pNET ) return true; return false;};
bool darknetWraper::detect( cv::Mat &Picture ){ DarknetNetworkPtr tNET = ( DarknetNetworkPtr ) m_pNET; if ( m_pNET == NULL ) return false; int nW, nH, nC; darknet_network_dimensions( tNET, &nW, &nH, &nC ); //image im = rgb_mat_to_rgb_image( Picture );
Darknet::Image im = mat_to_image( Picture );
// Resize image to network dimensions
//image sized = letterbox_image( im, tNET->w, tNET->h );
Darknet::Image sized = letterbox_image( im, nW, 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[ tNET->n-1 ].outputs ); free_image( im ); free_image( sized ); return true;};
|