inference.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * @Description: 推理模块
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2021-10-13 09:37:51
  6. * @LastEditors: lishengyin
  7. * @LastEditTime: 2022-07-21 11:39:37
  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 "Poller/EventPoller.h"
  29. #include "Util/SqlPool.h"
  30. #include "Network/TcpClient.h"
  31. #include "Poller/Timer.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 "CNStreamInferData.h"
  45. #include "Base64.h"
  46. #include "osd.h"
  47. #include <list>
  48. #include "Disposition.hpp"
  49. #include "InfineFilter.hpp"
  50. #define OK 0
  51. #define ERR -1
  52. using namespace std;
  53. using namespace toolkit;
  54. namespace gsd_ds{
  55. // 基于Deepstream
  56. class Inference
  57. {
  58. private:
  59. // Deepstream
  60. GMainLoop *loop = nullptr;
  61. GstElement *pipeline = nullptr,*streammux =nullptr, *sink = nullptr, *pgie = nullptr,
  62. *queue1, *queue2, *queue3, *queue4, *queue5, *nvvidconv = nullptr, *caps_filter = nullptr, *nvvidconv_postosd = nullptr,
  63. *encoder = nullptr,
  64. *codecparser = nullptr,
  65. *rtppay = nullptr, *videorate = nullptr,
  66. *nvosd = nullptr,*tiler = nullptr;
  67. GstElement *transform = nullptr;
  68. GstBus *bus = nullptr;
  69. guint bus_watch_id = 0;
  70. GstPad *pgie_src_pad = nullptr;
  71. GstPad *tiler_sink_pad = nullptr;
  72. guint tiler_rows, tiler_columns;
  73. guint pgie_batch_size;
  74. float box_thickness_ = 1;
  75. bool Play = false;
  76. bool enable = false;
  77. public:
  78. std::shared_ptr<std::vector<DataSource>> m_DataList = nullptr;
  79. std::shared_ptr<InferInfo> m_InferInfo = nullptr;
  80. std::shared_ptr<recorder> m_recorder = nullptr;
  81. std::shared_ptr<ThreadPool> m_pool = nullptr;
  82. Inference();
  83. ~Inference();
  84. public:
  85. /**
  86. * @description: 创建实例
  87. * @param {*}
  88. * @return {*}
  89. */
  90. static std::shared_ptr<Inference> CreateNew();
  91. /**
  92. * @description: 推理模块初始化
  93. * @param {vector<DataSource>} DataList 数据源集合
  94. * @return {*}
  95. * @author: lishengyin
  96. */
  97. int32_t Init();
  98. /**
  99. * @description: 启动任务
  100. * @param {*}
  101. * @return {*}
  102. * @author: lishengyin
  103. */
  104. int32_t StartTask();
  105. /**
  106. * @description: 停止任务
  107. * @param {*}
  108. * @return {*}
  109. * @author: lishengyin
  110. */
  111. void StopTask();
  112. /**
  113. * @description: 重启任务
  114. * @param {*}
  115. * @return {*}
  116. * @author: lishengyin
  117. */
  118. int32_t RestartTask();
  119. /**
  120. * @description: 状态返回
  121. * @param {*}
  122. * @return {*}
  123. */
  124. static void StateResulit(GstStateChangeReturn& state_return);
  125. /**
  126. * @description: 获取Alive
  127. * @return {*}
  128. */
  129. bool getAlive();
  130. /**
  131. * @description: 动态调节
  132. * @return {*}
  133. */
  134. void DynamicAdjust();
  135. /**
  136. * @description: Destory()
  137. * @return {*}
  138. */
  139. void Destory();
  140. /**
  141. * @description: Quit
  142. * @return {*}
  143. */
  144. void QuitLoop();
  145. /**
  146. * @description: getTargetNum
  147. * @return {*}
  148. */
  149. static int getTargetNum(NvDsObjectMetaList* objs);
  150. public:
  151. /**
  152. * @description: 获取推理结果
  153. * @param {GstPad *} pad
  154. * @param {GstPadProbeInfo *} info
  155. * @param {gpointer} u_data
  156. * @return {*}
  157. * @author: lishengyin
  158. */
  159. static GstPadProbeReturn tiler_sink_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * probe_info, gpointer u_data);
  160. /**
  161. * @description: 监听bus
  162. * @param {GstBus *} bus
  163. * @param {GstMessage *} msg
  164. * @param {gpointer} data
  165. * @return {*}
  166. * @author: lishengyin
  167. */
  168. static gboolean bus_call (GstBus * bus, GstMessage * msg, gpointer data);
  169. /**
  170. * @description: 解码模块监听child_added
  171. * @param {*}
  172. * @return {*}
  173. * @author: lishengyin
  174. */
  175. static void decodebin_child_added (GstChildProxy * child_proxy, GObject * object,
  176. gchar * name, gpointer user_data);
  177. /**
  178. * @description: 创建解码bin
  179. * @param {guint} index
  180. * @param {gchar *} filename
  181. * @return {*}
  182. * @author: lishengyin
  183. */
  184. static GstElement * create_uridecode_bin (guint index, gchar * filename);
  185. /**
  186. * @description: 解码模块监听pad-added
  187. * @param {GstElement *} decodebin
  188. * @param {GstPad *} pad
  189. * @param {gpointer} data
  190. * @return {*}
  191. * @author: lishengyin
  192. */
  193. static void cb_newpad (GstElement * decodebin, GstPad * pad, gpointer data);
  194. /**
  195. * @description: 启动rtsp
  196. * @param {guint} rtsp_port_num
  197. * @param {guint} updsink_port_num
  198. * @param {guint64} udp_buffer_size
  199. * @return {*}
  200. */
  201. static gboolean start_rtsp_streaming (guint rtsp_port_num, guint updsink_port_num, guint64 udp_buffer_size);
  202. /**
  203. * @description: FloatToString
  204. * @param {float} number
  205. * @return {*}
  206. */
  207. static std::string FloatToString(float number);
  208. /**
  209. * @description: 添加数据源
  210. * @param {int} source_Id 数据源ID
  211. * @param {string} uri 数据源Url
  212. * @return {*} 是否添加成功
  213. * @author: lishengyin
  214. */
  215. GstElement* add_sources (int source_Id, std::string uri);
  216. /**
  217. * @description: 删除数据源
  218. * @param {gint} source_id
  219. * @return {*}
  220. * @author: lishengyin
  221. */
  222. void stop_release_source (gint source_id);
  223. /**
  224. * @description: 修改数据源地址
  225. * @param {GstElement} *source_bin
  226. * @param {string} uri
  227. * @return {*}
  228. */
  229. void ModifyUri(GstElement *source_bin, std::string uri);
  230. /**
  231. * @description: 添加数据源
  232. * @param {int} sourceId
  233. * @return {*}
  234. */
  235. void AddSources(int sourceId);
  236. /**
  237. * @description: 设置Batch
  238. * @param {int} num
  239. * @return {*}
  240. */
  241. void SetBatch(int num);
  242. };
  243. }