浏览代码

加入推理超时处理

lishengyin 3 年之前
父节点
当前提交
af9b8c0fc1

+ 0 - 1
modules/dataType/include/DataSource.h

@@ -4,7 +4,6 @@
 #include <vector>
 
 
-
 class DataSource
 {
 private:

+ 2 - 1
modules/inference/include/inference.h

@@ -4,7 +4,7 @@
  * @Autor: lishengyin
  * @Date: 2021-10-13 09:37:51
  * @LastEditors: lishengyin
- * @LastEditTime: 2021-12-06 09:34:24
+ * @LastEditTime: 2021-12-06 10:49:27
  */
 #pragma once
 #include <iostream>
@@ -38,6 +38,7 @@
 #include "Poller/Timer.h"
 #include "TCPClient.h"
 #include <mutex>
+#include <cuda_runtime.h>
 
 // opencv
 #include <opencv2/core.hpp>

+ 76 - 41
modules/inference/src/inference.cpp

@@ -4,7 +4,7 @@
  * @Autor: lishengyin
  * @Date: 2021-10-13 09:35:37
  * @LastEditors: lishengyin
- * @LastEditTime: 2021-12-06 09:53:18
+ * @LastEditTime: 2021-12-09 09:21:18
  */
 #include "inference.h"
 
@@ -52,6 +52,7 @@ GMutex eos_lock;
 
 GstElement *g_streammux = NULL;
 std::shared_ptr<InferInfo> g_InferInfo = NULL;
+// std::shared_ptr<cv::VideoWriter> g_videoWriter = nullptr;
 
 namespace MIVA{
     std::shared_ptr<Inference> infer = NULL;
@@ -211,11 +212,11 @@ namespace MIVA{
                 this->tiler_src_pad_buffer_probe, NULL, NULL);
         gst_object_unref (this->tiler_src_pad);
 
-        this->osd_sink_pad = gst_element_get_static_pad (this->nvosd, "src");
-        gst_pad_add_probe (this->osd_sink_pad, GST_PAD_PROBE_TYPE_BUFFER,
-                osd_sink_pad_buffer_probe, NULL, NULL);
+        // this->osd_sink_pad = gst_element_get_static_pad (this->nvosd, "src");
+        // gst_pad_add_probe (this->osd_sink_pad, GST_PAD_PROBE_TYPE_BUFFER,
+        //         osd_sink_pad_buffer_probe, NULL, NULL);
 
-        gst_object_unref (this->osd_sink_pad);
+        // gst_object_unref (this->osd_sink_pad);
         return OK;
     }
 
@@ -252,6 +253,26 @@ namespace MIVA{
                 g_main_loop_run(this->loop);
             });
         }
+        // if(g_videoWriter == nullptr) {
+        //     time_t rawtime;
+        //     char ctime[80];
+        //     struct tm *info;
+        //     time(&rawtime);
+        //     info = localtime(&rawtime);
+        //     strftime(ctime, 80, "%Y-%m-%d_%H:%M:%S", info);
+        //     string fileName = ctime;
+        //     fileName += ".mp4";
+        //     ErrorL << fileName;
+        //     g_videoWriter = std::make_shared<cv::VideoWriter>(fileName, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 25, cv::Size(640, 480));
+        //     if(g_videoWriter->isOpened()){
+        //         ErrorL << "open()" << endl;
+        //     }else{
+        //         ErrorL << "close()" << endl;
+        //     }
+        // }else{
+        //     g_videoWriter->release();
+        //     g_videoWriter = nullptr;
+        // }
         pool.start();
         return OK;
     }
@@ -307,6 +328,10 @@ namespace MIVA{
             }
             sourceId++;
         }
+        // if(g_videoWriter != nullptr) {
+        //     g_videoWriter->release();
+        //     g_videoWriter = nullptr;
+        // }
         NoticeCenter::Instance().emitEvent(NOTICE_RELEASE);
     }
 
