From 555efd4af671d76807fa78431305b4873342f481 Mon Sep 17 00:00:00 2001 From: uvos Date: Fri, 7 Jun 2024 14:04:48 +0200 Subject: [PATCH] Add the option to focus on a spcific person --- SmartCrop/facerecognizer.cpp | 21 ++++++++++++++------- SmartCrop/facerecognizer.h | 9 ++++++++- SmartCrop/main.cpp | 9 +++++---- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/SmartCrop/facerecognizer.cpp b/SmartCrop/facerecognizer.cpp index a01fb3c..3caddae 100644 --- a/SmartCrop/facerecognizer.cpp +++ b/SmartCrop/facerecognizer.cpp @@ -106,28 +106,35 @@ void FaceRecognizer::clearReferances() referanceFeatures.clear(); } -std::pair FaceRecognizer::isMatch(const cv::Mat& input, bool alone) +FaceRecognizer::Detection FaceRecognizer::isMatch(const cv::Mat& input, bool alone) { cv::Mat faces = detectFaces(input); - if(alone && faces.rows > 1) - return {-2, 0}; + Detection bestMatch; + bestMatch.confidence = 0; + bestMatch.person = -1; - std::pair bestMatch = {-1, 0}; + if(alone && faces.rows > 1) + { + bestMatch.person = -2; + return bestMatch; + } for(int i = 0; i < faces.rows; ++i) { cv::Mat face; - recognizer->alignCrop(input, faces.row(0), face); + recognizer->alignCrop(input, faces.row(i), face); cv::Mat features; recognizer->feature(face, features); features = features.clone(); for(size_t referanceIndex = 0; referanceIndex < referanceFeatures.size(); ++referanceIndex) { double score = recognizer->match(referanceFeatures[referanceIndex], features, cv::FaceRecognizerSF::FR_COSINE); - if(score > threshold && score > bestMatch.second) + if(score > threshold && score > bestMatch.confidence) { - bestMatch = {referanceIndex, score}; + bestMatch.confidence = score; + bestMatch.person = referanceIndex; + bestMatch.rect = cv::Rect(faces.at(i, 0), faces.at(i, 1), faces.at(i, 2), faces.at(i, 3)); } } } diff --git a/SmartCrop/facerecognizer.h b/SmartCrop/facerecognizer.h index 20a2d9d..b8d818d 100644 --- a/SmartCrop/facerecognizer.h +++ b/SmartCrop/facerecognizer.h @@ -11,6 +11,13 @@ class FaceRecognizer { public: + struct Detection + { + int person; + float confidence; + cv::Rect rect; + }; + class LoadException : public std::exception { private: @@ -33,7 +40,7 @@ private: public: FaceRecognizer(std::filesystem::path recognizerPath = "", const std::filesystem::path& detectorPath = "", const std::vector& referances = std::vector()); cv::Mat detectFaces(const cv::Mat& input); - std::pair isMatch(const cv::Mat& input, bool alone = false); + Detection isMatch(const cv::Mat& input, bool alone = false); bool addReferances(const std::vector& referances); void setThreshold(double threashold); double getThreshold(); diff --git a/SmartCrop/main.cpp b/SmartCrop/main.cpp index a368aef..d659181 100644 --- a/SmartCrop/main.cpp +++ b/SmartCrop/main.cpp @@ -243,12 +243,12 @@ static void reduceSize(cv::Mat& image, const cv::Size& targetSize) if(image.cols > image.rows) { double ratio = static_cast(longTargetSize)/image.cols; - cv::resize(image, image, {longTargetSize, static_cast(image.rows*ratio)}, 0, 0, cv::INTER_CUBIC); + cv::resize(image, image, {longTargetSize, static_cast(image.rows*ratio)}, 0, 0, ratio < 1 ? cv::INTER_AREA : cv::INTER_CUBIC); } else { double ratio = static_cast(longTargetSize)/image.rows; - cv::resize(image, image, {static_cast(image.cols*ratio), longTargetSize}, 0, 0, cv::INTER_CUBIC); + cv::resize(image, image, {static_cast(image.cols*ratio), longTargetSize}, 0, 0, ratio < 1 ? cv::INTER_AREA : cv::INTER_CUBIC); } } } @@ -276,12 +276,13 @@ void pipeline(const std::filesystem::path& path, const Config& config, Yolo& yol { cv::Mat person = image(detection.box); reconizerMutex.lock(); - std::pair match = recognizer->isMatch(person); + FaceRecognizer::Detection match = recognizer->isMatch(person); reconizerMutex.unlock(); - if(match.first >= 0) + if(match.person >= 0) { detection.priority += 10; hasmatch = true; + detections.push_back({0, "Face", match.confidence, 20, {255, 0, 0}, match.rect}); } } Log(Log::DEBUG)<