You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 lines
13 KiB

1 year ago
  1. /**
  2. * @file IDetector.h
  3. * @brief Face detector interfaces.
  4. * @copyright VisionLabs LLC
  5. * @date 16.02.2017
  6. * */
  7. #pragma once
  8. #include "FSDKError.h"
  9. #include "IFaceDetectionBatch.h"
  10. #include "IObject.h"
  11. #include "Types.h"
  12. #include "Types/Face.h"
  13. #include <fsdk/vlc/future.h>
  14. #include <functional>
  15. namespace fsdk {
  16. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  17. DECLARE_SMARTPTR(IDetector);
  18. #endif
  19. /**
  20. * @brief Sensor type structure.
  21. * Determines which type of camera sensor is used to perform face recognition.
  22. * */
  23. enum class SensorType {
  24. Visible = 0, //!< Visible sensor type.
  25. NIR //!< NIR sensor type.
  26. };
  27. /**
  28. * @defgroup DetectorGroup Face detector.
  29. * @brief Face detector public interfaces and related types and structures.
  30. * @{
  31. * */
  32. /**
  33. * @brief Object detector type enumeration.
  34. * */
  35. enum ObjectDetectorClassType {
  36. FACE_DET_DEFAULT = 0, //!< Default detector cpecified in config file.
  37. FACE_DET_V1 = 4, //!< Detector type 1.
  38. FACE_DET_V2 = 5, //!< Detector type 2.
  39. FACE_DET_V3 = 6, //!< Detector type 3.
  40. FACE_DET_COUNT = 7, //!< Detector type count.
  41. };
  42. /**
  43. * @brief Strategy of BestDetections comparer.
  44. */
  45. enum DetectionComparerType {
  46. DCT_CONFIDENCE = 0, //!< BestDetection - detections with highest score.
  47. DCT_CENTER, //!< BestDetection - most centered detection.
  48. DCT_CENTER_AND_CONFIDENCE, //!< bestDetection - most centered with high score.
  49. DCT_SIZE, //!< bestDetection - the largest detection.
  50. DCT_COUNT
  51. };
  52. /**
  53. * @brief Detection type enumeration.
  54. * */
  55. enum DetectionType {
  56. DT_BBOX = 0, //!< Get bounding boxes of faces.
  57. DT_LANDMARKS5 = 1<<0, //!< Get bounding boxes and 5 facial landmarks.
  58. DT_LANDMARKS68 = 1<<1, //!< Get bounding boxes and 68 facial landmarks.
  59. DT_ALL = 0xffff //!< Get all supported parameters.
  60. };
  61. inline DetectionType operator | (DetectionType a, DetectionType b)
  62. { return static_cast<DetectionType>(static_cast<int>(a) | static_cast<int>(b)); }
  63. /**
  64. * @brief Interface of BestDetection comparer. Implement it if you want to use own BestDetection strategy.
  65. */
  66. class IDetectionComparer {
  67. public:
  68. virtual bool compare(const Image& img, const Detection& a, const Detection& b) const = 0;
  69. virtual ~IDetectionComparer() = default;
  70. };
  71. /**
  72. * @brief Syntax sugar, allows you to use lambdas to define a BestDetection comparer.
  73. */
  74. class FunctionDetectionComparer : public IDetectionComparer {
  75. public:
  76. typedef std::function<bool(const Image& img, const Detection&, const Detection&)> Function;
  77. explicit FunctionDetectionComparer(const Function& function) : func(function)
  78. {}
  79. bool compare(const Image& img, const Detection& a, const Detection& b) const
  80. { return func(img, a, b); }
  81. private:
  82. Function func;
  83. };
  84. /**
  85. * @brief face detector interface.
  86. * */
  87. struct IDetector : IRefCounted {
  88. /**
  89. * @brief Detect faces and their parameters on multiple images.
  90. * @param [in] images span of source images.
  91. * @param [in] rectangles input rectangles of interest (ROI) span.
  92. * @param [in] perImageNum the max number of detections per input image.
  93. * @param [in] type type of the detection.
  94. * @return ResultValue with error code and IFaceDetectionBatch object.
  95. * @see Ref, Span, Image, Rect, DetectionType, IFaceDetectionBatch, ResultValue and FSDKError for details.
  96. * @note images format must be R8G8B8, @see Format.
  97. * @note all spans should be based on user owned continuous collections.
  98. * @note all spans should be equal size.
  99. * */
  100. virtual ResultValue<FSDKError, Ref<IFaceDetectionBatch>>
  101. detect(
  102. Span<const Image> images,
  103. Span<const Rect> ROIs,
  104. uint32_t perImageNum,
  105. DetectionType type = DT_BBOX
  106. ) noexcept = 0;
  107. /**
  108. * @brief Light function to get just one best detection from single input image.
  109. * @param [in] image source image.
  110. * @param [in] rect rectangle of interest in the image.
  111. * @param [in] type type of detection: BBox, 5landmarks or 68landmarks.
  112. * @return ResultValue with error code and a Face object (detection bbox, landmarks, etc).
  113. * @see Face, Image, Rect, DetectionType, ResultValue and FSDKError for details.
  114. * @note image format must be R8G8B8, @see Format.
  115. */
  116. virtual ResultValue<FSDKError, Face>
  117. detectOne(
  118. const Image& image,
  119. const Rect& rect,
  120. DetectionType type = DT_BBOX) noexcept = 0;
  121. /**
  122. * @brief Batched redetect faces on multiple images.
  123. * based on the detection results for the previous frames.
  124. * @param [in] images span of source images.
  125. * @param [in] detectionBatch result of detection on the previous frames -
  126. * Ref with an IFaceDetectionBatch object.
  127. * @param [in] type type of redetection.
  128. * @return ResultValue with error code and IFaceDetectionBatch object.
  129. * @see Ref, Span, Image, DetectionType, IFaceDetectionBatch, ResultValue and FSDKError for details.
  130. * @note images format must be R8G8B8, @see Format.
  131. * @note all spans should be based on user owned continuous collections.
  132. * @note images span should be the same size with detectionBatch size.
  133. * @note In case if some face from the input detectionBatch was not found
  134. * the corresponding detection in the output IFaceDetectionBatch object
  135. * will be invalid.
  136. */
  137. virtual ResultValue<FSDKError, Ref<IFaceDetectionBatch>>
  138. redetect(
  139. Span<const Image> images,
  140. Ref<IFaceDetectionBatch> detectionBatch,
  141. DetectionType type = DT_BBOX
  142. ) noexcept = 0;
  143. /**
  144. * @brief Batched redetect faces on multiple images.
  145. * based on the detection results for the previous frames.
  146. * @param [in] images span of source images.
  147. * @param [in] detections span of detection coordinates in corresponding source images space
  148. * from the previous frames. It is a two dimensional Span. There is one Span of the rectangles for each image.
  149. * @param [in] type type of redetection.
  150. * @return ResultValue with error code and IFaceDetectionBatch object.
  151. * @see Ref, Span, Image, DetectionType, Detection, IFaceDetectionBatch, ResultValue and FSDKError for details.
  152. * @note images format must be R8G8B8, @see Format.
  153. * @note all spans should be based on user owned continuous collections.
  154. * @note all spans should be equal size.
  155. * @note If for some of the input detections the redetected face will not be found the
  156. * appropriate detection in the IFaceDetectionBatch object will be invalid.
  157. */
  158. virtual ResultValue<FSDKError, Ref<IFaceDetectionBatch>>
  159. redetect(
  160. Span<const Image> images,
  161. Span<Span<const Detection>> detections,
  162. DetectionType type = DT_BBOX
  163. ) noexcept = 0;
  164. /**
  165. * @brief Redetect face.
  166. * @param [in] image source image. Format must be R8G8B8.
  167. * @param [in] detection detection coordinates in image space from the previous frame to make redetect.
  168. * @param [in] type type of detection: BBox, 5landmarks or 68landmarks.
  169. * @return ResultValue with error code and Face.
  170. * @see Face, Image, DetectionType, Detection, ResultValue and FSDKError for details.
  171. * @note image format must be R8G8B8, @see Format.
  172. * */
  173. virtual ResultValue<FSDKError, Face>
  174. redetectOne(
  175. const Image& image,
  176. const Detection& detection,
  177. DetectionType type = DT_BBOX) noexcept = 0;
  178. /**
  179. * @brief Set detection comparer from SDK defined list.
  180. * @param [in] comparerType type of the comparer for detections.
  181. * @see DetectionComparerType for details.
  182. */
  183. virtual void setDetectionComparer(DetectionComparerType comparerType) noexcept = 0;
  184. /**
  185. * @brief Set custom detection comparer object.
  186. * @param [in] comparer pointer to user defined comparer object.
  187. * @see IDetectionComparer for details.
  188. * @note Client code still owns comparer object
  189. */
  190. virtual void setCustomDetectionComparer(const IDetectionComparer* comparer) noexcept = 0;
  191. /**
  192. * @brief Validate input of multiple frames in a single function call.
  193. * @param [in] images span of source images.
  194. * @param [in] detections span of detection coordinates in corresponding source images space
  195. * from the previous frames. It is a two dimensional Span. There is one Span of Detections for each image.
  196. * @param [out] errors output span of errors for each image.
  197. * It is a two dimensional Span. There is one Span of the errors for each image.
  198. * @return Result with error code.
  199. * @see Span, Image, Detection, Result and FSDKError for details.
  200. * @note images format must be R8G8B8, @see Format.
  201. * @note all spans should be based on user owned continuous collections.
  202. * @note all spans should be equal size.
  203. * */
  204. virtual Result<FSDKError>
  205. validate(
  206. Span<const Image> images,
  207. Span<Span<const Detection>> detections,
  208. Span<Span<Result<FSDKError>>> errors) const noexcept = 0;
  209. /**
  210. * @brief Validate input of multiple frames in a single function call.
  211. * @param [in] images span of source images.
  212. * @param [in] rects span of rectangle coordinates of corresponding source images.
  213. * @param [in] detectionPerImageNum max number of detections per input image.
  214. * @param [out] errors output span of errors for each image.
  215. * @return Result with error code.
  216. * @see Span, Image, Rect, Result and FSDKError for details.
  217. * @note images format must be R8G8B8, @see Format.
  218. * @note all spans should be based on user owned continuous collections.
  219. * @note all spans should be equal size.
  220. * */
  221. virtual Result<FSDKError>
  222. validate(
  223. Span<const Image> images,
  224. Span<const Rect> rects,
  225. uint32_t detectionPerImageNum,
  226. Span<Result<FSDKError>> outErrors) const noexcept = 0;
  227. /**
  228. * @brief Validate input of multiple frames in a single function call.
  229. * @param [in] images span of source images.
  230. * @param [in] detectionBatch result of detection on the previous frames -
  231. * Ref with an IFaceDetectionBatch object.
  232. * @param [out] errors output span of errors for each image.
  233. * @return Result with error code.
  234. * @see Ref, Span, Image, IFaceDetectionBatch, Result and FSDKError for details.
  235. * @note images format must be R8G8B8, @see Format.
  236. * @note all spans should be based on user owned continuous collections.
  237. * @note all spans should be equal size.
  238. * */
  239. virtual Result<FSDKError>
  240. validate(
  241. Span<const Image> images,
  242. Ref<IFaceDetectionBatch> detectionBatch,
  243. Span<Result<FSDKError>> outErrors) const noexcept = 0;
  244. /**
  245. * @brief Common aliases for IDetector asynchronous interface.
  246. * */
  247. using FaceBatchResult = ResultValue<FSDKError, IFaceDetectionBatchPtr>;
  248. using FaceBatchFuture = vlc::future<IFaceDetectionBatchPtr>;
  249. /**
  250. * @brief Asynchronously detect faces and their parameters on multiple images.
  251. * @param [in] images span of source images.
  252. * @param [in] rectangles input rectangles of interest (ROI) span.
  253. * @param [in] perImageNum the max number of detections per input image.
  254. * @param [in] type type of the detection.
  255. * @return Future ResultValue with error code and IFaceDetectionBatch object.
  256. * @see Ref, Span, Image, Rect, DetectionType, IFaceDetectionBatch,
  257. * ResultValue, FSDKError and vlc::future for details.
  258. * @note images format must be R8G8B8, @see Format.
  259. * @note all spans should be based on user owned continuous collections.
  260. * @note all spans should be equal size.
  261. * @note this method is experimental and interface may be changed in the future.
  262. * @note this method is not marked as noexcept and may throw an exception.
  263. * */
  264. virtual FaceBatchFuture detectAsync(
  265. Span<const Image> images,
  266. Span<const Rect> rectangles,
  267. uint32_t perImageNum,
  268. DetectionType type = DT_BBOX) const = 0;
  269. /**
  270. * @brief Asynchronously redetect faces on multiple images.
  271. * based on the detection results for the previous frames.
  272. * @param [in] images span of source images.
  273. * @param [in] detectionBatch result of detection on the previous frames -
  274. * Ref with an IFaceDetectionBatch object.
  275. * @param [in] type type of redetection.
  276. * @return Future ResultValue with error code and IFaceDetectionBatch object.
  277. * @see Ref, Span, Image, DetectionType, IFaceDetectionBatch,
  278. * ResultValue, FSDKError and vlc::future for details.
  279. * @note images format must be R8G8B8, @see Format.
  280. * @note all spans should be based on user owned continuous collections.
  281. * @note images span should be the same size with detectionBatch size.
  282. * @note In case if some face from the input detectionBatch was not found
  283. * the corresponding detection in the output IFaceDetectionBatch object
  284. * will be invalid.
  285. * @note this method is experimental and interface may be changed in the future.
  286. * @note this method is not marked as noexcept and may throw an exception.
  287. * */
  288. virtual FaceBatchFuture redetectAsync(
  289. Span<const Image> images,
  290. IFaceDetectionBatchPtr detectionBatch,
  291. DetectionType type = DT_BBOX) const = 0;
  292. };
  293. /** @} */
  294. }