ObjectLocalizerProcessorJob.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Component
19  * @author Kai Welke (welke at kit dot edu)
20  * @date 2013
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
28 
29 // IVT
30 #include <Image/ImageProcessor.h>
31 
32 
33 using namespace visionx;
34 
36  processor(processor), jobPending(false), jobFinished(false), abortRequested(false)
37 {
38 }
39 
40 void
41 ObjectLocalizerProcessorJob::start(std::vector<std::string> objectClassNames)
42 {
43  // set data and notify processing thread
44  std::unique_lock lock(jobMutex);
45  this->objectClassNames = objectClassNames;
46  jobPending = true;
47  jobFinished = false;
48 }
49 
50 void
52 {
53  abortRequested = false;
54 }
55 
56 memoryx::ObjectLocalizationResultList
58 {
59  while (!jobFinished)
60  {
61  usleep(1000);
62  }
63  jobFinished = false;
64  std::unique_lock lock(jobMutex);
65  return result;
66 }
67 
68 void
70 {
71  abortRequested = true;
72 }
73 
74 void
76 {
77  if (!processor)
78  {
79  ARMARX_ERROR << "No processor set";
80  return;
81  }
82 
83  // allow initialization of recognition methods
84  processor->initRecognizer();
85 
86  // fill object database with objects from prior knowledge
87  processor->initObjectClasses();
88 
89  // main loop
90  memoryx::ObjectLocalizationResultList localResult;
91 
92  while (!abortRequested)
93  {
94  try
95  {
96  // wait for new processing job
97  while (!jobPending && !abortRequested)
98  {
99  usleep(1000);
100  }
101  jobPending = false;
102  ARMARX_VERBOSE << "Job received";
103 
104  if (abortRequested)
105  {
106  ARMARX_INFO << "ObjectLocalizerProcessorJob::process(): exit requested";
107  continue;
108  }
109 
110 
111  localResult.clear();
112 
113  ARMARX_DEBUG << "ObjectLocalizerProcessorJob::process(): starting localization";
114  int numImages = processor->getImages(
115  processor->imageProviderName, processor->cameraImages, processor->imageMetaInfo);
116  if (numImages > 0)
117  {
118  // recognize
119  if (processor->getResultImagesEnabled())
120  {
121  if (processor->isResultImageMaskEnabled())
122  {
123  Eigen::Vector3i colorMask = processor->getColorMask();
124  for (int n = 0; n < numImages; n++)
125  {
126  for (int j = 0; j < processor->resultImages[n]->height; j++)
127  {
128  for (int i = 0; i < processor->resultImages[n]->width; i++)
129  {
130  processor->resultImages[n]
131  ->pixels[3 * (j * processor->resultImages[n]->width + i) +
132  0] = colorMask(0);
133  processor->resultImages[n]
134  ->pixels[3 * (j * processor->resultImages[n]->width + i) +
135  1] = colorMask(1);
136  processor->resultImages[n]
137  ->pixels[3 * (j * processor->resultImages[n]->width + i) +
138  2] = colorMask(2);
139  }
140  }
141  }
142  }
143  else
144  {
145  ::ImageProcessor::CopyImage(processor->cameraImages[0],
146  processor->resultImages[0]);
147  if (numImages > 1)
148  {
149  ::ImageProcessor::CopyImage(processor->cameraImages[1],
150  processor->resultImages[1]);
151  }
152  }
153  }
154  ARMARX_DEBUG << "starting localization algo";
155  localResult = processor->localizeObjectClasses(objectClassNames,
156  processor->cameraImages,
157  processor->imageMetaInfo,
158  processor->resultImages);
159 
160  // visualize
161  if (processor->getResultImagesEnabled())
162  {
163  processor->provideResultImages(processor->resultImages,
164  processor->imageMetaInfo);
165  }
166  ARMARX_DEBUG << "finished localization algo";
167  }
168  else
169  {
170  ARMARX_WARNING << deactivateSpam(1) << "Could not get images";
171  }
172  }
173  catch (std::exception& e)
174  {
175  ARMARX_ERROR << "Localization failed: " << processor->getName();
177  }
178 
179  // set result and notify
180  std::unique_lock lock(jobMutex);
181  result = localResult;
182  jobFinished = true;
183  }
184 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
visionx::ObjectLocalizerProcessor::resultImages
CByteImage ** resultImages
Definition: ObjectLocalizerProcessor.h:431
ObjectLocalizerProcessorJob.h
visionx::ObjectLocalizerProcessor::localizeObjectClasses
memoryx::ObjectLocalizationResultList localizeObjectClasses(const memoryx::ObjectClassNameList &objectClassNames, const Ice::Current &c=Ice::emptyCurrent) override
The process method is inherited from the ObjectLocalizationProcessorInterface and is called by the Wo...
Definition: ObjectLocalizerProcessor.cpp:192
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ObjectLocalizerProcessor::getResultImagesEnabled
bool getResultImagesEnabled() const
Retrieve whether result images are enabled.
Definition: ObjectLocalizerProcessor.h:335
visionx::ObjectLocalizerProcessor::initRecognizer
virtual bool initRecognizer()=0
ObjectLocalizerProcessor interface: The initRecognizer method needs to be implemented by any ObjectLo...
visionx::ObjectLocalizerProcessorJob::exit
void exit()
Requests exit to the job.
Definition: ObjectLocalizerProcessorJob.cpp:69
magic_enum::detail::n
constexpr auto n() noexcept
Definition: magic_enum.hpp:418
visionx::ObjectLocalizerProcessorJob::waitForResult
memoryx::ObjectLocalizationResultList waitForResult()
Wait for the localization result.
Definition: ObjectLocalizerProcessorJob.cpp:57
visionx::ObjectLocalizerProcessor::imageMetaInfo
armarx::MetaInfoSizeBasePtr imageMetaInfo
Definition: ObjectLocalizerProcessor.h:432
ObjectLocalizerProcessor.h
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
visionx::ObjectLocalizerProcessor::cameraImages
CByteImage * cameraImages[2]
Definition: ObjectLocalizerProcessor.h:429
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
visionx::ObjectLocalizerProcessor::isResultImageMaskEnabled
bool isResultImageMaskEnabled() const
Definition: ObjectLocalizerProcessor.h:374
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
visionx::ObjectLocalizerProcessorJob::reset
void reset()
Definition: ObjectLocalizerProcessorJob.cpp:51
visionx::ImageProcessor::getImages
int getImages(CByteImage **ppImages)
Poll images from provider.
Definition: ImageProcessor.cpp:395
visionx::ObjectLocalizerProcessor::imageProviderName
std::string imageProviderName
Definition: ObjectLocalizerProcessor.h:423
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
visionx::ObjectLocalizerProcessor::getColorMask
Eigen::Vector3i getColorMask() const
Definition: ObjectLocalizerProcessor.h:380
visionx::ObjectLocalizerProcessorJob::start
void start(std::vector< std::string > objectClassNames)
Start a localization job for the given objectClassName.
Definition: ObjectLocalizerProcessorJob.cpp:41
visionx::ImageProcessor::provideResultImages
void provideResultImages(CByteImage **images, armarx::MetaInfoSizeBasePtr info=nullptr)
sends result images for visualization
Definition: ImageProcessor.cpp:274
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:157
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
visionx::ObjectLocalizerProcessorJob::ObjectLocalizerProcessorJob
ObjectLocalizerProcessorJob(ObjectLocalizerProcessor *processor)
Init the job by providing the ObjectLocalizerProcessor that implements the required functionality.
Definition: ObjectLocalizerProcessorJob.cpp:35
visionx::ObjectLocalizerProcessorJob::process
void process()
Processing method.
Definition: ObjectLocalizerProcessorJob.cpp:75
visionx::ObjectLocalizerProcessor
ObjectLocalizerProcessor.
Definition: ObjectLocalizerProcessor.h:128