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.

69 lines
2.3 KiB

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