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
28 
31 
32 
34 
35 #include <Image/ImageProcessor.h>
36 
37 namespace visionx
38 {
39 
40  std::string
42  {
43  return "ImageToArMem";
44  }
45 
48  {
51 
52  defs->topic(debugObserver);
53 
54  defs->optional(p.imageProviderName, "img.ProviderName", "Name of the image provier.");
55  defs->optional(p.providerSegmentName,
56  "img.ProviderSegmentName",
57  "The provider (segment) name (if not specified in the entity ID).\n"
58  "If '(auto)', the image provider name is used.");
59 
60  defs->optional(p.clearProviderSegmentWhenExists,
61  "mem.ClearProviderSegmentsWhenExisting",
62  "If true, the provider segments are cleared when this component starts.");
63 
64  defs->optional(p.commitCameraCalib,
65  "mem.commitCameraCalibration",
66  "Commit camera calibration to memory");
67 
68  defs->optional(p.maxFrequencyOfCommit,
69  "mem.maxCommitFrequency",
70  "Maximal frequency with which to commit images to Vision Memory in Hz. \n"
71  "Actual commit frequency might be lower, as we wait for next image.");
72 
73  p.images.define(*defs);
74 
75  return defs;
76  }
77 
78  void
80  {
81  // Parse properties.
82  if (p.providerSegmentName == "(auto)")
83  {
84  p.providerSegmentName = p.imageProviderName;
85  }
86  p.images.read(*this, p.providerSegmentName);
87 
88  imageToArMem.addImagesRGB(p.images.rgbEntityID, p.images.rgbIndices);
89  imageToArMem.addImagesDepth(p.images.depthEntityID, p.images.depthIndices);
90 
91  ARMARX_INFO << "Memory image structure: \n" << imageToArMem.summarizeStructure();
92  ARMARX_VERBOSE << "Using image provider '" << p.imageProviderName << "'.";
93 
94  usingImageProvider(p.imageProviderName);
95  }
96 
97  void
99  {
100  // Connect to image provider.
101  {
102  imageProviderInfo = getImageProvider(p.imageProviderName);
103 
104  // Init input images.
105  imageToArMem.initImages(imageProviderInfo.imageFormat);
106  inputImages = imageToArMem.makeCByteImageBuffer();
107  inputImageBuffer.clear();
108  for (CByteImage& img : inputImages)
109  {
110  inputImageBuffer.emplace_back(&img);
111  }
112  }
113 
114  // Connect to memory server.
115  {
116  try
117  {
118  imageToArMem.setWriter(memoryNameSystem().useWriter(imageToArMem.getMemoryID()));
119  }
121  {
122  ARMARX_ERROR << e.what();
123  }
124 
125  imageToArMem.setDebugObserver(debugObserver);
126 
127  imageToArMem.addProviderSegments();
128  }
129  }
130 
131  void
133  {
134  }
135 
136  void
138  {
139  }
140 
141  void
143  {
144  const armarx::Duration timeout = armarx::Duration::MilliSeconds(1000);
145  armarx::Duration timeGetImages;
146 
147  int numImages = 0;
148  int millisecondsSinceLastCommit = 0;
149  if (waitForImages(p.imageProviderName, static_cast<int>(timeout.toMilliSeconds())))
150  {
152  int timeDifference = (n.toMicroSecondsSinceEpoch() - this->timeOfLastAcceptedImage.toMicroSecondsSinceEpoch())/1000;
153  ARMARX_DEBUG << "Time difference during commit is " << timeDifference;
154  float maxFrequency = p.maxFrequencyOfCommit;
155  float minWaitingTime = (1/maxFrequency) * 1000;
156  ARMARX_DEBUG << "minWaitingTime is " << minWaitingTime;
157  if (timeDifference > minWaitingTime){
158  ARMARX_DEBUG << "resetting time of last accepted image";
159  this->timeOfLastAcceptedImage = armarx::DateTime::Now(); //if we accept the image we reset the time
160  TIMING_START(GetImages);
161  numImages = getImages(p.imageProviderName, inputImageBuffer.data(), imageMetaInfo);
162  TIMING_END_STREAM(GetImages, ARMARX_VERBOSE);
163  timeGetImages = armarx::Duration::MicroSeconds(GetImages.toMicroSeconds());
164 
165  if (numImages == static_cast<int>(inputImageBuffer.size()))
166  {
167  using namespace armarx::armem;
168  imageToArMem.useImageBuffers(
169  inputImageBuffer.data(),
170  Time(Duration::MicroSeconds(imageMetaInfo->timeProvided)));
171  imageToArMem.commitImages(); //TODO: limit how much images will be send to memory
172  }
173  else
174  {
175  ARMARX_ERROR << "Received unexpected number of input images. Got " << numImages
176  << " instead of " << inputImageBuffer.size() << ".";
177  }
178 
179 
180  // Initially commit the camera calibration once. Has to be done in the processing as it doesn't wait for the proxy
181  if (!commitedCameraCalib and p.commitCameraCalib and imageProviderInfo.proxy)
182  {
183  if (visionx::StereoCalibrationInterfacePrx calibrationProvider =
184  visionx::StereoCalibrationInterfacePrx::checkedCast(imageProviderInfo.proxy);
185  calibrationProvider)
186  {
188  << "Found stereo camera calibration from StereoCalibrationInterface.";
189  visionx::StereoCalibration stereoCameraCalibration =
190  calibrationProvider->getStereoCalibration();
191  imageToArMem.commitCameraCalibration(stereoCameraCalibration);
192  }
193  else
194  {
195  ARMARX_INFO << "Could not cast to stereo calibration interface. Creating default monocular ";
196  visionx::MonocularCalibration monocularCameraCalibration =
198 
199  imageToArMem.commitCameraCalibration(monocularCameraCalibration);
200  }
201  commitedCameraCalib = true;
202  }
203  }
204  else{
205  ARMARX_DEBUG << "Ignoring image due to high frequency of commits but lower max. Frequency set in properties";
206  }
207  }
208  else
209  {
210  ARMARX_WARNING << "Timeout while waiting for camera images (> " << timeout << ").";
211  return;
212  }
213 
214  if (debugObserver)
215  {
216  debugObserver->setDebugChannel(
217  getName(),
218  {
219  {"getImages() [us]", new armarx::Variant(timeGetImages.toMicroSecondsDouble())},
220  });
221  }
222  }
223 
224 } // 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:98
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:41
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:132
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
visionx::ImageToArMem::process
void process() override
Process the vision component.
Definition: ImageToArMem.cpp:142
visionx::ImageToArMem::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: ImageToArMem.cpp:137
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:79
armarx::core::time::DateTime::toMicroSecondsSinceEpoch
std::int64_t toMicroSecondsSinceEpoch() const
Definition: DateTime.cpp:95
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:47
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
time_minimal.h
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