yolov8_obb.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include"yolov8_obb.h"
  2. //using namespace std;
  3. //using namespace cv;
  4. //using namespace cv::dnn;
  5. bool Yolov8Obb::ReadModel(cv::dnn::Net& net, std::string& netPath, bool isCuda = false) {
  6. try {
  7. if (!CheckModelPath(netPath))
  8. return false;
  9. #if CV_VERSION_MAJOR==4 &&CV_VERSION_MINOR<7
  10. std::cout << "OBB Need OpenCV Version >=4.7.0" << std::endl;
  11. return false;
  12. #endif
  13. net = cv::dnn::readNetFromONNX(netPath);
  14. #if CV_VERSION_MAJOR==4 &&CV_VERSION_MINOR==7&&CV_VERSION_REVISION==0
  15. net.enableWinograd(false); //bug of opencv4.7.x in AVX only platform ,https://github.com/opencv/opencv/pull/23112 and https://github.com/opencv/opencv/issues/23080
  16. //net.enableWinograd(true); //If your CPU supports AVX2, you can set it true to speed up
  17. #endif
  18. }
  19. catch (const std::exception&) {
  20. return false;
  21. }
  22. if (isCuda) {
  23. //cuda
  24. net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
  25. net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA); //or DNN_TARGET_CUDA_FP16
  26. }
  27. else {
  28. //cpu
  29. std::cout << "Inference device: CPU" << std::endl;
  30. net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
  31. net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
  32. }
  33. return true;
  34. }
  35. bool Yolov8Obb::Detect(cv::Mat& srcImg, cv::dnn::Net& net, std::vector<OutputParams>& output) {
  36. cv::Mat blob;
  37. output.clear();
  38. int col = srcImg.cols;
  39. int row = srcImg.rows;
  40. cv::Mat netInputImg;
  41. cv::Vec4d params;
  42. LetterBox(srcImg, netInputImg, params, cv::Size(_netWidth, _netHeight));
  43. cv::dnn::blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(_netWidth, _netHeight), cv::Scalar(0, 0, 0), true, false);
  44. //**************************************************************************************************************************************************/
  45. //如果在其他设置没有问题的情况下但是结果偏差很大,可以尝试下用下面两句语句
  46. // If there is no problem with other settings, but results are a lot different from Python-onnx , you can try to use the following two sentences
  47. //
  48. //$ cv::dnn::blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(_netWidth, _netHeight), cv::Scalar(104, 117, 123), true, false);
  49. //$ cv::dnn::blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(_netWidth, _netHeight), cv::Scalar(114, 114,114), true, false);
  50. //****************************************************************************************************************************************************/
  51. net.setInput(blob);
  52. std::vector<cv::Mat> net_output_img;
  53. net.forward(net_output_img, net.getUnconnectedOutLayersNames()); //get outputs
  54. std::vector<int> class_ids;// res-class_id
  55. std::vector<float> confidences;// res-conf
  56. std::vector<cv::RotatedRect> boxes;// res-box
  57. cv::Mat output0 = cv::Mat(cv::Size(net_output_img[0].size[2], net_output_img[0].size[1]), CV_32F, (float*)net_output_img[0].data).t(); //[bs,20,21504]=>[bs,21504,20]
  58. int net_width = output0.cols;
  59. int rows = output0.rows;
  60. int class_score_length = net_width - 5;
  61. int angle_index = net_width - 1;
  62. float* pdata = (float*)output0.data;
  63. for (int r = 0; r < rows; ++r) {
  64. cv::Mat scores(1, class_score_length, CV_32FC1, pdata + 4);
  65. cv::Point classIdPoint;
  66. double max_class_socre;
  67. minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
  68. max_class_socre = (float)max_class_socre;
  69. if (max_class_socre >= _classThreshold) {
  70. //rect [x,y,w,h]
  71. float x = (pdata[0] - params[2]) / params[0];
  72. float y = (pdata[1] - params[3]) / params[1];
  73. float w = pdata[2] / params[0];
  74. float h = pdata[3] / params[1];
  75. float angle = pdata[angle_index] / CV_PI *180.0;
  76. class_ids.push_back(classIdPoint.x);
  77. confidences.push_back(max_class_socre);
  78. //cv::RotatedRect temp_rotated;
  79. //BBox2Obb(x, y, w, h, angle, temp_rotated);
  80. //boxes.push_back(temp_rotated);
  81. boxes.push_back(cv::RotatedRect(cv::Point2f(x, y), cv::Size(w, h), angle));
  82. }
  83. pdata += net_width;//next line
  84. }
  85. //NMS
  86. std::vector<int> nms_result;
  87. cv::dnn::NMSBoxes(boxes, confidences, _classThreshold, _nmsThreshold, nms_result);
  88. std::vector<std::vector<float>> temp_mask_proposals;
  89. //cv::Rect holeImgRect(0, 0, srcImg.cols, srcImg.rows);
  90. for (int i = 0; i < nms_result.size(); ++i) {
  91. int idx = nms_result[i];
  92. OutputParams result;
  93. result.id = class_ids[idx];
  94. result.confidence = confidences[idx];
  95. result.rotatedBox = boxes[idx];
  96. if (result.rotatedBox.size.width<1|| result.rotatedBox.size.height<1)
  97. continue;
  98. output.push_back(result);
  99. }
  100. if (output.size())
  101. return true;
  102. else
  103. return false;
  104. }