当前位置:   article > 正文

目标检测多线程框架(1)_图像帧 队列 多线程 目标检测

图像帧 队列 多线程 目标检测

目标检测用到的异步多线程处理(1)-之异步处理   完整工程下载

采用C11以上语法支持

这里为了好说明问题就不用ffmpeg ,保证代码足够简单易懂以下还是用opencv的VideoCapture描述为主.

通常在目标检测分析处理中大致思路是下面这样的:

   如下:

  1. //视频模拟camera
  2. void testCPUvideo(const char* name)
  3. {
  4. cv::VideoCapture vcap;
  5. cv::Mat fps_curr;
  6. vcap.open(name);
  7. vcap.set(CAP_PROP_POS_FRAMES, 1);
  8. if (!vcap.isOpened())
  9. return ;
  10. while (true)
  11. {
  12. vcap >> fps_curr;
  13. if (fps_curr.empty())
  14. return ;
  15. //DNN(fps_curr, my_objinfo); //目标检测
  16. //draw(fps_curr, my_objinfo); //解析my_objinfo绘制框
  17. cv::namedWindow("CPU_Video_play", cv::WINDOW_OPENGL); //gl
  18. cv::imshow("CPU_Video_play", fps_curr);
  19. //pushrtsp(fps_curr,1920,1080); //推出去rtsp
  20. cv::waitKey(1);
  21. }
  22. cv::waitKey(0);
  23. }

这是一个明显的单线程线性的处理方法,很多事情在同步阻塞下是不能操作的,这也导致了中间不能插入太多要求.真正项目上是力不从心的.

