ROS下使用intel Realsense摄像头进行人脸检测

转载地址:http://blog.csdn.net/may0324/article/details/50984752

使用准备条件:

ROS-indigo 

intel Realsense摄像头(我使用的依旧是R200)

确保已经正常安装驱动,安装方法见博文

http://blog.csdn.net/may0324/article/details/50981540


1.首先到github下载ros-realsense源码包,该包包含已经定义好的packages和nodes

https://github.com/intel-ros/realsense

2.新建工作区目录,如

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mkdir workspace  

3.进入创建的工作区目录,病创建文件目录,命名为src

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. cd workspace  
  2. mkdir src  
4.进入src目录,并将下载包中的camera解压到此目录内

5.部署完成后就可以编译了,在workspace目录下执行命令

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. catkin_make  
6.编译成功后,执行
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. source devel/setup.bash  
这个自动生成的脚本文件设置了若干环境变量,从而使 ROS 能够找到你创建的功能包和新生成的可执行文件

7.完成后就可以执行包中自带的launch文件打开摄像头了

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. roslaunch realsense_camera realsense_r200_nodelet_standalone_preset.launch   

启动后发现摄像头灯亮起,表明在工作,然而并没有什么新奇的东西发生,这是为什么呢?

其实是因为该节点只是在不断的发布消息(就是不同形式的图像信息,如RGB,红外,深度,点云等),但是并没有节点订阅该消息,所以为了观看摄像头拍摄的图像,我们需要再写一个节点订阅该消息,并将图像显示出来,接着再进行人脸检测等后续功能。为了以后功能拓展方便,我们直接写个新的功能包(package)。

这里我们定义的场景是,该功能节点订阅realsense发布的图像消息,解码并显示出来,同时调用OpenCV的haar分类器进行人脸检测,并将检测到的人脸封装成消息发布出去,所以这个节点本身是个订阅者(subscriber),同时也是个发布者(publisher)。

1.进入workspace的src目录内,创建包目录

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. catkin_create_pkg client std_msgs rospy roscpp  
这里面 std_msgs rospy roscpp 是我们通常一个最基本的C++包之中需要的依赖项。后面这些依赖项均可以通过配置 package.xml 进行更改

2.进入创建的包client目录中,创建msg文件夹,并创建需要发布的人脸消息文件facebox.msg和faces.msg:

facebox.msg

uint16 top
uint16 left
uint16 width
uint16 height

faces.msg

facebox[] face_boxes
uint16 image_width
uint16 image_height

3. 进入client/src目录中,创建主程序文件client.cpp,并写入如下内容

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <ros/ros.h>  
  2. #include <image_transport/image_transport.h>  
  3. #include <cv_bridge/cv_bridge.h>  
  4. #include <sensor_msgs/image_encodings.h>  
  5. #include <opencv2/objdetect/objdetect.hpp>  
  6. #include <opencv2/imgproc/imgproc.hpp>  
  7. #include <opencv2/highgui/highgui.hpp>  
  8. #include <std_msgs/String.h>  
  9.   
  10. #include <client/faces.h>  
  11. #include <client/facebox.h>  
  12.   
  13. using namespace std;  
  14. using namespace cv ;  
  15.   
  16. CascadeClassifier face_cascade ;  
  17. bool showResult = true ;  
  18. ros::Publisher pub;  
  19. int frame_id = 0 ;  
  20.   
  21. vector<Rect> detectFaces(Mat frame) {  
  22.     vector<Rect> faces;  
  23.     Mat bufferMat;  
  24.     cvtColor(frame, bufferMat, COLOR_BGR2GRAY);  
  25.     equalizeHist(bufferMat, bufferMat);  
  26.     face_cascade.detectMultiScale(bufferMat, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));  
  27.     return faces;  
  28. }  
  29.   
  30. void imageCB(const sensor_msgs::ImageConstPtr& msg) {  
  31.     cv_bridge::CvImagePtr cvPtr;  
  32.     try {  
  33.         cvPtr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);  
  34.     } catch (cv_bridge::Exception& e) {  
  35.         ROS_ERROR("cv_bridge exception: %s", e.what());  
  36.         return;  
  37.     }  
  38.   
  39.     vector<Rect> faces = detectFaces(cvPtr->image);  
  40.     client::faces faces_msg;  
  41.     client::facebox _facebox;  
  42.     faces_msg.image_width = cvPtr->image.cols;  
  43.     faces_msg.image_height = cvPtr->image.rows;  
  44.   
  45.     for (int i = 0; i < faces.size(); i++) {  
  46.         _facebox.top = faces[i].y;  
  47.         _facebox.left = faces[i].x;  
  48.         _facebox.width = faces[i].width;  
  49.         _facebox.height = faces[i].height;  
  50.         faces_msg.face_boxes.push_back(_facebox);  
  51.         if (showResult)  
  52.             rectangle(cvPtr->image, faces[i], CV_RGB(100, 100, 255), 1);  
  53.     }   
  54.       
  55.     frame_id++ ;  
  56.   
  57.     pub.publish(faces_msg);  
  58.     if (showResult) {  
  59.         imshow("Live Feed", cvPtr->image);  
  60.         waitKey(3);  
  61.     }  
  62. }  
  63.   
  64.   
  65. int main(int argc, char **argv) {  
  66.    face_cascade.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml");  
  67.     ros::init(argc, argv, "client");  
  68.     ros::NodeHandle nh;  
  69.     nh.param("/client/showResult", showResult, true);  
  70.     pub = nh.advertise<client::faces>("/faces", 5);  
  71.     ros::Subscriber sub = nh.subscribe("/camera/color/image_raw", 1, imageCB);  
  72.     ros::spin();  
  73. }  

