InfineFilter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-03-09 10:58:34
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-09-21 14:14:27
  8. */
  9. #include "InfineFilter.hpp"
  10. namespace gsd
  11. {
  12. /**
  13. * @description: 获取ptr
  14. * @param {*}
  15. * @return {*}
  16. */
  17. std::shared_ptr<InfineFilter> InfineFilter::getPtr(){
  18. static std::shared_ptr<InfineFilter> m_infineFilter = nullptr;
  19. if(m_infineFilter == nullptr) m_infineFilter = std::shared_ptr<InfineFilter>(new InfineFilter);
  20. return m_infineFilter;
  21. }
  22. /**
  23. * @description: 判定结果
  24. * @param {*}
  25. * @return {*}
  26. */
  27. int8_t InfineFilter::judgementResult(std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  28. int8_t result = ERR;
  29. // 过滤小目标
  30. MinBBoxFilter(cnStreamInferData);
  31. int result1;
  32. int result2;
  33. switch (filterLevel)
  34. {
  35. case 0:
  36. result = OK;
  37. break;
  38. case 1:
  39. result = this->LastOneJudgment(cnStreamInferData);
  40. break;
  41. case 2:
  42. result1 = this->IntervalJudgment();
  43. result2 = this->LastOneJudgment(cnStreamInferData);
  44. result = (result1 == OK && result2 == OK ? OK : ERR);
  45. break;
  46. case 3:
  47. result = this->LastOneJudgment(cnStreamInferData);
  48. if(result == ERR) break;
  49. this->SimilarityJudgment(cnStreamInferData);
  50. if(cnStreamInferData->Objects.size() == 0) result = ERR;
  51. break;
  52. default:
  53. break;
  54. }
  55. return result;
  56. }
  57. /**
  58. * @description: 最小BBox过滤器
  59. * @return {*}
  60. */
  61. int8_t InfineFilter::MinBBoxFilter(std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  62. if(this->bboxSize == 0) return OK;
  63. vector<std::vector<InferInfo>::iterator> ret;
  64. for(auto iter = cnStreamInferData->Objects.begin(); iter != cnStreamInferData->Objects.end(); iter++){
  65. double t = (iter->BBox.h * (double)cnStreamInferData->width) * (iter->BBox.w * (double)1920);
  66. if(t < (double)bboxSize) ret.push_back(iter);
  67. }
  68. for(auto iter = ret.begin(); iter != ret.end(); iter++){
  69. cnStreamInferData->Objects.erase(*iter);
  70. }
  71. return OK;
  72. }
  73. /**
  74. * @description: 获取是否相似
  75. * @param {InferInfo} infer1
  76. * @param {InferInfo} infer2
  77. * @return {*}
  78. */
  79. int8_t InfineFilter::CalculateSimilarity(InferInfo& infer1, InferInfo& infer2, std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  80. // Label
  81. if(infer1.Label != infer2.Label) return ERR;
  82. // Source
  83. // BBOX
  84. // double difference = fabs(infer1.BBox.x - infer2.BBox.x);
  85. // if(difference > 10) return ERR;
  86. // difference = fabs(infer1.BBox.y - infer2.BBox.y );
  87. // if(difference > 10) return ERR;
  88. // difference = fabs(infer1.BBox.w - infer2.BBox.w );
  89. // if(difference > 10) return ERR;
  90. // difference = fabs(infer1.BBox.h - infer2.BBox.h );
  91. // if(difference > 10) return ERR;
  92. auto result = this->getIou(infer1, infer2, cnStreamInferData);
  93. if(result < 0.5) return ERR;
  94. return OK;
  95. }
  96. /**
  97. * @description: 设置灵敏度
  98. * @param {int} FilterLevel
  99. * @return {*}
  100. */
  101. int8_t InfineFilter::setFilterLevel(int FilterLevel){
  102. if(filterLevel < 0 || filterLevel > 3) return ERR;
  103. this->filterLevel = FilterLevel;
  104. return OK;
  105. }
  106. /**
  107. * @description: 设置时间间隔
  108. * @param {int} Interval
  109. * @return {*}
  110. */
  111. int8_t InfineFilter::setInterval(int Interval){
  112. if(Interval < 0 || Interval > 60000) return ERR;
  113. this->interval = Interval;
  114. return OK;
  115. }
  116. /**
  117. * @description: 设置超时时间
  118. * @param {int} timeOut
  119. * @return {*}
  120. */
  121. int8_t InfineFilter::setTimeOut(int timeOut){
  122. if(timeOut < 0) return ERR;
  123. this->TimeOut = timeOut;
  124. return OK;
  125. }
  126. /**
  127. * @description: 与上一次的结果判断
  128. * @param {*}
  129. * @return {*}
  130. */
  131. int8_t InfineFilter::LastOneJudgment(std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  132. static std::shared_ptr<CNStreamInferData> old = nullptr;
  133. if(cnStreamInferData->Objects.size() == 0) return OK;
  134. if(old == nullptr) {
  135. old = cnStreamInferData;
  136. return OK;
  137. }
  138. // 判断是否为同一个视频内
  139. if(old->videoPath == cnStreamInferData->videoPath){
  140. // 分析结果数量
  141. int num = 0;
  142. if(old->Objects.size() == cnStreamInferData->Objects.size()){
  143. for(auto iter1 = old->Objects.begin(); iter1 != old->Objects.end(); iter1++){
  144. for(auto iter2 = cnStreamInferData->Objects.begin(); iter2 != cnStreamInferData->Objects.end(); iter2++){
  145. if(CalculateSimilarity(*iter1, *iter2, cnStreamInferData) == OK) num++;
  146. }
  147. }
  148. }
  149. if(num == old->Objects.size() && num != 0) {
  150. return ERR;
  151. }
  152. }
  153. if(cnStreamInferData->Objects.size() != 0) old = cnStreamInferData;
  154. return OK;
  155. }
  156. /**
  157. * @description: 时间间隔判定
  158. * @param {*}
  159. * @return {*}
  160. */
  161. int8_t InfineFilter::IntervalJudgment(){
  162. static std::shared_ptr<Ticker> ticker = nullptr;
  163. if(ticker == nullptr) {
  164. ticker = std::make_shared<Ticker>();
  165. return OK;
  166. }
  167. if(ticker->elapsedTime() >= config::getPtr()->InferInterval){
  168. ticker->resetTime();
  169. return OK;
  170. }
  171. return ERR;
  172. }
  173. /**
  174. * @description: 历史判定
  175. * @param {*}
  176. * @return {*}
  177. */
  178. int8_t InfineFilter::HistoricalJudgment(std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  179. // 历史检查
  180. this->HistoricalCheck();
  181. auto task = [&](){
  182. HistoryData historyData;
  183. historyData.ticker = std::make_shared<Ticker>();
  184. historyData.CnStreamInferData = std::make_shared<CNStreamInferData>();
  185. historyData.CnStreamInferData->StreamName = cnStreamInferData->StreamName;
  186. historyData.CnStreamInferData->FrameCount = cnStreamInferData->FrameCount;
  187. historyData.CnStreamInferData->width = cnStreamInferData->width;
  188. historyData.CnStreamInferData->height = cnStreamInferData->height;
  189. historyData.CnStreamInferData->Objects = cnStreamInferData->Objects;
  190. historyData.CnStreamInferData->videoPath = cnStreamInferData->videoPath;
  191. this->HistoryDatas.push_back(historyData);
  192. };
  193. if(HistoryDatas.empty()){
  194. task();
  195. return OK;
  196. }
  197. for(auto iter = HistoryDatas.begin(); iter != HistoryDatas.end(); iter++){
  198. int num = 0;
  199. if(iter->CnStreamInferData->Objects.size() == cnStreamInferData->Objects.size()){
  200. for(auto iter1 = iter->CnStreamInferData->Objects.begin(); iter1 != iter->CnStreamInferData->Objects.end(); iter1++){
  201. for(auto iter2 = cnStreamInferData->Objects.begin(); iter2 != cnStreamInferData->Objects.end(); iter2++){
  202. if(CalculateSimilarity(*iter1, *iter2, cnStreamInferData) == OK) num++;
  203. }
  204. }
  205. }
  206. if(num == iter->CnStreamInferData->Objects.size()) {
  207. iter->ticker->resetTime();
  208. return ERR;
  209. }
  210. }
  211. task();
  212. return OK;
  213. }
  214. /**
  215. * @description: 历史检查
  216. * @param {*}
  217. * @return {*}
  218. */
  219. void InfineFilter::HistoricalCheck(){
  220. if(filterLevel != 3) return;
  221. vector<vector<HistoryData>::iterator> ret;
  222. for(auto iter = this->HistoryDatas.begin(); iter != this->HistoryDatas.end(); iter++){
  223. if(iter->ticker != nullptr && iter->ticker->elapsedTime() > TimeOut){
  224. ret.push_back(iter);
  225. }
  226. }
  227. if(ret.empty()) return;
  228. for(auto iter = ret.begin(); iter != ret.end(); iter++){
  229. this->HistoryDatas.erase(*iter);
  230. }
  231. }
  232. /**
  233. * @description: 相似度对比
  234. * @return {*}
  235. */
  236. int8_t InfineFilter::SimilarityJudgment(std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  237. this->SimilarityCheck();
  238. auto task = [&](){
  239. HistoryData historyData;
  240. historyData.CnStreamInferData = std::make_shared<CNStreamInferData>();
  241. historyData.CnStreamInferData->StreamName = cnStreamInferData->StreamName;
  242. historyData.CnStreamInferData->FrameCount = cnStreamInferData->FrameCount;
  243. historyData.CnStreamInferData->width = cnStreamInferData->width;
  244. historyData.CnStreamInferData->height = cnStreamInferData->height;
  245. historyData.CnStreamInferData->Objects = cnStreamInferData->Objects;
  246. historyData.CnStreamInferData->videoPath = cnStreamInferData->videoPath;
  247. LatestDatas.push(historyData);
  248. if(LatestDatas.size() >= 3) LatestDatas.pop();
  249. };
  250. if(LatestDatas.empty()){
  251. task();
  252. return OK;
  253. }
  254. auto iter_front = LatestDatas.front();
  255. vector<vector<InferInfo>::iterator> result;
  256. bool IsResult = false;
  257. // 对比前两帧
  258. for(auto iter = cnStreamInferData->Objects.begin(); iter != cnStreamInferData->Objects.end(); iter++){
  259. IsResult = false;
  260. for(auto iter1 : iter_front.CnStreamInferData->Objects){
  261. if(CalculateSimilarity(*iter, iter1, cnStreamInferData) == OK){
  262. SimilarityData data;
  263. data.ticker = std::make_shared<Ticker>();
  264. data.info = *iter;
  265. this->SimilarityDatas.push_back(data);
  266. result.push_back(iter);
  267. IsResult = true;
  268. break;
  269. }
  270. }
  271. if(IsResult) continue;
  272. if(LatestDatas.size() <= 1) continue;
  273. auto iter_back = LatestDatas.back();
  274. for(auto iter1 : iter_back.CnStreamInferData->Objects){
  275. if(CalculateSimilarity(*iter, iter1, cnStreamInferData) == OK){
  276. SimilarityData data;
  277. data.ticker = std::make_shared<Ticker>();
  278. data.info = *iter;
  279. this->SimilarityDatas.push_back(data);
  280. result.push_back(iter);
  281. break;
  282. }
  283. }
  284. }
  285. for(auto iter = result.begin(); iter != result.end(); iter++){
  286. cnStreamInferData->Objects.erase(*iter);
  287. }
  288. result.clear();
  289. // 对比相似库
  290. for(auto iter = cnStreamInferData->Objects.begin(); iter != cnStreamInferData->Objects.end(); iter++){
  291. for(auto iter1 = this->SimilarityDatas.begin(); iter1 != this->SimilarityDatas.end(); iter1++){
  292. if(CalculateSimilarity(*iter, iter1->info, cnStreamInferData) == OK){
  293. iter1->info = *iter;
  294. iter1->ticker->resetTime();
  295. result.push_back(iter);
  296. break;
  297. }
  298. }
  299. }
  300. // clear
  301. for(auto iter = result.begin(); iter != result.end(); iter++){
  302. cnStreamInferData->Objects.erase(*iter);
  303. }
  304. task();
  305. return OK;
  306. }
  307. /**
  308. * @description: 相似库对比
  309. * @return {*}
  310. */
  311. int8_t InfineFilter::SimilarityResult(std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  312. static queue<HistoryData> nx_latestDatas;
  313. auto task = [&](){
  314. HistoryData historyData;
  315. historyData.CnStreamInferData = std::make_shared<CNStreamInferData>();
  316. historyData.CnStreamInferData->StreamName = cnStreamInferData->StreamName;
  317. historyData.CnStreamInferData->FrameCount = cnStreamInferData->FrameCount;
  318. historyData.CnStreamInferData->width = cnStreamInferData->width;
  319. historyData.CnStreamInferData->height = cnStreamInferData->height;
  320. historyData.CnStreamInferData->Objects = cnStreamInferData->Objects;
  321. historyData.CnStreamInferData->videoPath = cnStreamInferData->videoPath;
  322. nx_latestDatas.push(historyData);
  323. if(nx_latestDatas.size() >= 3) nx_latestDatas.pop();
  324. };
  325. vector<vector<InferInfo>::iterator> result;
  326. if(nx_latestDatas.empty()){
  327. task();
  328. if(this->SimilarityDatas.empty()) return OK;
  329. }
  330. else{
  331. bool IsResult = false;
  332. auto iter_front = nx_latestDatas.front();
  333. for(auto iter = cnStreamInferData->Objects.begin(); iter != cnStreamInferData->Objects.end(); iter++){
  334. IsResult = false;
  335. for(auto iter1 : iter_front.CnStreamInferData->Objects){
  336. if(CalculateSimilarity(*iter, iter1, cnStreamInferData) == OK){
  337. SimilarityData data;
  338. data.ticker = std::make_shared<Ticker>();
  339. data.info = *iter;
  340. this->SimilarityDatas.push_back(data);
  341. result.push_back(iter);
  342. IsResult = true;
  343. break;
  344. }
  345. }
  346. if(IsResult) continue;
  347. if(nx_latestDatas.size() <= 1) continue;
  348. auto iter_back = nx_latestDatas.back();
  349. for(auto iter1 : iter_back.CnStreamInferData->Objects){
  350. if(CalculateSimilarity(*iter, iter1, cnStreamInferData) == OK){
  351. SimilarityData data;
  352. data.ticker = std::make_shared<Ticker>();
  353. data.info = *iter;
  354. this->SimilarityDatas.push_back(data);
  355. result.push_back(iter);
  356. break;
  357. }
  358. }
  359. }
  360. for(auto iter = result.begin(); iter != result.end(); iter++){
  361. cnStreamInferData->Objects.erase(*iter);
  362. }
  363. result.clear();
  364. task();
  365. }
  366. if(this->SimilarityDatas.empty()) return OK;
  367. for(auto iter = cnStreamInferData->Objects.begin(); iter != cnStreamInferData->Objects.end(); iter++){
  368. for(auto iter1 = this->SimilarityDatas.begin(); iter1 != this->SimilarityDatas.end(); iter1++){
  369. if(CalculateSimilarity(*iter, iter1->info, cnStreamInferData) == OK){
  370. iter1->info = *iter;
  371. iter1->ticker->resetTime();
  372. result.push_back(iter);
  373. break;
  374. }
  375. }
  376. }
  377. if(result.empty()) return OK;
  378. // clear
  379. for(auto iter = result.begin(); iter != result.end(); iter++){
  380. cnStreamInferData->Objects.erase(*iter);
  381. }
  382. return OK;
  383. }
  384. /**
  385. * @description: 时间对比
  386. * @return {*}
  387. */
  388. void InfineFilter::SimilarityCheck(){
  389. if(filterLevel != 3) return;
  390. for(auto iter = this->SimilarityDatas.begin(); iter != this->SimilarityDatas.end();){
  391. if(iter->ticker != nullptr && iter->ticker->elapsedTime() > TimeOut){
  392. iter = this->SimilarityDatas.erase(iter);
  393. }else iter++;
  394. }
  395. }
  396. /**
  397. * @description: 获取Iou
  398. * @return {*}
  399. */
  400. float InfineFilter::getIou(InferInfo infer1, InferInfo infer2,std::shared_ptr<CNStreamInferData>& cnStreamInferData){
  401. int max_x = std::max(infer1.BBox.x * cnStreamInferData->width, infer2.BBox.x * cnStreamInferData->width);
  402. int min_x = std::min((infer1.BBox.x + infer1.BBox.w) * cnStreamInferData->width , (infer2.BBox.x + infer2.BBox.w) * cnStreamInferData->width);
  403. int max_y = std::max(infer1.BBox.y * cnStreamInferData->height, infer2.BBox.y * cnStreamInferData->height);
  404. int min_y = std::min((infer1.BBox.y + infer1.BBox.h) * cnStreamInferData->height, (infer2.BBox.y + infer2.BBox.h) * cnStreamInferData->height);
  405. if(min_x <= max_x || min_y <= max_y) return 0;
  406. float over_area = (min_x - max_x) * (min_y - max_y); // 计算重叠面积
  407. float area_a = (infer1.BBox.w * cnStreamInferData->width) * (infer1.BBox.h * cnStreamInferData->height);
  408. float area_b = (infer2.BBox.w * cnStreamInferData->width) * (infer2.BBox.h * cnStreamInferData->height);
  409. float iou = over_area / (area_a + area_b - over_area);
  410. return iou;
  411. }
  412. } // namespace gsd