Skip to content

Commit e1b0637

Browse files
committed
Added buffer-based model loading to FaceRecognizerSF
- Implemented a new `create` method in `FaceRecognizerSF` to allow model and configuration loading from memory buffers (std::vector<uchar>), similar to the existing functionality in `FaceDetectorYN`. - Updated `face_recognize.cpp` with a new constructor in `FaceRecognizerSFImpl` that supports buffer-based loading for both model weights and network configuration. - Ensured compatibility with both file-based and buffer-based model loading by maintaining consistent backend and target settings across both constructors. - This change improves flexibility, allowing FaceRecognizerSF to be instantiated from memory buffers, which is useful for dynamic model loading scenarios such as embedded systems or applications where models are loaded in-memory.
1 parent 450e741 commit e1b0637

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)