@@ -465,48 +490,57 @@ namespace MIVA{
         NvBufSurface *surface = (NvBufSurface *)in_map_info.data;
         NvDsBatchMeta *batch_meta = gst_buffer_get_nvds_batch_meta (buf);
         l_frame = batch_meta->frame_meta_list; 
-        
-        if (l_frame) {
+
+        for (l_frame = batch_meta->frame_meta_list; l_frame != NULL;l_frame = l_frame->next) //从批量中获取某一帧图
+        {
             NvDsFrameMeta *frame_meta = (NvDsFrameMeta *) (l_frame->data);
             /* Validate user meta */
             src_data = (char*) malloc(surface->surfaceList[frame_meta->batch_id].dataSize);
             if(src_data == NULL) {
                 g_print("Error: failed to malloc src_data \n");
             }
-        #ifdef PLATFORM_TEGRA
-            NvBufSurfaceMap (surface, -1, -1, NVBUF_MAP_READ);
-            NvBufSurfacePlaneParams *pParams = &surface->surfaceList[frame_meta->batch_id].planeParams;
-            unsigned int offset = 0;
-            for(unsigned int num_planes=0; num_planes < pParams->num_planes; num_planes++){
-                if(num_planes>0)
-                    offset += pParams->height[num_planes-1]*(pParams->bytesPerPix[num_planes-1]*pParams->width[num_planes-1]);
-                for (unsigned int h = 0; h < pParams->height[num_planes]; h++) {
-                memcpy((void *)(src_data+offset+h*pParams->bytesPerPix[num_planes]*pParams->width[num_planes]),
-                        (void *)((char *)surface->surfaceList[frame_meta->batch_id].mappedAddr.addr[num_planes]+h*pParams->pitch[num_planes]),
-                        pParams->bytesPerPix[num_planes]*pParams->width[num_planes]
-                        );
+            #ifdef PLATFORM_TEGRA
+                NvBufSurfaceMap (surface, -1, -1, NVBUF_MAP_READ);
+                NvBufSurfacePlaneParams *pParams = &surface->surfaceList[frame_meta->batch_id].planeParams;
+                unsigned int offset = 0;
+                for(unsigned int num_planes=0; num_planes < pParams->num_planes; num_planes++){
+                    if(num_planes>0)
+                        offset += pParams->height[num_planes-1]*(pParams->bytesPerPix[num_planes-1]*pParams->width[num_planes-1]);
+                    for (unsigned int h = 0; h < pParams->height[num_planes]; h++) {
+                    memcpy((void *)(src_data+offset+h*pParams->bytesPerPix[num_planes]*pParams->width[num_planes]),
+                            (void *)((char *)surface->surfaceList[frame_meta->batch_id].mappedAddr.addr[num_planes]+h*pParams->pitch[num_planes]),
+                            pParams->bytesPerPix[num_planes]*pParams->width[num_planes]
+                            );
+                    }
                 }
-            }
-            NvBufSurfaceSyncForDevice (surface, -1, -1);
-            NvBufSurfaceUnMap (surface, -1, -1);
-        #else
-            cudaMemcpy((void*)src_data,
-                        (void*)surface->surfaceList[frame_meta->batch_id].dataPtr,
-                        surface->surfaceList[frame_meta->batch_id].dataSize,
-                        cudaMemcpyDeviceToHost);
-        #endif 
-        
-        gint frame_width = (gint)surface->surfaceList[frame_meta->batch_id].width;
-        gint frame_height = (gint)surface->surfaceList[frame_meta->batch_id].height;
-        gint frame_step = surface->surfaceList[frame_meta->batch_id].pitch;
-        cv::Mat frame = cv::Mat(frame_height, frame_width, CV_8UC4, src_data, frame_step);
-        // g_print("%d\n",frame.channels());
-        // g_print("%d\n",frame.rows);
-        // g_print("%d\n",frame.cols);
-
-        cv::Mat out_mat = cv::Mat (cv::Size(frame_width, frame_height), CV_8UC3);
-        cv::cvtColor(frame, out_mat, CV_RGBA2BGR);
-        cv::imwrite("test.jpg", out_mat);
+                NvBufSurfaceSyncForDevice (surface, -1, -1);
+                NvBufSurfaceUnMap (surface, -1, -1);
+            #else
+                cudaMemcpy((void*)src_data,
+                            (void*)surface->surfaceList[frame_meta->batch_id].dataPtr,
+                            surface->surfaceList[frame_meta->batch_id].dataSize,
+                            cudaMemcpyDeviceToHost);
+            #endif 
+            gint frame_width = (gint)surface->surfaceList[frame_meta->batch_id].width;
+            gint frame_height = (gint)surface->surfaceList[frame_meta->batch_id].height;
+            gint frame_step = surface->surfaceList[frame_meta->batch_id].pitch;
+
+            cv::Mat frame = cv::Mat(frame_height, frame_width, CV_8UC4, src_data, frame_step);
+            // g_print("%d\n",frame.channels());
+            // g_print("%d\n",frame.rows);
+            // g_print("%d\n",frame.cols);
+
+            cv::Mat out_mat = cv::Mat (cv::Size(frame_width, frame_height), CV_8UC3);
+            cv::cvtColor(frame, out_mat, CV_RGBA2BGR);
+
+            // cv::Rect rect(0, 0, frame_width/4, frame_height/4);
+            // cv::Mat image_roi = out_mat(rect); 
+
+            // cv::resize(image_roi, image_roi, cv::Size(frame_width, frame_height));
+            
+            // cv::imwrite(std::to_string(frame_meta->source_id) + ".jpg", out_mat);
+            // cv::imshow("12", out_mat);
+            // if(g_videoWriter != nullptr) *g_videoWriter << out_mat;
             if(src_data != NULL) {
                 free(src_data);
                 src_data = NULL;
@@ -514,6 +548,7 @@ namespace MIVA{
         }
         gst_buffer_unmap (buf, &in_map_info);
         return GST_PAD_PROBE_OK;
+        
     }
 
     /**
@@ -634,7 +669,7 @@ namespace MIVA{
             g_object_set (object, "enable-max-performance", TRUE, NULL);
             g_object_set (object, "bufapi-version", TRUE, NULL);
             g_object_set (object, "drop-frame-interval", 0, NULL);
-            g_object_set (object, "num-extra-surfaces", 2, NULL);
+            // g_object_set (object, "num-extra-surfaces", 2, NULL);
         #else
             g_object_set (object, "gpu-id", 0, NULL);
         #endif

+ 2 - 1
modules/userApp/include/user_app.h

@@ -4,7 +4,7 @@
  * @Autor: lishengyin
  * @Date: 2021-10-13 09:41:50
  * @LastEditors: lishengyin
- * @LastEditTime: 2021-11-29 11:54:02
+ * @LastEditTime: 2021-12-09 09:23:21
  */
 #pragma once
 
@@ -178,6 +178,7 @@ namespace MIVA
         Timer::Ptr m_timer2;
         Timer::Ptr m_timer3;
         Timer::Ptr m_timer4;
+        Timer::Ptr m_timer5;
 
     private:
 

+ 9 - 2
modules/userApp/src/user_app.cpp

@@ -4,7 +4,7 @@
  * @Autor: lishengyin
  * @Date: 2021-10-13 09:35:42
  * @LastEditors: lishengyin
- * @LastEditTime: 2021-11-29 15:44:11
+ * @LastEditTime: 2021-12-09 09:38:27
  */
 #include "user_app.h"
 
@@ -43,7 +43,7 @@ namespace MIVA
     {
         //设置日志
         Logger::Instance().add(std::make_shared<ConsoleChannel>());
-        Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
+        // Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
 
         this->m_appName = appName;
 
@@ -676,6 +676,13 @@ namespace MIVA
                 if(m_Infer->StartTask() == OK){
                     this->play = true;
                     this->start = true;
+                    // 超时处理
+                    this->m_timer5 = std::make_shared<Timer>(20.0f,[&](){
+                        if(this->play){
+                            NoticeCenter::Instance().emitEvent(NOTICE_INFEREPER);
+                        }
+                        return false;
+                    },nullptr);
                 }
             }
         }