当前位置:   article > 正文

ROS系统——部署OpenVINO版Nanodet超轻量目标检测器_ros 轻量化目标识别

ros 轻量化目标识别

目录

0 背景

本人的实测效果:

1 环境搭建

2 先熟悉OpenVINO版nanodet的流程

3  在ROS里部署openvino版nanodet的流程

4 源码

4.1 main.cpp内容

4.2 CMakeLists.txt

4.3 我的cv_bridge模块修改的内容

4.4 我的~/.bashrc中内容

5 效果图


0 背景

系统平台:

  • Ubuntu 18.04,ROS系统

计算资源:

  • 一个i5 CPU

计算量:

  • 4个摄像头需要开展目标检测,每个摄像头每秒10帧以上。

技术路线:

  • 常规的yolo4-tiny或yolo5s速度都还不够,调研发现一个Nanodet超轻量目标检测算法,在coco数据集上其mAP和yolo4-tiny相当,但是其速度提升了300%!

nanodet目标检测算法的性能如下:

ModelResolutionCOCO mAPLatency(ARM 4 Threads)FLOPSParamsModel Size
NanoDet-m320*32020.610.23ms0.72G0.95M1.8MB(FP16) | 980KB(INT8)
NanoDet-m416*41623.516.44ms1.2G0.95M1.8MB(FP16) | 980KB(INT8)
NanoDet-m-1.5x320*32023.513.53ms1.44G2.08M3.9MB(FP16) | 2MB(INT8)
NanoDet-m-1.5x416*41626.821.53ms2.42G2.08M3.9MB(FP16) | 2MB(INT8)
NanoDet-g416*41622.9Not Designed For ARM4.2G3.81M7.7MB(FP16) | 3.6MB(INT8)
YoloV3-Tiny416*41616.637.6ms5.62G8.86M33.7MB
YoloV4-Tiny416*41621.732.81ms6.96G6.06M23.0MB

本人的实测效果:

笔记本 i7(8th) ,模型NanoDet-m  320*320

  • pytorch版单帧图像推理:0.05秒左右。
  • openvino单帧推理速度:0.006秒左右(for 100次的平均值),提升近10倍,约166~200帧/s。
  • openvino推理4750帧1920*1080的avi视频,带bbox绘制和可视化输出,约70秒跑完,能实现约每秒70帧速度!

接下来讲一讲怎么在ROS里部署openvino版nanodet:

1 环境搭建

建议安装OpenVINO 2021.4 LTS 版本(2020.4版在onnx转IR时,代码会遇到问题)。

安装方法:《ubuntu + oepncv + PCL + realsenseSDK + ROS + OpenVino开发环境搭建》

2 先熟悉OpenVINO版nanodet的流程

建议先在nanodet的GitHub官网去下载源码,并按照里面的教程尝试跑通openvino版nanodet的整个流程。

3  在ROS里部署openvino版nanodet的流程

流程参考:ROS系统中从零开始部署YoloV4目标检测算法(3种方式)

友情提醒:openvino安装时会自动在自身路径内嵌入安装一个最新版OpenCV,需要修改ROS系统中cv_bridge模块的OpenCV路径为openvino安装位置里面那个OpenCV路径,具体原因这里就不说了。

4 源码

我创建的package程序结构如下:

  • ①main.cpp(自己的程序,改造于nanodet中main.cpp)
  • ②nanodet_openvino.h(nanodet项目中源码)
  • ③nanodet_openvino.cpp(nanodet项目中源码)
  • ④CMakeLists.txt
  • ⑤package.xml

