34 #include <Image/ByteImage.h>
35 #include <Image/ImageProcessor.h>
36 #include <SegmentableRecognition/SegmentableDatabase.h>
46 #include <gui/GLContext.h>
52 SegmentableObjectRecognition::SegmentableObjectRecognition()
56 SegmentableObjectRecognition::~SegmentableObjectRecognition()
61 SegmentableObjectRecognition::onExitObjectLocalizerProcessor()
66 SegmentableObjectRecognition::initRecognizer()
70 Eigen::Vector3f minPoint = getProperty<Eigen::Vector3f>(
"MinPoint").getValue();
71 Eigen::Vector3f maxPoint = getProperty<Eigen::Vector3f>(
"MaxPoint").getValue();
73 Math3d::SetVec(validResultBoundingBoxMin, minPoint(0), minPoint(1), minPoint(2));
74 Math3d::SetVec(validResultBoundingBoxMax, maxPoint(0), maxPoint(1), maxPoint(2));
76 minPixelsPerRegion = getProperty<float>(
"MinPixelsPerRegion").getValue();
77 maxEpipolarDistance = getProperty<float>(
"MaxEpipolarDistance").getValue();
78 std::string colorParemeterFilename = getProperty<std::string>(
"ColorParameterFile").getValue();
82 ARMARX_ERROR <<
"Could not find color parameter file in ArmarXDataPath: "
83 << colorParemeterFilename;
87 if (!segmentableRecognition)
89 colorParameters.reset(
new CColorParameterSet());
91 if (!colorParameters->LoadFromFile(colorParemeterFilename.c_str()))
93 throw armarx::LocalException(
"Could not read color parameter file.");
98 segmentableRecognition.reset(
new CSegmentableRecognition());
103 ImageFormatInfo imageFormat = getImageFormat();
104 contextGL.reset(
new CGLContext());
108 contextGL->CreateContext(imageFormat.dimension.width, imageFormat.dimension.height);
112 contextGL->MakeCurrent();
114 m_pOpenGLVisualizer.reset(
new COpenGLVisualizer());
115 m_pOpenGLVisualizer->InitByCalibration(getStereoCalibration()->GetRightCalibration());
120 bool success = segmentableRecognition->InitWithoutDatabase(colorParameters.get(),
121 getStereoCalibration());
125 throw armarx::LocalException(
"Could not initialize segmentable object database.");
128 float sizeRatioThreshold = getProperty<float>(
"SizeRatioThreshold").getValue();
129 float correlationThreshold = getProperty<float>(
"CorrelationThreshold").getValue();
130 segmentableRecognition->GetObjectDatabase()->SetRecognitionThresholds(sizeRatioThreshold,
131 correlationThreshold);
151 std::string dataPath = recognitionWrapper->getDataFiles();
152 ObjectColor color = recognitionWrapper->getObjectColor();
154 std::string colorString;
155 CColorParameterSet::Translate(color, colorString);
156 ARMARX_VERBOSE <<
"Color: " << (int)color <<
" (" << colorString <<
")";
160 if (!segmentableRecognition->GetObjectDatabase()->AddClass(
161 dataPath,
"", objectClassEntity->getName()))
163 ARMARX_INFO <<
"Skipped object class: " << objectClassEntity->getName();
168 std::string className = objectClassEntity->getName();
169 objectColors.insert(std::make_pair(className, color));
176 <<
"Datapath is empty for " << objectClassEntity->getName()
177 <<
" object class - make sure you set the correct model file in PriorKnowledge!";
183 memoryx::ObjectLocalizationResultList
184 SegmentableObjectRecognition::localizeObjectClasses(
185 const std::vector<std::string>& objectClassNames,
186 CByteImage** cameraImages,
187 armarx::MetaInfoSizeBasePtr imageMetaInfo,
188 CByteImage** resultImages)
191 if (objectClassNames.size() < 1)
193 ARMARX_WARNING <<
"objectClassNames.size() = " << objectClassNames.size()
194 <<
", something is wrong here! ";
196 memoryx::ObjectLocalizationResultList resultList;
200 std::string allObjectNames;
202 for (
size_t i = 0; i < objectClassNames.size(); i++)
204 allObjectNames.append(
" ");
205 allObjectNames.append(objectClassNames.at(i));
210 contextGL->MakeCurrent();
212 Object3DList objectList;
214 if (objectClassNames.size() < 10)
218 for (
size_t i = 0; i < objectClassNames.size(); i++)
220 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
221 ARMARX_VERBOSE <<
"Localizing " << objectClassNames.at(i) <<
", color: " << color
224 segmentableRecognition->DoRecognitionSingleObject(cameraImages,
226 objectClassNames.at(i).c_str(),
229 objectColors[objectClassNames.at(i)],
231 getImagesAreUndistorted());
233 Object3DList result = segmentableRecognition->GetObject3DList();
235 for (
size_t j = 0; j < result.size(); j++)
237 objectList.push_back(result.at(j));
247 segmentableRecognition->DoRecognition(cameraImages,
254 getImagesAreUndistorted());
256 objectList = segmentableRecognition->GetObject3DList();
260 ARMARX_INFO <<
"Found " << objectList.size() <<
" objects";
264 if (objectList.size() == 0)
269 for (
size_t i = 0; i < objectClassNames.size(); i++)
271 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
277 visualizeResults(objectList, resultImages);
280 const auto agentName = getProperty<std::string>(
"AgentName").getValue();
282 memoryx::ObjectLocalizationResultList resultList;
284 for (Object3DList::iterator iter = objectList.begin(); iter != objectList.end(); iter++)
288 (
std::find(objectClassNames.begin(), objectClassNames.end(), iter->sName) !=
289 objectClassNames.end());
291 if (iter->localizationValid && queriedClass)
293 float x = iter->pose.translation.x;
294 float y = iter->pose.translation.y;
295 float z = iter->pose.translation.z;
297 if (seq.count(iter->sName))
303 seq[iter->sName] = 0;
309 mapValues[
"y"] =
new Variant(y);
310 mapValues[
"z"] =
new Variant(z);
311 mapValues[
"name"] =
new Variant(iter->sName);
312 mapValues[
"sequence"] =
new Variant(seq[iter->sName]);
313 mapValues[
"timestamp"] =
new Variant(imageMetaInfo->timeProvided / 1000.0 / 1000.0);
314 debugObserver->setDebugChannel(
"ObjectRecognition", mapValues);
318 if (
x > validResultBoundingBoxMin.x && y > validResultBoundingBoxMin.y &&
319 z > validResultBoundingBoxMin.z &&
x < validResultBoundingBoxMax.x &&
320 y < validResultBoundingBoxMax.y && z < validResultBoundingBoxMax.z)
323 memoryx::ObjectLocalizationResult result;
326 Eigen::Vector3f position(
327 iter->pose.translation.x, iter->pose.translation.y, iter->pose.translation.z);
329 orientation << iter->pose.rotation.r1, iter->pose.rotation.r2,
330 iter->pose.rotation.r3, iter->pose.rotation.r4, iter->pose.rotation.r5,
331 iter->pose.rotation.r6, iter->pose.rotation.r7, iter->pose.rotation.r8,
332 iter->pose.rotation.r9;
340 result.positionNoise = calculateLocalizationUncertainty(
341 iter->region_left.centroid, iter->region_right.centroid);
344 result.recognitionCertainty =
345 0.5f + 0.5f * calculateRecognitionCertainty(iter->sName, *iter);
346 result.objectClassName = iter->sName;
349 resultList.push_back(result);
359 ARMARX_VERBOSE <<
"Finished localizing " << objectClassNames.at(0);
365 SegmentableObjectRecognition::calculateRecognitionCertainty(
const std::string& objectClassName,
366 const Object3DEntry& entry)
368 float foundProb = entry.quality * entry.quality2;
369 float notFoundProb = (1 - entry.quality) * (1 - entry.quality2);
376 return foundProb / (foundProb + notFoundProb);
380 SegmentableObjectRecognition::visualizeResults(
const Object3DList& objectList,
381 CByteImage** resultImages)
383 m_pOpenGLVisualizer->ActivateShading(
true);
384 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
387 m_pOpenGLVisualizer->SetProjectionMatrix(getStereoCalibration()->GetRightCalibration());
389 m_pOpenGLVisualizer->Clear();
392 for (
int i = 0; i < (int)objectList.size(); i++)
394 const Object3DEntry& entry = objectList.at(i);
395 CSegmentableDatabase::DrawObjectFromFile(
396 m_pOpenGLVisualizer.get(), entry.sOivFilePath.c_str(), entry.pose);
399 const int nImageIndex = 1;
401 if (resultImages && resultImages[nImageIndex])
403 CByteImage tempImage(resultImages[nImageIndex]);
404 m_pOpenGLVisualizer->GetImage(&tempImage);
405 ::ImageProcessor::FlipY(&tempImage, &tempImage);
406 const int nBytes = 3 * tempImage.width * tempImage.height;
407 const unsigned char* pixels = tempImage.pixels;
408 unsigned char* output = resultImages[nImageIndex]->pixels;
410 for (
int i = 0; i < nBytes; i += 3)
414 const unsigned char g = pixels[i];