下面对上面做一个改进.将解码->处理->渲染(显示)分别拆开到独立线程处理.如下图:

  1. void testMutiThread(const char* name)
  2. {
  3. //数据结构
  4. using MAT = cv::Mat;
  5. using MyFrame = struct
  6. {
  7. std::queue<MAT> image_data_queue; //解码数据
  8. std::mutex decode_mutex;
  9. std::queue<MAT> process_data_queue; //处理数据
  10. std::mutex process_mutex;
  11. std::queue<MAT> draw_data_queue; //渲染数据
  12. std::mutex draw_mutex;
  13. };
  14. static MyFrame m_Frame;
  15. std::atomic_bool brun1(true), brun2(true), brun3(true);
  16. auto CollectTd = [&]
  17. {
  18. cv::VideoCapture vcap;
  19. MAT capture264;
  20. vcap.open(name); //打开文件
  21. vcap.set(cv::CAP_PROP_POS_FRAMES, 1);
  22. if (!vcap.isOpened())
  23. return;
  24. int numflag(0);
  25. while (true)
  26. {
  27. std::string strnum = std::to_string(numflag++);
  28. vcap >> capture264; //此处模拟相机采集数据
  29. if (!capture264.empty())
  30. {
  31. std::lock_guard<std::mutex> lock_guard(m_Frame.decode_mutex);
  32. cout << "CollectThread image_data>> " << strnum << endl;
  33. m_Frame.image_data_queue.push(capture264);
  34. }
  35. else {
  36. brun1 = brun2 = brun3 = false;
  37. break;
  38. }
  39. this_thread::sleep_for(std::chrono::milliseconds(30));
  40. }
  41. };
  42. auto DecodeTd =[&]
  43. {
  44. while (brun1)
  45. {
  46. if (!m_Frame.image_data_queue.empty())
  47. {
  48. MAT image_data;
  49. {
  50. std::lock_guard<std::mutex> lock_guard(m_Frame.decode_mutex);
  51. image_data = m_Frame.image_data_queue.front();
  52. m_Frame.image_data_queue.pop();
  53. cout << "DecodeThread image_data<< " << endl;
  54. }
  55. //从收到的数据进行解码,解码后送到处理线程
  56. MAT process_data = image_data;
  57. {
  58. std::lock_guard<std::mutex> lock_guard(m_Frame.process_mutex);
  59. cout << "DecodeThread process_data>> " << endl;
  60. m_Frame.process_data_queue.push(process_data);
  61. }
  62. }
  63. else
  64. this_thread::sleep_for(std::chrono::milliseconds(50));
  65. }
  66. };
  67. auto ProcessTd = [&]
  68. {
  69. while (brun2)
  70. {
  71. if (!m_Frame.process_data_queue.empty())
  72. {
  73. MAT process_data;
  74. {
  75. std::lock_guard<std::mutex> lock_guard(m_Frame.process_mutex);
  76. process_data = m_Frame.process_data_queue.front();
  77. m_Frame.process_data_queue.pop();
  78. cout << "ProcessThread process_data<< " << endl;
  79. }
  80. if (!process_data.empty())
  81. {
  82. //处理图像 处理完送到渲染线程
  83. //ImageProcess(process_data);
  84. MAT draw_data = process_data;
  85. std::lock_guard<std::mutex> lock_guard(m_Frame.draw_mutex);
  86. cout << "ProcessThread draw_data>> " << endl;
  87. m_Frame.draw_data_queue.push(draw_data);
  88. }
  89. }
  90. else
  91. this_thread::sleep_for(std::chrono::milliseconds(50));
  92. }
  93. };
  94. auto DrawTd = [&]
  95. {
  96. cv::namedWindow("DrawThread", cv::WINDOW_OPENGL); //gl
  97. cv::resizeWindow("DrawThread", 800, 600);
  98. while (brun3)
  99. {
  100. if (!m_Frame.draw_data_queue.empty())
  101. {
  102. MAT draw_data;
  103. {
  104. std::lock_guard<std::mutex> lock_guard(m_Frame.draw_mutex);
  105. draw_data = m_Frame.draw_data_queue.front();
  106. m_Frame.draw_data_queue.pop();
  107. //用opencv渲染
  108. cv::imshow("DrawThread", draw_data);
  109. cv::waitKey(1);
  110. cout << "DrawThread draw_data<< " << endl;
  111. }
  112. }
  113. else
  114. this_thread::sleep_for(std::chrono::milliseconds(50));
  115. }
  116. };
  117. std::thread td1(CollectTd);
  118. std::thread td2(DecodeTd);
  119. std::thread td3(ProcessTd);
  120. std::thread td4(DrawTd);
  121. td1.join();
  122. td2.join();
  123. td3.join();
  124. td4.join();
  125. this_thread::sleep_for(std::chrono::hours(1));
  126. }

当然实际中可能还有更多的处理线程,这里我做个笔记. 所以 还是以类的形式更加直观,这里扩展一下:

采用6个线程来模拟,数据采用5个队列内部各自处理

