face.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_OBJDETECT_FACE_HPP
  5. #define OPENCV_OBJDETECT_FACE_HPP
  6. #include <opencv2/core.hpp>
  7. /** @defgroup dnn_face DNN-based face detection and recognition
  8. */
  9. namespace cv
  10. {
  11. /** @brief DNN-based face detector, model download link: https://github.com/ShiqiYu/libfacedetection.train/tree/master/tasks/task1/onnx.
  12. */
  13. class CV_EXPORTS_W FaceDetectorYN
  14. {
  15. public:
  16. virtual ~FaceDetectorYN() {};
  17. /** @brief Set the size for the network input, which overwrites the input size of creating model. Call this method when the size of input image does not match the input size when creating model
  18. *
  19. * @param input_size the size of the input image
  20. */
  21. CV_WRAP virtual void setInputSize(const Size& input_size) = 0;
  22. CV_WRAP virtual Size getInputSize() = 0;
  23. /** @brief Set the score threshold to filter out bounding boxes of score less than the given value
  24. *
  25. * @param score_threshold threshold for filtering out bounding boxes
  26. */
  27. CV_WRAP virtual void setScoreThreshold(float score_threshold) = 0;
  28. CV_WRAP virtual float getScoreThreshold() = 0;
  29. /** @brief Set the Non-maximum-suppression threshold to suppress bounding boxes that have IoU greater than the given value
  30. *
  31. * @param nms_threshold threshold for NMS operation
  32. */
  33. CV_WRAP virtual void setNMSThreshold(float nms_threshold) = 0;
  34. CV_WRAP virtual float getNMSThreshold() = 0;
  35. /** @brief Set the number of bounding boxes preserved before NMS
  36. *
  37. * @param top_k the number of bounding boxes to preserve from top rank based on score
  38. */
  39. CV_WRAP virtual void setTopK(int top_k) = 0;
  40. CV_WRAP virtual int getTopK() = 0;
  41. /** @brief A simple interface to detect face from given image
  42. *
  43. * @param image an image to detect
  44. * @param faces detection results stored in a cv::Mat
  45. */
  46. CV_WRAP virtual int detect(InputArray image, OutputArray faces) = 0;
  47. /** @brief Creates an instance of this class with given parameters
  48. *
  49. * @param model the path to the requested model
  50. * @param config the path to the config file for compability, which is not requested for ONNX models
  51. * @param input_size the size of the input image
  52. * @param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  53. * @param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
  54. * @param top_k keep top K bboxes before NMS
  55. * @param backend_id the id of backend
  56. * @param target_id the id of target device
  57. */
  58. CV_WRAP static Ptr<FaceDetectorYN> create(const String& model,
  59. const String& config,
  60. const Size& input_size,
  61. float score_threshold = 0.9f,
  62. float nms_threshold = 0.3f,
  63. int top_k = 5000,
  64. int backend_id = 0,
  65. int target_id = 0);
  66. };
  67. /** @brief DNN-based face recognizer, model download link: https://drive.google.com/file/d/1ClK9WiB492c5OZFKveF3XiHCejoOxINW/view.
  68. */
  69. class CV_EXPORTS_W FaceRecognizerSF
  70. {
  71. public:
  72. virtual ~FaceRecognizerSF() {};
  73. /** @brief Definition of distance used for calculating the distance between two face features
  74. */
  75. enum DisType { FR_COSINE=0, FR_NORM_L2=1 };
  76. /** @brief Aligning image to put face on the standard position
  77. * @param src_img input image
  78. * @param face_box the detection result used for indicate face in input image
  79. * @param aligned_img output aligned image
  80. */
  81. CV_WRAP virtual void alignCrop(InputArray src_img, InputArray face_box, OutputArray aligned_img) const = 0;
  82. /** @brief Extracting face feature from aligned image
  83. * @param aligned_img input aligned image
  84. * @param face_feature output face feature
  85. */
  86. CV_WRAP virtual void feature(InputArray aligned_img, OutputArray face_feature) = 0;
  87. /** @brief Calculating the distance between two face features
  88. * @param _face_feature1 the first input feature
  89. * @param _face_feature2 the second input feature of the same size and the same type as _face_feature1
  90. * @param dis_type defining the similarity with optional values "FR_OSINE" or "FR_NORM_L2"
  91. */
  92. CV_WRAP virtual double match(InputArray _face_feature1, InputArray _face_feature2, int dis_type = FaceRecognizerSF::FR_COSINE) const = 0;
  93. /** @brief Creates an instance of this class with given parameters
  94. * @param model the path of the onnx model used for face recognition
  95. * @param config the path to the config file for compability, which is not requested for ONNX models
  96. * @param backend_id the id of backend
  97. * @param target_id the id of target device
  98. */
  99. CV_WRAP static Ptr<FaceRecognizerSF> create(const String& model, const String& config, int backend_id = 0, int target_id = 0);
  100. };
  101. } // namespace cv
  102. #endif