4.1 main.cpp内容

  1. #include <ros/ros.h>
  2. #include <cv_bridge/cv_bridge.h>
  3. #include "nanodet_openvino.h"
  4. #include <opencv2/highgui/highgui.hpp>
  5. using namespace std;
  6. #include "std_msgs/Int8.h"
  7. #include "std_msgs/String.h"
  8. ros::Publisher pub;
  9. auto detector = NanoDet("/home/ym/dream2021/xidiji/catkin_ym/src/nanodet_pkg/2nanodet.xml");
  10. struct object_rect {
  11. int x;
  12. int y;
  13. int width;
  14. int height;
  15. };
  16. int resize_uniform(cv::Mat& src, cv::Mat& dst, cv::Size dst_size, object_rect& effect_area)
  17. {
  18. int w = src.cols;
  19. int h = src.rows;
  20. int dst_w = dst_size.width;
  21. int dst_h = dst_size.height;
  22. //std::cout << "src: (" << h << ", " << w << ")" << std::endl;
  23. dst = cv::Mat(cv::Size(dst_w, dst_h), CV_8UC3, cv::Scalar(0));
  24. float ratio_src = w * 1.0 / h;
  25. float ratio_dst = dst_w * 1.0 / dst_h;
  26. int tmp_w = 0;
  27. int tmp_h = 0;
  28. if (ratio_src > ratio_dst) {
  29. tmp_w = dst_w;
  30. tmp_h = floor((dst_w * 1.0 / w) * h);
  31. }
  32. else if (ratio_src < ratio_dst) {
  33. tmp_h = dst_h;
  34. tmp_w = floor((dst_h * 1.0 / h) * w);
  35. }
  36. else {
  37. cv::resize(src, dst, dst_size);
  38. effect_area.x = 0;
  39. effect_area.y = 0;
  40. effect_area.width = dst_w;
  41. effect_area.height = dst_h;
  42. return 0;
  43. }
  44. //std::cout << "tmp: (" << tmp_h << ", " << tmp_w << ")" << std::endl;
  45. cv::Mat tmp;
  46. cv::resize(src, tmp, cv::Size(tmp_w, tmp_h));
  47. if (tmp_w != dst_w) {
  48. int index_w = floor((dst_w - tmp_w) / 2.0);
  49. //std::cout << "index_w: " << index_w << std::endl;
  50. for (int i = 0; i < dst_h; i++) {
  51. memcpy(dst.data + i * dst_w * 3 + index_w * 3, tmp.data + i * tmp_w * 3, tmp_w * 3);
  52. }
  53. effect_area.x = index_w;
  54. effect_area.y = 0;
  55. effect_area.width = tmp_w;
  56. effect_area.height = tmp_h;
  57. }
  58. else if (tmp_h != dst_h) {
  59. int index_h = floor((dst_h - tmp_h) / 2.0);
  60. //std::cout << "index_h: " << index_h << std::endl;
  61. memcpy(dst.data + index_h * dst_w * 3, tmp.data, tmp_w * tmp_h * 3);
  62. effect_area.x = 0;
  63. effect_area.y = index_h;
  64. effect_area.width = tmp_w;
  65. effect_area.height = tmp_h;
  66. }
  67. else {
  68. printf("error\n");
  69. }
  70. //cv::imshow("dst", dst);
  71. //cv::waitKey(0);
  72. return 0;
  73. }
  74. const int color_list[80][3] =
  75. {
  76. //{255 ,255 ,255}, //bg
  77. {216 , 82 , 24},
  78. {236 ,176 , 31},
  79. {125 , 46 ,141},
  80. {118 ,171 , 47},
  81. { 76 ,189 ,237},
  82. {238 , 19 , 46},
  83. { 76 , 76 , 76},
  84. {153 ,153 ,153},
  85. {255 , 0 , 0},
  86. {255 ,127 , 0},
  87. {190 ,190 , 0},
  88. { 0 ,255 , 0},
  89. { 0 , 0 ,255},
  90. {170 , 0 ,255},
  91. { 84 , 84 , 0},
  92. { 84 ,170 , 0},
  93. { 84 ,255 , 0},
  94. {170 , 84 , 0},
  95. {170 ,170 , 0},
  96. {170 ,255 , 0},
  97. {255 , 84 , 0},
  98. {255 ,170 , 0},
  99. {255 ,255 , 0},
  100. { 0 , 84 ,127},
  101. { 0 ,170 ,127},
  102. { 0 ,255 ,127},
  103. { 84 , 0 ,127},
  104. { 84 , 84 ,127},
  105. { 84 ,170 ,127},
  106. { 84 ,255 ,127},
  107. {170 , 0 ,127},
  108. {170 , 84 ,127},
  109. {170 ,170 ,127},
  110. {170 ,255 ,127},
  111. {255 , 0 ,127},
  112. {255 , 84 ,127},
  113. {255 ,170 ,127},
  114. {255 ,255 ,127},
  115. { 0 , 84 ,255},
  116. { 0 ,170 ,255},
  117. { 0 ,255 ,255},
  118. { 84 , 0 ,255},
  119. { 84 , 84 ,255},
  120. { 84 ,170 ,255},
  121. { 84 ,255 ,255},
  122. {170 , 0 ,255},
  123. {170 , 84 ,255},
  124. {170 ,170 ,255},
  125. {170 ,255 ,255},
  126. {255 , 0 ,255},
  127. {255 , 84 ,255},
  128. {255 ,170 ,255},
  129. { 42 , 0 , 0},
  130. { 84 , 0 , 0},
  131. {127 , 0 , 0},
  132. {170 , 0 , 0},
  133. {212 , 0 , 0},
  134. {255 , 0 , 0},
  135. { 0 , 42 , 0},
  136. { 0 , 84 , 0},
  137. { 0 ,127 , 0},
  138. { 0 ,170 , 0},
  139. { 0 ,212 , 0},
  140. { 0 ,255 , 0},
  141. { 0 , 0 , 42},
  142. { 0 , 0 , 84},
  143. { 0 , 0 ,127},
  144. { 0 , 0 ,170},
  145. { 0 , 0 ,212},
  146. { 0 , 0 ,255},
  147. { 0 , 0 , 0},
  148. { 36 , 36 , 36},
  149. { 72 , 72 , 72},
  150. {109 ,109 ,109},
  151. {145 ,145 ,145},
  152. {182 ,182 ,182},
  153. {218 ,218 ,218},
  154. { 0 ,113 ,188},
  155. { 80 ,182 ,188},
  156. {127 ,127 , 0},
  157. };
  158. void draw_bboxes(const cv::Mat& bgr, const std::vector<BoxInfo>& bboxes, object_rect effect_roi)
  159. {
  160. static const char* class_names[] = { "person", "bicycle", "car", "motorcycle", "airplane", "bus",
  161. "train", "truck", "boat", "traffic light", "fire hydrant",
  162. "stop sign", "parking meter", "bench", "bird", "cat", "dog",
  163. "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe",
  164. "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
  165. "skis", "snowboard", "sports ball", "kite", "baseball bat",
  166. "baseball glove", "skateboard", "surfboard", "tennis racket",
  167. "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl",
  168. "banana", "apple", "sandwich", "orange", "broccoli", "carrot",
  169. "hot dog", "pizza", "donut", "cake", "chair", "couch",
  170. "potted plant", "bed", "dining table", "toilet", "tv", "laptop",
  171. "mouse", "remote", "keyboard", "cell phone", "microwave", "oven",
  172. "toaster", "sink", "refrigerator", "book", "clock", "vase",
  173. "scissors", "teddy bear", "hair drier", "toothbrush"
  174. };
  175. cv::Mat image = bgr.clone();
  176. int src_w = image.cols;
  177. int src_h = image.rows;
  178. int dst_w = effect_roi.width;
  179. int dst_h = effect_roi.height;
  180. float width_ratio = (float)src_w / (float)dst_w;
  181. float height_ratio = (float)src_h / (float)dst_h;
  182. for (size_t i = 0; i < bboxes.size(); i++)
  183. {
  184. const BoxInfo& bbox = bboxes[i];
  185. cv::Scalar color = cv::Scalar(color_list[bbox.label][0], color_list[bbox.label][1], color_list[bbox.label][2]);
  186. //fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f %.2f\n", bbox.label, bbox.score,
  187. // bbox.x1, bbox.y1, bbox.x2, bbox.y2);
  188. cv::rectangle(image, cv::Rect(cv::Point((bbox.x1 - effect_roi.x) * width_ratio, (bbox.y1 - effect_roi.y) * height_ratio),
  189. cv::Point((bbox.x2 - effect_roi.x) * width_ratio, (bbox.y2 - effect_roi.y) * height_ratio)), color);
  190. char text[256];
  191. sprintf(text, "%s %.1f%%", class_names[bbox.label], bbox.score * 100);
  192. int baseLine = 0;
  193. cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, &baseLine);
  194. int x = (bbox.x1 - effect_roi.x) * width_ratio;
  195. int y = (bbox.y1 - effect_roi.y) * height_ratio - label_size.height - baseLine;
  196. if (y < 0)
  197. y = 0;
  198. if (x + label_size.width > image.cols)
  199. x = image.cols - label_size.width;
  200. cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
  201. color, -1);
  202. cv::putText(image, text, cv::Point(x, y + label_size.height),
  203. cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 255, 255));
  204. }
  205. cv::imshow("image", image);
  206. }
  207. void imageCallback(const sensor_msgs::ImageConstPtr &msg)
  208. {
  209. cv::Mat img;
  210. cv_bridge::CvImageConstPtr cv_ptr;
  211. cv_ptr = cv_bridge::toCvShare(msg, "bgr8");
  212. img = cv_ptr->image;
  213. object_rect effect_roi;
  214. cv::Mat resized_img;
  215. resize_uniform(img, resized_img, cv::Size(320, 320), effect_roi);
  216. auto results = detector.detect(resized_img, 0.4, 0.5);
  217. draw_bboxes(img, results, effect_roi);
  218. cv::waitKey(10);
  219. }
  220. int main(int argc, char **argv)
  221. {
  222. //创建node第一步需要用到的函数
  223. ros::init(argc, argv, "nanodet_m"); //第3个参数为本节点名,
  224. //ros::NodeHandle : 和topic、service、param等交互的公共接口
  225. //创建ros::NodeHandle对象,也就是节点的句柄,它可以用来创建Publisher、Subscriber以及做其他事情。
  226. //句柄(Handle)这个概念可以理解为一个“把手”,你握住了门把手,就可以很容易把整扇门拉开,而不必关心门是
  227. //什么样子。NodeHandle就是对节点资源的描述,有了它你就可以操作这个节点了,比如为程序提供服务、监听某个topic上的消息、访问和修改param等等。
  228. ros::NodeHandle nd; //实例化句柄,初始化node
  229. // Create a ROS subscriber for the input point cloud
  230. //ros::Subscriber sub = nd.subscribe("/camera1/depth/color/points", 1, pclCloudCallback);
  231. ros::Subscriber sub = nd.subscribe("/camera/color/image_raw", 1, imageCallback);
  232. std::cout << "sub:" << sub << std::endl;
  233. // Create a ROS publisher for the output point cloud
  234. pub = nd.advertise<std_msgs::String>("nanodet_out", 1);
  235. ros::spin();
  236. return 0;
  237. }

