LivenessDetector.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // liveness.h
  3. // liveness
  4. //
  5. // Created by youmu on 15-1-6.
  6. // Copyright (c) 2015 Megvii Inc. All rights reserved.
  7. //
  8. /*!
  9. * \file LivenessDetector.h
  10. */
  11. #import <UIKit/UIKit.h>
  12. #import <MGBaseKit/MGBaseKit.h>
  13. #import <AVFoundation/AVFoundation.h>
  14. #import "LivenessEnumType.h"
  15. #define MG_LANDMARK_POINTS_NUM 7
  16. /*!
  17. * 人脸属性
  18. */
  19. typedef struct {
  20. /** 是否包含人脸 */
  21. bool has_face ;
  22. /** 左右旋转角度 */
  23. float yaw ;
  24. /** 上下旋转角度 */
  25. float pitch ;
  26. /** 运动模糊程度 */
  27. float blurness_motion ;
  28. /** 高斯模糊程度 */
  29. float blurness_gaussian ;
  30. /** 人脸位置 */
  31. CGRect face_rect;
  32. CGPoint face_landmarks[MG_LANDMARK_POINTS_NUM];
  33. /** 左眼睁闭程度 */
  34. float eye_left_hwratio ;
  35. /** 右眼睁闭程度 */
  36. float eye_right_hwratio ;
  37. /** 嘴部睁闭程度 */
  38. float mouth_hwratio ;
  39. /** 眼镜判定 */
  40. float glasses ;
  41. /** 亮度 */
  42. float brightness ;
  43. /** 人脸有效面积比 */
  44. float integrity ;
  45. /** 当前帧的属性状态 */
  46. MGLivenessFrameType frame_type;
  47. /** 连续性检测 */
  48. bool not_video;
  49. /** 平滑左右旋转角度 */
  50. float smooth_yaw ;
  51. /** 平滑上下旋转角度 */
  52. float smooth_pitch ;
  53. float eye_left_det;
  54. float eye_right_det;
  55. float mouth_det;
  56. /** 遮挡判断 */
  57. float eye_left_occlusion;
  58. float eye_right_occlusion;
  59. float mouth_occlusion;
  60. /** 眨眼判断 */
  61. bool eye_blink;
  62. /** 张嘴判断 */
  63. bool mouth_open;
  64. /** 是否通过3D验证 */
  65. bool pitch3d;
  66. /** 综合图片质量 */
  67. float quality ;
  68. float smooth_quality;
  69. bool face_too_large;
  70. }MGFaceAttr;
  71. /*!
  72. * 加密水印图片二进制数据
  73. */
  74. @interface MGLivenessDetectionFrameEncodedData: NSObject
  75. @property (readonly) NSData* data;
  76. @property (readonly) MGFaceAttr attr;
  77. @end
  78. /**!
  79. * 单帧检测结果的类,包含单帧检测出人脸的所有属性,此类无需构造,仅用于回调
  80. */
  81. @interface MGLivenessDetectionFrame : NSObject
  82. /** 检测帧对应图片*/
  83. @property (readonly) UIImage* image;
  84. /** 图片中的人脸属性 */
  85. @property (readonly) MGFaceAttr attr;
  86. -(NSComparisonResult) compare: (MGLivenessDetectionFrame*) aFrame;
  87. -(id) duplicate;
  88. /** 根据人脸位置裁剪仅包含人脸的图片 */
  89. -(UIImage*) croppedImageOfFace;
  90. -(MGLivenessDetectionFrame*) croppedFrameOfFaceWithMaxImageSize: (int)maxImageSize;
  91. /*!
  92. * 获取带水印和加密的图片,如果人脸不存在则返回nil
  93. * @param cropping 若为true则根据人脸位置裁剪图片
  94. * @param maxSize 压缩最大边长限制
  95. * @param compressionQuality JPEG压缩质量,范围[0.0, 1.0]
  96. * @param code 水印数字编码
  97. * @param watermark 是否需要加水印
  98. * @param encrypted 是否进行加密处理
  99. * @return 压缩编码后的图片二进制数据以及对应的人脸属性,如果人脸不存在则返回nil
  100. */
  101. -(MGLivenessDetectionFrameEncodedData*) getImageDataWithCropping: (bool)cropping andMaxSize: (int)maxSize andJPEGCompressionQuality: (CGFloat)compressionQuality andCode: (int)code andNeedWatermark:(bool)watermark andNeedEncrypted: (bool)encrypted;
  102. /** 根据YAW/PITCH获取转换二维坐标,当人脸不存在时不保证返回值有意义 */
  103. +(CGPoint) get2DPointWithMinPitch:(float)minPitch andMaxPitch:(float) maxPitch andMinYaw:(float) minYaw
  104. andMaxYaw:(float) maxYaw andCurPitch:(float) curPitch andCurYaw:(float) curYaw;
  105. -(CGPoint) get2DPointWithMinPitch:(float)minPitch andMaxPitch:(float) maxPitch andMinYaw:(float) minYaw
  106. andMaxYaw:(float) maxYaw;
  107. -(CGPoint) get2DPoint;
  108. /** 判断二维坐标合法性 */
  109. +(bool) isValid2DPoint:(CGPoint)point;
  110. @end
  111. /*!
  112. *
  113. */
  114. @interface FaceIDData : NSObject
  115. @property (readonly) NSDictionary* images;
  116. @property (readonly) NSString* delta;
  117. @end
  118. /*!
  119. * 此接口通过 MGLivenessDetector 的 setDelegate 函数进行注册,在活体检测的过程中会经由不同的事件触发不同的回调方法
  120. */
  121. @protocol MGLivenessProtocolDelegate <NSObject>
  122. @required
  123. /*!
  124. * 当前活体检测的动作检测失败时,调用此方法。
  125. *
  126. * @param failedType 动作检测失败的原因(具体选项请参照MGLivenessDetectionFailedType)
  127. * @return 无
  128. */
  129. -(void)onDetectionFailed:(MGLivenessDetectionFailedType)failedType;
  130. /*!
  131. * 当前活体检测的动作检测成功时,调用此方法。
  132. * @param validFrame 当前动作中采集的质量最好帧。
  133. * @return 下一个需要进行检测的动作的枚举类型(具体选项请参照MGLivenessDetectionType),如果需要结束活体检测流程,则返回类型DETECTION_TYPE_DONE
  134. */
  135. -(MGLivenessDetectionType)onDetectionSuccess:(MGLivenessDetectionFrame *)validFrame;
  136. /*!
  137. * 每检测一帧图像,都会调用一次此函数。
  138. * @param frame 检测到的人脸的相关信息。
  139. * @param timeout 剩余的超时时间
  140. * @return 无
  141. */
  142. -(void)onFrameDetected:(MGLivenessDetectionFrame *)frame andTimeout:(float) timeout;
  143. @end
  144. /*!
  145. * 活体检测器类,所有活体动作的检测均由此检测器完成
  146. */
  147. @interface MGLivenessDetector : NSObject<LicenseProtocolDelegate>
  148. /** 回调接口 */
  149. @property (nonatomic, assign) id <MGLivenessProtocolDelegate> delegate;
  150. /*!
  151. * 检测器的构造函数,对于所有检测请求默认使用 DISPATCH_QUEUE_PRIORITY_DEFAULT 队列进行异步调用
  152. *
  153. * @param options 包含构造检测器的各种配置,具体选项请参照 @link DetectorOptionKeys "检测器配置选项"@endlink
  154. * @return 根据配置生成的新检测器,若初始化失败,则返回nil
  155. */
  156. +(MGLivenessDetector*) detectorOfOptions: (NSDictionary*) options;
  157. /*!
  158. * 检测器的构造函数
  159. *
  160. * @param options 包含构造检测器的各种参数,具体选项请参照 @link DetectorOptionKeys "检测器配置选项"@endlink
  161. * @param queue 检测请求的异步执行队列
  162. * @return 根据配置生成的新检测器,若初始化失败,则返回nil
  163. */
  164. +(MGLivenessDetector*) detectorOfOptions: (NSDictionary*) options andDispatchQueue: (dispatch_queue_t) queue;
  165. /*!
  166. * 更新检测器参数
  167. *
  168. * @param options 包含构造检测器的各种参数,具体选项请参照 @link DetectorOptionKeys "检测器配置选项"@endlink
  169. * @return 无
  170. */
  171. -(void) setOptions: (NSDictionary*) options;
  172. /*!
  173. * 将视频流获取的原始数据传入检测器进行异步活体检测,检测结果将以异步的形式通知delegate
  174. *
  175. * @param imgBuffer AVCaptureOutput获取的原始数据流,RGB格式
  176. * @param orientation 手机朝向,用于旋转/翻转图像
  177. * @return 请求是否成功
  178. */
  179. -(bool) detectWithBuffer: (CMSampleBufferRef) imgBuffer orientation:(UIImageOrientation) orientation;
  180. /*!
  181. * 将视频流获取的原始数据传入检测器进行异步活体检测,检测结果将以异步的形式通知delegate
  182. *
  183. * @param image 待检测图片,不需要事先旋转/翻转,图片的orientation信息将被用于旋转/翻转图像
  184. * @return 请求是否成功
  185. */
  186. -(bool) detectWithImage: (UIImage*) image;
  187. /*!
  188. * 强制更改当前的活体检测类型,在调用reset方法后必须调用一次该方法后才会进行活体检测,否则delegate的onDetectionSuccess将不会被触发
  189. *
  190. * @param detectionType 活体检测类型,具体选项请参照 MGLivenessDetectionType
  191. * @return 无
  192. */
  193. -(void) changeDetectionType:(MGLivenessDetectionType)detectionType;
  194. /*!
  195. * 当活体检测结束后获得过程中间采集的高质量图片样本
  196. *
  197. * @return 活体检测过程中,每个动作产生一张人脸。其中如果一个动作过程中有质量合格的人脸,则输出质量合格的。否则就输出质量相对最好的一张。
  198. */
  199. -(NSArray*) getValidFrame;
  200. -(FaceIDData*) getFaceIDDataWithMaxImageSize: (int) maxSize;
  201. -(FaceIDData*) getFaceIDData;
  202. /*!
  203. * 重置Detector类的状态,当用户需要重新开始活体检测流程时,调用此函数。
  204. *
  205. * @return 无
  206. */
  207. -(void) reset;
  208. /*!
  209. * 重置当前活体检测状态,仅仅重置当前动作缓存,不会对之前动作的检测记录产生影响。
  210. *
  211. * @return 无
  212. */
  213. -(void) resetAction;
  214. /*!
  215. * 获取算法日志(如有需要可调用)
  216. *
  217. * @return 活体检测算法日志
  218. */
  219. -(NSString*) getAlgorithmLog;
  220. +(NSString*) getCachedAlgorithmLog;
  221. /*!
  222. * 获取版本信息
  223. *
  224. * @return 版本描述
  225. */
  226. +(NSString*) getVersion;
  227. /*!
  228. * 多张人脸的情况下,设置需检测的人脸所在位置
  229. *
  230. * @param rect 人脸位置
  231. * @return 无
  232. */
  233. -(void) setFaceRect: (CGRect) rect;
  234. @end