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  

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