4.2 CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.0.2)
  2. project(nanodet_pkg)
  3. # Compile as C++11, supported in ROS Kinetic and newer
  4. add_compile_options(-std=c++11)
  5. ## Find catkin macros and libraries
  6. ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
  7. ## is used, also find other catkin packages
  8. find_package(catkin REQUIRED COMPONENTS
  9. roscpp
  10. rospy
  11. std_msgs
  12. cv_bridge
  13. )
  14. find_package(InferenceEngine REQUIRED)
  15. find_package(ngraph REQUIRED)
  16. catkin_package(
  17. # INCLUDE_DIRS include
  18. # LIBRARIES julei_pkg
  19. # CATKIN_DEPENDS roscpp rospy std_msgs
  20. # DEPENDS system_lib
  21. )
  22. include_directories(
  23. # include
  24. ${catkin_INCLUDE_DIRS}
  25. )
  26. add_executable(nanodet main.cpp nanodet_openvino.cpp)
  27. target_link_libraries(
  28. nanodet
  29. ${InferenceEngine_LIBRARIES}
  30. ${NGRAPH_LIBRARIES}
  31. ${catkin_LIBRARIES}
  32. )

4.3 我的cv_bridge模块修改的内容

具体修改位置和方法见第3章。

  1. ......
  2. if(NOT "/opt/intel/openvino_2021/opencv/include;/opt/intel/openvino_2021/opencv/include/opencv2" STREQUAL " ")
  3. set(cv_bridge_INCLUDE_DIRS "")
  4. set(_include_dirs "/opt/intel/openvino_2021/opencv/include;/opt/intel/openvino_2021/opencv/include/opencv2")
  5. ......
  6. set(libraries "cv_bridge;/opt/intel/openvino_2021/opencv/lib/libopencv_core.so.4.5.3;/opt/intel/openvino_2021/opencv/lib/libopencv_imgproc.so.4.5.3;/opt/intel/openvino_2021/opencv/lib/libopencv_imgcodecs.so.4.5.3;/opt/intel/openvino_2021/opencv/lib/libopencv_calib3d.so.4.5.3;/opt/intel/openvino_2021/opencv/lib/libopencv_highgui.so.4.5.3")
  7. ......

4.4 我的~/.bashrc中内容

  1. #这一段是openvino安装后需要写入的。
  2. source /opt/intel/openvino_2021/bin/setupvars.sh
  3. source /opt/intel/openvino_2021/opencv/setupvars.sh
  4. source /home/ym/dream2021/xidiji/catkin_ym/devel/setup.bash

5 效果图

测试方法:

  • 第1步:启动realsense相机
    • 新建一个终端,输入:roslaunch realsense2_camera rs_camera.launch
  • 第2步:运行nanodet程序
    • 再新建一个终端,输入:rosrun nanodet_pkg nanodet

然后,就能看到一个实时检测窗口出现,效果如下:

 

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

闽ICP备14008679号