people_detect_nodelet.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, Kei Okada.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Kei Okada nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 // https://github.com/Itseez/opencv/blob/2.4/samples/cpp/peopledetect.cpp
41 #include <ros/ros.h>
42 #include "opencv_apps/nodelet.h"
44 #include <cv_bridge/cv_bridge.h>
46 
47 #include <opencv2/highgui/highgui.hpp>
48 #include <opencv2/imgproc/imgproc.hpp>
49 #include <opencv2/objdetect/objdetect.hpp>
50 
51 #include <dynamic_reconfigure/server.h>
52 #include "opencv_apps/PeopleDetectConfig.h"
53 #include "opencv_apps/Rect.h"
54 #include "opencv_apps/RectArrayStamped.h"
55 
56 namespace opencv_apps
57 {
58 class PeopleDetectNodelet : public opencv_apps::Nodelet
59 {
64 
66 
67  typedef opencv_apps::PeopleDetectConfig Config;
68  typedef dynamic_reconfigure::Server<Config> ReconfigureServer;
71 
72  int queue_size_;
73  bool debug_view_;
75 
76  std::string window_name_;
77  static bool need_config_update_;
78 
79  cv::HOGDescriptor hog_;
80 
81  double hit_threshold_;
82  int win_stride_;
83  int padding_;
84  double scale0_;
85  int group_threshold_;
86 
87  void reconfigureCallback(Config& new_config, uint32_t level)
88  {
89  config_ = new_config;
90  hit_threshold_ = config_.hit_threshold;
91  win_stride_ = config_.win_stride;
92  padding_ = config_.padding;
93  scale0_ = config_.scale0;
94  group_threshold_ = config_.group_threshold;
95  }
96 
97  const std::string& frameWithDefault(const std::string& frame, const std::string& image_frame)
98  {
99  if (frame.empty())
100  return image_frame;
101  return frame;
102  }
103 
104  void imageCallbackWithInfo(const sensor_msgs::ImageConstPtr& msg, const sensor_msgs::CameraInfoConstPtr& cam_info)
105  {
106  doWork(msg, cam_info->header.frame_id);
107  }
108 
109  void imageCallback(const sensor_msgs::ImageConstPtr& msg)
110  {
111  doWork(msg, msg->header.frame_id);
112  }
113 
114  static void trackbarCallback(int /*unused*/, void* /*unused*/)
115  {
116  need_config_update_ = true;
117  }
118 
119  void doWork(const sensor_msgs::ImageConstPtr& msg, const std::string& input_frame_from_msg)
120  {
121  // Work on the image.
122  try
123  {
124  // Convert the image into something opencv can handle.
126 
127  // Messages
128  opencv_apps::RectArrayStamped found_msg;
129  found_msg.header = msg->header;
130 
131  // Do the work
133  {
134  cv::namedWindow(window_name_, cv::WINDOW_AUTOSIZE);
135  }
136 
137  std::vector<cv::Rect> found, found_filtered;
138  double t = (double)cv::getTickCount();
139  // run the detector with default parameters. to get a higher hit-rate
140  // (and more false alarms, respectively), decrease the hitThreshold and
141  // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
142  hog_.detectMultiScale(frame, found, hit_threshold_, cv::Size(win_stride_, win_stride_),
144  t = (double)cv::getTickCount() - t;
145  NODELET_INFO("tdetection time = %gms", t * 1000. / cv::getTickFrequency());
146  size_t i, j;
147  for (i = 0; i < found.size(); i++)
148  {
149  cv::Rect r = found[i];
150  for (j = 0; j < found.size(); j++)
151  if (j != i && (r & found[j]) == r)
152  break;
153  if (j == found.size())
154  found_filtered.push_back(r);
155  }
156  for (i = 0; i < found_filtered.size(); i++)
157  {
158  cv::Rect r = found_filtered[i];
159  // the HOG detector returns slightly larger rectangles than the real objects.
160  // so we slightly shrink the rectangles to get a nicer output.
161  r.x += cvRound(r.width * 0.1);
162  r.width = cvRound(r.width * 0.8);
163  r.y += cvRound(r.height * 0.07);
164  r.height = cvRound(r.height * 0.8);
165  cv::rectangle(frame, r.tl(), r.br(), cv::Scalar(0, 255, 0), 3);
166 
167  opencv_apps::Rect rect_msg;
168  rect_msg.x = r.x;
169  rect_msg.y = r.y;
170  rect_msg.width = r.width;
171  rect_msg.height = r.height;
172  found_msg.rects.push_back(rect_msg);
173  }
174 
175  //-- Show what you got
176  if (debug_view_)
177  {
178  cv::imshow(window_name_, frame);
179  int c = cv::waitKey(1);
180  }
181 
182  // Publish the image.
183  sensor_msgs::Image::Ptr out_img = cv_bridge::CvImage(msg->header, msg->encoding, frame).toImageMsg();
184  img_pub_.publish(out_img);
185  msg_pub_.publish(found_msg);
186  }
187  catch (cv::Exception& e)
188  {
189  NODELET_ERROR("Image processing error: %s %s %s %i", e.err.c_str(), e.func.c_str(), e.file.c_str(), e.line);
190  }
191 
192  prev_stamp_ = msg->header.stamp;
193  }
194 
195  void subscribe() // NOLINT(modernize-use-override)
196  {
197  NODELET_DEBUG("Subscribing to image topic.");
198  if (config_.use_camera_info)
199  cam_sub_ = it_->subscribeCamera("image", queue_size_, &PeopleDetectNodelet::imageCallbackWithInfo, this);
200  else
201  img_sub_ = it_->subscribe("image", queue_size_, &PeopleDetectNodelet::imageCallback, this);
202  }
203 
204  void unsubscribe() // NOLINT(modernize-use-override)
205  {
206  NODELET_DEBUG("Unsubscribing from image topic.");
207  img_sub_.shutdown();
208  cam_sub_.shutdown();
209  }
210 
211 public:
212  virtual void onInit() // NOLINT(modernize-use-override)
213  {
214  Nodelet::onInit();
216 
217  pnh_->param("queue_size", queue_size_, 3);
218  pnh_->param("debug_view", debug_view_, false);
219  if (debug_view_)
220  {
221  always_subscribe_ = true;
222  }
223  prev_stamp_ = ros::Time(0, 0);
224 
225  window_name_ = "people detector";
226 
227  reconfigure_server_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
228  dynamic_reconfigure::Server<Config>::CallbackType f =
230  reconfigure_server_->setCallback(f);
231 
232  hog_.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
233 
234  img_pub_ = advertiseImage(*pnh_, "image", 1);
235  msg_pub_ = advertise<opencv_apps::RectArrayStamped>(*pnh_, "found", 1);
236 
238  }
239 };
241 } // namespace opencv_apps
242 
243 namespace people_detect
244 {
246 {
247 public:
248  virtual void onInit() // NOLINT(modernize-use-override)
249  {
250  ROS_WARN("DeprecationWarning: Nodelet people_detect/people_detect is deprecated, "
251  "and renamed to opencv_apps/people_detect.");
253  }
254 };
255 } // namespace people_detect
256 
257 #ifdef USE_PLUGINLIB_CLASS_LIST_MACROS_H
259 #else
261 #endif
nodelet.h
opencv_apps::PeopleDetectNodelet::trackbarCallback
static void trackbarCallback(int, void *)
Definition: people_detect_nodelet.cpp:178
opencv_apps::PeopleDetectNodelet::doWork
void doWork(const sensor_msgs::ImageConstPtr &msg, const std::string &input_frame_from_msg)
Definition: people_detect_nodelet.cpp:183
NODELET_ERROR
#define NODELET_ERROR(...)
opencv_apps::Nodelet::onInitPostProcess
virtual void onInitPostProcess()
Post processing of initialization of nodelet. You need to call this method in order to use always_sub...
Definition: nodelet.cpp:77
ros::Publisher
image_encodings.h
image_transport::ImageTransport
opencv_apps::PeopleDetectNodelet::window_name_
std::string window_name_
Definition: people_detect_nodelet.cpp:140
opencv_apps::PeopleDetectNodelet::padding_
int padding_
Definition: people_detect_nodelet.cpp:147
boost::shared_ptr< image_transport::ImageTransport >
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
opencv_apps::PeopleDetectNodelet::cam_sub_
image_transport::CameraSubscriber cam_sub_
Definition: people_detect_nodelet.cpp:126
opencv_apps::PeopleDetectNodelet::hog_
cv::HOGDescriptor hog_
Definition: people_detect_nodelet.cpp:143
opencv_apps::PeopleDetectNodelet::onInit
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method.
Definition: people_detect_nodelet.cpp:276
ros.h
opencv_apps::PeopleDetectNodelet::group_threshold_
int group_threshold_
Definition: people_detect_nodelet.cpp:149
boost::placeholders::_1
boost::arg< 1 > _1
Definition: nodelet.cpp:44
opencv_apps::PeopleDetectNodelet::unsubscribe
void unsubscribe()
This method is called when publisher is unsubscribed by other nodes. Shut down subscribers in this me...
Definition: people_detect_nodelet.cpp:268
opencv_apps::Nodelet
Nodelet to automatically subscribe/unsubscribe topics according to subscription of advertised topics.
Definition: nodelet.h:90
opencv_apps::PeopleDetectNodelet::reconfigureCallback
void reconfigureCallback(Config &new_config, uint32_t level)
Definition: people_detect_nodelet.cpp:151
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
opencv_apps::PeopleDetectNodelet::win_stride_
int win_stride_
Definition: people_detect_nodelet.cpp:146
opencv_apps::PeopleDetectNodelet::debug_view_
bool debug_view_
Definition: people_detect_nodelet.cpp:137
f
f
opencv_apps::PeopleDetectNodelet::config_
Config config_
Definition: people_detect_nodelet.cpp:133
image_transport::Subscriber
opencv_apps::Nodelet::advertiseImage
image_transport::Publisher advertiseImage(ros::NodeHandle &nh, const std::string &topic, int queue_size)
Advertise an image topic and watch the publisher. Publishers which are created by this method....
Definition: nodelet.h:201
class_list_macros.h
opencv_apps::PeopleDetectNodelet::imageCallback
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
Definition: people_detect_nodelet.cpp:173
people_detect::PeopleDetectNodelet
Definition: people_detect_nodelet.cpp:245
opencv_apps::PeopleDetectNodelet::subscribe
void subscribe()
This method is called when publisher is subscribed by other nodes. Set up subscribers in this method.
Definition: people_detect_nodelet.cpp:259
image_transport::CameraSubscriber
opencv_apps::PeopleDetectNodelet::reconfigure_server_
boost::shared_ptr< ReconfigureServer > reconfigure_server_
Definition: people_detect_nodelet.cpp:134
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(opencv_apps::PeopleDetectNodelet, nodelet::Nodelet)
opencv_apps::PeopleDetectNodelet::msg_pub_
ros::Publisher msg_pub_
Definition: people_detect_nodelet.cpp:127
image_transport::CameraSubscriber::shutdown
void shutdown()
opencv_apps::PeopleDetectNodelet::img_pub_
image_transport::Publisher img_pub_
Definition: people_detect_nodelet.cpp:124
boost::placeholders::_2
boost::arg< 2 > _2
Definition: nodelet.cpp:45
opencv_apps::Nodelet::always_subscribe_
bool always_subscribe_
A flag to disable watching mechanism and always subscribe input topics. It can be specified via ~alwa...
Definition: nodelet.h:300
ROS_WARN
#define ROS_WARN(...)
opencv_apps::PeopleDetectNodelet::queue_size_
int queue_size_
Definition: people_detect_nodelet.cpp:136
image_transport::Publisher::publish
void publish(const sensor_msgs::Image &message) const
opencv_apps
Demo code to calculate moments.
Definition: nodelet.h:68
opencv_apps::PeopleDetectNodelet::scale0_
double scale0_
Definition: people_detect_nodelet.cpp:148
opencv_apps::PeopleDetectNodelet::hit_threshold_
double hit_threshold_
Definition: people_detect_nodelet.cpp:145
opencv_apps::PeopleDetectNodelet::frameWithDefault
const std::string & frameWithDefault(const std::string &frame, const std::string &image_frame)
Definition: people_detect_nodelet.cpp:161
image_transport.h
opencv_apps::PeopleDetectNodelet::prev_stamp_
ros::Time prev_stamp_
Definition: people_detect_nodelet.cpp:138
people_detect
Definition: people_detect_nodelet.cpp:243
NODELET_INFO
#define NODELET_INFO(...)
opencv_apps::PeopleDetectNodelet
Definition: people_detect_nodelet.cpp:90
opencv_apps::PeopleDetectNodelet::img_sub_
image_transport::Subscriber img_sub_
Definition: people_detect_nodelet.cpp:125
face_recognition_trainer.t
t
Definition: face_recognition_trainer.py:96
nodelet::Nodelet
ros::Time
opencv_apps::PeopleDetectNodelet::ReconfigureServer
dynamic_reconfigure::Server< Config > ReconfigureServer
Definition: people_detect_nodelet.cpp:132
image_transport::Publisher
cv_bridge.h
cv_bridge::CvImage
class_list_macros.hpp
opencv_apps::PeopleDetectNodelet::Config
opencv_apps::PeopleDetectConfig Config
Definition: people_detect_nodelet.cpp:131
sensor_msgs::image_encodings::BGR8
const std::string BGR8
cv_bridge::toCvShare
CvImageConstPtr toCvShare(const sensor_msgs::Image &source, const boost::shared_ptr< void const > &tracked_object, const std::string &encoding=std::string())
opencv_apps::PeopleDetectNodelet::need_config_update_
static bool need_config_update_
Definition: people_detect_nodelet.cpp:141
opencv_apps::PeopleDetectNodelet::it_
boost::shared_ptr< image_transport::ImageTransport > it_
Definition: people_detect_nodelet.cpp:129
opencv_apps::Nodelet::nh_
boost::shared_ptr< ros::NodeHandle > nh_
Shared pointer to nodehandle.
Definition: nodelet.h:272
opencv_apps::PeopleDetectNodelet::imageCallbackWithInfo
void imageCallbackWithInfo(const sensor_msgs::ImageConstPtr &msg, const sensor_msgs::CameraInfoConstPtr &cam_info)
Definition: people_detect_nodelet.cpp:168
opencv_apps::Nodelet::onInit
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method.
Definition: nodelet.cpp:60
opencv_apps::Nodelet::pnh_
boost::shared_ptr< ros::NodeHandle > pnh_
Shared pointer to private nodehandle.
Definition: nodelet.h:277
NODELET_DEBUG
#define NODELET_DEBUG(...)
people_detect::PeopleDetectNodelet::onInit
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method.
Definition: people_detect_nodelet.cpp:248
image_transport::Subscriber::shutdown
void shutdown()


opencv_apps
Author(s): Kei Okada
autogenerated on Fri May 23 2025 02:49:49