face.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. namespace cv
  8. {
  9. //! @addtogroup objdetect_dnn_face
  10. //! @{
  11. /** @brief DNN-based face detector
  12. model download link: https://github.com/opencv/opencv_zoo/tree/master/models/face_detection_yunet
  13. */
  14. class CV_EXPORTS_W FaceDetectorYN
  15. {
  16. public:
  17. virtual ~FaceDetectorYN() {};
  18. /** @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
  19. *
  20. * @param input_size the size of the input image
  21. */
  22. CV_WRAP virtual void setInputSize(const Size& input_size) = 0;
  23. CV_WRAP virtual Size getInputSize() = 0;
  24. /** @brief Set the score threshold to filter out bounding boxes of score less than the given value
  25. *
  26. * @param score_threshold threshold for filtering out bounding boxes
  27. */
  28. CV_WRAP virtual void setScoreThreshold(float score_threshold) = 0;
  29. CV_WRAP virtual float getScoreThreshold() = 0;
  30. /** @brief Set the Non-maximum-suppression threshold to suppress bounding boxes that have IoU greater than the given value
  31. *
  32. * @param nms_threshold threshold for NMS operation
  33. */
  34. CV_WRAP virtual void setNMSThreshold(float nms_threshold) = 0;
  35. CV_WRAP virtual float getNMSThreshold() = 0;
  36. /** @brief Set the number of bounding boxes preserved before NMS
  37. *
  38. * @param top_k the number of bounding boxes to preserve from top rank based on score
  39. */
  40. CV_WRAP virtual void setTopK(int top_k) = 0;
  41. CV_WRAP virtual int getTopK() = 0;
  42. /** @brief Detects faces in the input image. Following is an example output.
  43. * ![image](pics/lena-face-detection.jpg)
  44. * @param image an image to detect
  45. * @param faces detection results stored in a 2D cv::Mat of shape [num_faces, 15]
  46. * - 0-1: x, y of bbox top left corner
  47. * - 2-3: width, height of bbox
  48. * - 4-5: x, y of right eye (blue point in the example image)
  49. * - 6-7: x, y of left eye (red point in the example image)
  50. * - 8-9: x, y of nose tip (green point in the example image)
  51. * - 10-11: x, y of right corner of mouth (pink point in the example image)
  52. * - 12-13: x, y of left corner of mouth (yellow point in the example image)
  53. * - 14: face score
  54. */
  55. CV_WRAP virtual int detect(InputArray image, OutputArray faces) = 0;
  56. /** @brief Creates an instance of this class with given parameters
  57. *
  58. * @param model the path to the requested model
  59. * @param config the path to the config file for compability, which is not requested for ONNX models
  60. * @param input_size the size of the input image
  61. * @param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
  62. * @param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
  63. * @param top_k keep top K bboxes before NMS
  64. * @param backend_id the id of backend
  65. * @param target_id the id of target device
  66. */
  67. CV_WRAP static Ptr<FaceDetectorYN> create(const String& model,
  68. const String& config,
  69. const Size& input_size,
  70. float score_threshold = 0.9f,
  71. float nms_threshold = 0.3f,
  72. int top_k = 5000,
  73. int backend_id = 0,
  74. int target_id = 0);
  75. };
  76. /** @brief DNN-based face recognizer
  77. model download link: https://github.com/opencv/opencv_zoo/tree/master/models/face_recognition_sface
  78. */
  79. class CV_EXPORTS_W FaceRecognizerSF
  80. {
  81. public:
  82. virtual ~FaceRecognizerSF() {};
  83. /** @brief Definition of distance used for calculating the distance between two face features
  84. */
  85. enum DisType { FR_COSINE=0, FR_NORM_L2=1 };
  86. /** @brief Aligning image to put face on the standard position
  87. * @param src_img input image
  88. * @param face_box the detection result used for indicate face in input image
  89. * @param aligned_img output aligned image
  90. */
  91. CV_WRAP virtual void alignCrop(InputArray src_img, InputArray face_box, OutputArray aligned_img) const = 0;
  92. /** @brief Extracting face feature from aligned image
  93. * @param aligned_img input aligned image
  94. * @param face_feature output face feature
  95. */
  96. CV_WRAP virtual void feature(InputArray aligned_img, OutputArray face_feature) = 0;
  97. /** @brief Calculating the distance between two face features
  98. * @param face_feature1 the first input feature
  99. * @param face_feature2 the second input feature of the same size and the same type as face_feature1
  100. * @param dis_type defining the similarity with optional values "FR_OSINE" or "FR_NORM_L2"
  101. */
  102. CV_WRAP virtual double match(InputArray face_feature1, InputArray face_feature2, int dis_type = FaceRecognizerSF::FR_COSINE) const = 0;
  103. /** @brief Creates an instance of this class with given parameters
  104. * @param model the path of the onnx model used for face recognition
  105. * @param config the path to the config file for compability, which is not requested for ONNX models
  106. * @param backend_id the id of backend
  107. * @param target_id the id of target device
  108. */
  109. CV_WRAP static Ptr<FaceRecognizerSF> create(const String& model, const String& config, int backend_id = 0, int target_id = 0);
  110. };
  111. //! @}
  112. } // namespace cv
  113. #endif