28 #include <RobotAPI/interface/core/RobotStateObserverInterface.h>
32 #include <Image/ByteImage.h>
33 #include <Image/ImageProcessor.h>
36 #include <ObjectFinder/CompactRegionFilter.h>
37 #include <ObjectFinder/ObjectFinderStereo.h>
38 #include <SegmentableRecognition/SegmentableDatabase.h>
42 #include <gui/GLContext.h>
48 BlobRecognition::BlobRecognition()
52 BlobRecognition::~BlobRecognition()
57 BlobRecognition::onExitObjectLocalizerProcessor()
62 BlobRecognition::initRecognizer()
66 Eigen::Vector3f minPoint = getProperty<Eigen::Vector3f>(
"MinPoint").getValue();
67 Eigen::Vector3f maxPoint = getProperty<Eigen::Vector3f>(
"MaxPoint").getValue();
69 Math3d::SetVec(validResultBoundingBoxMin, minPoint(0), minPoint(1), minPoint(2));
70 Math3d::SetVec(validResultBoundingBoxMax, maxPoint(0), maxPoint(1), maxPoint(2));
72 minPixelsPerRegion = getProperty<float>(
"MinPixelsPerRegion").getValue();
73 maxEpipolarDistance = getProperty<float>(
"MaxEpipolarDistance").getValue();
74 std::string colorParameterFilename = getProperty<std::string>(
"ColorParameterFile").getValue();
78 ARMARX_ERROR <<
"Could not find color parameter file in ArmarXDataPath: "
79 << colorParameterFilename;
82 if (!objectFinderStereo)
84 colorParameters.reset(
new CColorParameterSet());
86 if (!colorParameters->LoadFromFile(colorParameterFilename.c_str()))
88 throw armarx::LocalException(
"Could not read color parameter file.");
91 objectFinderStereo.reset(
new CObjectFinderStereo());
93 ImageFormatInfo imageFormat = getImageFormat();
94 contextGL.reset(
new CGLContext());
95 contextGL->CreateContext(imageFormat.dimension.width, imageFormat.dimension.height);
96 contextGL->MakeCurrent();
98 m_pOpenGLVisualizer.reset(
new COpenGLVisualizer());
99 m_pOpenGLVisualizer->InitByCalibration(getStereoCalibration()->GetRightCalibration());
101 objectFinderStereo->SetColorParameterSet(colorParameters.get());
102 objectFinderStereo->SetRegionFilter(
new CCompactRegionFilter());
109 objectFinderStereo->Init(getStereoCalibration());
123 std::string dataPath = recognitionWrapper->getDataFiles();
124 ObjectColor color = recognitionWrapper->getObjectColor();
126 std::string colorString;
127 CColorParameterSet::Translate(color, colorString);
128 ARMARX_VERBOSE <<
"Color: " << (int)color <<
" (" << colorString <<
")";
133 std::string className = objectClassEntity->getName();
134 objectColors.insert(std::make_pair(className, color));
135 colorToObjectClass.insert(std::make_pair(color, className));
142 <<
"Datapath is empty for " << objectClassEntity->getName()
143 <<
" object class - make sure you set the correct model file in PriorKnowledge!";
149 memoryx::ObjectLocalizationResultList
150 BlobRecognition::localizeObjectClasses(
const std::vector<std::string>& objectClassNames,
151 CByteImage** cameraImages,
152 armarx::MetaInfoSizeBasePtr imageMetaInfo,
153 CByteImage** resultImages)
157 localRobot, robotStateComponent, imageMetaInfo->timeProvided / 1000.0);
159 objectFinderStereo->PrepareImages(cameraImages);
160 objectFinderStereo->ClearObjectList();
162 if (objectClassNames.size() < 1)
164 ARMARX_WARNING <<
"objectClassNames.size() = " << objectClassNames.size()
165 <<
", something is wrong here! ";
167 memoryx::ObjectLocalizationResultList resultList;
171 std::string allObjectNames;
172 for (
size_t i = 0; i < objectClassNames.size(); i++)
174 allObjectNames.append(
" ");
175 allObjectNames.append(objectClassNames.at(i));
178 contextGL->MakeCurrent();
183 for (
size_t i = 0; i < objectClassNames.size(); i++)
185 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
186 ARMARX_VERBOSE <<
"Localizing " << objectClassNames.at(i) <<
", color: " << color <<
flush;
188 ObjectColor color = objectColors[objectClassNames.at(i)];
190 objectFinderStereo->FindObjects(
191 cameraImages, resultImages, color, minPixelsPerRegion,
true);
194 objectFinderStereo->Finalize(validResultBoundingBoxMin.z,
195 validResultBoundingBoxMax.z,
199 getImagesAreUndistorted());
200 Object3DList objectList = objectFinderStereo->m_objectList;
202 ARMARX_INFO <<
"Found " << objectList.size() <<
" objects";
206 if (objectList.size() == 0)
211 for (
size_t i = 0; i < objectClassNames.size(); i++)
213 CColorParameterSet::Translate(objectColors[objectClassNames.at(i)], color);
218 const auto agentName = getProperty<std::string>(
"AgentName").getValue();
219 memoryx::ObjectLocalizationResultList resultList;
221 for (Object3DEntry
const& entry : objectList)
223 bool queriedClass =
false;
225 if (colorToObjectClass.count(entry.color))
227 queriedClass = (
std::find(objectClassNames.begin(),
228 objectClassNames.end(),
229 colorToObjectClass[entry.color]) != objectClassNames.end());
235 std::string objectName = colorToObjectClass[entry.color];
236 float x = entry.pose.translation.x;
237 float y = entry.pose.translation.y;
238 float z = entry.pose.translation.z;
240 if (seq.count(objectName))
252 mapValues[
"y"] =
new Variant(y);
253 mapValues[
"z"] =
new Variant(z);
254 mapValues[
"name"] =
new Variant(objectName);
255 mapValues[
"sequence"] =
new Variant(seq[objectName]);
256 mapValues[
"timestamp"] =
new Variant(imageMetaInfo->timeProvided / 1000.0 / 1000.0);
257 debugObserver->setDebugChannel(
"ObjectRecognition", mapValues);
260 if (
x > validResultBoundingBoxMin.x && y > validResultBoundingBoxMin.y &&
261 z > validResultBoundingBoxMin.z &&
x < validResultBoundingBoxMax.x &&
262 y < validResultBoundingBoxMax.y && z < validResultBoundingBoxMax.z)
265 memoryx::ObjectLocalizationResult result;
268 Eigen::Vector3f positionVec(
269 entry.pose.translation.x, entry.pose.translation.y, entry.pose.translation.z);
274 float expectedHeight = 1020.0;
275 if (std::fabs(position->z - expectedHeight) < 50.0)
278 position->z = expectedHeight;
279 position->changeFrame(
localRobot, referenceFrameName);
284 orientation->changeFrame(
localRobot, referenceFrameName);
286 result.position = position;
287 result.orientation = orientation;
290 result.positionNoise = calculateLocalizationUncertainty(
291 entry.region_left.centroid, entry.region_right.centroid);
294 result.recognitionCertainty =
295 0.5f + 0.5f * calculateRecognitionCertainty(objectName, entry);
296 result.objectClassName = objectName;
299 resultList.push_back(result);
304 <<
"Ignoring object localization result with height "
316 ARMARX_VERBOSE <<
"Finished localizing " << objectClassNames.at(0);
322 BlobRecognition::calculateRecognitionCertainty(
const std::string& objectClassName,
323 const Object3DEntry& entry)
325 float foundProb = entry.quality * entry.quality2;
326 float notFoundProb = (1 - entry.quality) * (1 - entry.quality2);
333 return foundProb / (foundProb + notFoundProb);