Blame view

src/reprocessing_module/save_snapshot_reprocessing.cpp 3.07 KB
09c2d08c   Hu Chunming   arm交付版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  #include "save_snapshot_reprocessing.h"
  #include <thread>
  
  #include "opencv2/opencv.hpp"
  #include <opencv2/imgproc/types_c.h>
  #include <algorithm>
  #include "../common/logger.hpp"
  #include "../util/vpc_util.h"
  
  const bool DRAW_ON_IMG = false;
  
  int save_img_thread_process(void* param) {
    save_snapshot_reprocessing *pThreadParam = (save_snapshot_reprocessing *)param;
    if (pThreadParam != nullptr){
      pThreadParam->save_img_process();
    }
    return 0;
  }
  
  // 初始化快照保存模块 开启图片保存线程
  save_snapshot_reprocessing::save_snapshot_reprocessing(int devId) {
  #ifdef POST_USE_RABBITMQ
    callback_ = nullptr;
  #endif
  
    bFinish = false;
    m_save_img_thread = std::thread(save_img_thread_process, this);
  
    m_devId = devId;
    jpegUtil.jpeg_init(m_devId);
  }
  
  save_snapshot_reprocessing::~save_snapshot_reprocessing(){
    // 结束线程
    bFinish = true;
    m_save_img_thread.join();
    jpegUtil.jpeg_release();
  }
  
  // 释放资源
  void save_snapshot_reprocessing::save_snapshot_reprocessing_release() {
  
    std::unique_lock<std::mutex> l(waitforsave_img_queue_mutex);
  
    while (!waitforsave_img_queue.empty()) {
      ImgSaveInfo cur_image = waitforsave_img_queue.front();
      waitforsave_img_queue.pop();
  
      if(!cur_image.file_path.empty()){
        VPCUtil::vpc_img_release(cur_image.img_info);
      }
    }
  
    l.unlock();
  }
  
  #ifdef POST_USE_RABBITMQ
  
  // 设置MQ返回回调函数 方便内部调用MQ推送结果
  void save_snapshot_reprocessing::set_callback(callback_t cb) {
    callback_ = cb;
  }
  
  #endif // #ifdef POST_USE_RABBITMQ
  
  
  void save_snapshot_reprocessing::reprocessing_process_wo_locus_async(ImgSaveInfo saveInfo){
  
     while(!bFinish){
      waitforsave_img_queue_mutex.lock();
      if(waitforsave_img_queue.size() > 100){
        waitforsave_img_queue_mutex.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
        continue;
      }
      waitforsave_img_queue.push(saveInfo);
      waitforsave_img_queue_mutex.unlock();
      break;
    }
    
  }
  
  void save_snapshot_reprocessing::save_img_process() {
    while (true) {
      if (bFinish){
        break;
      }
  
      std::unique_lock<std::mutex> l(waitforsave_img_queue_mutex);
      if (!waitforsave_img_queue.empty()) {
        LOG_DEBUG("waitforsave_image_queue size: {}", waitforsave_img_queue.size());
        ImgSaveInfo cur_image = waitforsave_img_queue.front();
        waitforsave_img_queue.pop();
        l.unlock();
  
28ca1c66   Zhao Shuaihua   代码优化
96
        bool bSaved = false;
09c2d08c   Hu Chunming   arm交付版
97
        if(!cur_image.file_path.empty()){
28ca1c66   Zhao Shuaihua   代码优化
98
          bSaved = jpegUtil.jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path);
ed1887df   Hu Chunming   修复限制目标结果数量导致漏目标情形...
99
100
101
          if(!bSaved){
            LOG_ERROR("jpeg_encode failed");
          }
09c2d08c   Hu Chunming   arm交付版
102
103
104
105
106
        }
        VPCUtil::vpc_img_release(cur_image.img_info);
  
  #ifdef POST_USE_RABBITMQ
        // LOG_INFO("mq publish process in: {}", cur_image.json_str);
28ca1c66   Zhao Shuaihua   代码优化
107
        if (bSaved && callback_ != nullptr && cur_image.json_str.length() > 0) {
09c2d08c   Hu Chunming   arm交付版
108
109
110
111
112
113
114
115
116
117
118
119
          // LOG_INFO("mq publish process begin");
          callback_(cur_image.json_str.c_str());
          LOG_INFO("mq publish process end: {} ", cur_image.json_str);
        }
  #endif
  
      } else {
        l.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(2));
      }
  
    }
28ca1c66   Zhao Shuaihua   代码优化
120
  }