4.修改package.xml,添加所需依赖项

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <buildtool_depend>catkin</buildtool_depend>  
  2. <build_depend>roscpp</build_depend>  
  3. <build_depend>rospy</build_depend>  
  4. <build_depend>std_msgs</build_depend>  
  5. <build_depend>cv_bridge</build_depend>  
  6. <build_depend>image_transport</build_depend>  
  7. <build_depend>message_generation</build_depend>  
  8. <build_depend>sensor_msgs</build_depend>  
  9. <build_depend>realsense_camera</build_depend>  
  10. <run_depend>cv_bridge</run_depend>  
  11. <run_depend>image_transport</run_depend>  
  12. <run_depend>roscpp</run_depend>  
  13. <run_depend>rospy</run_depend>  
  14. <run_depend>std_msgs</run_depend>  
  15. <run_depend>sensor_msgs</run_depend>  
  16. <run_depend>message_runtime</run_depend>  
  17. <run_depend>realsense_camera</run_depend>  
其中的realsense_camera就是我们要依赖的realsen_camera库

5.修改CMakeLists.txt,文件内容如下

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. cmake_minimum_required(VERSION 2.8.3)  
  2. project(client)  
  3.   
  4. ## Find catkin macros and libraries  
  5. ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)  
  6. ## is used, also find other catkin packages  
  7. find_package(catkin REQUIRED COMPONENTS  
  8.   cv_bridge  
  9.   image_transport  
  10.   roscpp  
  11.   rospy  
  12.   std_msgs  
  13.   sensor_msgs  
  14.   message_generation  
  15.   realsense_camera  
  16. )  
  17. find_package(OpenCV REQUIRED)  
  18. ## System dependencies are found with CMake's conventions  
  19. # find_package(Boost REQUIRED COMPONENTS system)  
  20.   
  21.   
  22. ## Uncomment this if the package has a setup.py. This macro ensures  
  23. ## modules and global scripts declared therein get installed  
  24. ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html  
  25. # catkin_python_setup()  
  26.   
  27. ################################################  
  28. ## Declare ROS messages, services and actions ##  
  29. ################################################  
  30.   
  31. ## To declare and build messages, services or actions from within this  
  32. ## package, follow these steps:  
  33. ## * Let MSG_DEP_SET be the set of packages whose message types you use in  
  34. ##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).  
  35. ## * In the file package.xml:  
  36. ##   * add a build_depend tag for "message_generation"  
  37. ##   * add a build_depend and a run_depend tag for each package in MSG_DEP_SET  
  38. ##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in  
  39. ##     but can be declared for certainty nonetheless:  
  40. ##     * add a run_depend tag for "message_runtime"  
  41. ## * In this file (CMakeLists.txt):  
  42. ##   * add "message_generation" and every package in MSG_DEP_SET to  
  43. ##     find_package(catkin REQUIRED COMPONENTS ...)  
  44. ##   * add "message_runtime" and every package in MSG_DEP_SET to  
  45. ##     catkin_package(CATKIN_DEPENDS ...)  
  46. ##   * uncomment the add_*_files sections below as needed  
  47. ##     and list every .msg/.srv/.action file to be processed  
  48. ##   * uncomment the generate_messages entry below  
  49. ##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)  
  50.   
  51. ## Generate messages in the 'msg' folder  
  52.  add_message_files(  
  53.    FILES  
  54. #   Message1.msg  
  55.    facebox.msg  
  56.    faces.msg  
  57.  )  
  58.   
  59. ## Generate services in the 'srv' folder  
  60. # add_service_files(  
  61. #   FILES  
  62. #   Service1.srv  
  63. #   Service2.srv  
  64. # )  
  65.   
  66. ## Generate actions in the 'action' folder  
  67. # add_action_files(  
  68. #   FILES  
  69. #   Action1.action  
  70. #   Action2.action  
  71. # )  
  72.   
  73. ## Generate added messages and services with any dependencies listed here  
  74.  generate_messages(  
  75.    DEPENDENCIES  
  76.    std_msgs  
  77.    geometry_msgs  
  78.  )  
  79.   
  80. ################################################  
  81. ## Declare ROS dynamic reconfigure parameters ##  
  82. ################################################  
  83.   
  84. ## To declare and build dynamic reconfigure parameters within this  
  85. ## package, follow these steps:  
  86. ## * In the file package.xml:  
  87. ##   * add a build_depend and a run_depend tag for "dynamic_reconfigure"  
  88. ## * In this file (CMakeLists.txt):  
  89. ##   * add "dynamic_reconfigure" to  
  90. ##     find_package(catkin REQUIRED COMPONENTS ...)  
  91. ##   * uncomment the "generate_dynamic_reconfigure_options" section below  
  92. ##     and list every .cfg file to be processed  
  93.   
  94. ## Generate dynamic reconfigure parameters in the 'cfg' folder  
  95. # generate_dynamic_reconfigure_options(  
  96. #   cfg/DynReconf1.cfg  
  97. #   cfg/DynReconf2.cfg  
  98. # )  
  99.   
  100. ###################################  
  101. ## catkin specific configuration ##  
  102. ###################################  
  103. ## The catkin_package macro generates cmake config files for your package  
  104. ## Declare things to be passed to dependent projects  
  105. ## INCLUDE_DIRS: uncomment this if you package contains header files  
  106. ## LIBRARIES: libraries you create in this project that dependent projects also need  
  107. ## CATKIN_DEPENDS: catkin_packages dependent projects also need  
  108. ## DEPENDS: system dependencies of this project that dependent projects also need  
  109. catkin_package(  
  110.   #INCLUDE_DIRS include  
  111.   #LIBRARIES client  
  112.   #CATKIN_DEPENDS roscpp rospy std_msgs  
  113.   #DEPENDS system_lib  
  114.   CATKIN_DEPENDS message_runtime  
  115. )  
  116.   
  117. ###########  
  118. ## Build ##  
  119. ###########  
  120.   
  121. ## Specify additional locations of header files  
  122. ## Your package locations should be listed before other locations  
  123. # include_directories(include)  
  124. include_directories(  
  125.   ${catkin_INCLUDE_DIRS}  
  126.   ${OpenCV_INCLUDE_DIRS}  
  127. )  
  128.   
  129. ## Declare a C++ library  
  130. # add_library(client  
  131. #   src/${PROJECT_NAME}/client.cpp  
  132. # )  
  133.   
  134. ## Add cmake target dependencies of the library  
  135. ## as an example, code may need to be generated before libraries  
  136. ## either from message generation or dynamic reconfigure  
  137. # add_dependencies(client ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})  
  138.   
  139. ## Declare a C++ executable  
  140. # add_executable(client_node src/client_node.cpp)  
  141.   
  142. ## Add cmake target dependencies of the executable  
  143. ## same as for the library above  
  144. # add_dependencies(client_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})  
  145.   
  146. ## Specify libraries to link a library or executable target against  
  147. # target_link_libraries(client_node  
  148. #   ${catkin_LIBRARIES}  
  149. # )  
  150.   
  151. #############  
  152. ## Install ##  
  153. #############  
  154.   
  155. # all install targets should use catkin DESTINATION variables  
  156. # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html  
  157.   
  158. ## Mark executable scripts (Python etc.) for installation  
  159. ## in contrast to setup.py, you can choose the destination  
  160. # install(PROGRAMS  
  161. #   scripts/my_python_script  
  162. #   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}  
  163. # )  
  164.   
  165. ## Mark executables and/or libraries for installation  
  166. # install(TARGETS client client_node  
  167. #   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}  
  168. #   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}  
  169. #   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}  
  170. # )  
  171.   
  172. ## Mark cpp header files for installation  
  173. # install(DIRECTORY include/${PROJECT_NAME}/  
  174. #   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}  
  175. #   FILES_MATCHING PATTERN "*.h"  
  176. #   PATTERN ".svn" EXCLUDE  
  177. # )  
  178.   
  179. ## Mark other files for installation (e.g. launch and bag files, etc.)  
  180. # install(FILES  
  181. #   # myfile1  
  182. #   # myfile2  
  183. #   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}  
  184. # )  
  185.   
  186. #############  
  187. ## Testing ##  
  188. #############  
  189.   
  190. ## Add gtest based cpp test target and link libraries  
  191. # catkin_add_gtest(${PROJECT_NAME}-test test/test_client.cpp)  
  192. # if(TARGET ${PROJECT_NAME}-test)  
  193. #   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})  
  194. # endif()  
  195.   
  196. ## Add folders to be run by python nosetests  
  197. # catkin_add_nosetests(test)  
  198.   
  199. add_executable(  
  200.     client  
  201.     src/client.cpp  
  202. )  
  203. target_link_libraries(  
  204.     client  
  205.     ${catkin_LIBRARIES}  
  206.     ${OpenCV_LIBS}  
  207. )  
