123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /*
- * @Description: 推理模块
- * @Version: 1.0
- * @Autor: lishengyin
- * @Date: 2021-10-13 09:37:51
- * @LastEditors: lishengyin
- * @LastEditTime: 2022-10-09 09:45:46
- */
- #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 "Util/SqlPool.h"
- #include "Network/TcpClient.h"
- #include "Poller/Timer.h"
- #include "TCPClient.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 <list>
- using namespace std;
- namespace MIVA{
- // 基于Deepstream
- class Inference
- {
- private:
- // Deepstream
- GMainLoop *loop = NULL;
- GstElement *pipeline = NULL,*streammux =NULL, *sink = NULL, *pgie = NULL,
- *queue1, *queue2, *queue3, *queue4, *queue5, *nvvidconv = NULL,
- *nvosd = NULL,*tiler = NULL;
- GstElement *transform = NULL;
- GstBus *bus = NULL;
- guint bus_watch_id;
- GstPad *pgie_src_pad = NULL;
- GstPad *tiler_sink_pad = NULL;
- guint tiler_rows, tiler_columns;
- guint pgie_batch_size;
- public:
- std::shared_ptr<std::vector<DataSource>> m_DataList = NULL;
- std::shared_ptr<InferInfo> m_InferInfo = nullptr;
- std::shared_ptr<recorder> m_recorder = nullptr;
-
- bool Play = false;
- bool enable = false;
- int InferNum = 0;
- 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 {*}
- */
- int32_t Recorder();
- /**
- * @description: 暂停任务
- * @param {*}
- * @return {*}
- */
- void PausedTask();
- /**
- * @description: 检查任务
- * @param {*}
- * @return {*}
- */
- void CheckTask();
- /**
- * @description: Destory()
- * @return {*}
- */
- void Destory();
- /**
- * @description: QuitLoop
- * @param {*}
- * @return {*}
- */
- void QuitLoop();
- /**
- * @description: 状态返回
- * @param {*}
- * @return {*}
- */
- static void StateResulit(GstStateChangeReturn& state_return);
- 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: 判断点是否在区域内
- * @param {Point} p
- * @param {int} nCount
- * @return {*}
- */
- static int PtInPolygon (Point p, vector<Point>& ptPolygon, int nCount);
- /**
- * @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 {GstElement} *source_bin
- * @return {*}
- */
- void FreeElement(int source_Id,GstElement *source_bin);
- /**
- * @description: 添加数据源
- * @param {int} sourceId
- * @return {*}
- */
- void AddSources(int sourceId);
- /**
- * @description: 设置Batch
- * @param {int} num
- * @return {*}
- */
- void SetBatch(int num);
- };
- }
|