YoloObjectDetector.hpp
Go to the documentation of this file.
1 /*
2  * YoloObjectDetector.h
3  *
4  * Created on: Dec 19, 2016
5  * Author: Marko Bjelonic
6  * Institute: ETH Zurich, Robotic Systems Lab
7  */
8 
9 #pragma once
10 
11 // c++
12 #include <pthread.h>
13 #include <chrono>
14 #include <cmath>
15 #include <iostream>
16 #include <string>
17 #include <thread>
18 #include <vector>
19 
20 // ROS
22 #include <geometry_msgs/Point.h>
24 #include <ros/ros.h>
25 #include <sensor_msgs/Image.h>
27 #include <std_msgs/Header.h>
28 
29 // OpenCv
30 #include <cv_bridge/cv_bridge.h>
31 #include <opencv2/highgui/highgui.hpp>
32 #include <opencv2/imgproc/imgproc.hpp>
33 #include <opencv2/objdetect/objdetect.hpp>
34 
35 // darknet_ros_msgs
36 #include <darknet_ros_msgs/BoundingBox.h>
37 #include <darknet_ros_msgs/BoundingBoxes.h>
38 #include <darknet_ros_msgs/CheckForObjectsAction.h>
39 #include <darknet_ros_msgs/ObjectCount.h>
40 
41 // Darknet.
42 #ifdef GPU
43 #include "cublas_v2.h"
44 #include "cuda_runtime.h"
45 #include "curand.h"
46 #endif
47 
48 extern "C" {
49 #include <sys/time.h>
50 #include "box.h"
51 #include "cost_layer.h"
52 #include "detection_layer.h"
53 #include "network.h"
54 #include "parser.h"
55 #include "region_layer.h"
56 #include "utils.h"
57 }
58 
59 // Image interface.
61 
62 extern "C" cv::Mat image_to_mat(image im);
63 extern "C" image mat_to_image(cv::Mat m);
64 extern "C" int show_image(image p, const char* name, int ms);
65 
66 namespace darknet_ros {
67 
69 typedef struct {
70  float x, y, w, h, prob;
71  int num, Class;
72 } RosBox_;
73 
74 typedef struct {
75  cv::Mat image;
76  std_msgs::Header header;
78 
80  public:
85 
90 
91  private:
96  bool readParameters();
97 
101  void init();
102 
107  void cameraCallback(const sensor_msgs::ImageConstPtr& msg);
108 
113 
118 
123  bool isCheckingForObjects() const;
124 
129  bool publishDetectionImage(const cv::Mat& detectionImage);
130 
133  using CheckForObjectsActionServerPtr = std::shared_ptr<CheckForObjectsActionServer>;
134 
137 
140  std::vector<std::string> classLabels_;
141 
144 
147 
152 
154  std::vector<std::vector<RosBox_> > rosBoxes_;
155  std::vector<int> rosBoxCounter_;
156  darknet_ros_msgs::BoundingBoxes boundingBoxesResults_;
157 
161 
164 
165  // Yolo running on thread.
166  std::thread yoloThread_;
167 
168  // Darknet.
169  char** demoNames_;
170  image** demoAlphabet_;
172 
173  network* net_;
174  std_msgs::Header headerBuff_[3];
175  image buff_[3];
176  image buffLetter_[3];
177  int buffId_[3];
178  int buffIndex_ = 0;
179  float fps_ = 0;
180  float demoThresh_ = 0;
181  float demoHier_ = .5;
182  int running_ = 0;
183  cv::Mat disp_;
184  int demoDelay_ = 0;
185  int demoFrame_ = 3;
186  float** predictions_;
187  int demoIndex_ = 0;
188  int demoDone_ = 0;
189  float* lastAvg2_;
190  float* lastAvg_;
191  float* avg_;
192  int demoTotal_ = 0;
193  double demoTime_;
194 
200  char* demoPrefix_;
201 
202  std_msgs::Header imageHeader_;
203  cv::Mat camImageCopy_;
204  boost::shared_mutex mutexImageCallback_;
205 
206  bool imageStatus_ = false;
207  boost::shared_mutex mutexImageStatus_;
208 
209  bool isNodeRunning_ = true;
210  boost::shared_mutex mutexNodeStatus_;
211 
213  boost::shared_mutex mutexActionStatus_;
214 
215  // double getWallTime();
216 
217  int sizeNetwork(network* net);
218 
219  void rememberNetwork(network* net);
220 
221  detection* avgPredictions(network* net, int* nboxes);
222 
223  void* detectInThread();
224 
225  void* fetchInThread();
226 
227  void* displayInThread(void* ptr);
228 
229  void* displayLoop(void* ptr);
230 
231  void* detectLoop(void* ptr);
232 
233  void setupNetwork(char* cfgfile, char* weightfile, char* datafile, float thresh, char** names, int classes, int delay, char* prefix,
234  int avg_frames, float hier, int w, int h, int frames, int fullscreen);
235 
236  void yolo();
237 
239 
240  bool getImageStatus(void);
241 
242  bool isNodeRunning(void);
243 
244  void* publishInThread();
245 };
246 
247 } /* namespace darknet_ros*/
Bounding box of the detected object.
image mat_to_image(cv::Mat m)
image_transport::ImageTransport imageTransport_
Advertise and subscribe to image topics.
detection * avgPredictions(network *net, int *nboxes)
std::vector< std::vector< RosBox_ > > rosBoxes_
Detected objects.
std::shared_ptr< CheckForObjectsActionServer > CheckForObjectsActionServerPtr
void cameraCallback(const sensor_msgs::ImageConstPtr &msg)
ros::Publisher detectionImagePublisher_
Publisher of the bounding box image.
int frameWidth_
Camera related parameters.
void setupNetwork(char *cfgfile, char *weightfile, char *datafile, float thresh, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen)
cv::Mat image_to_mat(image im)
std::vector< std::string > classLabels_
darknet_ros_msgs::BoundingBoxes boundingBoxesResults_
CheckForObjectsActionServerPtr checkForObjectsActionServer_
Check for objects action server.
int show_image(image p, const char *name, int ms)
ros::NodeHandle nodeHandle_
ROS node handle.
bool publishDetectionImage(const cv::Mat &detectionImage)
image_transport::Subscriber imageSubscriber_
ROS subscriber and publisher.


darknet_ros
Author(s): Marko Bjelonic
autogenerated on Wed May 5 2021 02:58:30