inference.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * @Description: 推理模块
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-10-13 09:37:51
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-10-09 09:45:46
  8. */
  9. #pragma once
  10. #include <iostream>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <gst/gst.h>
  14. #include <glib.h>
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <string.h>
  18. #include <sys/time.h>
  19. #include "nvdsmeta.h"
  20. #include "gstnvdsmeta.h"
  21. #include "gst-nvmessage.h"
  22. #include "nvbufsurface.h"
  23. #include "DataSource.h"
  24. #include "InferRange.h"
  25. // ZLMediaKit
  26. #include "Util/logger.h"
  27. #include "Util/NoticeCenter.h"
  28. #include "Util/SqlPool.h"
  29. #include "Network/TcpClient.h"
  30. #include "Poller/Timer.h"
  31. #include "TCPClient.h"
  32. #include "recorder.h"
  33. #include <mutex>
  34. #include <cuda_runtime.h>
  35. #include <map>
  36. #include <queue>
  37. #include <vector>
  38. // opencv
  39. #include <opencv2/core.hpp>
  40. #include <opencv2/videoio.hpp>
  41. #include <opencv2/highgui.hpp>
  42. #include <opencv2/imgproc.hpp>
  43. #include <opencv2/imgproc/types_c.h>
  44. #include <list>
  45. using namespace std;
  46. namespace MIVA{
  47. // 基于Deepstream
  48. class Inference
  49. {
  50. private:
  51. // Deepstream
  52. GMainLoop *loop = NULL;
  53. GstElement *pipeline = NULL,*streammux =NULL, *sink = NULL, *pgie = NULL,
  54. *queue1, *queue2, *queue3, *queue4, *queue5, *nvvidconv = NULL,
  55. *nvosd = NULL,*tiler = NULL;
  56. GstElement *transform = NULL;
  57. GstBus *bus = NULL;
  58. guint bus_watch_id;
  59. GstPad *pgie_src_pad = NULL;
  60. GstPad *tiler_sink_pad = NULL;
  61. guint tiler_rows, tiler_columns;
  62. guint pgie_batch_size;
  63. public:
  64. std::shared_ptr<std::vector<DataSource>> m_DataList = NULL;
  65. std::shared_ptr<InferInfo> m_InferInfo = nullptr;
  66. std::shared_ptr<recorder> m_recorder = nullptr;
  67. bool Play = false;
  68. bool enable = false;
  69. int InferNum = 0;
  70. Inference();
  71. ~Inference();
  72. public:
  73. /**
  74. * @description: 创建实例
  75. * @param {*}
  76. * @return {*}
  77. */
  78. static std::shared_ptr<Inference> CreateNew();
  79. /**
  80. * @description: 推理模块初始化
  81. * @param {vector<DataSource>} DataList 数据源集合
  82. * @return {*}
  83. * @author: lishengyin
  84. */
  85. int32_t Init();
  86. /**
  87. * @description: 启动任务
  88. * @param {*}
  89. * @return {*}
  90. * @author: lishengyin
  91. */
  92. int32_t StartTask();
  93. /**
  94. * @description: 停止任务
  95. * @param {*}
  96. * @return {*}
  97. * @author: lishengyin
  98. */
  99. void StopTask();
  100. /**
  101. * @description: 重启任务
  102. * @param {*}
  103. * @return {*}
  104. * @author: lishengyin
  105. */
  106. int32_t RestartTask();
  107. /**
  108. * @description:记录
  109. * @param {*}
  110. * @return {*}
  111. */
  112. int32_t Recorder();
  113. /**
  114. * @description: 暂停任务
  115. * @param {*}
  116. * @return {*}
  117. */
  118. void PausedTask();
  119. /**
  120. * @description: 检查任务
  121. * @param {*}
  122. * @return {*}
  123. */
  124. void CheckTask();
  125. /**
  126. * @description: Destory()
  127. * @return {*}
  128. */
  129. void Destory();
  130. /**
  131. * @description: QuitLoop
  132. * @param {*}
  133. * @return {*}
  134. */
  135. void QuitLoop();
  136. /**
  137. * @description: 状态返回
  138. * @param {*}
  139. * @return {*}
  140. */
  141. static void StateResulit(GstStateChangeReturn& state_return);
  142. public:
  143. /**
  144. * @description: 获取推理结果
  145. * @param {GstPad *} pad
  146. * @param {GstPadProbeInfo *} info
  147. * @param {gpointer} u_data
  148. * @return {*}
  149. * @author: lishengyin
  150. */
  151. static GstPadProbeReturn tiler_sink_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * probe_info, gpointer u_data);
  152. /**
  153. * @description: 监听bus
  154. * @param {GstBus *} bus
  155. * @param {GstMessage *} msg
  156. * @param {gpointer} data
  157. * @return {*}
  158. * @author: lishengyin
  159. */
  160. static gboolean bus_call (GstBus * bus, GstMessage * msg, gpointer data);
  161. /**
  162. * @description: 解码模块监听child_added
  163. * @param {*}
  164. * @return {*}
  165. * @author: lishengyin
  166. */
  167. static void decodebin_child_added (GstChildProxy * child_proxy, GObject * object,
  168. gchar * name, gpointer user_data);
  169. /**
  170. * @description: 创建解码bin
  171. * @param {guint} index
  172. * @param {gchar *} filename
  173. * @return {*}
  174. * @author: lishengyin
  175. */
  176. static GstElement * create_uridecode_bin (guint index, gchar * filename);
  177. /**
  178. * @description: 解码模块监听pad-added
  179. * @param {GstElement *} decodebin
  180. * @param {GstPad *} pad
  181. * @param {gpointer} data
  182. * @return {*}
  183. * @author: lishengyin
  184. */
  185. static void cb_newpad (GstElement * decodebin, GstPad * pad, gpointer data);
  186. /**
  187. * @description: 判断点是否在区域内
  188. * @param {Point} p
  189. * @param {int} nCount
  190. * @return {*}
  191. */
  192. static int PtInPolygon (Point p, vector<Point>& ptPolygon, int nCount);
  193. /**
  194. * @description: 添加数据源
  195. * @param {int} source_Id 数据源ID
  196. * @param {string} uri 数据源Url
  197. * @return {*} 是否添加成功
  198. * @author: lishengyin
  199. */
  200. GstElement* add_sources (int source_Id, std::string uri);
  201. /**
  202. * @description: 删除数据源
  203. * @param {gint} source_id
  204. * @return {*}
  205. * @author: lishengyin
  206. */
  207. void stop_release_source (gint source_id);
  208. /**
  209. * @description: 修改数据源地址
  210. * @param {GstElement} *source_bin
  211. * @param {string} uri
  212. * @return {*}
  213. */
  214. void ModifyUri(GstElement *source_bin, std::string uri);
  215. /**
  216. * @description: 释放元素
  217. * @param {GstElement} *source_bin
  218. * @return {*}
  219. */
  220. void FreeElement(int source_Id,GstElement *source_bin);
  221. /**
  222. * @description: 添加数据源
  223. * @param {int} sourceId
  224. * @return {*}
  225. */
  226. void AddSources(int sourceId);
  227. /**
  228. * @description: 设置Batch
  229. * @param {int} num
  230. * @return {*}
  231. */
  232. void SetBatch(int num);
  233. };
  234. }