AbandClipStrategy.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "AbandClipStrategy.h"
  2. #include "ClipCfg.h"
  3. #include "AbandObjCfg.h"
  4. #include "AbandClipCfg.h"
  5. #include "OSTime.h"
  6. #include "SysUtils.h"
  7. NAMESPACE_MAS_BEGIN
  8. NAMESPACE_ABANDCLIP_BEGIN
  9. AbandClipStrategy::AbandClipStrategy() :
  10. Strategy("AbandClip", "遗留物检测判定策略") {}
  11. AbandClipStrategy::~AbandClipStrategy()
  12. {
  13. this->Dispose();
  14. }
  15. TZ_INT AbandClipStrategy::Initialize()
  16. {
  17. m_inited = TRUE;
  18. return MEC_OK;
  19. }
  20. TZ_INT AbandClipStrategy::Dispose()
  21. {
  22. m_inited = FALSE;
  23. return MEC_OK;
  24. }
  25. TZ_INT AbandClipStrategy::SetStrategyCfg(const std::string & param)
  26. {
  27. if (!m_inited) return MEC_NOT_INITED;
  28. AbandClipCfg cfg;
  29. AbandClipCfg::fromJson(param, cfg);
  30. m_featureThreshold = cfg.FeatureThreshold;
  31. std::ifstream file(cfg.FeatureFilePath);
  32. if (!file.is_open())
  33. {
  34. TZLogError("Open file failed!!!");
  35. return MEC_FAILED;
  36. }
  37. std::string jsonContent((std::istreambuf_iterator<char>(file)),
  38. std::istreambuf_iterator<char>());
  39. if (jsonContent.empty())
  40. {
  41. TZLogError("Json content is empty!!!");
  42. return MEC_FAILED;
  43. }
  44. file.close();
  45. tzc::json::JsonDoc document;
  46. document.Parse(jsonContent.c_str());
  47. if (document.HasParseError())
  48. {
  49. TZLogError("Parse json failed!!!");
  50. return MEC_FAILED;
  51. }
  52. for (auto it = document.MemberBegin(); it != document.MemberEnd(); ++it)
  53. {
  54. if (!it->name.IsString() || !it->value.IsArray())
  55. {
  56. continue;
  57. }
  58. TextFeature feature;
  59. feature.text = it->name.GetString();
  60. // 提取数组内容
  61. for (const auto& v : it->value.GetArray())
  62. {
  63. if (v.IsNumber())
  64. {
  65. DataItem dataItem;
  66. dataItem.val = v.GetFloat();
  67. feature.features.push_back(dataItem);
  68. }
  69. }
  70. m_textFeatureList.push_back(feature);
  71. }
  72. return MEC_OK;
  73. }
  74. TZ_INT AbandClipStrategy::DoStrategy(SPtr<StreamInfo> & streamInfo)
  75. {
  76. if (!m_inited) return MEC_NOT_INITED;
  77. auto & detMap = streamInfo->GetAllDetRst();
  78. StraRst rst;
  79. rst.RstType = SRT_ALARM;
  80. if (detMap.count("Clip"))
  81. {
  82. m_clipLock.Lock();
  83. m_qClipQueue.push(detMap["Clip"]->Result);
  84. m_clipLock.Unlock();
  85. }
  86. else
  87. {
  88. TZLogWarn("Rst not found!!!");
  89. streamInfo->SetStraRst(m_key, rst, streamInfo->GetMediaRsc());
  90. return MEC_FAILED;
  91. }
  92. m_clipLock.Lock();
  93. if (m_qClipQueue.empty())
  94. {
  95. m_clipLock.Unlock();
  96. streamInfo->SetStraRst(m_key, rst, streamInfo->GetMediaRsc());
  97. return MEC_OK;
  98. }
  99. std::string jClipRst = m_qClipQueue.front();
  100. m_qClipQueue.pop();
  101. m_clipLock.Unlock();
  102. // Clip Start
  103. clip::ClipDetectResult clipRst;
  104. clip::ClipDetectResult::fromJson(jClipRst, clipRst);
  105. std::vector<TZ_FLOAT> abandobjFeature;
  106. for (const auto& data : clipRst.output_data)
  107. {
  108. abandobjFeature.push_back(data.val);
  109. TZLogInfo("AbandObjFeature: %f", data.val);
  110. }
  111. if (abandobjFeature.empty())
  112. {
  113. TZLogWarn("AbandObjFeature is empty!!!");
  114. streamInfo->SetStraRst(m_key, rst, streamInfo->GetMediaRsc());
  115. return MEC_FAILED;
  116. }
  117. // Clip End
  118. const TZ_FLOAT MIN_DOT_PRODUCT = std::numeric_limits<TZ_FLOAT>::lowest();
  119. TZ_FLOAT maxDotProduct = MIN_DOT_PRODUCT;
  120. std::string bestMatchText;
  121. for (const auto& textFeature : m_textFeatureList)
  122. {
  123. std::vector<TZ_FLOAT> featureValues;
  124. for (const auto& dataItem : textFeature.features)
  125. {
  126. featureValues.push_back(dataItem.val);
  127. }
  128. TZ_FLOAT dotProduct = calculateDotProduct(featureValues, abandobjFeature);
  129. if (dotProduct > maxDotProduct && dotProduct > m_featureThreshold) {
  130. maxDotProduct = dotProduct;
  131. bestMatchText = textFeature.text;
  132. }
  133. }
  134. if (!bestMatchText.empty())
  135. {
  136. rst.RstType = SRT_ALARM;
  137. std::ostringstream oss;
  138. oss << "Detected left-behind item, class: " << bestMatchText;
  139. rst.RstName = oss.str();
  140. rst.BeginTime = TIME_STAMP_NOW;
  141. }
  142. else
  143. {
  144. rst.RstType = SRT_ALARM;
  145. rst.RstName = "Detected left-behind, class: Unknown";
  146. }
  147. streamInfo->SetStraRst(m_key, rst, streamInfo->GetMediaRsc());
  148. return MEC_OK;
  149. }
  150. TZ_FLOAT AbandClipStrategy::calculateDotProduct(const std::vector<float>& matrix1, const std::vector<float>& matrix2)
  151. {
  152. TZ_FLOAT dotProduct = 0.0f;
  153. for (TZ_INT i = 0; i < 512; ++i)
  154. {
  155. dotProduct += matrix1[i] * matrix2[i];
  156. }
  157. return dotProduct;
  158. }
  159. NAMESPACE_ABANDCLIP_END
  160. NAMESPACE_MAS_END