Skip to content

Commit 0f23420

Browse files
authored
Merge pull request opencv#26278 from Quantizs:feature-create-face-recognizer-from-buffer
Added buffer-based model loading to FaceRecognizerSF
2 parents 687e37e + e1b0637 commit 0f23420

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

modules/objdetect/include/opencv2/objdetect/face.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ class CV_EXPORTS_W FaceRecognizerSF
155155
* @param target_id the id of target device
156156
*/
157157
CV_WRAP static Ptr<FaceRecognizerSF> create(CV_WRAP_FILE_PATH const String& model, CV_WRAP_FILE_PATH const String& config, int backend_id = 0, int target_id = 0);
158+
159+
/**
160+
* @brief Creates an instance of this class from a buffer containing the model weights and configuration.
161+
* @param framework Name of the framework (ONNX, etc.)
162+
* @param bufferModel A buffer containing the binary model weights.
163+
* @param bufferConfig A buffer containing the network configuration.
164+
* @param backend_id The id of the backend.
165+
* @param target_id The id of the target device.
166+
*
167+
* @return A pointer to the created instance of FaceRecognizerSF.
168+
*/
169+
CV_WRAP static Ptr<FaceRecognizerSF> create(const String& framework,
170+
const std::vector<uchar>& bufferModel,
171+
const std::vector<uchar>& bufferConfig,
172+
int backend_id = 0,
173+
int target_id = 0);
158174
};
159175

160176
//! @}

modules/objdetect/src/face_recognize.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ class FaceRecognizerSFImpl : public FaceRecognizerSF
2626
net.setPreferableBackend(backend_id);
2727
net.setPreferableTarget(target_id);
2828
}
29+
30+
FaceRecognizerSFImpl(const String& framework,
31+
const std::vector<uchar>& bufferModel,
32+
const std::vector<uchar>& bufferConfig,
33+
int backend_id, int target_id)
34+
{
35+
net = dnn::readNet(framework, bufferModel, bufferConfig);
36+
CV_Assert(!net.empty());
37+
38+
net.setPreferableBackend(backend_id);
39+
net.setPreferableTarget(target_id);
40+
}
41+
2942
void alignCrop(InputArray _src_img, InputArray _face_mat, OutputArray _aligned_img) const override
3043
{
3144
Mat face_mat = _face_mat.getMat();
@@ -189,4 +202,17 @@ Ptr<FaceRecognizerSF> FaceRecognizerSF::create(const String& model, const String
189202
#endif
190203
}
191204

205+
Ptr<FaceRecognizerSF> FaceRecognizerSF::create(const String& framework,
206+
const std::vector<uchar>& bufferModel,
207+
const std::vector<uchar>& bufferConfig,
208+
int backend_id, int target_id)
209+
{
210+
#ifdef HAVE_OPENCV_DNN
211+
return makePtr<FaceRecognizerSFImpl>(framework, bufferModel, bufferConfig, backend_id, target_id);
212+
#else
213+
CV_UNUSED(bufferModel); CV_UNUSED(bufferConfig); CV_UNUSED(backend_id); CV_UNUSED(target_id);
214+
CV_Error(cv::Error::StsNotImplemented, "cv::FaceRecognizerSF requires enabled 'dnn' module");
215+
#endif
216+
}
217+
192218
} // namespace cv

0 commit comments

Comments
 (0)