Component.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::Yolo
17  * @author Christian R. G. Dreher <c.dreher@kit.edu>
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #pragma once
25 
26 
27 // STD/STL
28 #include <memory>
29 #include <mutex>
30 #include <string>
31 #include <vector>
32 
33 // IVT
34 #include <Image/ByteImage.h>
35 
36 // Darknet
37 #include <darknet.h>
38 
39 // Ice
40 #include <Ice/Current.h>
41 #include <IceUtil/Time.h>
42 
43 // ArmarX
48 #include <VisionX/interface/core/ImageProviderInterface.h>
49 #include <VisionX/interface/components/YoloComponentInterface.h>
50 #include <VisionX/interface/components/YoloObjectListener.h>
51 
52 
53 namespace visionx::yolo
54 {
55 
56  class Component:
57  virtual public visionx::ImageProcessor,
58  virtual public ComponentInterface,
59  virtual public armarx::Logging
60  {
61 
62  public:
63 
64  static const std::string default_name;
65 
66  private:
67 
68  bool m_image_received;
69 
70  std::mutex m_input_image_mutex;
71 
72  std::string m_image_provider_id;
73  visionx::ImageProviderInfo m_image_provider_info;
74 
75  IceUtil::Time m_timestamp_last_image;
76 
77  ::CByteImage** m_input_image_buf;
78  CByteImageUPtr m_input_image;
79  CByteImageUPtr m_output_image;
80 
81  armarx::RunningTask<Component>::pointer_type m_task_object_detection;
82 
83  visionx::ImageProviderInterfacePrx m_image_provider;
84  unsigned int m_image_provider_channel;
85 
86  ObjectListener::ProxyType m_object_detection_listener;
87 
88  IceUtil::Time m_minimum_loop_time;
89 
90  /**
91  * @brief Darknet's network.
92  */
93  darknet::network m_network;
94 
95  std::vector<std::string> m_darknet_classes;
96 
97  public:
98 
99  virtual ~Component() override;
100 
101  /**
102  * @see visionx::ImageProcessor::onInitImageProcessor()
103  */
104  virtual void onInitImageProcessor() override;
105 
106  /**
107  * @see visionx::ImageProcessor::onConnectImageProcessor()
108  */
109  virtual void onConnectImageProcessor() override;
110 
111  /**
112  * @see visionx::ImageProcessor::onDisconnectImageProcessor()
113  */
114  virtual void onDisconnectImageProcessor() override;
115 
116  /**
117  * @see visionx::ImageProcessor::onExitImageProcessor()
118  */
119  virtual void onExitImageProcessor() override;
120 
121  /**
122  * @see armarx::ManagedIceObject::getDefaultName()
123  */
124  virtual std::string getDefaultName() const override;
125 
126  /**
127  * @see visionx::ImageProcessor::process()
128  */
129  virtual void process() override;
130 
131  /**
132  * @see PropertyUser::createPropertyDefinitions()
133  */
135 
136  /**
137  * @brief Converts Darknet types to VisionX types.
138  * @param detections Array of detections.
139  * @param classes Available classes as strings in correct order.
140  * @return Converted Ice types as vector of DetectedObjects.
141  */
142  static std::vector<DetectedObject> convertOutput(
143  const std::vector<darknet::detection>& detections,
144  const std::vector<std::string>& classes
145  );
146 
147  /**
148  * @brief Renders the objects detectedObjects, belonging to one of a class defined in
149  * classes onto the outputImage (initialsed from inputImage).
150  * @param inputImage The base image output_image is initialised from.
151  * @param detectedObjects All detected objects to be rendered.
152  * @param classes List of all classes an object can belong to.
153  * @param outputImage Ouput variable of the resulting rendered image.
154  */
155  static void renderOutput(
156  const ::CByteImage* inputImage,
157  const std::vector<DetectedObject>& detectedObjects,
158  ::CByteImage* outputImage
159  );
160 
161  /**
162  * @brief Restores the default parameter values.
163  */
164  virtual void restoreDefaults(const Ice::Current& = Ice::Current{}) override;
165 
166  /**
167  * @brief Returns the currently set thresh parameter.
168  * @return Currently set thresh parameter.
169  */
170  virtual double getThresh(const Ice::Current&) override;
171 
172  /**
173  * @brief Sets the new thresh parameter.
174  * @param thresh New thresh.
175  */
176  virtual void setThresh(double thresh, const Ice::Current&) override;
177 
178  /**
179  * @brief Returns the currently set hierThresh parameter.
180  * @return Currently set hierThresh parameter.
181  */
182  virtual double getHierThresh(const Ice::Current&) override;
183 
184  virtual float getFpsCap(const Ice::Current&) override;
185 
186  /**
187  * @brief Sets the new hierThresh parameter.
188  * @param hier_thresh New hierThresh.
189  */
190  virtual void setHierThresh(double hier_thresh, const Ice::Current&) override;
191 
192  /**
193  * @brief Returns the currently set nms parameter.
194  * @return Currently set nms parameter.
195  */
196  virtual double getNms(const Ice::Current&) override;
197 
198  /**
199  * @brief Sets the new nms parameter.
200  * @param nms New nms.
201  */
202  virtual void setNms(double nms, const Ice::Current&) override;
203 
204  virtual void setFpsCap(float fps_cap, const Ice::Current& = Ice::Current{}) override;
205 
206  /**
207  * @brief Returns the list of all known classes.
208  * @return List of all known classes.
209  */
210  virtual std::vector<std::string> getClasses(const Ice::Current&) override;
211 
212  private:
213 
214  /**
215  * @brief Bootstraps Darknet.
216  *
217  * Initialises Darknet so it can quickly be accessed afterwards. Bootstrapping usually
218  * takes a while.
219  */
220  virtual void bootstrap_darknet();
221 
222  /**
223  * @brief Run Darknet to detect objects in the current image.
224  */
225  virtual void run_object_detection_task();
226 
227  };
228 
229 }
230 
231 
232 namespace visionx
233 {
235 }
visionx::yolo::Component::setThresh
virtual void setThresh(double thresh, const Ice::Current &) override
Sets the new thresh parameter.
Definition: Component.cpp:240
visionx::yolo::Component::process
virtual void process() override
Definition: Component.cpp:199
visionx::yolo::Component::onExitImageProcessor
virtual void onExitImageProcessor() override
Definition: Component.cpp:185
visionx::yolo::Component::getDefaultName
virtual std::string getDefaultName() const override
Definition: Component.cpp:192
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::yolo::Component::onConnectImageProcessor
virtual void onConnectImageProcessor() override
Definition: Component.cpp:116
visionx::yolo::Component::~Component
virtual ~Component() override
Definition: Component.cpp:68
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:87
visionx::yolo::Component::createPropertyDefinitions
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:575
visionx::yolo::Component::renderOutput
static void renderOutput(const ::CByteImage *inputImage, const std::vector< DetectedObject > &detectedObjects, ::CByteImage *outputImage)
Renders the objects detectedObjects, belonging to one of a class defined in classes onto the outputIm...
Definition: Component.cpp:482
visionx::yolo::Component::convertOutput
static std::vector< DetectedObject > convertOutput(const std::vector< darknet::detection > &detections, const std::vector< std::string > &classes)
Converts Darknet types to VisionX types.
Definition: Component.cpp:443
visionx::yolo::Component::setFpsCap
virtual void setFpsCap(float fps_cap, const Ice::Current &=Ice::Current{}) override
Definition: Component.cpp:292
visionx::yolo
Definition: Component.h:53
visionx::yolo::Component::setNms
virtual void setNms(double nms, const Ice::Current &) override
Sets the new nms parameter.
Definition: Component.cpp:270
RunningTask.h
visionx::yolo::Component::onDisconnectImageProcessor
virtual void onDisconnectImageProcessor() override
Definition: Component.cpp:161
visionx::yolo::Component::default_name
static const std::string default_name
Definition: Component.h:64
visionx::ImageProviderInfo
Definition: ImageProcessor.h:466
visionx::yolo::Component::restoreDefaults
virtual void restoreDefaults(const Ice::Current &=Ice::Current{}) override
Restores the default parameter values.
Definition: Component.cpp:314
visionx::CByteImageUPtr
std::unique_ptr< CByteImage > CByteImageUPtr
Definition: ImageProvider.h:57
ImageProcessor.h
visionx::yolo::Component::setHierThresh
virtual void setHierThresh(double hier_thresh, const Ice::Current &) override
Sets the new hierThresh parameter.
Definition: Component.cpp:255
visionx::yolo::Component
Definition: Component.h:56
visionx::yolo::Component::getHierThresh
virtual double getHierThresh(const Ice::Current &) override
Returns the currently set hierThresh parameter.
Definition: Component.cpp:248
armarx::ProxyType
ProxyType
Definition: ProxyPropertyDefinition.h:41
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
visionx::yolo::Component::getThresh
virtual double getThresh(const Ice::Current &) override
Returns the currently set thresh parameter.
Definition: Component.cpp:233
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
IceUtil::Handle
Definition: forward_declarations.h:29
visionx::yolo::Component::onInitImageProcessor
virtual void onInitImageProcessor() override
Definition: Component.cpp:75
ImageUtil.h
visionx::yolo::Component::getFpsCap
virtual float getFpsCap(const Ice::Current &) override
Definition: Component.cpp:278
Logging.h
visionx::yolo::Component::getNms
virtual double getNms(const Ice::Current &) override
Returns the currently set nms parameter.
Definition: Component.cpp:263
visionx::yolo::Component::getClasses
virtual std::vector< std::string > getClasses(const Ice::Current &) override
Returns the list of all known classes.
Definition: Component.cpp:307