FaceSDK.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. //
  2. // FaceSDK.h
  3. // FaceSDK
  4. //
  5. // Created by Nick(xuli02) on 15/3/31.
  6. // Copyright (c) 2015年 Baidu. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <UIKit/UIKit.h>
  10. typedef enum {
  11. FaceSDKImgTypeYUV,
  12. FaceSDKImgTypeRGB,
  13. } FaceSDKImageType;
  14. typedef enum {
  15. FaceSDKMethodTypeSDM = 0,
  16. FaceSDKMethodTypeCDNN = 1,
  17. FaceSDKMethodTypeSDM_7PTS = 2,
  18. FaceSDKMethodTypeSDM_15PTS = 3,
  19. } FaceSDKMethodType;
  20. typedef enum {
  21. FaceSDKDetectMethodType_BOOST,
  22. FaceSDKDetectMethodType_CNN,
  23. FaceSDKDetectMethodType_NIR
  24. } FaceSDKDetectMethodType;
  25. typedef enum {
  26. FaceSDKParsingModelType_NOT_USE = 0,
  27. FaceSDKParsingModelType_CLASS_NUM_3 = 1,
  28. FaceSDKParsingModelType_CLASS_NUM_7 = 2,
  29. FaceSDKParsingModelType_CLASS_NUM_10 = 3,
  30. } FaceSDKParsingModelType;
  31. typedef enum {
  32. QualitySDKModelType_NOT_USE,
  33. QualitySDKModelType_BLUR,
  34. QualitySDKModelType_OCCLUSION,
  35. } QualitySDKModelType;
  36. typedef enum {
  37. LicenseSuccess = 0,
  38. LicenseInitError,
  39. LicenseDecryptError,
  40. LicenseInfoFormatError,
  41. LicenseExpired,
  42. LicenseMissRequiredInfo,
  43. LicenseInfoCheckError,
  44. LicenseLocalFileError,
  45. LicenseRemoteDataError
  46. } FaceLicenseErrorCode;
  47. NS_ASSUME_NONNULL_BEGIN
  48. @interface LivenessState : NSObject
  49. /** 眼状态 */
  50. @property(nonatomic, assign) BOOL isLiveEye;
  51. @property(nonatomic, assign) NSInteger leftEyeStatus;
  52. @property(nonatomic, assign) NSInteger rightEyeStatus;
  53. /** 嘴状态 */
  54. @property(nonatomic, assign) BOOL isLiveMouth;
  55. @property(nonatomic, assign) NSInteger mouthStatus;
  56. /** 3D info*/
  57. @property(nonatomic, assign) BOOL isLiveYawLeft;
  58. @property(nonatomic, assign) BOOL isLiveYawRight;
  59. @property(nonatomic, assign) NSInteger yawAngles;
  60. @property(nonatomic, assign) BOOL isLivePitchUp;
  61. @property(nonatomic, assign) BOOL isLivePitchDown;
  62. @property(nonatomic, assign) NSInteger pitchAngles;
  63. @end
  64. @interface FaceSDK : NSObject
  65. /**
  66. * 初始化方法
  67. *
  68. * @return 实例
  69. */
  70. + (FaceSDK * _Nullable)sharedInstance;
  71. /**
  72. * 鉴权方法
  73. * SDK鉴权方法 必须在使用其他方法之前设置,否则会导致SDK不可用
  74. * @param licenseId 鉴权api key
  75. * @param localLicencePath 本地鉴权文件路径
  76. */
  77. - (void)initWithLicenseID:(NSString *)licenseId andLocalLicenceFile:(NSString *)localLicencePath;
  78. - (void)initWithToken:(NSString *)token;
  79. /**
  80. * 开启网络鉴权
  81. * 默认开启
  82. *
  83. * @param remoteAuthorize 是否开启网络鉴权
  84. */
  85. - (void)setRemoteAuthorize:(BOOL)remoteAuthorize;
  86. /**
  87. * SDK是否可用
  88. * 如果可用返回结果为0
  89. *
  90. * @return SDK可用结果
  91. */
  92. - (FaceLicenseErrorCode)canWork;
  93. /**
  94. * 人脸识别
  95. *
  96. * @param image 图像
  97. * @param methodType 识别算法模型 CNN / NIR
  98. * @param faceRects output人脸框的位置
  99. * @param faceNumber output识别人脸的数目
  100. * @param minSize 最小人脸size
  101. */
  102. - (void)detectFaceInImage:(UIImage *)image
  103. withMethodType:(FaceSDKDetectMethodType)methodType
  104. andGetFaceRects:( NSArray *_Nullable*_Nullable)faceRects
  105. andFaceNumber:(NSInteger *)faceNumber
  106. andMinimumFaceSize:(NSInteger)minSize;
  107. /**
  108. * 人脸对齐
  109. *
  110. * @param image 图像
  111. * @param methodType 识别算法模型 SDM / CDNN
  112. * @param faceRect 人脸位置
  113. * @param faceShape output人脸特征点
  114. * @param numberOfPoint output人脸特征点个数
  115. * @param score output人脸置信度
  116. */
  117. - (void)alignFaceInImage:(UIImage *)image
  118. withMethodType:(FaceSDKMethodType)methodType
  119. andFaceRect:(CGRect)faceRect
  120. andOutputFaceShape:( NSArray *_Nullable*_Nullable)faceShape
  121. andNumOfPoints:(NSInteger *)numberOfPoint
  122. andScore:(CGFloat *)score;
  123. /**
  124. * 人脸精确对齐
  125. *
  126. * @param image 图像
  127. * @param methodType 识别算法模型 SDM / CDNN
  128. * @param faceShape input/output人脸特征点
  129. * @param numberOfPoints 人脸特征点个数
  130. */
  131. - (void)fineAlignFaceInImage:(UIImage *)image
  132. withMethodType:(FaceSDKMethodType)methodType
  133. andFaceShape:(NSArray *_Nullable*_Nullable)faceShape
  134. andNumOfPoints:(NSInteger)numberOfPoints;
  135. /**
  136. * 人脸追踪
  137. *
  138. * @param image 图像
  139. * @param methodType 识别算法模型 SDM / CDNN
  140. * @param faceShape input and output人脸特征点
  141. * @param numberOfPoints 人脸特征点个数
  142. * @param score output置信度
  143. */
  144. - (void)trackFaceInImage:(UIImage *)image
  145. withMethodType:(FaceSDKMethodType)methodType
  146. andFaceShape:( NSArray *_Nullable*_Nullable)faceShape
  147. andNumOfPoints:(NSInteger )numberOfPoints
  148. andScore:(CGFloat *)score;
  149. /**
  150. * 获取头部姿态(1)
  151. *
  152. * @param faceShape input and output 人脸特征点
  153. * @param numberOfPoints 人脸特征点个数
  154. * @param rows 图像宽
  155. * @param colums 图像高
  156. *
  157. * @return 头部姿态 pitch,yaw,roll
  158. */
  159. - (NSDictionary *)headPostEstimationWithFaceShape:(NSArray *)faceShape
  160. andImgDataRowNum:(NSInteger)rows
  161. andImgColumNum:(NSInteger)colums
  162. andNumOfPoints:(NSInteger )numberOfPoints;
  163. /**
  164. * 人脸解析
  165. *
  166. * @param image 图像
  167. * @param modelType 解析模式:outline / detail features
  168. * @param faceShape 人脸特征点
  169. * @param numberOfPoints 人脸特征点个数
  170. *
  171. * @return
  172. */
  173. - (unsigned char *)parsingFaceInImage:(UIImage *)image
  174. withParsingModelType:(FaceSDKParsingModelType)modelType
  175. andFaceShape:(NSArray *)faceShape
  176. andNumOfPoints:(NSInteger )numberOfPoints;
  177. /**
  178. * 获取头部姿态(1)
  179. *
  180. * @param image 图像
  181. * @param faceShape input and output 人脸特征点
  182. * @param numberOfPoints 人脸特征点个数
  183. *
  184. * @return 头部姿态 pitch,yaw,roll
  185. */
  186. - (NSDictionary *)headPostEstimationWithImage:(UIImage *)image
  187. andFaceShape:(NSArray *)faceShape
  188. andNumOfPoints:(NSInteger )numberOfPoints;
  189. /**
  190. * 活体检测
  191. *
  192. * @param image 图像
  193. * @param faceShape input and output 人脸特征点
  194. * @param numberOfPoints 人脸特征点个数
  195. * @param flag 如果track失败传入0 其他传入1
  196. * @param state output 活体
  197. */
  198. - (void)livenessDetectInImage:(UIImage *)image
  199. andFaceShape:( NSArray *_Nullable*_Nullable)faceShape
  200. andNumOfPoints:(NSInteger )numberOfPoints
  201. andFlag:(NSInteger)flag
  202. andOutPutState:(LivenessState *)state;
  203. /**
  204. * 图片网纹处理
  205. *
  206. * @param image 图像
  207. * @param width 图片处理后的宽
  208. * @param height 图片处理后的高
  209. * @param outImage output处理后的图片
  210. */
  211. - (void)removeTextureWith:(UIImage *)image
  212. outImageWidth:(NSInteger *_Nullable)width
  213. outImageHeight:(NSInteger *_Nullable)height
  214. outImage:(UIImage *_Nullable*_Nullable)outImage;
  215. /**
  216. * 图片超分辨率处理(芯片照预处理)
  217. *
  218. * @param image 图像
  219. * @param width 图片处理后的宽
  220. * @param height 图片处理后的高
  221. * @param outImage output处理后的图片
  222. */
  223. - (void)superResolutionWith:(UIImage *)image
  224. outImageWidth:(NSInteger *_Nullable)width
  225. outImageHeight:(NSInteger *_Nullable)height
  226. outImage:(UIImage *_Nullable*_Nullable)outImage;
  227. //~*~*~*~*~*~*~*~*~*~**~**~*~**~*~**~**~**~**~~*~*~*~*~*~*~*~*~*~**~**~*~**~*~**~**~**~**~
  228. #pragma mark - Utils
  229. /**
  230. * 根据特征点获取人脸各部位结构 (目前仅适用于72个特征点的模型)
  231. *
  232. * @param faceShape 人脸特征点
  233. *
  234. * @return 人脸结构
  235. * {0,1,2,3,4,5,6,7,8,9,10,11,12}; //contour
  236. * {13,14,15,16,17,18,19,20,13,21}; //left eye
  237. * {22,23,24,25,26,27,28,29,22}; //left eye brow
  238. * {30,31,32,33,34,35,36,37,30,38}; //right eye
  239. * {39,40,41,42,43,44,45,46,39}; //right eye brow
  240. * {47,48,49,50,51,52,53,54,55,56,47}; //nose
  241. * {51,57,52}; //nose tip
  242. * {58,59,60,61,62,63,64,65,58}; //lips outer contour
  243. * {58,66,67,68,62,69,70,71,58}; //lips inner contour
  244. *
  245. */
  246. - (NSDictionary *)faceDetailFromFaceShapeArray:(NSArray *)faceShape;
  247. /**
  248. * 静默活体检测
  249. *
  250. * @param image 图像
  251. * @param shapePoints 人脸特征点
  252. */
  253. - (CGFloat)livenessSilentDetectInImage:(UIImage *)image
  254. faceShape:(NSArray *_Nullable*_Nullable)shapePoints;
  255. @end
  256. #pragma mark - FaceVerifier
  257. typedef enum {
  258. FaceVerifierActionTypeDelete = 0,
  259. FaceVerifierActionTypeRegister,
  260. FaceVerifierActionTypeVerify,
  261. FaceVerifierActionTypeRecognition,
  262. FaceVerifierActionTypeCheckName,
  263. } FaceVerifierActionType;
  264. typedef enum {
  265. ErrorCodeOK,
  266. ErrorCodePitchOutofDownRange, // 头部偏低
  267. ErrorCodePitchOutofUpRange, // 头部偏高
  268. ErrorCodeYawOutofLeftRange, // 头部偏左
  269. ErrorCodeYawOutofRightRange, // 头部偏右
  270. ErrorCodePoorIllumination, // 光照不足
  271. ErrorCodeNoFaceDetected, // 没有检测到人脸
  272. ErrorCodeDataNotReady, // 数据没有准备好
  273. ErrorCodeDataHitOne, // 采集到一张图片
  274. ErrorCodeDataHitLast, // 采集完最后一张
  275. ErrorCodeImgBlured, // 图像模糊
  276. ErrorCodeOcclusionLeftEye, // 左眼有遮挡
  277. ErrorCodeOcclusionRightEye, // 右眼有遮挡
  278. ErrorCodeOcclusionNose, // 鼻子有遮挡
  279. ErrorCodeOcclusionMouth, // 嘴巴有遮挡
  280. ErrorCodeOcclusionLeftContour, // 左脸颊有遮挡
  281. ErrorCodeOcclusionRightContour, // 右脸颊有遮挡
  282. ErrorCodeOcclusionChinCoutour, // 下颚有遮挡
  283. ErrorCodeFaceNotComplete, // 没有完成
  284. ErrorCodeUnknowType // 未知错误
  285. } FaceVerifierErrorCode;
  286. @interface FaceVerifier : NSObject
  287. /**
  288. * 初始化方法
  289. *
  290. * @return 实例
  291. */
  292. + (FaceVerifier * _Nullable)sharedInstance;
  293. /**
  294. * SDK鉴权方法
  295. * SDK鉴权方法 必须在使用其他方法之前设置,否则会导致SDK不可用
  296. *
  297. * @param licenseId 鉴权api key
  298. * @param localLicencePath 本地鉴权文件路径
  299. */
  300. - (void)setLicenseID:(NSString *)licenseId andLocalLicenceFile:(NSString *)localLicencePath;
  301. - (void)initWithToken:(NSString *)token;
  302. /**
  303. * 开启网络鉴权
  304. * 默认开启
  305. *
  306. * @param remoteAuthorize 是否开启网络鉴权
  307. */
  308. - (void)setRemoteAuthorize:(BOOL)remoteAuthorize;
  309. /**
  310. * SDK是否可用
  311. * 如果可用返回结果为0
  312. *
  313. * @return SDK可用结果
  314. */
  315. - (FaceLicenseErrorCode)canWork;
  316. /**
  317. * 设置追踪最小人脸尺寸
  318. * 默认100
  319. *
  320. * @param size 人脸尺寸
  321. */
  322. - (void)setMinFaceSize:(NSInteger)size;
  323. - (void)setIfNeedFineAlign:(BOOL)isFineAlign;
  324. /**
  325. * 设置是否检测活体
  326. * 默认false
  327. *
  328. * @param isVerifyLife 否检测活体
  329. */
  330. - (void)setIsVerifyLive:(BOOL)isVerifyLife;
  331. /**
  332. * 设置追踪目标时间间隔
  333. * 默认500ms
  334. *
  335. * @param interval 时间间隔
  336. */
  337. - (void)setTrackByDetectionInterval:(NSTimeInterval)interval;
  338. /**
  339. * 设置未跟踪目标时的时间间隔
  340. * 默认1000ms
  341. *
  342. * @param interval 时间间隔
  343. */
  344. - (void)setDetectInVideoInterval:(NSTimeInterval)interval;
  345. /**
  346. * 设置抠图大小
  347. * 默认256像素
  348. *
  349. * @param width 抠图大小
  350. */
  351. - (void)setCropFaceSizeWithWidth:(CGFloat)width;
  352. /**
  353. * 设置非人脸的置信度阈值
  354. * 默认0.5 取值范围0 ~ 1
  355. *
  356. * @param thr 置信度阈值
  357. */
  358. - (void)setNotFaceThr:(CGFloat)thr;
  359. /**
  360. * 设置遮挡阈值
  361. * 默认0.5 取值范围0 ~ 1
  362. *
  363. * @param thr 遮挡阈值
  364. */
  365. - (void)setOccluThr:(CGFloat)thr;
  366. /**
  367. * 设置光照阈值
  368. * 默认40 取值范围0 ~ 255
  369. *
  370. * @param thr 光照阈值
  371. */
  372. - (void)setIllumThr:(NSInteger)thr;
  373. /**
  374. * 设置模糊阈值
  375. * 默认0.7 取值范围0 ~ 1
  376. * @param thr 模糊阈值
  377. */
  378. - (void)setBlurThr:(CGFloat)thr;
  379. /**
  380. * 设置Align模型类型
  381. * 默认CDNN
  382. *
  383. * @param type 模型类型
  384. */
  385. - (void)setAlignMethodType:(NSInteger)type;
  386. /**
  387. * 设置Detect模型类型
  388. * 默认CNN
  389. *
  390. * @param type 模型类型
  391. */
  392. - (void)setDetectMethodType:(NSInteger)type;
  393. /**
  394. * 设置3D姿态角
  395. * 默认15°
  396. *
  397. * @param degree pitch
  398. * @param yawDegree yaw
  399. * @param rollDegree roll
  400. */
  401. - (void)setEulurAngleThrPitch:(NSInteger)degree
  402. yaw:(NSInteger)yawDegree
  403. roll:(NSInteger)rollDegree;
  404. /**
  405. * 设置人脸抠图张数
  406. * 默认3张 最多三张
  407. *
  408. * @param imageNum 抠图数量
  409. */
  410. - (void)setMaxRegImgNum:(NSInteger)imageNum;
  411. /**
  412. * 质量检测
  413. * 默认false
  414. *
  415. * @param flag 是否执行质量检测
  416. */
  417. - (void)setIsCheckQuality:(BOOL)flag;
  418. /**
  419. * 设置抠图时截取的人脸大小倍数
  420. * 默认3.0
  421. *
  422. * @param thr 人脸大小倍数
  423. */
  424. - (void)setCropFaceEnlargeRatio:(CGFloat)thr;
  425. /**
  426. * 获取人脸抠图大小 以像素为单位
  427. *
  428. * @return 抠图大小
  429. */
  430. - (NSInteger)getCropFaceSize;
  431. /**
  432. * 获取追踪到的人脸
  433. *
  434. * @return 追踪到的人脸数组
  435. */
  436. - (NSArray *)getTrackedFace;
  437. /**
  438. * 获取人脸抠图
  439. *
  440. * @return 人脸抠图数据集
  441. */
  442. - (NSArray *)regImages;
  443. /** 是否正在tracking */
  444. - (BOOL)isUnderTrack;
  445. /** 人脸特征点集合 */
  446. - (NSArray *)faceShapes;
  447. /** 清除追踪人脸数据*/
  448. - (void)clearTrackedFaces;
  449. /** 活体检测状态,仅对单人模式有效(maxfacecount = 1) */
  450. - (LivenessState *)livenessState;
  451. /** 注册用的HttpString仅对单人模式有效(maxfacecount = 1) */
  452. - (NSString *)httpString;
  453. /** errorcode for the process of preparing data */
  454. - (NSInteger)errorCode;
  455. /**
  456. * 多人脸追踪
  457. *
  458. * @param image 图像
  459. * @param count 追踪人脸数目
  460. */
  461. - (void)trackWithImage:(UIImage *)image andMaxFaceCount:(NSUInteger)count;
  462. /**
  463. * 人脸追踪识别
  464. *
  465. * @param image 图像
  466. * @param actionType 识别模式
  467. * @return 人脸识别结果
  468. */
  469. - (FaceVerifierErrorCode)prepareDataWithImage:(UIImage *)image
  470. andActionType:(FaceVerifierActionType)actionType;
  471. /**
  472. * 最大人脸追踪识别
  473. *
  474. * @param image 图像
  475. * @param actionType 识别模式
  476. * @return 最大人脸识别结果
  477. */
  478. - (FaceVerifierErrorCode)prepareDataForMaxFaceWithImage:(UIImage *)image
  479. andActionType:(FaceVerifierActionType)actionType;
  480. /**
  481. * 人脸质量检测
  482. *
  483. * @param image 图像
  484. * @param faceShape 人脸特征点
  485. * @param numberOfPoints 人脸特征点个数
  486. * @param bluriness output人脸模糊值
  487. * @param illum output光照值
  488. * @param occlusion output人脸部位
  489. * @param nOccluPart output人脸部位数量
  490. */
  491. - (void)imageQualityWith:(UIImage *)image
  492. andFaceShape:(NSArray *_Nullable*_Nullable)faceShape
  493. andNumOfPoints:(NSInteger)numberOfPoints
  494. bluriness:(CGFloat *)bluriness
  495. illum:(NSInteger *)illum
  496. occlusion:(NSArray *_Nullable*_Nullable)occlusion
  497. nOccluPart:(NSInteger *)nOccluPart;
  498. /**
  499. * 人脸抠图
  500. *
  501. * @param image 图像
  502. * @param faceShape 人脸特征点
  503. * @param numberOfPoints 人脸特征点数目
  504. * @param width 抠图宽
  505. * @param height 抠图高
  506. * @param cropImage output抠图结果图片
  507. * @param cropShaps output抠图结果图片特征点
  508. */
  509. - (void)cropFaceImageWith:(UIImage *)image
  510. FaceShape:(NSArray *_Nullable*_Nullable)faceShape
  511. numOfPoints:(NSInteger)numberOfPoints
  512. faceImageWidth:(NSInteger)width
  513. faceImageHeight:(NSInteger)height
  514. cropImage:(UIImage *_Nullable*_Nullable)cropImage
  515. cropShaps:(NSArray *_Nullable*_Nullable)cropShaps;
  516. /**
  517. * 开启日志打印
  518. * 默认关闭
  519. *
  520. * @param visible 是否开启日志打印
  521. */
  522. - (void)setLogVisible:(BOOL)visible;
  523. /**
  524. * 获取SDK版本
  525. *
  526. * @return SDK当前版本
  527. */
  528. - (NSString *)getVersion;
  529. @end
  530. @interface TrackedFaceInfoModel : NSObject
  531. /** 人脸属性 */
  532. @property(nonatomic, assign) CGRect faceRect;
  533. @property(nonatomic, assign) NSInteger faceId;
  534. @property(nonatomic, assign) CGFloat score;
  535. @property(nonatomic, assign) BOOL isDetect;
  536. @property(nonatomic, assign) NSInteger angle;
  537. @property(nonatomic, assign) CGFloat conf;
  538. @property(nonatomic, copy) NSArray *landMarks;
  539. @property(nonatomic, copy) NSArray *headPose;
  540. @property(nonatomic, copy) NSArray *isLive;
  541. @property(nonatomic, copy) NSArray *regImage;
  542. @property(nonatomic, copy) NSArray *regLandmarks;
  543. @property(nonatomic, copy) NSArray *imgDigest;
  544. @end
  545. NS_ASSUME_NONNULL_END