FaceBaseViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // FaceBaseViewController.m
  3. // IDLFaceSDKDemoOC
  4. //
  5. // Created by 阿凡树 on 2017/5/23.
  6. // Copyright © 2017年 Baidu. All rights reserved.
  7. //
  8. #import "FaceBaseViewController.h"
  9. #import <IDLFaceSDK/IDLFaceSDK.h>
  10. #import "VideoCaptureDevice.h"
  11. #import "ImageUtils.h"
  12. #import "RemindView.h"
  13. #define scaleValue 0.7
  14. #define ScreenRect [UIScreen mainScreen].bounds
  15. #define ScreenWidth [UIScreen mainScreen].bounds.size.width
  16. #define ScreenHeight [UIScreen mainScreen].bounds.size.height
  17. @interface FaceBaseViewController () <CaptureDataOutputProtocol>
  18. @property (nonatomic, readwrite, retain) VideoCaptureDevice *videoCapture;
  19. @property (nonatomic, readwrite, retain) UILabel *remindLabel;
  20. @property (nonatomic, readwrite, retain) RemindView * remindView;
  21. @property (nonatomic, readwrite, retain) UILabel * remindDetailLabel;
  22. @property (nonatomic, readwrite, retain) UIImageView * successImage;
  23. @end
  24. @implementation FaceBaseViewController
  25. - (void)dealloc
  26. {
  27. [[NSNotificationCenter defaultCenter] removeObserver:self];
  28. }
  29. - (void) setHasFinished:(BOOL)hasFinished {
  30. _hasFinished = hasFinished;
  31. if (hasFinished) {
  32. [self.videoCapture stopSession];
  33. self.videoCapture.delegate = nil;
  34. }
  35. }
  36. - (void)warningStatus:(WarningStatus)status warning:(NSString *)warning
  37. {
  38. __weak typeof(self) weakSelf = self;
  39. dispatch_async(dispatch_get_main_queue(), ^{
  40. if (status == PoseStatus) {
  41. [weakSelf.remindLabel setHidden:true];
  42. [weakSelf.remindView setHidden:false];
  43. [weakSelf.remindDetailLabel setHidden:false];
  44. weakSelf.remindDetailLabel.text = warning;
  45. }else if (status == occlusionStatus) {
  46. [weakSelf.remindLabel setHidden:false];
  47. [weakSelf.remindView setHidden:true];
  48. [weakSelf.remindDetailLabel setHidden:false];
  49. weakSelf.remindDetailLabel.text = warning;
  50. weakSelf.remindLabel.text = @"脸部有遮挡";
  51. }else {
  52. [weakSelf.remindLabel setHidden:false];
  53. [weakSelf.remindView setHidden:true];
  54. [weakSelf.remindDetailLabel setHidden:true];
  55. weakSelf.remindLabel.text = warning;
  56. }
  57. });
  58. }
  59. - (void)singleActionSuccess:(BOOL)success
  60. {
  61. __weak typeof(self) weakSelf = self;
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. if (success) {
  64. [weakSelf.successImage setHidden:false];
  65. }else {
  66. [weakSelf.successImage setHidden:true];
  67. }
  68. });
  69. }
  70. - (void)viewDidLoad {
  71. [super viewDidLoad];
  72. // 初始化相机处理类
  73. self.videoCapture = [[VideoCaptureDevice alloc] init];
  74. self.videoCapture.delegate = self;
  75. // 用于播放视频流
  76. self.detectRect = CGRectMake(ScreenWidth*(1-scaleValue)/2.0, ScreenHeight*(1-scaleValue)/2.0, ScreenWidth*scaleValue, ScreenHeight*scaleValue);
  77. self.displayImageView = [[UIImageView alloc] initWithFrame:self.detectRect];
  78. self.displayImageView.contentMode = UIViewContentModeScaleAspectFit;
  79. [self.view addSubview:self.displayImageView];
  80. self.coverImage = [ImageUtils getImageResourceForName:@"facecover"];
  81. CGRect circleRect = [ImageUtils convertRectFrom:CGRectMake(125, 334, 500, 500) imageSize:self.coverImage.size detectRect:ScreenRect];
  82. self.previewRect = CGRectMake(circleRect.origin.x - circleRect.size.width*(1/scaleValue-1)/2.0, circleRect.origin.y - circleRect.size.height*(1/scaleValue-1)/2.0 - 60, circleRect.size.width/scaleValue, circleRect.size.height/scaleValue);
  83. //画圈
  84. self.circleView = [[CircleView alloc] initWithFrame:ScreenRect];
  85. self.circleView.circleRect = circleRect;
  86. [self.view addSubview:self.circleView];
  87. // 遮罩
  88. UIImageView* coverImageView = [[UIImageView alloc] initWithFrame:ScreenRect];
  89. coverImageView.image = [ImageUtils getImageResourceForName:@"facecover"];
  90. coverImageView.contentMode = UIViewContentModeScaleAspectFill;
  91. [self.view addSubview:coverImageView];
  92. //successImage
  93. self.successImage = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMinX(circleRect)+CGRectGetWidth(circleRect)/2.0-57/2.0, CGRectGetMinY(circleRect)-57/2.0, 57, 57)];
  94. self.successImage.image = [ImageUtils getImageResourceForName:@"success"];
  95. [self.view addSubview:self.successImage];
  96. [self.successImage setHidden:true];
  97. // 关闭
  98. UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  99. [closeButton setImage:[ImageUtils getImageResourceForName:@"close"] forState:UIControlStateNormal];
  100. [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  101. closeButton.frame = CGRectMake(20, 30, 30, 30);
  102. [self.view addSubview:closeButton];
  103. // 提示框
  104. self.remindLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, circleRect.origin.y-70, ScreenWidth, 30)];
  105. self.remindLabel.textAlignment = NSTextAlignmentCenter;
  106. self.remindLabel.textColor = OutSideColor;
  107. self.remindLabel.font = [UIFont boldSystemFontOfSize:22.0];
  108. [self.view addSubview:self.remindLabel];
  109. self.remindView = [[RemindView alloc]initWithFrame:CGRectMake((ScreenWidth-200)/2.0, CGRectGetMinY(self.remindLabel.frame), 200, 45)];
  110. [self.view addSubview:self.remindView];
  111. [self.remindView setHidden:YES];
  112. self.remindDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(circleRect)+20, ScreenWidth, 30)];
  113. self.remindDetailLabel.font = [UIFont systemFontOfSize:20];
  114. self.remindDetailLabel.textColor = [UIColor whiteColor];
  115. self.remindDetailLabel.textAlignment = NSTextAlignmentCenter;
  116. self.remindDetailLabel.text = @"建议略微抬头";
  117. [self.view addSubview:self.remindDetailLabel];
  118. [self.remindDetailLabel setHidden:true];
  119. // 监听重新返回APP
  120. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillResignAction) name:UIApplicationWillResignActiveNotification object:nil];
  121. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
  122. /*
  123. // 设置最小检测人脸阈值
  124. [[FaceSDKManager sharedInstance] setMinFaceSize:200];
  125. // 设置截取人脸图片大小
  126. [[FaceSDKManager sharedInstance] setCropFaceSizeWidth:400];
  127. // 设置人脸遮挡阀值
  128. [[FaceSDKManager sharedInstance] setOccluThreshold:0.5];
  129. // 设置亮度阀值
  130. [[FaceSDKManager sharedInstance] setIllumThreshold:40];
  131. // 设置图像模糊阀值
  132. [[FaceSDKManager sharedInstance] setBlurThreshold:0.7];
  133. // 设置头部姿态角度
  134. [[FaceSDKManager sharedInstance] setEulurAngleThrPitch:10 yaw:10 roll:10];
  135. // 设置是否进行人脸图片质量检测
  136. [[FaceSDKManager sharedInstance] setIsCheckQuality:YES];
  137. // 设置超时时间
  138. [[FaceSDKManager sharedInstance] setConditionTimeout:10];
  139. // 设置人脸检测精度阀值
  140. [[FaceSDKManager sharedInstance] setNotFaceThreshold:0.6];
  141. // 设置照片采集张数
  142. [[FaceSDKManager sharedInstance] setMaxCropImageNum:1];
  143. */
  144. }
  145. - (void)viewDidDisappear:(BOOL)animated {
  146. [super viewDidDisappear:animated];
  147. self.hasFinished = YES;
  148. self.videoCapture.runningStatus = NO;
  149. }
  150. - (void)viewWillAppear:(BOOL)animated {
  151. [super viewWillAppear:animated];
  152. _hasFinished = NO;
  153. self.videoCapture.runningStatus = YES;
  154. [self.videoCapture startSession];
  155. }
  156. - (void)didReceiveMemoryWarning {
  157. [super didReceiveMemoryWarning];
  158. // Dispose of any resources that can be recreated.
  159. }
  160. - (void)faceProcesss:(UIImage *)image {
  161. }
  162. - (void)closeAction {
  163. _hasFinished = YES;
  164. self.videoCapture.runningStatus = NO;
  165. [self dismissViewControllerAnimated:YES completion:nil];
  166. }
  167. #pragma mark - Notification
  168. - (void)onAppWillResignAction {
  169. _hasFinished = YES;
  170. }
  171. - (void)onAppBecomeActive {
  172. _hasFinished = NO;
  173. }
  174. #pragma mark - CaptureDataOutputProtocol
  175. - (void)captureOutputSampleBuffer:(UIImage *)image {
  176. if (_hasFinished) {
  177. return;
  178. }
  179. __weak typeof(self) weakSelf = self;
  180. dispatch_async(dispatch_get_main_queue(), ^{
  181. weakSelf.displayImageView.image = image;
  182. });
  183. [self faceProcesss:image];
  184. }
  185. - (void)captureError {
  186. NSString *errorStr = @"出现未知错误,请检查相机设置";
  187. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  188. if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
  189. errorStr = @"相机权限受限,请在设置中启用";
  190. }
  191. __weak typeof(self) weakSelf = self;
  192. dispatch_async(dispatch_get_main_queue(), ^{
  193. UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:errorStr preferredStyle:UIAlertControllerStyleAlert];
  194. UIAlertAction* action = [UIAlertAction actionWithTitle:@"知道啦" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  195. NSLog(@"知道啦");
  196. }];
  197. [alert addAction:action];
  198. UIViewController* fatherViewController = weakSelf.presentingViewController;
  199. [weakSelf dismissViewControllerAnimated:YES completion:^{
  200. [fatherViewController presentViewController:alert animated:YES completion:nil];
  201. }];
  202. });
  203. }
  204. @end