FuncIllegalInvasion.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "FuncIllegalInvasion.h"
  2. #include "GlobalVariable.h"
  3. #include "GlobalConfig.h"
  4. #include "FromRedis.h"
  5. #include "ToEQMDataBase.h"
  6. #include "UniversalFunc.h"
  7. RoomIllegalInvasionInfo::RoomIllegalInvasionInfo(const RoomIllegalInvasionInfo& o)
  8. {
  9. isAlarm = o.isAlarm;
  10. RoomID = o.RoomID;
  11. RoomType = o.RoomType;
  12. numMaxFace = o.numMaxFace;
  13. numMaxPerson = o.numMaxPerson;
  14. strBoxList = o.strBoxList;
  15. strMessage = o.strMessage;
  16. strImage = o.strImage;
  17. CameraID = o.CameraID;
  18. }
  19. RoomIllegalInvasionInfo& RoomIllegalInvasionInfo::operator=(const RoomIllegalInvasionInfo& o)
  20. {
  21. if(this != &o) {
  22. isAlarm = o.isAlarm;
  23. RoomID = o.RoomID;
  24. RoomType = o.RoomType;
  25. numMaxFace = o.numMaxFace;
  26. numMaxPerson = o.numMaxPerson;
  27. strBoxList = o.strBoxList;
  28. strMessage = o.strMessage;
  29. strImage = o.strImage;
  30. CameraID = o.CameraID;
  31. }
  32. return *this;
  33. }
  34. /* 添加房间 */
  35. void ListRoomIll::addRoom(int RoomID, int RoomType)
  36. {
  37. auto p = findRoom(RoomID, RoomType);
  38. if(p == nullptr)
  39. {
  40. RoomIllegalInvasionInfo ill;
  41. ill.RoomID = RoomID;
  42. ill.RoomType = RoomType;
  43. listRoomIll.push_back(ill);
  44. }
  45. }
  46. /* 查找是否有相同的房间 */
  47. RoomIllegalInvasionInfo* ListRoomIll::findRoom(int RoomID, int RoomType)
  48. {
  49. for(auto& it : listRoomIll)
  50. {
  51. if(it.RoomID == RoomID && it.RoomType == RoomType)
  52. {
  53. return ⁢
  54. }
  55. }
  56. return nullptr;
  57. }
  58. IllegalInvasionInfo::IllegalInvasionInfo(const IllegalInvasionInfo& other)
  59. {
  60. isInsertEQM = other.isInsertEQM;
  61. PKID = other.PKID;
  62. CameraID = other.CameraID;
  63. RoomID = other.RoomID;
  64. ChannelID = other.ChannelID;
  65. RoomType = other.RoomType;
  66. FirstTime = other.FirstTime;
  67. strActionDec = other.strActionDec;
  68. strImageInfo = other.strImageInfo;
  69. }
  70. IllegalInvasionInfo& IllegalInvasionInfo::operator=(const IllegalInvasionInfo& other)
  71. {
  72. if(this != &other)
  73. {
  74. isInsertEQM = other.isInsertEQM;
  75. PKID = other.PKID;
  76. CameraID = other.CameraID;
  77. RoomID = other.RoomID;
  78. ChannelID = other.ChannelID;
  79. RoomType = other.RoomType;
  80. FirstTime = other.FirstTime;
  81. strActionDec = other.strActionDec;
  82. strImageInfo = other.strImageInfo;
  83. }
  84. return *this;
  85. }
  86. /* 添加信息 */
  87. void ListIllegalInvasionInfo::addIllInfo(IllegalInvasionInfo& info)
  88. {
  89. if(findIllInfo(info) == nullptr)
  90. {
  91. listIll.push_back(info);
  92. }
  93. }
  94. /* 查找相同的信息 */
  95. IllegalInvasionInfo* ListIllegalInvasionInfo::findIllInfo(IllegalInvasionInfo& info)
  96. {
  97. for(auto& it : listIll)
  98. {
  99. if(it.RoomID == info.RoomID && it.RoomType == info.RoomType)
  100. {
  101. return ⁢
  102. }
  103. }
  104. return nullptr;
  105. }
  106. IllegalInvasionInfo* ListIllegalInvasionInfo::findIllInfo(int roomID, int roomType)
  107. {
  108. for(auto& it : listIll)
  109. {
  110. if(it.RoomID == roomID && it.RoomType == roomType)
  111. {
  112. return ⁢
  113. }
  114. }
  115. return nullptr;
  116. }
  117. /* 删除报警信息 */
  118. void ListIllegalInvasionInfo::deleteIllInfo(IllegalInvasionInfo& info)
  119. {
  120. for(auto it = listIll.begin(); it != listIll.end();)
  121. {
  122. if(it->RoomID == info.RoomID && it->RoomType == info.RoomType)
  123. {
  124. it = listIll.erase(it);
  125. }else {
  126. ++it;
  127. }
  128. }
  129. }
  130. void ListIllegalInvasionInfo::deleteIllInfo(int roomID, int roomType)
  131. {
  132. for(auto it = listIll.begin(); it != listIll.end();)
  133. {
  134. if(it->RoomID == roomID && it->RoomType == roomType)
  135. {
  136. it = listIll.erase(it);
  137. }else {
  138. ++it;
  139. }
  140. }
  141. }
  142. /* ===================================================================================
  143. * 功能类:FuncIllegalInvasion
  144. * =================================================================================== */
  145. FuncIllegalInvasion::FuncIllegalInvasion()
  146. {
  147. m_logger = spdlog::get("SPAServer");
  148. if(m_logger == nullptr)
  149. {
  150. SPDLOG_ERROR("SPAServer logger is nullptr");
  151. return;
  152. }
  153. m_pListAlarm = std::make_shared<std::list<AlarmInfo>>();
  154. m_pListIllInfo = std::make_shared<ListIllegalInvasionInfo>();
  155. }
  156. FuncIllegalInvasion::~FuncIllegalInvasion()
  157. {
  158. }
  159. /* 是否报警 */
  160. bool FuncIllegalInvasion::isAlarm(const std::vector<PersonInfo>& vecPersion)
  161. {
  162. for(const auto& it : vecPersion)
  163. {
  164. if(it.PersonID == "-1")
  165. {
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. /* 线程功能 */
  172. void FuncIllegalInvasion::task()
  173. {
  174. std::string strFaceActionID; /* 人脸识别的Action */
  175. std::string strCountActionID; /* 人员计数的Action */
  176. while (GThreadInfo.getRunning())
  177. {
  178. /* 更新线程信息 */
  179. GThreadInfo.updateFuncInfo(m_funcAct);
  180. if(m_funcAct.RunState == RunTimeState::RUN_STATE_STOP)
  181. {
  182. break;
  183. }
  184. /* 判断是否在检测时间段内 */
  185. if(!isInDetectTime(m_funcAct.StartTime, m_funcAct.EndTime))
  186. {
  187. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  188. continue;
  189. }
  190. /* 更新算法ActionID */
  191. {
  192. g_actionList.mutexRW.lockForRead();
  193. strFaceActionID = g_actionList.ActFace;
  194. strCountActionID = g_actionList.ActPersonNumber;
  195. g_actionList.mutexRW.unlock();
  196. }
  197. /* 读取Redis数据 */
  198. m_pListAlarm->clear();
  199. for(const auto& RoomInfo : m_funcAct.listRoomCamActInfo)
  200. {
  201. for(const auto& it : RoomInfo.mapCameraAction)
  202. {
  203. std::string strKey = std::to_string(it.first) + ":" + it.second;
  204. std::string strRetValue;
  205. if(!m_fromRedis->getRedisString(strKey, strRetValue))
  206. {
  207. SPDLOG_LOGGER_ERROR(m_logger, "读取Redis数据失败, Key:{}", strKey);
  208. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  209. continue;
  210. }
  211. /* 解析数据 */
  212. AlarmInfo alarmInfo;
  213. // parseRedisData(strRetValue, alarmInfo);
  214. parseRedisBaseData(strRetValue, alarmInfo);
  215. parseRedisBBoxesData(strRetValue, alarmInfo);
  216. /* 判断事件的时效性,超过多少秒不更新就可能是超脑挂了 */
  217. if(isEventTimeVaild(alarmInfo.EventTime))
  218. {
  219. SPDLOG_LOGGER_WARN(m_logger, "Redis Key:{} 数据长时间没有更新,EventTime:{}",strKey, alarmInfo.EventTime);
  220. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  221. continue;
  222. }
  223. m_pListAlarm->push_back(alarmInfo);
  224. }
  225. }
  226. /* 找出房间的个数,按照房间进行判断 */
  227. ListRoomIll listRoomIll;
  228. for(auto& it : m_funcAct.listRoomCamActInfo)
  229. {
  230. listRoomIll.addRoom(it.RoomID, it.RoomType);
  231. }
  232. /* 根据房间解析人脸识别的非法入侵,检测到了非法人脸,就可以直接报警了 */
  233. for(auto& room : listRoomIll.getData())
  234. {
  235. for(const auto& alarm : *m_pListAlarm)
  236. {
  237. if(room.RoomID != alarm.RoomID)
  238. {
  239. continue;
  240. }
  241. /* 统计这个房间内《人脸算法》检测到的最大人员数 */
  242. if(alarm.ActionID == strFaceActionID)
  243. {
  244. if(alarm.vecPersonInfo.size() > room.numMaxFace)
  245. {
  246. room.numMaxFace = alarm.vecPersonInfo.size();
  247. room.strBoxList = alarm.listBbox;
  248. room.vecPersonInfo = alarm.vecPersonInfo;
  249. }
  250. }
  251. /* 判断有没有非法入侵人员 */
  252. if(isAlarm(alarm.vecPersonInfo))
  253. {
  254. room.isAlarm = true;
  255. room.strMessage = "人员非法入侵(未知人员)";
  256. if( !alarm.ImageInfo.empty() )
  257. {
  258. room.strImage = alarm.ImageInfo;
  259. }
  260. room.CameraID = alarm.DeviceID;
  261. }
  262. }
  263. }
  264. /* 判断《人脸算法》识别到的数目和《人员计数算法》识别到的人数是否相等,如果上面报警了,这里就不用检测了 */
  265. for(auto& room : listRoomIll.getData())
  266. {
  267. if(room.isAlarm)
  268. {
  269. continue;
  270. }
  271. /* 这个房间的人脸算法没有识别到非法入侵 */
  272. for(const auto& alarm : *m_pListAlarm)
  273. {
  274. if(room.RoomID != alarm.RoomID)
  275. {
  276. continue;
  277. }
  278. /* 人员计数 */
  279. if(alarm.ActionID == strCountActionID)
  280. {
  281. /* 统计这个房间内《人员计数算法》检测到的最大人员数 */
  282. if(alarm.vecPersonInfo.size() > room.numMaxPerson)
  283. {
  284. room.numMaxPerson = alarm.vecPersonInfo.size();
  285. room.strBoxList = alarm.listBbox;
  286. if(!alarm.ImageInfo.empty())
  287. {
  288. room.strImage = alarm.ImageInfo;
  289. }
  290. room.CameraID = alarm.DeviceID;
  291. }
  292. }
  293. }
  294. /* 判断人数是否一致 */
  295. if(room.numMaxFace != room.numMaxPerson)
  296. {
  297. room.strMessage = fmt::format("非法入侵(人员计数 {} 和人脸数 {} 不一致)", room.numMaxPerson, room.numMaxFace);
  298. room.isAlarm = true;
  299. }
  300. }
  301. /* 将非法入侵的信息存储到数组缓存中,等待报警结束后判断是否需要写入EQM数据库 */
  302. for(auto& room : listRoomIll.getData())
  303. {
  304. auto pIll = m_pListIllInfo->findIllInfo(room.RoomID, room.RoomType);
  305. if(room.isAlarm)
  306. {
  307. /* 正在报警,检查缓存中是否有这个报警信息 */
  308. if(pIll == nullptr)
  309. {
  310. IllegalInvasionInfo info;
  311. info.ChannelID = m_funcAct.ChannelID;
  312. info.CameraID = room.CameraID;
  313. info.RoomID = room.RoomID;
  314. info.RoomType = room.RoomType;
  315. info.strActionDec = room.strMessage;
  316. info.strImageInfo = room.strImage;
  317. info.FirstTime = QDateTime::currentDateTime();;
  318. m_pListIllInfo->addIllInfo(info);
  319. }
  320. }
  321. else
  322. {
  323. /* 没有报警,检查是否是报警结束了,是否符合写入数据库的条件 */
  324. QDateTime currTime = QDateTime::currentDateTime();
  325. int secs = currTime.toSecsSinceEpoch() - pIll->FirstTime.toSecsSinceEpoch();
  326. if(secs >= GConfig.AppBadMan_Time)
  327. {
  328. if(!pIll->strImageInfo.empty())
  329. {
  330. /* 超过非法入侵的时间,写入数据库 */
  331. AlarmInfo ai;
  332. ai.ChannelID = m_funcAct.ChannelID;
  333. ai.RoomID = pIll->RoomID;
  334. ai.ActionID = g_actionList.ActPersonNumber;
  335. ai.ActionDes = pIll->strActionDec;
  336. ai.ImageInfo = pIll->strImageInfo;
  337. ai.StartTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  338. ai.EndTime = currTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  339. ai.EventTime = pIll->FirstTime.toString("yyyy-MM-dd hh:mm:ss").toStdString();
  340. ai.vecPersonInfo = room.vecPersonInfo;
  341. ai.listBbox = room.strBoxList;
  342. bool insertRet = m_toEQMDataBase->insertAlarmInfo(ai);
  343. if(insertRet)
  344. {
  345. std::string actionName = g_actionList.getActionName(ai.ActionID);
  346. SPDLOG_LOGGER_INFO(m_logger, "☆ 新增报警信息,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸,{}",
  347. pIll->ChannelID, pIll->RoomID, pIll->CameraID, actionName, ai.ActionDes, ai.listBbox.size(), ai.FaceIDList.size(), ai.ImageInfo);
  348. }
  349. }
  350. else {
  351. SPDLOG_LOGGER_WARN(m_logger, "× 取消新增报警信息,因为没有图片,频道[{}],房间[{}],摄像头[{}],{},{},有{}个区域,有{}个人脸",
  352. pIll->ChannelID, pIll->RoomID, pIll->CameraID, pIll->strActionDec, pIll->strImageInfo, room.strBoxList.size(), room.vecPersonInfo.size());
  353. }
  354. /* 删除这个非法入侵信息 */
  355. m_pListIllInfo->deleteIllInfo(*pIll);
  356. }
  357. }
  358. }
  359. std::this_thread::sleep_for(std::chrono::milliseconds(GConfig.ThreadSleepMS));
  360. }
  361. }