stream_detection.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cv_bridge/cv_bridge.h>
18 #include <message_filters/subscriber.h>
19 #include <message_filters/time_synchronizer.h>
20 #include <opencv2/opencv.hpp>
21 #include <ros/ros.h>
22 #include <object_msgs/ObjectInBox.h>
23 #include <object_msgs/ObjectsInBoxes.h>
24 
25 #define LINESPACING 20
26 
27 int getFPS()
28 {
29  static int fps = 0;
30  static boost::posix_time::ptime duration_start = boost::posix_time::microsec_clock::local_time();
31  static int frame_cnt = 0;
32 
33  frame_cnt++;
34 
35  boost::posix_time::ptime current = boost::posix_time::microsec_clock::local_time();
36  boost::posix_time::time_duration msdiff = current - duration_start;
37 
38  if (msdiff.total_milliseconds() > 1000)
39  {
40  fps = frame_cnt;
41  frame_cnt = 0;
42  duration_start = current;
43  }
44 
45  return fps;
46 }
47 
48 void syncCb(const sensor_msgs::ImageConstPtr& img, const object_msgs::ObjectsInBoxes::ConstPtr& objs_in_boxes)
49 {
50  cv::Mat cvImage = cv_bridge::toCvShare(img, "bgr8")->image;
51  int width = img->width;
52  int height = img->height;
53 
54  for (auto obj : objs_in_boxes->objects_vector)
55  {
56  std::stringstream ss;
57  ss << obj.object.object_name << ": " << obj.object.probability * 100 << '%';
58 
59  int xmin = obj.roi.x_offset;
60  int ymin = obj.roi.y_offset;
61  int w = obj.roi.width;
62  int h = obj.roi.height;
63 
64  int xmax = ((xmin + w) < width) ? (xmin + w) : width;
65  int ymax = ((ymin + h) < height) ? (ymin + h) : height;
66 
67  cv::Point left_top = cv::Point(xmin, ymin);
68  cv::Point right_bottom = cv::Point(xmax, ymax);
69  cv::rectangle(cvImage, left_top, right_bottom, cv::Scalar(0, 255, 0), 1, cv::LINE_8, 0);
70  cv::rectangle(cvImage, cvPoint(xmin, ymin), cvPoint(xmax, ymin + 20), cv::Scalar(0, 255, 0), -1);
71  cv::putText(cvImage, ss.str(), cvPoint(xmin + 5, ymin + 20), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 255), 1);
72  }
73 
74  std::stringstream ss;
75  int fps = getFPS();
76  ss << "FPS: " << fps;
77 
78  cv::putText(cvImage, ss.str(), cvPoint(LINESPACING, LINESPACING), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0));
79  cv::imshow("image_viewer", cvImage);
80 
81  int key = cv::waitKey(5);
82  if (key == 13 || key == 27 || key == 32 || key == 113)
83  {
84  ros::shutdown();
85  }
86 }
87 
88 int main(int argc, char** argv)
89 {
90  ros::init(argc, argv, "movidius_ncs_example_stream");
91  ros::NodeHandle nh;
92 
93  message_filters::Subscriber<sensor_msgs::Image> camSub(nh, "/camera/color/image_raw", 1);
94  message_filters::Subscriber<object_msgs::ObjectsInBoxes> objSub(nh, "/movidius_ncs_nodelet/detected_objects", 1);
95  message_filters::TimeSynchronizer<sensor_msgs::Image, object_msgs::ObjectsInBoxes> sync(camSub, objSub, 60);
96  sync.registerCallback(boost::bind(&syncCb, _1, _2));
97 
98  ros::spin();
99  return 0;
100 }
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
#define LINESPACING
ROSCPP_DECL void spin()
void syncCb(const sensor_msgs::ImageConstPtr &img, const object_msgs::ObjectsInBoxes::ConstPtr &objs_in_boxes)
ROSCPP_DECL void shutdown()
int getFPS()


movidius_ncs_example
Author(s): Xiaojun Huang
autogenerated on Mon Jun 10 2019 14:11:21