SimpleEpisodicMemoryImageConnector.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 
24 
25 
26 // STD/STL
27 
28 // IVT
29 #include <Image/ImageProcessor.h>
30 
31 // ArmarX
32 
33 using namespace visionx;
34 
36 {
38  getConfigIdentifier()));
39 }
40 
42 {
43  ARMARX_DEBUG << "Initializing SimpleEpisodicMemoryImageConnector";
44 
45  m_image_provider_id = getProperty<std::string>("ImageProviderName").getValue();
46  m_image_provider_channel = getProperty<unsigned int>("ImageProviderChannel");
47  ARMARX_VERBOSE << "Using image provider with ID '" << m_image_provider_id << "'.";
48 
49  usingImageProvider(m_image_provider_id);
50  usingProxy("SimpleEpisodicMemory");
51 
52  m_image_received = false;
53 
54  setFramerate(1);
55 }
56 
58 {
59  // Get SimpleEpisodicMemory
60  getProxy(m_simple_episodic_memory, m_simple_episodic_memory_proxy_name);
61 
62  // Connect to image provider.
63  m_image_provider_info = getImageProvider(m_image_provider_id);
64  m_image_provider = getProxy<visionx::ImageProviderInterfacePrx>(m_image_provider_id);
65 
66  // Init input image.
67  num_of_received_images = static_cast<unsigned int>(m_image_provider_info.numberImages);
68  m_input_image_buf = new ::CByteImage*[num_of_received_images];
69  for (unsigned int i = 0; i < num_of_received_images; ++i)
70  {
71  m_input_image_buf[i] = visionx::tools::createByteImage(m_image_provider_info);
72  }
73  m_input_image = visionx::tools::createByteImage(m_image_provider_info);
74 
75  // Kick off running task
76  m_periodic_task = new armarx::PeriodicTask<SimpleEpisodicMemoryImageConnector>(this, &SimpleEpisodicMemoryImageConnector::checkForNewImages, m_periodic_task_interval);
77  m_periodic_task->start();
78 }
79 
81 {
82  // Stop task.
83  {
84  m_periodic_task->stop();
85  }
86 
87  // Clear input image buffer.
88  {
89  for (unsigned int i = 0; i < num_of_received_images; ++i)
90  {
91  delete m_input_image_buf[i];
92  }
93  delete[] m_input_image_buf;
94  }
95 }
96 
98 {
99 }
100 
102 {
103  const IceUtil::Time timeout = IceUtil::Time::milliSeconds(1000);
104  if (!waitForImages(m_image_provider_id, static_cast<int>(timeout.toMilliSeconds())))
105  {
106  ARMARX_WARNING << "Timeout while waiting for camera images (>" << timeout << ")";
107  return;
108  }
109 
110  ARMARX_DEBUG << "Received a new image. Putting image to member variable";
111  std::lock_guard<std::mutex> lock{m_input_image_mutex};
112 
113  int num_images = getImages(m_image_provider_id, m_input_image_buf, m_image_meta_info);
114  m_timestamp_last_image = IceUtil::Time::microSeconds(m_image_meta_info->timeProvided);
115 
116  if (static_cast<unsigned int>(num_images) != num_of_received_images)
117  {
118  // ERROR
119  }
120  else
121  {
122  // Only consider channel image.
123  ::ImageProcessor::CopyImage(m_input_image_buf[m_image_provider_channel], m_input_image);
124  m_image_received = true;
125  }
126  ARMARX_DEBUG << "Wait for next image";
127 }
128 
129 void visionx::SimpleEpisodicMemoryImageConnector::checkForNewImages()
130 {
131 
132  if (!m_image_received)
133  {
134  return;
135  }
136 
137  ARMARX_DEBUG << "Got a new image for processing. Send image to EpisodicMemory. Image information: Size: " << m_input_image->width << ", " << m_input_image->height << ", " << m_input_image->bytesPerPixel;
138  std::lock_guard<std::mutex> lock{m_input_image_mutex};
139  m_image_received = false;
140 
141  // TODO convert2Blob and enable png compression
142 
143  memoryx::ImageEvent memory_image_event;
144  memory_image_event.providerName = m_image_provider_id + "__" + std::to_string(m_image_provider_channel);
145  memory_image_event.receivedInMs = m_timestamp_last_image.toMilliSecondsDouble();
146  memory_image_event.height = m_input_image->height;
147  memory_image_event.width = m_input_image->width;
148  memory_image_event.colourType = (m_input_image->bytesPerPixel == 3 ? memoryx::ColourSpace::RGB : memoryx::ColourSpace::GRAYSCALE);
149  memory_image_event.data = std::vector<unsigned char>(m_input_image->pixels, m_input_image->pixels + (m_input_image->height * m_input_image->width * m_input_image->bytesPerPixel));
150  m_simple_episodic_memory->registerImageEvent(memory_image_event);
151 
152  ARMARX_DEBUG << deactivateSpam(0.5) << "Processed a new image";
153 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
visionx::SimpleEpisodicMemoryImageConnector::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: SimpleEpisodicMemoryImageConnector.cpp:41
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
SimpleEpisodicMemoryImageConnector.h
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::SimpleEpisodicMemoryImageConnector::process
void process() override
Process the vision component.
Definition: SimpleEpisodicMemoryImageConnector.cpp:101
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
visionx::SimpleEpisodicMemoryImageConnectorPropertyDefinitions
Definition: SimpleEpisodicMemoryImageConnector.h:50
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
visionx::SimpleEpisodicMemoryImageConnector::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SimpleEpisodicMemoryImageConnector.cpp:35
visionx::SimpleEpisodicMemoryImageConnector::onDisconnectImageProcessor
void onDisconnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component looses network ...
Definition: SimpleEpisodicMemoryImageConnector.cpp:80
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::SimpleEpisodicMemoryImageConnector::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: SimpleEpisodicMemoryImageConnector.cpp:97
visionx::SimpleEpisodicMemoryImageConnector::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: SimpleEpisodicMemoryImageConnector.cpp:57
armarx::PeriodicTask
Definition: ArmarXManager.h:70
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34