6.最后最重要的是修改realsense的camera目录下的CMakeLists.txt文件,因为原文件并没有生成别的功能包所能使用的库,所以如果不修改,由于功能包间是独立的,client功能包将找不到realsense_camera功能包,从而编译错误。红字为添加的部分

find_package(PkgConfig REQUIRED)

add_service_files(
  FILES
  cameraConfiguration.srv
)

#add dynamic reconfigure api
generate_dynamic_reconfigure_options(
  cfg/camera_params.cfg
)

#此段很重要,表明要生成别的功能包所能使用的库
catkin_package(
#  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
)


include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_library(realsense_camera src/realsense_camera_nodelet.cpp)
target_link_libraries(realsense_camera
  ${catkin_LIBRARIES}
  /usr/local/lib/librealsense.so
)
add_dependencies(realsense_camera realsense_camera_generate_messages_cpp ${PROJECT_NAME}_gencfg)

7.全部修改完成后,编译。在client目录中新建launch目录,并在该目录中新建一个.launch文件,名字随便起,内容则是同时启动realsense_camera的节点和client的节点,内容如下:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <launch>  
  2.   
  3.     <arg name="mode" default="preset" />  
  4.     <arg name="enable_depth" default="true" />  
  5.     <arg name="enable_color" default="true" />  
  6.     <arg name="enable_pointcloud" default="true" />  
  7.     <arg name="enable_tf" default="true" />         
  8.       
  9.     <node pkg="nodelet" type="nodelet" name="standalone_nodelet"  args="manager" output="screen"/>  
  10.       
  11.     <node pkg="nodelet" type="nodelet" name="RealsenseNodelet"  
  12.         args="load realsense_camera/RealsenseNodelet standalone_nodelet">  
  13.         <param name="mode"              type="str"  value="$(arg mode)" />  
  14.         <param name="enable_depth"      type="bool" value="$(arg enable_depth)" />  
  15.         <param name="enable_color"      type="bool" value="$(arg enable_color)" />  
  16.         <param name="enable_pointcloud" type="bool" value="$(arg enable_pointcloud)" />  
  17.         <param name="enable_tf"         type="bool" value="$(arg enable_tf)" />  
  18.     </node>  
  19.   
  20.     <node pkg="client" type="client" name="clientnode"  output="screen">  
  21.           <param name="showResult" value="true"/>   
  22.         </node>  
  23. </launch>   
8.最后别忘了source devel/setup.bash。然后就可以启动啦
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. roslaunch client detectface.launch  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章