NanoDetector.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "NanoDetector.h"
  2. #include "dnninfer.h"
  3. CNanoDetector::CNanoDetector(const char* strModelFile, int iBatchSize, int iInferMode, int iDeviceIndex)
  4. {
  5. m_bInit = false;
  6. m_strModelFile = strModelFile;
  7. m_pInfer = NULL;
  8. m_iBatchSize = iBatchSize;
  9. m_iInferMode = iInferMode;
  10. m_iDeviceInedx = 0;
  11. }
  12. CNanoDetector::~CNanoDetector()
  13. {
  14. UnInit();
  15. }
  16. bool CNanoDetector::Init()
  17. {
  18. bool bRet = false;
  19. {
  20. m_pInfer = DnnInfer_Init(m_strModelFile.c_str(), m_iBatchSize, m_iInferMode, m_iDeviceInedx);
  21. if (m_pInfer != NULL)
  22. {
  23. m_bInit = true;
  24. bRet = true;
  25. }
  26. }
  27. return bRet;
  28. }
  29. bool CNanoDetector::UnInit()
  30. {
  31. if (m_bInit)
  32. {
  33. m_bInit = false;
  34. if (m_pInfer != NULL)
  35. {
  36. DnnInfer_Close(m_pInfer);
  37. }
  38. }
  39. return true;
  40. }
  41. const char* CNanoDetector::GetType()
  42. {
  43. return "CNanoDetector";
  44. }
  45. bool CNanoDetector::Execute(cv::Mat& inputImage)
  46. {
  47. m_vecDetectResult.clear();
  48. if (m_pInfer == NULL)
  49. return false;
  50. if (!inputImage.isContinuous())
  51. inputImage = inputImage.clone();
  52. Datum inputData(inputImage.data, Shape(1, inputImage.channels(), inputImage.rows, inputImage.cols), 1);
  53. DetResult detResult[512];
  54. int iUseCount = 0;
  55. DnnInfer_Det_InferV2(m_pInfer, &inputData, detResult, 512, iUseCount, 0.4, 0.5);
  56. for (int i = 0; i < iUseCount; i++)
  57. {
  58. DetectorResult detectResult;
  59. detectResult.hasMask = false;
  60. detectResult.image_id = 0;
  61. detectResult.label = detResult[i].label;
  62. detectResult.score = detResult[i].score;
  63. strcpy_s(detectResult.name, detResult[i].name);
  64. char strLabelName[32] = { 0 };
  65. DnnInfer_GetLabelName(m_pInfer, strLabelName, detResult[i].label);
  66. //printf("LabelName:%s %d %d %d %d\n", strLabelName);
  67. //detectResult.name = detResult[i].name;
  68. detectResult.left = detResult[i].left;
  69. detectResult.top = detResult[i].top;
  70. detectResult.width = detResult[i].width;
  71. detectResult.height = detResult[i].height;
  72. m_vecDetectResult.emplace_back(detectResult);
  73. }
  74. //cv::Mat mask, predict;
  75. //int target_label = 1;
  76. ////使mask = target_label的像素 = 1
  77. //cv::Mat target = mask == target_label;
  78. ////使 predict = target_label的像素 = 1
  79. //cv::Mat result = predict = target_label;
  80. //
  81. ////使标注和推理都等于targetlabel的像素=1
  82. //cv::Mat intersection = target == result;
  83. ////标注和推理非零像素相加 - 标注和推理都为1的像素
  84. //cv::Mat unionSet = cv::sum(target + result) - intersection;
  85. ////计算交集=1像素的和/=1的相机并集的和
  86. //float iou = cv::sum(intersection)[0] / cv::sum(unionSet)[0];
  87. return true;
  88. }
  89. bool CNanoDetector::GetResults(std::vector<DetectorResult>& vecDetectorResults)
  90. {
  91. vecDetectorResults = m_vecDetectResult;
  92. return true;
  93. }