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.

260 lines
9.8 KiB

1 year ago
  1. #pragma once
  2. #pragma once
  3. #include <fsdk/IObject.h>
  4. #include <fsdk/FSDKError.h>
  5. #include <fsdk/Optional.h>
  6. #include <fsdk/Types.h>
  7. namespace fsdk {
  8. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  9. DECLARE_SMARTPTR(IMedicalMaskEstimator);
  10. #endif
  11. /**
  12. * @brief MedicalMask estimator output enum.
  13. * This enum contains all possible estimation results.
  14. * */
  15. enum class MedicalMask {
  16. Mask = 0, //!< medical mask is on the face
  17. NoMask, //!< no medical mask on the face
  18. OccludedFace //!< face is occluded by something
  19. };
  20. /**
  21. * @brief MedicalMask estimator output enum.
  22. * This enum contains all possible extended estimation results.
  23. * */
  24. enum class MedicalMaskExtended {
  25. Mask = 0, //!< medical mask is on the face
  26. NoMask, //!< no medical mask on the face
  27. MaskNotInPlace, //!< mask is not on the right place
  28. OccludedFace //!< face is occluded by something
  29. };
  30. /**
  31. * @brief MedicalMask estimator output enum.
  32. * This enum contains all type of DetailedMaskType results.
  33. * */
  34. enum class DetailedMaskType {
  35. CorrectMask = 0, //!< correct mask on the face (mouth and nose are covered correctly)
  36. MouthCoveredWithMask, //!< mask covers only a mouth
  37. ClearFace, //!< clear face - no mask on the face
  38. ClearFaceWithMaskUnderChin, //!< clear face with a mask around of a chin, mask does not cover anything in the face region (from mouth to eyes)
  39. PartlyCoveredFace, //!< face is covered with not a medical mask or a full mask
  40. FullMask, //!< face is covered with a full mask (such as balaclava, sky mask, etc.)
  41. Count
  42. };
  43. /**
  44. * @brief MedicalMask estimator output structure.
  45. * This structure contains the result of estimation (@see MedicalMask enum)
  46. * and probability scores for each possible estimation result.
  47. * Probability scores are defined in [0,1] range.
  48. * */
  49. struct MedicalMaskEstimation {
  50. MedicalMask result; //!< estimation result (@see MedicalMask enum)
  51. DetailedMaskType maskType; //!< detailed type (@see DetailedMaskType enum)
  52. // scores
  53. float maskScore; //!< medical mask is on the face score
  54. float noMaskScore; //!< no medical mask on the face score
  55. float occludedFaceScore; //!< face is occluded by something score
  56. float scores[static_cast<int>(DetailedMaskType::Count)]; //!< detailed estimation scores
  57. /**
  58. * @brief Returns score of required detailed mask type.
  59. * @param [in] type detailed mask type.
  60. * @see DetailedMaskType for more info.
  61. * */
  62. inline float getScore(DetailedMaskType type) const;
  63. };
  64. /**
  65. * @brief MedicalMask estimator output structure.
  66. * This structure contains the result of estimation (@see MedicalMaskExtended enum)
  67. * and probability scores for each possible estimation result.
  68. * Probability scores are defined in [0,1] range.
  69. * */
  70. struct MedicalMaskEstimationExtended {
  71. MedicalMaskExtended result; //!< estimation result (@see MedicalMaskExtended enum)
  72. // scores
  73. float maskScore; //!< medical mask is on the face score
  74. float noMaskScore; //!< no medical mask on the face score
  75. float maskNotInPlace; //!< mask is not on the right place
  76. float occludedFaceScore; //!< face is occluded by something score
  77. };
  78. /**
  79. * @brief MedicalMask estimator interface.
  80. * @note Estimator predicts mask.
  81. * */
  82. struct IMedicalMaskEstimator : IRefCounted {
  83. /**
  84. * @brief Estimate MedicalMask probabilities.
  85. * @param [in] warp image with warped face.
  86. * @param [out] estimation estimation results.
  87. * @return Result with error code.
  88. * @see MedicalMaskEstimation, Image, Result and FSDKError for details.
  89. * @note images format must be R8G8B8, @see Format.
  90. * */
  91. virtual Result<FSDKError>
  92. estimate(
  93. const Image& warp,
  94. MedicalMaskEstimation& estimation) const noexcept = 0;
  95. /**
  96. * @brief Estimate MedicalMask probabilities.
  97. * @param [in] warp image with warped face.
  98. * @param [out] estimation estimation results.
  99. * @return Result with error code.
  100. * @see MedicalMaskEstimationExtended, Image, Result and FSDKError for details.
  101. * @note images format must be R8G8B8, @see Format.
  102. * */
  103. virtual Result<FSDKError>
  104. estimate(
  105. const Image& warp,
  106. MedicalMaskEstimationExtended& estimation) const noexcept = 0;
  107. /**
  108. * @brief Estimate MedicalMask probabilities.
  109. * @param [in] image source image.
  110. * @param [in] detection detection coordinates in image space.
  111. * @param [out] estimation estimation results.
  112. * @return Result with error code.
  113. * @see MedicalMaskEstimation, Detection, Image, Result and FSDKError for details.
  114. * @note images format must be R8G8B8, @see Format.
  115. * */
  116. virtual Result<FSDKError>
  117. estimate(
  118. const Image& image,
  119. const Detection& detection,
  120. MedicalMaskEstimation& estimation) const noexcept = 0;
  121. /**
  122. * @brief Estimate MedicalMask probabilities.
  123. * @param [in] image source image.
  124. * @param [in] detection detection coordinates in image space.
  125. * @param [out] estimation estimation results.
  126. * @return Result with error code.
  127. * @see MedicalMaskEstimationExtended, Detection, Image, Result and FSDKError for details.
  128. * @note images format must be R8G8B8, @see Format.
  129. * */
  130. virtual Result<FSDKError>
  131. estimate(
  132. const Image& image,
  133. const Detection& detection,
  134. MedicalMaskEstimationExtended& estimation) const noexcept = 0;
  135. /**
  136. * @brief Estimate Medical Mask probabilities.
  137. * @param [in] warps span of images with warped faces.
  138. * @param [out] estimations span of estimations.
  139. * @return Result with error code.
  140. * @see Span, MedicalMaskEstimation, Image, Result and FSDKError for details.
  141. * @note images format must be R8G8B8, @see Format.
  142. * @note all spans should be based on user owned continuous collections.
  143. * @note all spans should be equal size.
  144. * */
  145. virtual Result<FSDKError> estimate(
  146. Span<const Image> warps,
  147. Span<MedicalMaskEstimation> estimations) const noexcept = 0;
  148. /**
  149. * @brief Estimate Medical Mask probabilities.
  150. * @param [in] warps span of images with warped faces.
  151. * @param [out] estimations span of estimations.
  152. * @return Result with error code.
  153. * @see Span, MedicalMaskEstimationExtended, Image, Result and FSDKError for details.
  154. * @note images format must be R8G8B8, @see Format.
  155. * @note all spans should be based on user owned continuous collections.
  156. * @note all spans should be equal size.
  157. * */
  158. virtual Result<FSDKError> estimate(
  159. Span<const Image> warps,
  160. Span<MedicalMaskEstimationExtended> estimations) const noexcept = 0;
  161. /**
  162. * @brief Estimate Medical Mask probabilities.
  163. * @param [in] images span of source images.
  164. * @param [in] detections span of detection coordinates in corresponding source images space.
  165. * @param [out] estimations span of estimations.
  166. * @return Result with error code.
  167. * @see Span, Detection, MedicalMaskEstimation, Image, Result and FSDKError for details.
  168. * @note images format must be R8G8B8, @see Format.
  169. * @note all spans should be based on user owned continuous collections.
  170. * @note all spans should be equal size.
  171. * */
  172. virtual Result<FSDKError> estimate(
  173. Span<const Image> images,
  174. Span<const Detection> detections,
  175. Span<MedicalMaskEstimation> estimations) const noexcept = 0;
  176. /**
  177. * @brief Estimate Medical Mask probabilities.
  178. * @param [in] images span of source images;
  179. * @param [in] detections span of detection coordinates in corresponding source images space.
  180. * @param [out] estimations span of estimations;
  181. * @return Result with error code.
  182. * @see Span, Detection, MedicalMaskEstimationExtended, Image, Result and FSDKError for details.
  183. * @note images format must be R8G8B8, @see Format.
  184. * @note all spans should be based on user owned continuous collections.
  185. * @note all spans should be equal size.
  186. * */
  187. virtual Result<FSDKError> estimate(
  188. Span<const Image> images,
  189. Span<const Detection> detections,
  190. Span<MedicalMaskEstimationExtended> estimations) const noexcept = 0;
  191. /**
  192. * @brief Validate input of multiple frames in a single function call.
  193. * @param [in] warps span of images with warped faces.
  194. * @param [out] errors output span of errors for each image.
  195. * @return Result with error code.
  196. * @see Span, Image, Result and FSDKError for details.
  197. * @note images format must be R8G8B8, @see Format.
  198. * @note all spans should be based on user owned continuous collections.
  199. * @note all spans should be equal size.
  200. * */
  201. virtual Result<FSDKError>
  202. validate(
  203. Span<const Image> warps,
  204. Span<Result<FSDKError>> errors) const noexcept = 0;
  205. /**
  206. * @brief Validate input of multiple frames in a single function call.
  207. * @param [in] images span of source images.
  208. * @param [in] detections span of detection coordinates in corresponding source images space.
  209. * @param [out] errors output span of errors for each image.
  210. * @return Result with error code.
  211. * @see Span, Detection, Image, Result and FSDKError for details.
  212. * @note images format must be R8G8B8, @see Format.
  213. * @note all spans should be based on user owned continuous collections.
  214. * @note all spans should be equal size.
  215. * */
  216. virtual Result<FSDKError>
  217. validate(
  218. Span<const Image> images,
  219. Span<const Detection> detections,
  220. Span<Result<FSDKError>> errors) const noexcept = 0;
  221. };
  222. /*
  223. Implementation details.
  224. */
  225. float MedicalMaskEstimation::getScore(DetailedMaskType type) const {
  226. switch (type) {
  227. case DetailedMaskType::CorrectMask:
  228. case DetailedMaskType::MouthCoveredWithMask:
  229. case DetailedMaskType::ClearFace:
  230. case DetailedMaskType::ClearFaceWithMaskUnderChin:
  231. case DetailedMaskType::PartlyCoveredFace:
  232. case DetailedMaskType::FullMask:
  233. return scores[static_cast<int>(type)];
  234. default:
  235. return 0.f;
  236. }
  237. }
  238. } // namespace fsdk