darknet_detector.h
Go to the documentation of this file.
00001 //
00002 // Created by banerjs on 12/13/16.
00003 //
00004 
00005 #ifndef RAIL_OBJECT_DETECTOR_DARKNET_DETECTOR_H
00006 #define RAIL_OBJECT_DETECTOR_DARKNET_DETECTOR_H
00007 
00008 #include <string>
00009 #include <vector>
00010 #include <time.h>
00011 #include <boost/bind.hpp>
00012 #include <boost/function.hpp>
00013 #include <boost/thread/mutex.hpp>
00014 #include <boost/thread/thread.hpp>
00015 #include <opencv2/imgproc/imgproc.hpp>
00016 
00017 #include <ros/ros.h>
00018 #include <ros/callback_queue.h>
00019 #include <ros/advertise_service_options.h>
00020 #include <ros/package.h>
00021 #include <image_transport/image_transport.h>
00022 #include <cv_bridge/cv_bridge.h>
00023 #include <sensor_msgs/image_encodings.h>
00024 #include <sensor_msgs/Image.h>
00025 
00026 #include "rail_object_detection_msgs/Detections.h"
00027 #include "rail_object_detection_msgs/Object.h"
00028 #include "rail_object_detection_msgs/SceneQuery.h"
00029 #include "rail_object_detection_msgs/ImageQuery.h"
00030 
00031 #include "detector.h"
00032 
00033 using namespace rail_object_detection_msgs;
00034 
00035 namespace rail_object_detector
00036 {
00037 
00038 // Weird requirement to redefine the structures here
00039 extern "C" {
00040 typedef struct darknet_object
00041 {
00042   char *label;
00043   float probability;
00044   unsigned short centroid_x;
00045   unsigned short centroid_y;
00046   unsigned short left_bot_x;
00047   unsigned short left_bot_y;
00048   unsigned short right_top_x;
00049   unsigned short right_top_y;
00050 } darknet_object;
00051 
00052 typedef enum
00053 {
00054   CONSTANT, STEP, EXP, POLY, STEPS, SIG, RANDOM
00055 } learning_rate_policy;
00056 typedef struct tree tree;
00057 typedef struct layer layer;
00058 
00059 typedef struct network
00060 {
00061   float *workspace;
00062   int n;
00063   int batch;
00064   int *seen;
00065   float epoch;
00066   int subdivisions;
00067   float momentum;
00068   float decay;
00069   layer *layers;
00070   int outputs;
00071   float *output;
00072   learning_rate_policy policy;
00073 
00074   float learning_rate;
00075   float gamma;
00076   float scale;
00077   float power;
00078   int time_steps;
00079   int step;
00080   int max_batches;
00081   float *scales;
00082   int *steps;
00083   int num_steps;
00084   int burn_in;
00085 
00086   int adam;
00087   float B1;
00088   float B2;
00089   float eps;
00090 
00091   int inputs;
00092   int h, w, c;
00093   int max_crop;
00094   int min_crop;
00095   float angle;
00096   float aspect;
00097   float exposure;
00098   float saturation;
00099   float hue;
00100 
00101   int gpu_index;
00102   tree *hierarchy;
00103 
00104 #ifdef GPU
00105   float **input_gpu;
00106   float **truth_gpu;
00107 #endif
00108 } network;
00109 };
00110 
00111 class DarknetDetector
00112 {
00113 public:
00114   // Constructors
00115   DarknetDetector(ros::NodeHandle &handle, ros::NodeHandle &private_handle)
00116     : nh_(handle), private_nh_(private_handle), it_(private_handle),
00117       perform_detections_(false), detections_thread_(NULL)
00118   { }
00119 
00120   // Methods
00121 
00125   bool start();
00126 
00130   bool stop();
00131 
00135   void imageSubscriberCallback(const sensor_msgs::ImageConstPtr &msg);
00136 
00140   bool sceneQueryCallback(SceneQuery::Request &req, SceneQuery::Response &res);
00141 
00145   bool imageQueryCallback(ImageQuery::Request &req, ImageQuery::Response &res);
00146 
00151   void runBackgroundDetections();
00152 
00157    void backgroundDetectionCallback(const ros::TimerEvent &e);
00158 
00159 private:
00160   // Node Handles to communicate with the param server
00161   ros::NodeHandle nh_;
00162   ros::NodeHandle private_nh_;
00163 
00164   // Booleans to track which abilities to use
00165   bool use_scene_service_;
00166   bool use_image_service_;
00167   bool publish_detections_topic_;
00168 
00169   // Parameters for the abilities, such as publish frequencies, etc.
00170   float max_desired_publish_freq_;
00171 
00172   // Publishers, Subscribers, and Service Servers
00173   image_transport::ImageTransport it_;
00174   image_transport::Subscriber image_sub_;
00175   ros::Publisher detections_pub_;
00176   ros::ServiceServer scene_query_server_;
00177   ros::ServiceServer image_query_server_;
00178 
00179   // Locking mechanisms
00180   boost::mutex mutex_;
00181 
00182   // Image Pointer for the darknet detector
00183   sensor_msgs::ImageConstPtr latest_image_;   // Access to this is protected
00184 
00185   // Spinners and Queues
00186   ros::CallbackQueuePtr scene_callback_q_;
00187   ros::CallbackQueuePtr image_callback_q_;
00188   boost::shared_ptr<ros::AsyncSpinner> scene_spinner_;
00189   boost::shared_ptr<ros::AsyncSpinner> image_spinner_;
00190 
00191   // Asynchronous threads
00192   boost::thread *detections_thread_;
00193   bool perform_detections_;
00194 
00195   // Darknet variables
00196   float probability_threshold_;
00197   network net_;
00198   char **class_names_;
00199 
00200   // Private Methods
00201 
00208   bool detectObjects(cv_bridge::CvImagePtr cv_ptr, std::vector<Object>
00209     &detected_objects);
00210 };
00211 
00212 }
00213 #endif //RAIL_OBJECT_DETECTOR_DARKNET_DETECTOR_H


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:29