33 #include <boost/filesystem.hpp>
42 #include <Math/Math3d.h>
59 settings_priorMemory = getProperty<std::string>(
"PriorKnowledgeMemoryProxyName").getValue();
60 usingProxy(settings_priorMemory);
62 featureCalculator.addFeature<
CHArea>();
63 featureCalculator.addFeature<
CHVolume>();
65 featureCalculator.addFeature<
BBVolume>();
66 featureCalculator.addFeature<
BBArea>();
80 memoryPrx = getProxy<PriorKnowledgeInterfacePrx>(settings_priorMemory);
81 classesSegmentPrx = memoryPrx->getObjectClassesSegment();
82 databasePrx = memoryPrx->getCommonStorage();
86 connected = databasePrx->isConnected();
90 ObjectShapeClassification::refetchObjects()
93 boost::recursive_mutex::scoped_lock lock(mutexEntities);
94 EntityIdList ids = classesSegmentPrx->getAllEntityIds();
97 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
99 const EntityBasePtr entity = classesSegmentPrx->getEntityById(*it);
100 const ObjectClassPtr objClass = ObjectClassPtr::dynamicCast(entity);
105 dbObjects.push_back(objClass);
111 ObjectShapeClassification::checkFeatures()
113 for (
auto optr : dbObjects)
129 std::string ivFile =
"";
134 fileManager->ensureFileInCache(obj->getAttribute(
ATTR_IV_FILE), ivFile,
false);
139 ARMARX_WARNING <<
"No IV file found for object" << obj->getName();
147 template <
typename Pair>
151 return a.second < b.second;
156 ObjectShapeClassification::compareToDB(
Points& points)
158 std::vector<std::pair<std::string, Points>> objectsPoints;
161 for (
auto& obj : dbObjects)
163 auto points = getPointsFromIV(obj);
165 if (points.size() != 0)
167 objectsPoints.push_back(
TaggedPoints(obj->getName(), points));
173 auto newObjectFeatures = featureCalculator.getFeatures(points);
192 for (
const auto& feat : newObjectFeatures)
199 std::map<std::string, std::map<std::string, double>> objectDiffs;
200 std::map<std::string, double> maxDiffs;
205 for (
const auto& obj : objectsPoints)
208 auto features = featureCalculator.getFeatures(obj.second);
209 std::map<std::string, double> diffs;
212 for (
const auto& p : newObjectFeatures)
214 auto& feature = p.second;
215 double diff = feature->compare(*features[feature->name()]);
216 diffs[feature->name()] = diff;
219 double oldValue = maxDiffs[feature->name()];
220 maxDiffs[feature->name()] = (diff > oldValue) ? diff : oldValue;
223 objectDiffs[obj.first] = diffs;
226 std::map<std::string, double> objectDiffsTotal;
229 for (
const auto& obj : objectDiffs)
233 double totalDiff = std::accumulate(obj.second.begin(),
236 [&](
double prev,
const std::pair<std::string, double> p)
238 double diff = (p.second / maxDiffs[p.first]);
239 ARMARX_VERBOSE_S <<
"-- " << p.first <<
": " << diff;
240 return prev + (p.second / maxDiffs[p.first]);
242 objectDiffsTotal[obj.first] = totalDiff;
243 ARMARX_VERBOSE <<
"--+ Total score(" << obj.first <<
"): " << totalDiff <<
"\n";
248 *std::min_element(objectDiffsTotal.begin(), objectDiffsTotal.end(),
by_second());
250 ARMARX_INFO <<
"Best match found: " << bestMatch.first <<
" with score of " << bestMatch.second;
256 ObjectShapeClassification::matchToFoundPointCloud(
const Points& newPoints,
257 const Points& foundPointCloud)
276 for (
size_t i = 0; i < segmentedObjectPoints.size(); i++)
278 points.push_back(armarx::Vector3Ptr::dynamicCast(segmentedObjectPoints.at(i))->toEigen());
285 ARMARX_INFO <<
"Connected to DB, starting classification";
292 matchToFoundPointCloud(points, foundPointCloud.second);
296 return foundPointCloud.first;
300 ARMARX_WARNING <<
"No connection to DB. Unable to start classification";
303 return "ERROR: No similar object found";