BDFaceLivenessViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // LivenessViewController.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by 阿凡树 on 2017/5/23.
  6. // Copyright © 2017年 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceLivenessViewController.h"
  9. #import "BDFaceSuccessViewController.h"
  10. #import "BDFaceLivingConfigModel.h"
  11. #import "BDFaceImageShow.h"
  12. #import <IDLFaceSDK/IDLFaceSDK.h>
  13. @interface BDFaceLivenessViewController (){
  14. }
  15. @property (nonatomic, strong) NSArray *livenessArray;
  16. @property (nonatomic, assign) BOOL order;
  17. @property (nonatomic, assign) NSInteger numberOfLiveness;
  18. @property (nonatomic, assign) BOOL isAnimating;
  19. @end
  20. @implementation BDFaceLivenessViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // 刻度线背颜色
  24. self.circleProgressView.lineBgColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1 / 1.0];
  25. // 刻度线进度颜色
  26. self.circleProgressView.scaleColor = [UIColor colorWithRed:0 / 255.0 green:186 / 255.0 blue:242 / 255.0 alpha:1 / 1.0];
  27. [self.view addSubview:self.circleProgressView];
  28. // 提示动画设置
  29. [self.view addSubview:self.remindAnimationView];
  30. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  31. [self.remindAnimationView setActionImages];
  32. });
  33. }
  34. - (void)viewWillAppear:(BOOL)animated {
  35. [super viewWillAppear:animated];
  36. [[IDLFaceLivenessManager sharedInstance] startInitial];
  37. }
  38. - (void)viewDidDisappear:(BOOL)animated {
  39. [super viewDidDisappear:animated];
  40. [IDLFaceLivenessManager.sharedInstance reset];
  41. }
  42. - (void)onAppBecomeActive {
  43. [super onAppBecomeActive];
  44. [[IDLFaceLivenessManager sharedInstance] livenesswithList:_livenessArray order:_order numberOfLiveness:_numberOfLiveness];
  45. }
  46. - (void)onAppWillResignAction {
  47. [super onAppWillResignAction];
  48. [IDLFaceLivenessManager.sharedInstance reset];
  49. }
  50. - (void)didReceiveMemoryWarning {
  51. [super didReceiveMemoryWarning];
  52. // Dispose of any resources that can be recreated.
  53. }
  54. - (void)livenesswithList:(NSArray *)livenessArray order:(BOOL)order numberOfLiveness:(NSInteger)numberOfLiveness {
  55. _livenessArray = [NSArray arrayWithArray:livenessArray];
  56. _order = order;
  57. _numberOfLiveness = numberOfLiveness;
  58. [[IDLFaceLivenessManager sharedInstance] livenesswithList:livenessArray order:order numberOfLiveness:numberOfLiveness];
  59. }
  60. - (void)faceProcesss:(UIImage *)image {
  61. if (self.hasFinished) {
  62. return;
  63. }
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. self.isAnimating = [self.remindAnimationView isActionAnimating];
  66. });
  67. if (self.isAnimating){
  68. return;
  69. }
  70. __weak typeof(self) weakSelf = self;
  71. [[IDLFaceLivenessManager sharedInstance] livenessNormalWithImage:image previewRect:self.previewRect detectRect:self.detectRect completionHandler:^(NSDictionary *images, FaceInfo *faceInfo, LivenessRemindCode remindCode) {
  72. switch (remindCode) {
  73. case LivenessRemindCodeOK: {
  74. weakSelf.hasFinished = YES;
  75. [self warningStatus:CommonStatus warning:@"非常好"];
  76. NSLog(@"xxxxx0%@",images.allKeys);
  77. if (images[@"image"] != nil && [images[@"image"] count] != 0) {
  78. NSArray *imageArr = images[@"image"];
  79. for (FaceCropImageInfo * image in imageArr) {
  80. NSLog(@"cropImageWithBlack %f %f", image.cropImageWithBlack.size.height, image.cropImageWithBlack.size.width);
  81. NSLog(@"originalImage %f %f", image.originalImage.size.height, image.originalImage.size.width);
  82. }
  83. FaceCropImageInfo * bestImage = imageArr[0];
  84. [[BDFaceImageShow sharedInstance] setSuccessImage:bestImage.originalImage];
  85. [[BDFaceImageShow sharedInstance] setSilentliveScore:bestImage.silentliveScore];
  86. NSLog(@"xxxxx1");
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. UIViewController* fatherViewController = weakSelf.presentingViewController;
  89. NSLog(@"xxxxx2");
  90. [weakSelf dismissViewControllerAnimated:YES completion:^{
  91. weakSelf.resultHandler(bestImage.cropImageWithBlackEncryptStr);
  92. // BDFaceSuccessViewController *avc = [[BDFaceSuccessViewController alloc] init];
  93. // avc.modalPresentationStyle = UIModalPresentationFullScreen;
  94. // [fatherViewController presentViewController:avc animated:YES completion:nil];
  95. [self closeAction];
  96. }];
  97. });
  98. }
  99. [self singleActionSuccess:true];
  100. break;
  101. }
  102. case LivenessRemindCodePitchOutofDownRange:
  103. [self warningStatus:PoseStatus warning:@"请略微抬头" conditionMeet:false];
  104. [self singleActionSuccess:false];
  105. break;
  106. case LivenessRemindCodePitchOutofUpRange:
  107. [self warningStatus:PoseStatus warning:@"请略微低头" conditionMeet:false];
  108. [self singleActionSuccess:false];
  109. break;
  110. case LivenessRemindCodeYawOutofRightRange:
  111. [self warningStatus:PoseStatus warning:@"请略微向右转头" conditionMeet:false];
  112. [self singleActionSuccess:false];
  113. break;
  114. case LivenessRemindCodeYawOutofLeftRange:
  115. [self warningStatus:PoseStatus warning:@"请略微向左转头" conditionMeet:false];
  116. [self singleActionSuccess:false];
  117. break;
  118. case LivenessRemindCodePoorIllumination:
  119. [self warningStatus:CommonStatus warning:@"请使环境光线再亮些" conditionMeet:false];
  120. [self singleActionSuccess:false];
  121. break;
  122. case LivenessRemindCodeNoFaceDetected:
  123. [self warningStatus:CommonStatus warning:@"请将脸移入取景框" conditionMeet:false];
  124. [self singleActionSuccess:false];
  125. break;
  126. case LivenessRemindCodeImageBlured:
  127. [self warningStatus:CommonStatus warning:@"请握稳手机,视线正对屏幕" conditionMeet:false];
  128. [self singleActionSuccess:false];
  129. break;
  130. case LivenessRemindCodeOcclusionLeftEye:
  131. [self warningStatus:occlusionStatus warning:@"左眼有遮挡" conditionMeet:false];
  132. [self singleActionSuccess:false];
  133. break;
  134. case LivenessRemindCodeOcclusionRightEye:
  135. [self warningStatus:occlusionStatus warning:@"右眼有遮挡" conditionMeet:false];
  136. [self singleActionSuccess:false];
  137. break;
  138. case LivenessRemindCodeOcclusionNose:
  139. [self warningStatus:occlusionStatus warning:@"鼻子有遮挡" conditionMeet:false];
  140. [self singleActionSuccess:false];
  141. break;
  142. case LivenessRemindCodeOcclusionMouth:
  143. [self warningStatus:occlusionStatus warning:@"嘴巴有遮挡" conditionMeet:false];
  144. [self singleActionSuccess:false];
  145. break;
  146. case LivenessRemindCodeOcclusionLeftContour:
  147. [self warningStatus:occlusionStatus warning:@"左脸颊有遮挡" conditionMeet:false];
  148. [self singleActionSuccess:false];
  149. break;
  150. case LivenessRemindCodeOcclusionRightContour:
  151. [self warningStatus:occlusionStatus warning:@"右脸颊有遮挡" conditionMeet:false];
  152. [self singleActionSuccess:false];
  153. break;
  154. case LivenessRemindCodeOcclusionChinCoutour:
  155. [self warningStatus:occlusionStatus warning:@"下颚有遮挡" conditionMeet:false];
  156. [self singleActionSuccess:false];
  157. break;
  158. case LivenessRemindCodeTooClose:
  159. [self warningStatus:CommonStatus warning:@"请将脸部离远一点" conditionMeet:false];
  160. [self singleActionSuccess:false];
  161. break;
  162. case LivenessRemindCodeTooFar:
  163. [self warningStatus:CommonStatus warning:@"请将脸部靠近一点" conditionMeet:false];
  164. [self singleActionSuccess:false];
  165. break;
  166. case LivenessRemindCodeBeyondPreviewFrame:
  167. [self warningStatus:CommonStatus warning:@"请将脸移入取景框" conditionMeet:false];
  168. [self singleActionSuccess:false];
  169. break;
  170. case LivenessRemindCodeLiveEye:
  171. [self warningStatus:CommonStatus warning:@"眨眨眼" conditionMeet:true];
  172. [self singleActionSuccess:false];
  173. break;
  174. case LivenessRemindCodeLiveMouth:
  175. [self warningStatus:CommonStatus warning:@"张张嘴" conditionMeet:true];
  176. [self singleActionSuccess:false];
  177. break;
  178. case LivenessRemindCodeLiveYawRight:
  179. [self warningStatus:CommonStatus warning:@"向右缓慢转头" conditionMeet:true];
  180. [self singleActionSuccess:false];
  181. break;
  182. case LivenessRemindCodeLiveYawLeft:
  183. [self warningStatus:CommonStatus warning:@"向左缓慢转头" conditionMeet:true];
  184. [self singleActionSuccess:false];
  185. break;
  186. case LivenessRemindCodeLivePitchUp:
  187. [self warningStatus:CommonStatus warning:@"缓慢抬头" conditionMeet:true];
  188. [self singleActionSuccess:false];
  189. break;
  190. case LivenessRemindCodeLivePitchDown:
  191. [self warningStatus:CommonStatus warning:@"缓慢低头" conditionMeet:true];
  192. [self singleActionSuccess:false];
  193. break;
  194. case LivenessRemindCodeLiveYaw:
  195. [self warningStatus:CommonStatus warning:@"左右摇头" conditionMeet:true];
  196. [self singleActionSuccess:false];
  197. break;
  198. case LivenessRemindCodeSingleLivenessFinished:
  199. {
  200. [[IDLFaceLivenessManager sharedInstance] livenessProcessHandler:^(float numberOfLiveness, float numberOfSuccess, LivenessActionType currenActionType) {
  201. NSLog(@"Finished 非常好 %d %d %d", (int)numberOfLiveness, (int)numberOfSuccess, (int)currenActionType);
  202. dispatch_async(dispatch_get_main_queue(), ^{
  203. [self.circleProgressView setPercent:(CGFloat)(numberOfSuccess / numberOfLiveness)];
  204. });
  205. }];
  206. [self warningStatus:CommonStatus warning:@"非常好" conditionMeet:true];
  207. [self singleActionSuccess:true];
  208. }
  209. break;
  210. case LivenessRemindCodeFaceIdChanged:
  211. {
  212. [[IDLFaceLivenessManager sharedInstance] livenessProcessHandler:^(float numberOfLiveness, float numberOfSuccess, LivenessActionType currenActionType) {
  213. NSLog(@"face id changed %d %d %d", (int)numberOfLiveness, (int)numberOfSuccess, (int)currenActionType);
  214. dispatch_async(dispatch_get_main_queue(), ^{
  215. [self.circleProgressView setPercent:0];
  216. });
  217. }];
  218. [self warningStatus:CommonStatus warning:@"请将脸移入取景框" conditionMeet:true];
  219. }
  220. break;
  221. case LivenessRemindCodeVerifyInitError:
  222. [self warningStatus:CommonStatus warning:@"验证失败"];
  223. break;
  224. case LivenessRemindCodeVerifyDecryptError:
  225. [self warningStatus:CommonStatus warning:@"验证失败"];
  226. break;
  227. case LivenessRemindCodeVerifyInfoFormatError:
  228. [self warningStatus:CommonStatus warning:@"验证失败"];
  229. break;
  230. case LivenessRemindCodeVerifyExpired:
  231. [self warningStatus:CommonStatus warning:@"验证失败"];
  232. break;
  233. case LivenessRemindCodeVerifyMissRequiredInfo:
  234. [self warningStatus:CommonStatus warning:@"验证失败"];
  235. break;
  236. case LivenessRemindCodeVerifyInfoCheckError:
  237. [self warningStatus:CommonStatus warning:@"验证失败"];
  238. break;
  239. case LivenessRemindCodeVerifyLocalFileError:
  240. [self warningStatus:CommonStatus warning:@"验证失败"];
  241. break;
  242. case LivenessRemindCodeVerifyRemoteDataError:
  243. [self warningStatus:CommonStatus warning:@"验证失败"];
  244. break;
  245. case LivenessRemindCodeTimeout: {
  246. // 时间超时,重置之前采集数据
  247. [[IDLFaceLivenessManager sharedInstance] reset];
  248. dispatch_async(dispatch_get_main_queue(), ^{
  249. // 时间超时,ui进度重置0
  250. [self.circleProgressView setPercent:0];
  251. [self isTimeOut:YES];
  252. });
  253. break;
  254. }
  255. case LivenessRemindActionCodeTimeout:{
  256. [[IDLFaceLivenessManager sharedInstance] livenessProcessHandler:^(float numberOfLiveness, float numberOfSuccess, LivenessActionType currenActionType) {
  257. NSLog(@"动作超时 %d %d %d", (int)numberOfLiveness, (int)numberOfSuccess, (int)currenActionType);
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. [self.remindAnimationView startActionAnimating:(int)currenActionType];
  260. });
  261. }];
  262. }
  263. case LivenessRemindCodeConditionMeet: {
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. }];
  270. }
  271. - (void)selfReplayFunction{
  272. [[IDLFaceLivenessManager sharedInstance] reset];
  273. BDFaceLivingConfigModel* model = [BDFaceLivingConfigModel sharedInstance];
  274. [[IDLFaceLivenessManager sharedInstance] livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
  275. }
  276. - (void)warningStatus:(WarningStatus)status warning:(NSString *)warning conditionMeet:(BOOL)meet{
  277. [self warningStatus:status warning:warning];
  278. }
  279. @end