yolov8_utils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #include<iostream>
  3. #include <numeric>
  4. #include<opencv2/opencv.hpp>
  5. #ifdef _WIN32
  6. #include <io.h> // Windows: _access
  7. #else
  8. #include <unistd.h> // Linux/macOS: access
  9. #endif
  10. #define ORT_OLD_VISON 13 //ort1.12.0 之前的版本为旧版本API
  11. struct PoseKeyPoint {
  12. float x = 0;
  13. float y = 0;
  14. float confidence = 0;
  15. };
  16. struct OutputParams {
  17. int id; //结果类别id
  18. float confidence; //结果置信度
  19. cv::Rect box; //矩形框
  20. cv::RotatedRect rotatedBox; //obb结果矩形框
  21. cv::Mat boxMask; //矩形框内mask,节省内存空间和加快速度
  22. std::vector<PoseKeyPoint> keyPoints; //pose key points
  23. };
  24. struct MaskParams {
  25. //int segChannels = 32;
  26. //int segWidth = 160;
  27. //int segHeight = 160;
  28. int netWidth = 640;
  29. int netHeight = 640;
  30. float maskThreshold = 0.5;
  31. cv::Size srcImgShape;
  32. cv::Vec4d params;
  33. };
  34. struct PoseParams {
  35. float kptThreshold = 0.5;
  36. int kptRadius = 5;
  37. bool isDrawKptLine = false; //If True, the function will draw lines connecting keypoint for human pose.Default is True.
  38. cv::Scalar objColor = cv::Scalar(0, 0, 255);
  39. std::vector<std::vector<int>>skeleton = {
  40. {16, 14} ,{14, 12},{17, 15},{15, 13},
  41. {12, 13},{6, 12},{7, 13},{6, 7},{6, 8},{7, 9},
  42. {8, 10},{9, 11},{2, 3},{1, 2},{1, 3},{2, 4},
  43. {3, 5},{4, 6},{5, 7}
  44. };
  45. std::vector<cv::Scalar> posePalette =
  46. {
  47. cv::Scalar(255, 128, 0)
  48. };
  49. std::vector<int> limbColor = {0};
  50. std::vector<int> kptColor = {0};
  51. std::map<unsigned int, std::string> kptBodyNames{
  52. {0,"A"}
  53. };
  54. };
  55. bool CheckModelPath(std::string modelPath);
  56. bool CheckParams(int netHeight, int netWidth, const int* netStride, int strideSize);
  57. void DrawPred(cv::Mat& img,
  58. std::vector<OutputParams> result,
  59. std::vector<std::string> classNames,
  60. std::vector<cv::Scalar> color
  61. );
  62. void DrawPredPose(cv::Mat& img, std::vector<OutputParams> result, PoseParams& poseParams);
  63. void DrawRotatedBox(cv::Mat& srcImg, cv::RotatedRect box, cv::Scalar color, int thinkness);
  64. void LetterBox(const cv::Mat& image, cv::Mat& outImage,
  65. cv::Vec4d& params, //[ratio_x,ratio_y,dw,dh]
  66. const cv::Size& newShape = cv::Size(640, 640),
  67. bool autoShape = false,
  68. bool scaleFill = false,
  69. bool scaleUp = true,
  70. int stride = 32,
  71. const cv::Scalar& color = cv::Scalar(114, 114, 114));
  72. void GetMask(const cv::Mat& maskProposals, const cv::Mat& maskProtos, std::vector<OutputParams>& output, const MaskParams& maskParams);
  73. void GetMask2(const cv::Mat& maskProposals, const cv::Mat& maskProtos, OutputParams& output, const MaskParams& maskParams);
  74. int BBox2Obb(float centerX, float centerY, float boxW, float boxH, float angle, cv::RotatedRect& rotatedRect);