stream_classification.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/Object.h>
23 #include <object_msgs/Objects.h>
24 
25 #define LINESPACING 50
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::Objects::ConstPtr& objs)
49 {
50  cv::Mat cvImage = cv_bridge::toCvShare(img, "bgr8")->image;
51  int cnt = 0;
52 
53  for (auto obj : objs->objects_vector)
54  {
55  std::stringstream ss;
56  ss << obj.object_name << ": " << obj.probability * 100 << '%';
57  cv::putText(cvImage, ss.str(), cvPoint(LINESPACING, LINESPACING * (++cnt)), cv::FONT_HERSHEY_SIMPLEX, 1,
58  cv::Scalar(0, 255, 0));
59  }
60 
61  std::stringstream ss;
62  int fps = getFPS();
63  ss << "FPS: " << fps;
64 
65  cv::putText(cvImage, ss.str(), cvPoint(LINESPACING, LINESPACING * (++cnt)), cv::FONT_HERSHEY_SIMPLEX, 1,
66  cv::Scalar(0, 255, 0));
67  cv::imshow("image_viewer", cvImage);
68 
69  int key = cv::waitKey(5);
70  if (key == 13 || key == 27 || key == 32 || key == 113)
71  {
72  ros::shutdown();
73  }
74 }
75 
76 int main(int argc, char** argv)
77 {
78  ros::init(argc, argv, "movidius_ncs_example_stream");
79  ros::NodeHandle nh;
80 
81  message_filters::Subscriber<sensor_msgs::Image> camSub(nh, "/camera/color/image_raw", 1);
82  message_filters::Subscriber<object_msgs::Objects> objSub(nh, "/movidius_ncs_nodelet/classified_objects", 1);
83 
84  message_filters::TimeSynchronizer<sensor_msgs::Image, object_msgs::Objects> sync(camSub, objSub, 60);
85  sync.registerCallback(boost::bind(&syncCb, _1, _2));
86 
87  ros::spin();
88  return 0;
89 }
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)
void syncCb(const sensor_msgs::ImageConstPtr &img, const object_msgs::Objects::ConstPtr &objs)
ROSCPP_DECL void spin()
int getFPS()
#define LINESPACING
ROSCPP_DECL void shutdown()


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