部分参考示例代码如下:

  1. //**********************************************************************
  2. // C11 视频多线程框架
  3. // @2021-01-04阿甘整理
  4. //**********************************************************************
  5. //通用数据结构
  6. using MAT = cv::Mat;
  7. //共享数据结构
  8. struct MyFrame
  9. {
  10. std::queue<MAT> image_data_queue; //解码数据
  11. std::mutex decode_mutex;
  12. std::queue<MAT> process_data_queue; //处理数据
  13. std::mutex process_mutex;
  14. std::queue<MAT> push_data_queue; //推流数据
  15. std::mutex push_mutex;
  16. std::queue<MAT> draw_data_queue; //渲染数据
  17. std::mutex draw_mutex;
  18. std::queue<MAT> tracker_data_queue; //跟踪数据
  19. std::mutex tracker_mutex;
  20. //清空方法
  21. void clear(queue<MAT>& my_queue)
  22. {
  23. queue<MAT> empty_queue;
  24. std::swap(my_queue, empty_queue);
  25. }
  26. void clearall(void)
  27. {
  28. clear(image_data_queue);
  29. clear(process_data_queue);
  30. clear(push_data_queue);
  31. clear(draw_data_queue);
  32. clear(tracker_data_queue);
  33. }
  34. };
  35. //线程基类
  36. class BaseThread
  37. {
  38. public:
  39. BaseThread(MyFrame& mFrame);
  40. virtual ~BaseThread(void);
  41. MyFrame& m_Frame;
  42. public:
  43. void begintk(void);
  44. void endtk(void);
  45. bool getstatus(void);
  46. //线程函数用来多态重载
  47. virtual void run(void) = 0;
  48. private:
  49. std::atomic_bool m_status;
  50. std::thread th;
  51. };
  52. //采集线程
  53. class CollectTd :public BaseThread
  54. {
  55. public:
  56. using BaseThread::BaseThread;
  57. virtual void run(void);
  58. void setplayMp4(string url);
  59. private:
  60. string m_mp4;
  61. cv::VideoCapture vcap;
  62. MAT capture264;
  63. };
  64. //解码线程
  65. class DecodeTd :public BaseThread
  66. {
  67. public:
  68. using BaseThread::BaseThread;
  69. virtual void run(void);
  70. };
  71. //渲染线程
  72. class DrawTd :public BaseThread
  73. {
  74. public:
  75. using BaseThread::BaseThread;
  76. virtual void run(void);
  77. };
  78. //处理线程
  79. class ProcessTd :public BaseThread
  80. {
  81. public:
  82. using BaseThread::BaseThread;
  83. virtual void run(void);
  84. };
  85. //推流线程
  86. class PushTd :public BaseThread
  87. {
  88. public:
  89. using BaseThread::BaseThread;
  90. virtual void run(void);
  91. };
  92. //跟踪线程
  93. class TrackerTd :public BaseThread
  94. {
  95. public:
  96. using BaseThread::BaseThread;
  97. virtual void run(void);
  98. };
  99. //管理器
  100. class C11VideoFrame
  101. {
  102. public:
  103. static void testrun(void)
  104. {
  105. C11VideoFrame mgr;
  106. mgr.init();
  107. mgr.beginwork();
  108. this_thread::sleep_for(std::chrono::hours(10));
  109. }
  110. public:
  111. C11VideoFrame(void);
  112. ~C11VideoFrame(void);
  113. public:
  114. void init(void);
  115. void beginwork(void);
  116. void endwork(void);
  117. private:
  118. std::unique_ptr<CollectTd> m_collect;
  119. std::unique_ptr<DecodeTd> m_decode;
  120. std::unique_ptr<ProcessTd> m_Process;
  121. std::unique_ptr<PushTd> m_Push;
  122. std::unique_ptr<DrawTd> m_Draw;
  123. std::unique_ptr<TrackerTd> m_Tracker;
  124. std::thread th;
  125. MyFrame m_Frame; //中间数据结构
  126. };

