24 #include <Image/PrimitivesDrawerCV.h>
25 #include <Image/PrimitivesDrawer.h>
38 #include <SimoxUtility/algorithm/string/string_tools.h>
40 #include <Image/ImageProcessor.h>
41 #include <Image/IplImageAdaptor.h>
43 #include <Eigen/Geometry>
44 #include <Calibration/Calibration.h>
52 usingProxy(
"FaceRecognition");
53 usingProxy(
"LongtermMemory");
54 usingProxy(getProperty<std::string>(
"ImageProviderName").getValue());
57 offeringTopic(
"TextToSpeech");
63 getProxy(faceReg,
"FaceRecognition");
64 getProxy(ltm,
"LongtermMemory");
65 getProxy(imageProvider, getProperty<std::string>(
"ImageProviderName").getValue());
69 this->calibration.reset(
new CCalibration());
70 calibration->SetCameraParameters(987.99, 956.82, 639.32, 415.64,
72 Math3d::unit_mat, Math3d::zero_vec, 1280, 720);
74 tts = getTopic<armarx::TextListenerInterfacePrx>(
"TextToSpeech");
75 if (!ltm->hasSegment(faceSegmentName))
77 faceSegmentPrx = memoryx::EntityMemorySegmentInterfacePrx::checkedCast(ltm->addGenericSegment(faceSegmentName));
81 faceSegmentPrx = memoryx::EntityMemorySegmentInterfacePrx::checkedCast(ltm->getSegment(faceSegmentName));
92 getConfigIdentifier()));
100 memoryx::ObjectLocalizationResultList result;
104 auto faceLocations = faceReg->calculateFaceLocations();
106 auto agentName = getProperty<std::string>(
"AgentName").getValue();
107 auto cameraFrame = getProperty<std::string>(
"CameraFrameName").getValue();
108 for (visionx::FaceLocation faceLocation : faceLocations)
110 memoryx::EntityPtr oldFace = memoryx::EntityPtr::dynamicCast(faceSegmentPrx->getEntityByName(faceLocation.label));
111 bool updateFaceMemory =
true;
112 ARMARX_DEBUG <<
deactivateSpam(1) <<
"Rect: " << faceLocation.topLeft.e0 <<
", " << faceLocation.topLeft.e1 <<
", " << faceLocation.bottomRight.e0 <<
", " << faceLocation.bottomRight.e1 ;
113 float centerX = (faceLocation.bottomRight.e0 + faceLocation.topLeft.e0) * 0.5f;
114 float centerY = (faceLocation.bottomRight.e1 + faceLocation.topLeft.e1) * 0.5f;
115 memoryx::ObjectLocalizationResult r;
116 r.objectClassName =
"face";
117 r.instanceName = faceLocation.label;
121 float faceHeightPixel = faceLocation.bottomRight.e1 - faceLocation.topLeft.e1;
122 float distance = calibration->GetCameraParameters().focalLength.y * 160.f / (faceHeightPixel);
123 this->calibration->ImageToCameraCoordinates(
Vec2d {centerX, centerY}, cameraCoords,
distance,
false);
126 r.position =
new FramedPosition(Eigen::Vector3f(cameraCoords.x, cameraCoords.y,
distance), cameraFrame, agentName);
129 r.recognitionCertainty = 0.7f;
130 Eigen::Vector3f cov(10, 10, 10000);
135 if (faceLocation.label ==
"Unknown")
139 if (oldFace && oldFace->hasAttribute(
"LastGreetingTime"))
141 VariantPtr updateTimeVariant = VariantPtr::dynamicCast(oldFace->getAttribute(
"LastGreetingTime")->getValue());
144 updateFaceMemory &= updateTimeVariant->get<
TimestampVariant>()->toTime() + IceUtil::Time::secondsDouble(getProperty<float>(
"GreetAgainDelay").getValue()) <
TimeUtil::GetTime();
149 if (updateFaceMemory)
166 face->setName(faceLocation.label);
168 face->putAttribute(
"ImagePosition",
new armarx::Vector2(centerX, centerY));
171 float prob = updateAndCheckFaceExistenceProbability(faceLocation);
173 if (prob > getProperty<float>(
"GreetThreshold").getValue())
176 auto tts_label = simox::alg::replace_all(faceLocation.label,
"mirko",
"mir ko");
178 tts->reportTextWithParams(
"Hello %1%.", {tts_label});
184 faceSegmentPrx->upsertEntityByName(faceLocation.label, face);
192 float visionx::DeepFaceRecognition::updateAndCheckFaceExistenceProbability(
const visionx::FaceLocation& faceLocation)
194 auto& timeConfList = faceConfidenceHistory[faceLocation.label];
196 double filterRadius = getProperty<float>(
"FilterRadius").getValue();
197 double sigma = filterRadius / 2.5;
198 double sigmaSquare = sigma * sigma;
199 auto gaussianBellCurve = [&](
double x)
201 return exp(-
double((x) * (x)) / (sigmaSquare));
204 auto now = IceUtil::Time::now();
206 for (
auto it = timeConfList.begin(); it != timeConfList.end();)
210 double tDiff = (t - now).toMilliSecondsDouble() * 0.001;
211 double weight = gaussianBellCurve(tDiff);
215 it = timeConfList.erase(it);
223 timeConfList.push_back(std::make_pair(now, faceLocation.confidence));
225 for (
auto& timeConfPair : timeConfList)
228 double confidence = timeConfPair.second;
229 double tDiff = (t - now).toMilliSecondsDouble() * 0.001;
230 double weight = gaussianBellCurve(tDiff);
232 sum += confidence * weight;