27 #include <RobotAPI/interface/core/RobotStateObserverInterface.h>
32 #include <Image/ByteImage.h>
33 #include <Image/ImageProcessor.h>
36 #include <ObjectFinder/ObjectFinderStereo.h>
37 #include <ObjectFinder/CompactRegionFilter.h>
38 #include <SegmentableRecognition/SegmentableDatabase.h>
46 #include <gui/GLContext.h>
53 BlobRecognition::BlobRecognition()
57 BlobRecognition::~BlobRecognition()
62 void BlobRecognition::onExitObjectLocalizerProcessor()
67 bool BlobRecognition::initRecognizer()
71 Eigen::Vector3f minPoint = getProperty<Eigen::Vector3f>(
"MinPoint").getValue();
72 Eigen::Vector3f maxPoint = getProperty<Eigen::Vector3f>(
"MaxPoint").getValue();
74 Math3d::SetVec(validResultBoundingBoxMin, minPoint(0), minPoint(1), minPoint(2));
75 Math3d::SetVec(validResultBoundingBoxMax, maxPoint(0), maxPoint(1), maxPoint(2));
77 minPixelsPerRegion = getProperty<float>(
"MinPixelsPerRegion").getValue();
78 maxEpipolarDistance = getProperty<float>(
"MaxEpipolarDistance").getValue();
79 std::string colorParameterFilename = getProperty<std::string>(
"ColorParameterFile").getValue();
83 ARMARX_ERROR <<
"Could not find color parameter file in ArmarXDataPath: " << colorParameterFilename;
86 if (!objectFinderStereo)
88 colorParameters.reset(
new CColorParameterSet());
90 if (!colorParameters->LoadFromFile(colorParameterFilename.c_str()))
92 throw armarx::LocalException(
"Could not read color parameter file.");
95 objectFinderStereo.reset(
new CObjectFinderStereo());
97 ImageFormatInfo imageFormat = getImageFormat();
98 contextGL.reset(
new CGLContext());
99 contextGL->CreateContext(imageFormat.dimension.width, imageFormat.dimension.height);
100 contextGL->MakeCurrent();
102 m_pOpenGLVisualizer.reset(
new COpenGLVisualizer());
103 m_pOpenGLVisualizer->InitByCalibration(getStereoCalibration()->GetRightCalibration());
105 objectFinderStereo->SetColorParameterSet(colorParameters.get());
106 objectFinderStereo->SetRegionFilter(
new CCompactRegionFilter());
113 objectFinderStereo->Init(getStereoCalibration());
124 std::string dataPath = recognitionWrapper->getDataFiles();
125 ObjectColor color = recognitionWrapper->getObjectColor();
127 std::string colorString;
128 CColorParameterSet::Translate(color, colorString);
129 ARMARX_VERBOSE <<
"Color: " << (int)color <<
" (" << colorString <<
")";
134 std::string className = objectClassEntity->getName();
135 objectColors.insert(std::make_pair(className, color));
136 colorToObjectClass.insert(std::make_pair(color, className));
142 ARMARX_WARNING <<
"Datapath is empty for " << objectClassEntity->getName() <<
" object class - make sure you set the correct model file in PriorKnowledge!";
148 memoryx::ObjectLocalizationResultList BlobRecognition::localizeObjectClasses(
const std::vector<std::string>& objectClassNames, CByteImage** cameraImages, armarx::MetaInfoSizeBasePtr imageMetaInfo, CByteImage** resultImages)
153 objectFinderStereo->PrepareImages(cameraImages);
154 objectFinderStereo->ClearObjectList();
156 if (objectClassNames.size() < 1)
158 ARMARX_WARNING <<
"objectClassNames.size() = " << objectClassNames.size() <<
", something is wrong here! ";
160 memoryx::ObjectLocalizationResultList resultList;
164 std::string allObjectNames;
165 for (
size_t i = 0; i < objectClassNames.size(); i++)
167 allObjectNames.append(
" ");
168 allObjectNames.append(objectClassNames.at(i));
171 contextGL->MakeCurrent();
176 for (
size_t i = 0; i < objectClassNames.size(); i++)
178 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
179 ARMARX_VERBOSE <<
"Localizing " << objectClassNames.at(i) <<
", color: " << color <<
flush;
181 ObjectColor color = objectColors[objectClassNames.at(i)];
183 objectFinderStereo->FindObjects(cameraImages, resultImages, color, minPixelsPerRegion,
true);
187 objectFinderStereo->Finalize(validResultBoundingBoxMin.z, validResultBoundingBoxMax.z,
true, eNone, maxEpipolarDistance, getImagesAreUndistorted());
188 Object3DList objectList = objectFinderStereo->m_objectList;
190 ARMARX_INFO <<
"Found " << objectList.size() <<
" objects";
194 if (objectList.size() == 0)
199 for (
size_t i = 0; i < objectClassNames.size(); i++)
201 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
206 const auto agentName = getProperty<std::string>(
"AgentName").getValue();
207 memoryx::ObjectLocalizationResultList resultList;
209 for (Object3DEntry
const& entry : objectList)
211 bool queriedClass =
false;
213 if (colorToObjectClass.count(entry.color))
215 queriedClass = (std::find(objectClassNames.begin(), objectClassNames.end(), colorToObjectClass[entry.color]) != objectClassNames.end());
221 std::string objectName = colorToObjectClass[entry.color];
222 float x = entry.pose.translation.x;
223 float y = entry.pose.translation.y;
224 float z = entry.pose.translation.z;
226 if (seq.count(objectName))
237 mapValues[
"x"] =
new Variant(x);
238 mapValues[
"y"] =
new Variant(y);
239 mapValues[
"z"] =
new Variant(z);
240 mapValues[
"name"] =
new Variant(objectName);
241 mapValues[
"sequence"] =
new Variant(seq[objectName]);
242 mapValues[
"timestamp"] =
new Variant(imageMetaInfo->timeProvided / 1000.0 / 1000.0);
243 debugObserver->setDebugChannel(
"ObjectRecognition", mapValues);
246 if (x > validResultBoundingBoxMin.x && y > validResultBoundingBoxMin.y && z > validResultBoundingBoxMin.z &&
247 x < validResultBoundingBoxMax.x && y < validResultBoundingBoxMax.y && z < validResultBoundingBoxMax.z)
250 memoryx::ObjectLocalizationResult result;
253 Eigen::Vector3f positionVec(entry.pose.translation.x, entry.pose.translation.y, entry.pose.translation.z);
256 position->changeToGlobal(localRobot);
257 float expectedHeight = 1020.0;
258 if (std::fabs(position->z - expectedHeight) < 50.0)
261 position->z = expectedHeight;
262 position->changeFrame(localRobot, referenceFrameName);
266 orientation->changeFrame(localRobot, referenceFrameName);
268 result.position = position;
269 result.orientation = orientation;
272 result.positionNoise = calculateLocalizationUncertainty(entry.region_left.centroid, entry.region_right.centroid);
275 result.recognitionCertainty = 0.5f + 0.5f * calculateRecognitionCertainty(objectName, entry);
276 result.objectClassName = objectName;
279 resultList.push_back(result);
288 ARMARX_VERBOSE_S <<
"Refused unrealistic localization at position: " << x <<
" " << y <<
" " << z;
293 ARMARX_VERBOSE <<
"Finished localizing " << objectClassNames.at(0);
300 float BlobRecognition::calculateRecognitionCertainty(
const std::string& objectClassName,
const Object3DEntry& entry)
302 float foundProb = entry.quality * entry.quality2;
303 float notFoundProb = (1 - entry.quality) * (1 - entry.quality2);
310 return foundProb / (foundProb + notFoundProb);