24 #include <Image/PrimitivesDrawerCV.h>
25 #include <Image/PrimitivesDrawer.h>
35 #include <SimoxUtility/algorithm/string/string_tools.h>
37 #include <Image/ImageProcessor.h>
38 #include <Image/IplImageAdaptor.h>
40 #include <Eigen/Geometry>
48 stereoMatcher =
new CStereoMatcher();
50 model = cv::face::FisherFaceRecognizer(0, 1000.0);
55 std::string trainingDataPath = getProperty<std::string>(
"trainingDataPath").getValue();
59 ARMARX_ERROR <<
"Could not find data file in ArmarXDataPath: " << trainingDataPath;
62 std::string classifierFileName = getProperty<std::string>(
"classifierFileName").getValue();
66 ARMARX_ERROR <<
"Could not find data file in ArmarXDataPath: " << classifierFileName;
69 if (!std::filesystem::is_directory(trainingDataPath))
75 std::vector<std::string> fileNames;
77 for (
auto iter = std::filesystem::directory_iterator(trainingDataPath);
78 iter != std::filesystem::directory_iterator(); ++iter)
80 fileNames.push_back(iter->path().string());
83 std::sort(fileNames.begin(), fileNames.end());
85 std::string currentLabel;
87 std::vector<cv::Mat> images;
88 std::vector<int>
index;
90 for (std::string& fileName : fileNames)
92 const auto stem = std::filesystem::path(fileName).stem().string();
94 std::string label = strs[0];
96 if (currentLabel != label)
98 labels[labels.size()] = label;
101 images.push_back(cv::imread(fileName, CV_LOAD_IMAGE_GRAYSCALE));
102 index.push_back(labels.size() - 1);
105 ARMARX_LOG <<
"total number of classes: " << labels.size();
107 model->train(images,
index);
109 faceImageSize = images[0].size();
111 classifier.load(classifierFileName);
118 stereoMatcher->InitCameraParameters(getStereoCalibration(),
false);
124 delete stereoMatcher;
135 throw std::logic_error {
"bool armarx::FaceRecognition::addObjectClass not implemented yet"};
142 memoryx::ObjectLocalizationResultList resultList;
143 cv::Mat result = cv::cvarrToMat(IplImageAdaptor::Adapt(resultImages[0]));
146 const cv::Mat tempRGBImage = cv::cvarrToMat(IplImageAdaptor::Adapt(cameraImages[0]));
148 cv::cvtColor(tempRGBImage, original, CV_RGB2BGR);
151 cv::cvtColor(original, gray, CV_BGR2GRAY);
153 std::vector<cv::Rect_<int>> faces;
154 classifier.detectMultiScale(gray, faces, 1.05, 2);
156 const std::string refFrame = getProperty<std::string>(
"ReferenceFrameName").getValue();
157 const std::string agentName = getProperty<std::string>(
"AgentName").getValue();
160 for (cv::Rect rect : faces)
162 cv::Mat face = gray(rect);
165 cv::resize(face, faceResized, faceImageSize, 1.0, 1.0, cv::INTER_CUBIC);
167 int predictedLabel = -1;
168 double predictedConfidence = 0.0;
169 model->predict(faceResized, predictedLabel, predictedConfidence);
171 std::string temp = labels[predictedLabel] +
"=%0.1f";
172 std::string label = cv::format(temp.c_str(), predictedConfidence);
175 if (predictedLabel < 0)
177 cv::rectangle(result, rect, CV_RGB(0, 0, 255), 1);
181 cv::rectangle(result, rect, CV_RGB(0, 255, 0), 1);
183 int posX =
std::max(rect.tl().x - 10, 0);
184 int posY =
std::max(rect.tl().y - 10, 0);
185 putText(result, label,
cv::Point(posX, posY), cv::FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0, 255, 0), 2.0);
187 if (std::find(objectClassNames.begin(), objectClassNames.end(), label) != objectClassNames.end())
190 CByteImage* imgRightGray =
new CByteImage(cameraImages[1]->width, cameraImages[1]->height, CByteImage::eGrayScale);
191 CByteImage* imgLeftGray =
new CByteImage(cameraImages[1]->width, cameraImages[1]->height, CByteImage::eGrayScale);
192 ::ImageProcessor::ConvertImage(cameraImages[0], imgLeftGray);
193 ::ImageProcessor::ConvertImage(cameraImages[1], imgRightGray);
195 Vec2d vCorrespondingPointRight;
197 const int nDispMin = stereoMatcher->GetDisparityEstimate(10000);
198 const int nDispMax = stereoMatcher->GetDisparityEstimate(500);
202 int nMatchingResult = stereoMatcher->Match(imgLeftGray, imgRightGray, (
int) rect.x + rect.width / 2.0, (
int) rect.y + rect.height / 2.0,
203 std::max(rect.width, rect.height), nDispMin, nDispMax, vCorrespondingPointRight, vPoint3D, 0.7f,
true);
209 if (nMatchingResult >= 0)
211 Eigen::Vector3f position(vPoint3D.x, vPoint3D.y, vPoint3D.z);
214 memoryx::ObjectLocalizationResult result;
218 result.recognitionCertainty = 1.0 / predictedConfidence;
219 result.positionNoise = calculateLocalizationUncertainty(position);
220 result.objectClassName = label;
222 resultList.push_back(result);