实现部分示例:

  1. //**********************************************************************
  2. // C11 视频多线程框架
  3. // @2021-01-04阿甘整理
  4. //**********************************************************************
  5. BaseThread::BaseThread(MyFrame& mFrame) : m_status(false)
  6. , m_Frame(mFrame)
  7. {
  8. }
  9. BaseThread::~BaseThread(void)
  10. {
  11. endtk();
  12. }
  13. void BaseThread::begintk(void)
  14. {
  15. if (m_status)
  16. endtk();
  17. m_status = true;
  18. th = thread([&]()
  19. {
  20. this->run();
  21. });
  22. }
  23. void BaseThread::endtk(void)
  24. {
  25. m_status = false;
  26. if (th.joinable())
  27. th.join();
  28. }
  29. bool BaseThread::getstatus(void)
  30. {
  31. return m_status;
  32. }
  33. void CollectTd::run(void)
  34. {
  35. vcap.open(m_mp4); //打开文件
  36. vcap.set(cv::CAP_PROP_POS_FRAMES, 1);
  37. if (!vcap.isOpened())
  38. return;
  39. int numflag(0);
  40. while (getstatus())
  41. {
  42. std::string strnum = std::to_string(numflag++);
  43. vcap >> capture264; //此处模拟相机采集数据
  44. if (!capture264.empty())
  45. {
  46. std::lock_guard<std::mutex> lock_guard(m_Frame.decode_mutex);
  47. cout << "CollectThread image_data>> " << strnum << endl;
  48. m_Frame.image_data_queue.push(capture264);
  49. }
  50. else
  51. endtk();
  52. this_thread::sleep_for(std::chrono::milliseconds(30));
  53. }
  54. }
  55. void CollectTd::setplayMp4(string url)
  56. {
  57. m_mp4 = url;
  58. }
  59. void DecodeTd::run(void)
  60. {
  61. while (getstatus())
  62. {
  63. if (!m_Frame.image_data_queue.empty())
  64. {
  65. MAT image_data;
  66. {
  67. std::lock_guard<std::mutex> lock_guard(m_Frame.decode_mutex);
  68. image_data = m_Frame.image_data_queue.front();
  69. m_Frame.image_data_queue.pop();
  70. cout << "DecodeThread image_data<< " << endl;
  71. }
  72. //从收到的数据进行硬解码,解码后送到处理线程
  73. MAT process_data = image_data;
  74. {
  75. //cuvidxx操作
  76. std::lock_guard<std::mutex> lock_guard(m_Frame.process_mutex);
  77. cout << "DecodeThread process_data>> " << endl;
  78. m_Frame.process_data_queue.push(process_data);
  79. }
  80. }
  81. else
  82. this_thread::sleep_for(std::chrono::milliseconds(50));
  83. }
  84. }
  85. void ProcessTd::run(void)
  86. {
  87. while (getstatus())
  88. {
  89. if (!m_Frame.process_data_queue.empty())
  90. {
  91. MAT process_data;
  92. {
  93. std::lock_guard<std::mutex> lock_guard(m_Frame.process_mutex);
  94. process_data = m_Frame.process_data_queue.front();
  95. m_Frame.process_data_queue.pop();
  96. cout << "ProcessThread process_data<< " << endl;
  97. }
  98. if (!process_data.empty())
  99. {
  100. //处理图像 处理完送到渲染线程
  101. //ImageProcess(process_data);
  102. MAT draw_data = process_data;
  103. std::lock_guard<std::mutex> lock_guard(m_Frame.draw_mutex);
  104. cout << "ProcessThread draw_data>> " << endl;
  105. m_Frame.draw_data_queue.push(draw_data);
  106. }
  107. }
  108. else
  109. this_thread::sleep_for(std::chrono::milliseconds(50));
  110. }
  111. }
  112. void DrawTd::run(void)
  113. {
  114. cv::namedWindow("DrawThread", cv::WINDOW_OPENGL); //gl
  115. cv::resizeWindow("DrawThread", 800,600);
  116. while (getstatus())
  117. {
  118. if (!m_Frame.draw_data_queue.empty())
  119. {
  120. MAT draw_data;
  121. {
  122. std::lock_guard<std::mutex> lock_guard(m_Frame.draw_mutex);
  123. draw_data = m_Frame.draw_data_queue.front();
  124. m_Frame.draw_data_queue.pop();
  125. //用opencv渲染
  126. cv::imshow("DrawThread", draw_data);
  127. cv::waitKey(1);
  128. cout << "DrawThread draw_data<< " << endl;
  129. }
  130. //跟踪数据计算
  131. MAT tracker_data = draw_data;
  132. {
  133. std::lock_guard<std::mutex> lock_guard(m_Frame.tracker_mutex);
  134. cout << "DrawThread tracker_data>> " << endl;
  135. m_Frame.tracker_data_queue.push(tracker_data);
  136. }
  137. //处理推流数据
  138. MAT push_data = draw_data;
  139. {
  140. std::lock_guard<std::mutex> lock_guard(m_Frame.push_mutex);
  141. cout << "DrawThread push_data>> " << endl;
  142. m_Frame.push_data_queue.push(push_data);
  143. }
  144. }
  145. else
  146. this_thread::sleep_for(std::chrono::milliseconds(50));
  147. }
  148. }
  149. void PushTd::run(void)
  150. {
  151. while (getstatus())
  152. {
  153. if (!m_Frame.push_data_queue.empty())
  154. {
  155. MAT push_data;
  156. {
  157. std::lock_guard<std::mutex> lock_guard(m_Frame.push_mutex);
  158. push_data = m_Frame.push_data_queue.front();
  159. m_Frame.push_data_queue.pop();
  160. cout << "PushThread push_data<< " << endl;
  161. }
  162. //调用推送方法
  163. if (!push_data.empty())
  164. {
  165. //RtmpPuller(push_data.data, push_data.cols * push_data.rows * push_data.channels());
  166. }
  167. }
  168. else
  169. this_thread::sleep_for(std::chrono::milliseconds(50));
  170. }
  171. }
  172. void TrackerTd::run(void)
  173. {
  174. while (getstatus())
  175. {
  176. if (!m_Frame.tracker_data_queue.empty())
  177. {
  178. MAT tracker_data;
  179. {
  180. std::lock_guard<std::mutex> lock_guard(m_Frame.tracker_mutex);
  181. tracker_data = m_Frame.tracker_data_queue.front();
  182. m_Frame.tracker_data_queue.pop();
  183. cout << "TrackerThread tracker_data<< " << endl;
  184. }
  185. //中间跟踪器算法
  186. //tracker(tracker_data);
  187. }
  188. else
  189. this_thread::sleep_for(std::chrono::milliseconds(50));
  190. }
  191. }
  192. C11VideoFrame::C11VideoFrame(void)
  193. {
  194. }
  195. C11VideoFrame::~C11VideoFrame(void)
  196. {
  197. }
  198. void C11VideoFrame::init(void)
  199. {
  200. m_collect = unique_ptr<CollectTd>(new CollectTd(m_Frame));
  201. m_collect->setplayMp4("cam.mp4");
  202. m_decode = unique_ptr<DecodeTd>(new DecodeTd(m_Frame));
  203. m_Process = unique_ptr<ProcessTd>(new ProcessTd(m_Frame));
  204. m_Push = unique_ptr<PushTd>(new PushTd(m_Frame));
  205. m_Draw = unique_ptr<DrawTd>(new DrawTd(m_Frame));
  206. m_Tracker = unique_ptr<TrackerTd>(new TrackerTd(m_Frame));
  207. }
  208. void C11VideoFrame::beginwork(void)
  209. {
  210. m_decode->begintk();
  211. m_Process->begintk();
  212. m_Push->begintk();
  213. m_Draw->begintk();
  214. m_Tracker->begintk();
  215. m_collect->begintk();
  216. th = thread([&]()
  217. {
  218. while (true)
  219. {
  220. //采集线程停止 其他可以关闭了
  221. if (!m_collect->getstatus())
  222. {
  223. m_decode->endtk();
  224. m_Process->endtk();
  225. m_Push->endtk();
  226. m_Draw->endtk();
  227. m_Tracker->endtk();
  228. break;
  229. }
  230. this_thread::sleep_for(std::chrono::milliseconds(800));
  231. }
  232. });
  233. }
  234. void C11VideoFrame::endwork(void)
  235. {
  236. m_collect->endtk();
  237. m_decode->endtk();
  238. m_Process->endtk();
  239. m_Push->endtk();
  240. m_Draw->endtk();
  241. m_Tracker->endtk();
  242. }

最后运行效果和中间打印日志:还是以影像修复七里香来弄咯

总结:目前仅仅解决了在多线程中异步处理的思路,这里并没有对线程做干涉同步,而是用队列各自处理.在一定情况下还是有很大的折腾可自己搞的.中间的数据结构都是采用的数据拷贝这个是要根据需要自己折中选择的. 为了简单就这样了,就当是抛砖引玉,高手就见笑了.

视频分析中的多线程笔记记录(1)分享完毕.

完整下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/300297
推荐阅读
相关标签
  

闽ICP备14008679号