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.

65 lines
2.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknetWraper.h"
  2. #include <experimental/filesystem>
  3. #define DARKNET_INCLUDE_ORIGINAL_API
  4. #include <darknet/darknet.hpp>
  5. //#include <darknet/darknet_image.hpp>
  6. darknetWraper::darknetWraper(){
  7. m_pNET = NULL;
  8. };
  9. darknetWraper::~darknetWraper(){
  10. if ( m_pNET ) free_network_ptr( ( DarknetNetworkPtr ) m_pNET );
  11. };
  12. bool darknetWraper::l_n( CSTR conf, CSTR weights, uint8_t clear, uint8_t batch ){
  13. if ( !fileExist( conf ) ) return false;
  14. if ( !fileExist( weights ) ) return false;
  15. if ( batch ) {
  16. m_pNET = (void *)load_network_custom( conf, weights, clear, batch );
  17. } else {
  18. m_pNET = (void *)load_network( conf, weights, clear );
  19. };
  20. if ( m_pNET ) return true;
  21. return false;
  22. };
  23. bool darknetWraper::detect( cv::Mat &Picture ){
  24. DarknetNetworkPtr tNET = ( DarknetNetworkPtr ) m_pNET;
  25. if ( m_pNET == NULL ) return false;
  26. int nW, nH, nC;
  27. darknet_network_dimensions( tNET, &nW, &nH, &nC );
  28. //image im = rgb_mat_to_rgb_image( Picture );
  29. Darknet::Image im = mat_to_image( Picture );
  30. // Resize image to network dimensions
  31. //image sized = letterbox_image( im, tNET->w, tNET->h );
  32. Darknet::Image sized = letterbox_image( im, nW, nH );
  33. // 4. Run the prediction
  34. // The results are stored internally in the network structure
  35. network_predict_ptr( m_pNET, sized.data );
  36. // 5. Get and process results (example for a single image)
  37. int nms_t = 0.45; // NMS threshold
  38. float thresh = 0.5; // Confidence threshold
  39. detection *dets = get_network_boxes( tNET, Picture.cols, Picture.rows, thresh, thresh, 0, 1, &nms_t, 0);
  40. // You would then iterate through 'dets' to draw boxes using OpenCV calls
  41. // The bounding boxes (x, y, w, h) in `dets` are relative to the original image dimensions
  42. //LOG("Detections found. Processing results...");
  43. // Example loop to print detections
  44. for (int i = 0; i < tNET->layers[ tNET->n-1 ].outputs; ++i) {
  45. if( dets[i].prob[0] > thresh ){ // Check if confidence > threshold
  46. // Process the detection
  47. };
  48. };
  49. // 6. Free allocated memory
  50. free_detections( dets, m_pNet->layers[ tNET->n-1 ].outputs );
  51. free_image( im );
  52. free_image( sized );
  53. return true;
  54. };