25 #include <Eigen/Geometry>
27 #include <SimoxUtility/algorithm/string/string_tools.h>
36 #include <Image/ImageProcessor.h>
37 #include <Image/IplImageAdaptor.h>
38 #include <Image/PrimitivesDrawer.h>
39 #include <Image/PrimitivesDrawerCV.h>
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))
74 std::vector<std::string> fileNames;
76 for (
auto iter = std::filesystem::directory_iterator(trainingDataPath);
77 iter != std::filesystem::directory_iterator();
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);
117 stereoMatcher->InitCameraParameters(getStereoCalibration(),
false);
124 delete stereoMatcher;
138 throw std::logic_error{
"bool armarx::FaceRecognition::addObjectClass not implemented yet"};
141 memoryx::ObjectLocalizationResultList
143 CByteImage** cameraImages,
144 armarx::MetaInfoSizeBasePtr imageMetaInfo,
145 CByteImage** resultImages)
148 memoryx::ObjectLocalizationResultList resultList;
149 cv::Mat result = cv::cvarrToMat(IplImageAdaptor::Adapt(resultImages[0]));
152 const cv::Mat tempRGBImage = cv::cvarrToMat(IplImageAdaptor::Adapt(cameraImages[0]));
154 cv::cvtColor(tempRGBImage, original, CV_RGB2BGR);
157 cv::cvtColor(original, gray, CV_BGR2GRAY);
159 std::vector<cv::Rect_<int>> faces;
160 classifier.detectMultiScale(gray, faces, 1.05, 2);
162 const std::string refFrame = getProperty<std::string>(
"ReferenceFrameName").getValue();
163 const std::string agentName = getProperty<std::string>(
"AgentName").getValue();
166 <<
" possible faces";
167 for (cv::Rect rect : faces)
169 cv::Mat face = gray(rect);
172 cv::resize(face, faceResized, faceImageSize, 1.0, 1.0, cv::INTER_CUBIC);
174 int predictedLabel = -1;
175 double predictedConfidence = 0.0;
176 model->predict(faceResized, predictedLabel, predictedConfidence);
178 std::string temp = labels[predictedLabel] +
"=%0.1f";
179 std::string label = cv::format(temp.c_str(), predictedConfidence);
182 if (predictedLabel < 0)
184 cv::rectangle(result, rect, CV_RGB(0, 0, 255), 1);
188 cv::rectangle(result, rect, CV_RGB(0, 255, 0), 1);
190 int posX =
std::max(rect.tl().x - 10, 0);
191 int posY =
std::max(rect.tl().y - 10, 0);
195 cv::FONT_HERSHEY_PLAIN,
200 if (std::find(objectClassNames.begin(), objectClassNames.end(), label) !=
201 objectClassNames.end())
204 CByteImage* imgRightGray =
new CByteImage(
205 cameraImages[1]->width, cameraImages[1]->height, CByteImage::eGrayScale);
206 CByteImage* imgLeftGray =
new CByteImage(
207 cameraImages[1]->width, cameraImages[1]->height, CByteImage::eGrayScale);
208 ::ImageProcessor::ConvertImage(cameraImages[0], imgLeftGray);
209 ::ImageProcessor::ConvertImage(cameraImages[1], imgRightGray);
211 Vec2d vCorrespondingPointRight;
213 const int nDispMin = stereoMatcher->GetDisparityEstimate(10000);
214 const int nDispMax = stereoMatcher->GetDisparityEstimate(500);
218 int nMatchingResult = stereoMatcher->Match(imgLeftGray,
220 (
int)rect.x + rect.width / 2.0,
221 (
int)rect.y + rect.height / 2.0,
225 vCorrespondingPointRight,
234 if (nMatchingResult >= 0)
236 Eigen::Vector3f position(vPoint3D.x, vPoint3D.y, vPoint3D.z);
239 Eigen::AngleAxisf(-
M_PI, Eigen::Vector3f::UnitZ());
241 memoryx::ObjectLocalizationResult result;
246 result.recognitionCertainty = 1.0 / predictedConfidence;
247 result.positionNoise = calculateLocalizationUncertainty(position);
248 result.objectClassName = label;
250 resultList.push_back(result);