34 #include <Image/ByteImage.h>
35 #include <SegmentableRecognition/SegmentableDatabase.h>
36 #include <Image/ImageProcessor.h>
45 #include <gui/GLContext.h>
52 SegmentableObjectRecognition::SegmentableObjectRecognition()
56 SegmentableObjectRecognition::~SegmentableObjectRecognition()
61 void SegmentableObjectRecognition::onExitObjectLocalizerProcessor()
66 bool 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: " << colorParemeterFilename;
86 if (!segmentableRecognition)
88 colorParameters.reset(
new CColorParameterSet());
90 if (!colorParameters->LoadFromFile(colorParemeterFilename.c_str()))
92 throw armarx::LocalException(
"Could not read color parameter file.");
97 segmentableRecognition.reset(
new CSegmentableRecognition());
102 ImageFormatInfo imageFormat = getImageFormat();
103 contextGL.reset(
new CGLContext());
107 contextGL->CreateContext(imageFormat.dimension.width, imageFormat.dimension.height);
111 contextGL->MakeCurrent();
113 m_pOpenGLVisualizer.reset(
new COpenGLVisualizer());
114 m_pOpenGLVisualizer->InitByCalibration(getStereoCalibration()->GetRightCalibration());
119 bool success = segmentableRecognition->InitWithoutDatabase(colorParameters.get(), getStereoCalibration());
123 throw armarx::LocalException(
"Could not initialize segmentable object database.");
126 float sizeRatioThreshold = getProperty<float>(
"SizeRatioThreshold").getValue();
127 float correlationThreshold = getProperty<float>(
"CorrelationThreshold").getValue();
128 segmentableRecognition->GetObjectDatabase()->SetRecognitionThresholds(sizeRatioThreshold, correlationThreshold);
145 std::string dataPath = recognitionWrapper->getDataFiles();
146 ObjectColor color = recognitionWrapper->getObjectColor();
148 std::string colorString;
149 CColorParameterSet::Translate(color, colorString);
150 ARMARX_VERBOSE <<
"Color: " << (int)color <<
" (" << colorString <<
")";
154 if (!segmentableRecognition->GetObjectDatabase()->AddClass(dataPath,
"", objectClassEntity->getName()))
156 ARMARX_INFO <<
"Skipped object class: " << objectClassEntity->getName();
161 std::string className = objectClassEntity->getName();
162 objectColors.insert(std::make_pair(className, color));
168 ARMARX_WARNING <<
"Datapath is empty for " << objectClassEntity->getName() <<
" object class - make sure you set the correct model file in PriorKnowledge!";
174 memoryx::ObjectLocalizationResultList SegmentableObjectRecognition::localizeObjectClasses(
const std::vector<std::string>& objectClassNames, CByteImage** cameraImages, armarx::MetaInfoSizeBasePtr imageMetaInfo, CByteImage** resultImages)
177 if (objectClassNames.size() < 1)
179 ARMARX_WARNING <<
"objectClassNames.size() = " << objectClassNames.size() <<
", something is wrong here! ";
181 memoryx::ObjectLocalizationResultList resultList;
185 std::string allObjectNames;
187 for (
size_t i = 0; i < objectClassNames.size(); i++)
189 allObjectNames.append(
" ");
190 allObjectNames.append(objectClassNames.at(i));
195 contextGL->MakeCurrent();
197 Object3DList objectList;
199 if (objectClassNames.size() < 10)
203 for (
size_t i = 0; i < objectClassNames.size(); i++)
205 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
206 ARMARX_VERBOSE <<
"Localizing " << objectClassNames.at(i) <<
", color: " << color <<
flush;
208 segmentableRecognition->DoRecognitionSingleObject(cameraImages, resultImages, objectClassNames.at(i).c_str(), minPixelsPerRegion, maxEpipolarDistance, objectColors[objectClassNames.at(i)],
false, getImagesAreUndistorted());
210 Object3DList result = segmentableRecognition->GetObject3DList();
212 for (
size_t j = 0; j < result.size(); j++)
214 objectList.push_back(result.at(j));
224 segmentableRecognition->DoRecognition(cameraImages, resultImages, minPixelsPerRegion,
false, maxEpipolarDistance, eNone,
false, getImagesAreUndistorted());
226 objectList = segmentableRecognition->GetObject3DList();
231 ARMARX_INFO <<
"Found " << objectList.size() <<
" objects";
236 if (objectList.size() == 0)
241 for (
size_t i = 0; i < objectClassNames.size(); i++)
243 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
249 visualizeResults(objectList, resultImages);
252 const auto agentName = getProperty<std::string>(
"AgentName").getValue();
254 memoryx::ObjectLocalizationResultList resultList;
256 for (Object3DList::iterator iter = objectList.begin() ; iter != objectList.end() ; iter++)
259 bool queriedClass = (std::find(objectClassNames.begin(), objectClassNames.end(), iter->sName) != objectClassNames.end());
261 if (iter->localizationValid && queriedClass)
263 float x = iter->pose.translation.x;
264 float y = iter->pose.translation.y;
265 float z = iter->pose.translation.z;
267 if (seq.count(iter->sName))
273 seq[iter->sName] = 0;
280 mapValues[
"x"] =
new Variant(x);
281 mapValues[
"y"] =
new Variant(y);
282 mapValues[
"z"] =
new Variant(z);
283 mapValues[
"name"] =
new Variant(iter->sName);
284 mapValues[
"sequence"] =
new Variant(seq[iter->sName]);
285 mapValues[
"timestamp"] =
new Variant(imageMetaInfo->timeProvided / 1000.0 / 1000.0);
286 debugObserver->setDebugChannel(
"ObjectRecognition", mapValues);
291 if (x > validResultBoundingBoxMin.x && y > validResultBoundingBoxMin.y && z > validResultBoundingBoxMin.z &&
292 x < validResultBoundingBoxMax.x && y < validResultBoundingBoxMax.y && z < validResultBoundingBoxMax.z)
295 memoryx::ObjectLocalizationResult result;
298 Eigen::Vector3f position(iter->pose.translation.x, iter->pose.translation.y, iter->pose.translation.z);
300 orientation << iter->pose.rotation.r1, iter->pose.rotation.r2, iter->pose.rotation.r3,
301 iter->pose.rotation.r4, iter->pose.rotation.r5, iter->pose.rotation.r6,
302 iter->pose.rotation.r7, iter->pose.rotation.r8, iter->pose.rotation.r9;
308 result.positionNoise = calculateLocalizationUncertainty(iter->region_left.centroid, iter->region_right.centroid);
311 result.recognitionCertainty = 0.5f + 0.5f * calculateRecognitionCertainty(iter->sName, *iter);
312 result.objectClassName = iter->sName;
315 resultList.push_back(result);
319 ARMARX_VERBOSE_S <<
"Refused unrealistic localization at position: " << x <<
" " << y <<
" " << z;
324 ARMARX_VERBOSE <<
"Finished localizing " << objectClassNames.at(0);
331 float SegmentableObjectRecognition::calculateRecognitionCertainty(
const std::string& objectClassName,
const Object3DEntry& entry)
333 float foundProb = entry.quality * entry.quality2;
334 float notFoundProb = (1 - entry.quality) * (1 - entry.quality2);
341 return foundProb / (foundProb + notFoundProb);
346 void SegmentableObjectRecognition::visualizeResults(
const Object3DList& objectList, CByteImage** resultImages)
348 m_pOpenGLVisualizer->ActivateShading(
true);
349 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
352 m_pOpenGLVisualizer->SetProjectionMatrix(getStereoCalibration()->GetRightCalibration());
354 m_pOpenGLVisualizer->Clear();
357 for (
int i = 0; i < (int) objectList.size(); i++)
359 const Object3DEntry& entry = objectList.at(i);
360 CSegmentableDatabase::DrawObjectFromFile(m_pOpenGLVisualizer.get(), entry.sOivFilePath.c_str(), entry.pose);
363 const int nImageIndex = 1;
365 if (resultImages && resultImages[nImageIndex])
367 CByteImage tempImage(resultImages[nImageIndex]);
368 m_pOpenGLVisualizer->GetImage(&tempImage);
369 ::ImageProcessor::FlipY(&tempImage, &tempImage);
370 const int nBytes = 3 * tempImage.width * tempImage.height;
371 const unsigned char* pixels = tempImage.pixels;
372 unsigned char* output = resultImages[nImageIndex]->pixels;
374 for (
int i = 0; i < nBytes; i += 3)
378 const unsigned char g = pixels[i];