ImageToArMem.cpp
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::SimpleEpisodicMemoryImageConnector
17  * @author Fabian Peller ( fabian dot peller-konrad at kit dot edu )
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ImageToArMem.h"
24 
25 // IVT
27 
30 
32 
33 #include <Image/ImageProcessor.h>
34 
35 namespace visionx
36 {
37 
38  std::string
40  {
41  return "ImageToArMem";
42  }
43 
46  {
49 
50  defs->topic(debugObserver);
51 
52  defs->optional(p.imageProviderName, "img.ProviderName", "Name of the image provier.");
53  defs->optional(p.providerSegmentName,
54  "img.ProviderSegmentName",
55  "The provider (segment) name (if not specified in the entity ID).\n"
56  "If '(auto)', the image provider name is used.");
57 
58  defs->optional(p.clearProviderSegmentWhenExists,
59  "mem.ClearProviderSegmentsWhenExisting",
60  "If true, the provider segments are cleared when this component starts.");
61 
62  defs->optional(p.commitCameraCalib,
63  "mem.commitCameraCalibration",
64  "Commit camera calibration to memory");
65 
66  p.images.define(*defs);
67 
68  return defs;
69  }
70 
71  void
73  {
74  // Parse properties.
75  if (p.providerSegmentName == "(auto)")
76  {
77  p.providerSegmentName = p.imageProviderName;
78  }
79  p.images.read(*this, p.providerSegmentName);
80 
81  imageToArMem.addImagesRGB(p.images.rgbEntityID, p.images.rgbIndices);
82  imageToArMem.addImagesDepth(p.images.depthEntityID, p.images.depthIndices);
83 
84  ARMARX_INFO << "Memory image structure: \n" << imageToArMem.summarizeStructure();
85  ARMARX_VERBOSE << "Using image provider '" << p.imageProviderName << "'.";
86 
87  usingImageProvider(p.imageProviderName);
88  }
89 
90  void
92  {
93  // Connect to image provider.
94  {
95  getProxy(imageProvider, p.imageProviderName);
96  imageProviderInfo = getImageProvider(p.imageProviderName);
97 
98  // Init input images.
99  imageToArMem.initImages(imageProviderInfo.imageFormat);
100  inputImages = imageToArMem.makeCByteImageBuffer();
101  inputImageBuffer.clear();
102  for (CByteImage& img : inputImages)
103  {
104  inputImageBuffer.emplace_back(&img);
105  }
106  }
107 
108  // Connect to memory server.
109  {
110  try
111  {
112  imageToArMem.setWriter(memoryNameSystem().useWriter(imageToArMem.getMemoryID()));
113  }
115  {
116  ARMARX_ERROR << e.what();
117  }
118 
119  imageToArMem.setDebugObserver(debugObserver);
120 
121  imageToArMem.addProviderSegments();
122  }
123 
124  // Initially commit the camera calibraion
125  if (p.commitCameraCalib)
126  {
127  if (visionx::StereoCalibrationProviderInterfacePrx calibrationProvider =
128  visionx::StereoCalibrationProviderInterfacePrx::checkedCast(
129  imageProviderInfo.proxy);
130  calibrationProvider)
131  {
133  << "Found stereo camera calibration from StereoCalibrationProviderInterface.";
134  visionx::StereoCalibration stereoCameraCalibration =
135  calibrationProvider->getStereoCalibration();
136  imageToArMem.commitCameraCalibration(stereoCameraCalibration);
137  }
138  else if (visionx::StereoCalibrationInterfacePrx pointCloudAndStereoCalibrationProvider =
139  visionx::StereoCalibrationInterfacePrx::checkedCast(
140  imageProviderInfo.proxy);
141  calibrationProvider)
142  {
143  ARMARX_INFO << "Found stereo camera calibration from StereoCalibrationInterface.";
144  visionx::StereoCalibration stereoCameraCalibration =
145  calibrationProvider->getStereoCalibration();
146  imageToArMem.commitCameraCalibration(stereoCameraCalibration);
147  }
148  else
149  {
150  ARMARX_INFO << "Found monocular camera calibration. Could not cast to stereo "
151  "calibration interface.";
152  visionx::MonocularCalibration monocularCameraCalibration =
154 
155  imageToArMem.commitCameraCalibration(monocularCameraCalibration);
156  }
157  }
158  }
159 
160  void
162  {
163  }
164 
165  void
167  {
168  }
169 
170  void
172  {
173  const armarx::Duration timeout = armarx::Duration::MilliSeconds(1000);
174  armarx::Duration timeGetImages;
175 
176  int numImages = 0;
177  if (waitForImages(p.imageProviderName, static_cast<int>(timeout.toMilliSeconds())))
178  {
179  TIMING_START(GetImages);
180  numImages = getImages(p.imageProviderName, inputImageBuffer.data(), imageMetaInfo);
181  TIMING_END_STREAM(GetImages, ARMARX_VERBOSE);
182  timeGetImages = armarx::Duration::MicroSeconds(GetImages.toMicroSeconds());
183 
184  if (numImages == static_cast<int>(inputImageBuffer.size()))
185  {
186  using namespace armarx::armem;
187  imageToArMem.useImageBuffers(
188  inputImageBuffer.data(),
189  Time(Duration::MicroSeconds(imageMetaInfo->timeProvided)));
190  imageToArMem.commitImages();
191  }
192  else
193  {
194  ARMARX_ERROR << "Received unexpected number of input images. Got " << numImages
195  << " instead of " << inputImageBuffer.size() << ".";
196  }
197  }
198  else
199  {
200  ARMARX_WARNING << "Timeout while waiting for camera images (> " << timeout << ").";
201  return;
202  }
203 
204  ARMARX_DEBUG << "Wait for next image";
205 
206  if (debugObserver)
207  {
208  debugObserver->setDebugChannel(
209  getName(),
210  {
211  {"getImages() [us]", new armarx::Variant(timeGetImages.toMicroSecondsDouble())},
212  });
213  }
214  }
215 
216 } // namespace visionx
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
visionx::ImageToArMem::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: ImageToArMem.cpp:91
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
TIMING_START
#define TIMING_START(name)
Definition: TimeUtil.h:280
visionx::ImageToArMem::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: ImageToArMem.cpp:39
Writer.h
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ImageToArMem::onDisconnectImageProcessor
void onDisconnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component looses network ...
Definition: ImageToArMem.cpp:161
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
visionx::ImageToArMem::process
void process() override
Process the vision component.
Definition: ImageToArMem.cpp:171
visionx::ImageToArMem::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: ImageToArMem.cpp:166
TIMING_END_STREAM
#define TIMING_END_STREAM(name, os)
Definition: TimeUtil.h:300
ImageToArMem.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
error.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::ImageToArMem::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: ImageToArMem.cpp:72
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
visionx::ImageToArMem::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ImageToArMem.cpp:45
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
ImageUtil.h
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::core::time::Duration::toMilliSeconds
std::int64_t toMilliSeconds() const
Returns the amount of milliseconds.
Definition: Duration.cpp:69
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
Variant.h
visionx::tools::createDefaultMonocularCalibration
MonocularCalibration createDefaultMonocularCalibration()
Creates a MonocularCalibration with all parameters set to a neutral value.
Definition: ImageUtil.cpp:237
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:55
armarx::core::time::Duration::toMicroSecondsDouble
double toMicroSecondsDouble() const
Returns the amount of microseconds.
Definition: Duration.cpp:48