123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- /*
- * @Description: 推理模块
- * @Version: 1.0
- * @Autor: lishengyin
- * @Date: 2021-10-13 09:37:51
- * @LastEditors: lishengyin
- * @LastEditTime: 2022-07-21 11:39:37
- */
- #pragma once
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <gst/gst.h>
- #include <glib.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <sys/time.h>
- #include "nvdsmeta.h"
- #include "gstnvdsmeta.h"
- #include "gst-nvmessage.h"
- #include "nvbufsurface.h"
- #include "DataSource.h"
- #include "InferRange.h"
- // ZLMediaKit
- #include "Util/logger.h"
- #include "Util/NoticeCenter.h"
- #include "Poller/EventPoller.h"
- #include "Util/SqlPool.h"
- #include "Network/TcpClient.h"
- #include "Poller/Timer.h"
- #include "recorder.h"
- #include <mutex>
- #include <cuda_runtime.h>
- #include <map>
- #include <queue>
- #include <vector>
- // opencv
- #include <opencv2/core.hpp>
- #include <opencv2/videoio.hpp>
- #include <opencv2/highgui.hpp>
- #include <opencv2/imgproc.hpp>
- #include <opencv2/imgproc/types_c.h>
- #include "CNStreamInferData.h"
- #include "Base64.h"
- #include "osd.h"
- #include <list>
- #include "Disposition.hpp"
- #include "InfineFilter.hpp"
- #define OK 0
- #define ERR -1
- using namespace std;
- using namespace toolkit;
- namespace gsd_ds{
- // 基于Deepstream
- class Inference
- {
- private:
- // Deepstream
- GMainLoop *loop = nullptr;
- GstElement *pipeline = nullptr,*streammux =nullptr, *sink = nullptr, *pgie = nullptr,
- *queue1, *queue2, *queue3, *queue4, *queue5, *nvvidconv = nullptr, *caps_filter = nullptr, *nvvidconv_postosd = nullptr,
- *encoder = nullptr,
- *codecparser = nullptr,
- *rtppay = nullptr, *videorate = nullptr,
- *nvosd = nullptr,*tiler = nullptr;
-
- GstElement *transform = nullptr;
- GstBus *bus = nullptr;
- guint bus_watch_id = 0;
- GstPad *pgie_src_pad = nullptr;
- GstPad *tiler_sink_pad = nullptr;
- guint tiler_rows, tiler_columns;
- guint pgie_batch_size;
- float box_thickness_ = 1;
- bool Play = false;
- bool enable = false;
- public:
- std::shared_ptr<std::vector<DataSource>> m_DataList = nullptr;
- std::shared_ptr<InferInfo> m_InferInfo = nullptr;
- std::shared_ptr<recorder> m_recorder = nullptr;
- std::shared_ptr<ThreadPool> m_pool = nullptr;
- Inference();
- ~Inference();
- public:
- /**
- * @description: 创建实例
- * @param {*}
- * @return {*}
- */
- static std::shared_ptr<Inference> CreateNew();
- /**
- * @description: 推理模块初始化
- * @param {vector<DataSource>} DataList 数据源集合
- * @return {*}
- * @author: lishengyin
- */
- int32_t Init();
- /**
- * @description: 启动任务
- * @param {*}
- * @return {*}
- * @author: lishengyin
- */
- int32_t StartTask();
- /**
- * @description: 停止任务
- * @param {*}
- * @return {*}
- * @author: lishengyin
- */
- void StopTask();
- /**
- * @description: 重启任务
- * @param {*}
- * @return {*}
- * @author: lishengyin
- */
- int32_t RestartTask();
- /**
- * @description: 状态返回
- * @param {*}
- * @return {*}
- */
- static void StateResulit(GstStateChangeReturn& state_return);
- /**
- * @description: 获取Alive
- * @return {*}
- */
- bool getAlive();
- /**
- * @description: 动态调节
- * @return {*}
- */
- void DynamicAdjust();
- /**
- * @description: Destory()
- * @return {*}
- */
- void Destory();
- /**
- * @description: Quit
- * @return {*}
- */
- void QuitLoop();
- /**
- * @description: getTargetNum
- * @return {*}
- */
- static int getTargetNum(NvDsObjectMetaList* objs);
-
- public:
- /**
- * @description: 获取推理结果
- * @param {GstPad *} pad
- * @param {GstPadProbeInfo *} info
- * @param {gpointer} u_data
- * @return {*}
- * @author: lishengyin
- */
- static GstPadProbeReturn tiler_sink_pad_buffer_probe (GstPad * pad, GstPadProbeInfo * probe_info, gpointer u_data);
- /**
- * @description: 监听bus
- * @param {GstBus *} bus
- * @param {GstMessage *} msg
- * @param {gpointer} data
- * @return {*}
- * @author: lishengyin
- */
- static gboolean bus_call (GstBus * bus, GstMessage * msg, gpointer data);
- /**
- * @description: 解码模块监听child_added
- * @param {*}
- * @return {*}
- * @author: lishengyin
- */
- static void decodebin_child_added (GstChildProxy * child_proxy, GObject * object,
- gchar * name, gpointer user_data);
- /**
- * @description: 创建解码bin
- * @param {guint} index
- * @param {gchar *} filename
- * @return {*}
- * @author: lishengyin
- */
- static GstElement * create_uridecode_bin (guint index, gchar * filename);
- /**
- * @description: 解码模块监听pad-added
- * @param {GstElement *} decodebin
- * @param {GstPad *} pad
- * @param {gpointer} data
- * @return {*}
- * @author: lishengyin
- */
- static void cb_newpad (GstElement * decodebin, GstPad * pad, gpointer data);
- /**
- * @description: 启动rtsp
- * @param {guint} rtsp_port_num
- * @param {guint} updsink_port_num
- * @param {guint64} udp_buffer_size
- * @return {*}
- */
- static gboolean start_rtsp_streaming (guint rtsp_port_num, guint updsink_port_num, guint64 udp_buffer_size);
- /**
- * @description: FloatToString
- * @param {float} number
- * @return {*}
- */
- static std::string FloatToString(float number);
- /**
- * @description: 添加数据源
- * @param {int} source_Id 数据源ID
- * @param {string} uri 数据源Url
- * @return {*} 是否添加成功
- * @author: lishengyin
- */
- GstElement* add_sources (int source_Id, std::string uri);
-
- /**
- * @description: 删除数据源
- * @param {gint} source_id
- * @return {*}
- * @author: lishengyin
- */
- void stop_release_source (gint source_id);
- /**
- * @description: 修改数据源地址
- * @param {GstElement} *source_bin
- * @param {string} uri
- * @return {*}
- */
- void ModifyUri(GstElement *source_bin, std::string uri);
-
- /**
- * @description: 添加数据源
- * @param {int} sourceId
- * @return {*}
- */
- void AddSources(int sourceId);
- /**
- * @description: 设置Batch
- * @param {int} num
- * @return {*}
- */
- void SetBatch(int num);
- };
- }
|