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 
27 
28 // IVT
29 #include <Image/ImageProcessor.h>
30 
31 
32 using namespace visionx;
33 
35  : processor(processor)
36  , jobPending(false)
37  , jobFinished(false)
38  , abortRequested(false)
39 {
40 }
41 
42 void ObjectLocalizerProcessorJob::start(std::vector<std::string> objectClassNames)
43 {
44  // set data and notify processing thread
45  std::unique_lock lock(jobMutex);
46  this->objectClassNames = objectClassNames;
47  jobPending = true;
48  jobFinished = false;
49 }
50 
52 {
53  abortRequested = false;
54 }
55 
56 memoryx::ObjectLocalizationResultList ObjectLocalizerProcessorJob::waitForResult()
57 {
58  while (!jobFinished)
59  {
60  usleep(1000);
61  }
62  jobFinished = false;
63  std::unique_lock lock(jobMutex);
64  return result;
65 }
66 
68 {
69  abortRequested = true;
70 }
71 
73 {
74  if (!processor)
75  {
76  ARMARX_ERROR << "No processor set";
77  return;
78  }
79 
80  // allow initialization of recognition methods
81  processor->initRecognizer();
82 
83  // fill object database with objects from prior knowledge
84  processor->initObjectClasses();
85 
86  // main loop
87  memoryx::ObjectLocalizationResultList localResult;
88 
89  while (!abortRequested)
90  {
91  try
92  {
93  // wait for new processing job
94  while (!jobPending && !abortRequested)
95  {
96  usleep(1000);
97  }
98  jobPending = false;
99  ARMARX_VERBOSE << "Job received";
100 
101  if (abortRequested)
102  {
103  ARMARX_INFO << "ObjectLocalizerProcessorJob::process(): exit requested";
104  continue;
105  }
106 
107 
108  localResult.clear();
109 
110  ARMARX_DEBUG << "ObjectLocalizerProcessorJob::process(): starting localization";
111  int numImages = processor->getImages(processor->imageProviderName, processor->cameraImages, processor->imageMetaInfo);
112  if (numImages > 0)
113  {
114  // recognize
115  if (processor->getResultImagesEnabled())
116  {
117  if (processor->isResultImageMaskEnabled())
118  {
119  Eigen::Vector3i colorMask = processor->getColorMask();
120  for (int n = 0; n < numImages; n++)
121  {
122  for (int j = 0; j < processor->resultImages[n]->height; j++)
123  {
124  for (int i = 0; i < processor->resultImages[n]->width; i++)
125  {
126  processor->resultImages[n]->pixels[3 * (j * processor->resultImages[n]->width + i) + 0] = colorMask(0);
127  processor->resultImages[n]->pixels[3 * (j * processor->resultImages[n]->width + i) + 1] = colorMask(1);
128  processor->resultImages[n]->pixels[3 * (j * processor->resultImages[n]->width + i) + 2] = colorMask(2);
129  }
130  }
131  }
132  }
133  else
134  {
135  ::ImageProcessor::CopyImage(processor->cameraImages[0], processor->resultImages[0]);
136  if (numImages > 1)
137  {
138  ::ImageProcessor::CopyImage(processor->cameraImages[1], processor->resultImages[1]);
139  }
140  }
141  }
142  ARMARX_DEBUG << "starting localization algo";
143  localResult = processor->localizeObjectClasses(objectClassNames, processor->cameraImages, processor->imageMetaInfo, processor->resultImages);
144 
145  // visualize
146  if (processor->getResultImagesEnabled())
147  {
148  processor->provideResultImages(processor->resultImages, processor->imageMetaInfo);
149  }
150  ARMARX_DEBUG << "finished localization algo";
151  }
152  else
153  {
154  ARMARX_WARNING << deactivateSpam(1) << "Could not get images";
155  }
156  }
157  catch (std::exception& e)
158  {
159  ARMARX_ERROR << "Localization failed: " << processor->getName();
161  }
162 
163  // set result and notify
164  std::unique_lock lock(jobMutex);
165  result = localResult;
166  jobFinished = true;
167  }
168 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
visionx::ObjectLocalizerProcessor::resultImages
CByteImage ** resultImages
Definition: ObjectLocalizerProcessor.h:374
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:176
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ObjectLocalizerProcessor::getResultImagesEnabled
bool getResultImagesEnabled() const
Retrieve whether result images are enabled.
Definition: ObjectLocalizerProcessor.h:284
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:67
visionx::ObjectLocalizerProcessorJob::waitForResult
memoryx::ObjectLocalizationResultList waitForResult()
Wait for the localization result.
Definition: ObjectLocalizerProcessorJob.cpp:56
visionx::ObjectLocalizerProcessor::imageMetaInfo
armarx::MetaInfoSizeBasePtr imageMetaInfo
Definition: ObjectLocalizerProcessor.h:375
ObjectLocalizerProcessor.h
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
visionx::ObjectLocalizerProcessor::cameraImages
CByteImage * cameraImages[2]
Definition: ObjectLocalizerProcessor.h:372
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
visionx::ObjectLocalizerProcessor::isResultImageMaskEnabled
bool isResultImageMaskEnabled() const
Definition: ObjectLocalizerProcessor.h:319
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::ObjectLocalizerProcessorJob::reset
void reset()
Definition: ObjectLocalizerProcessorJob.cpp:51
visionx::ImageProcessor::getImages
int getImages(CByteImage **ppImages)
Poll images from provider.
Definition: ImageProcessor.cpp:351
visionx::ObjectLocalizerProcessor::imageProviderName
std::string imageProviderName
Definition: ObjectLocalizerProcessor.h:366
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
visionx::ObjectLocalizerProcessor::getColorMask
Eigen::Vector3i getColorMask() const
Definition: ObjectLocalizerProcessor.h:324
visionx::ObjectLocalizerProcessorJob::start
void start(std::vector< std::string > objectClassNames)
Start a localization job for the given objectClassName.
Definition: ObjectLocalizerProcessorJob.cpp:42
visionx::ImageProcessor::provideResultImages
void provideResultImages(CByteImage **images, armarx::MetaInfoSizeBasePtr info=nullptr)
sends result images for visualization
Definition: ImageProcessor.cpp:245
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
visionx::ObjectLocalizerProcessorJob::ObjectLocalizerProcessorJob
ObjectLocalizerProcessorJob(ObjectLocalizerProcessor *processor)
Init the job by providing the ObjectLocalizerProcessor that implements the required functionality.
Definition: ObjectLocalizerProcessorJob.cpp:34
visionx::ObjectLocalizerProcessorJob::process
void process()
Processing method.
Definition: ObjectLocalizerProcessorJob.cpp:72
visionx::ObjectLocalizerProcessor
ObjectLocalizerProcessor.
Definition: ObjectLocalizerProcessor.h:107