Toyger.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. //#if (defined HUMMINGBIRD) || (defined JASMINE)
  6. //#include "zface_common.h"
  7. //#endif
  8. //10.1.90新增,给xmedia提供的算法服务会读取这个变量,如果在运行中,算法启动失败
  9. static bool tg_algorithm_is_running;
  10. enum ProductCode {
  11. // 默认原子产品
  12. Default = 0,
  13. // 瓢虫
  14. Ladybird = 1,
  15. };
  16. enum ToygerAlgorithm{
  17. Toyger_Algorithm_None = 0,
  18. Toyger_Algorithm_Face = 1 << 0,
  19. Toyger_Algorithm_Doc = 1 << 1
  20. };
  21. /**
  22. * sdk算法能力
  23. */
  24. enum Capacity {
  25. /**
  26. * 无效能力
  27. */
  28. CAPACITY_INVALID = 0,
  29. /**
  30. * 检测,可与其它能力结合使用
  31. */
  32. CAPACITY_DETECTION = 1 << 0,
  33. /**
  34. * 活体,可与其它能力结合使用
  35. */
  36. CAPACITY_LIVENESS = 1 << 1,
  37. /**
  38. * 抽特征,可与其它能力结合使用
  39. */
  40. CAPACITY_FEATURE = 1 << 2,
  41. /**
  42. * 比对,可与其它能力结合使用
  43. */
  44. CAPACITY_VERIFY = 1 << 3,
  45. /**
  46. * 本地MTee环境比对
  47. * 不可与其他类型结合使用
  48. */
  49. CAPACITY_VERIFY_TEE = 1 << 4,
  50. /**
  51. * 嘴部模型
  52. */
  53. CAPACITY_MOUTH = 1 << 5
  54. };
  55. /**
  56. * 人脸库加载失败的原因
  57. */
  58. enum FEATURE_STORE_ERROR_CODE {
  59. /**
  60. * 成功
  61. */
  62. STORE_SUCCESS = 0,
  63. /**
  64. * 失败:未知原因
  65. */
  66. STORE_FAILED_UNKNOWN = 1,
  67. /**
  68. * 失败:feature版本错误
  69. */
  70. STORE_FAILED_VERSION_ERROR = 2,
  71. /**
  72. * 失败:无效的feautre
  73. */
  74. STORE_FAILED_INVALID_FEATURE = 3,
  75. /**
  76. * 失败:人脸库过大
  77. */
  78. STORE_FAILED_OVERSIZE = 4
  79. };
  80. enum ToygerLog{
  81. Toyger_Log_VERBOSE = 2,
  82. Toyger_Log_DEBUG = 3,
  83. Toyger_Log_INFO = 4,
  84. Toyger_Log_WARN = 5,
  85. Toyger_Log_ERROR = 6,
  86. Toyger_Log_ASSERT = 7,
  87. Toyger_Log_Performance = 8,
  88. };
  89. struct TGIRect{
  90. int left;
  91. int top;
  92. int right;
  93. int bottom;
  94. };
  95. struct TGFRect
  96. {
  97. float left;
  98. float top;
  99. float right;
  100. float bottom;
  101. };
  102. struct TGPoint
  103. {
  104. float x;
  105. float y;
  106. };
  107. struct TGQualityConfig
  108. {
  109. int max_face_num = 3;
  110. #ifdef LADYBIRD_SELECTOR
  111. int face_distance_threshold = 80;
  112. #endif
  113. #ifdef DRAGONFLY_SELECTOR
  114. int face_distance_threshold = 150;
  115. #endif
  116. int max_face_distance = 700;
  117. float min_brightness = 0.3;
  118. float min_face_width = 0.25;
  119. float min_integrity = 0.9;
  120. float min_pitch = -0.2;
  121. float max_pitch = 0.2;
  122. float min_yaw = -0.2;
  123. float max_yaw = 0.2;
  124. float max_gaussian = 0.15;
  125. float max_motion = 0;
  126. float min_quality = 20;
  127. float min_iod = 0.18;
  128. float max_iod = 0.45;
  129. float stack_time = 2.f;
  130. float blink_openness = 0.1;
  131. float eye_openness = 0.25;
  132. float eye_occlusion = 0.8;
  133. float lip_motion = 0.15;
  134. float quality_depth_min_quality = 0.5;
  135. int depth_cache_num = 2;
  136. int detectImageFormat;//BGRA = 2, BGR= 4,GRAY = 0,
  137. int detectMode = 0;//0是全检测模式,1是分阶段检测模式
  138. bool detectImageLight = false; // 是否启用人脸亮度检查,打开后[img_light, 208]范围内的人脸才能够通过
  139. int img_light = 82; //detectImageLight打开时有效,注意修改的只是人脸合法亮度范围的下限
  140. int max_image_light = 208; //启用人脸亮度检查时,人脸合法亮度的上限
  141. bool uploadMultiFace = false; //90版本需求,是否上传多人脸信息
  142. bool enableAttrDetect = false; //是否检测人脸属性
  143. };
  144. struct TGCommonConfig{
  145. int log_level; //0 - None, 1 - light, 2 - heavy
  146. int product_code;
  147. int useXNN = 0;
  148. bool auto_complete = true;
  149. std::string xnn_engine_config = "common:cpunum=2,xnnnextgen=1|xNNSec:enginenames=xInt8$";// xnn init config for wallet
  150. };
  151. #ifdef ZDOC
  152. struct TGDocConfig{
  153. int algoType;
  154. int exposure;
  155. int blur;
  156. int card_detect_score;
  157. int rotate_times;
  158. int top_left_x;
  159. int top_left_y;
  160. int bottom_right_x;
  161. int bottom_right_y;
  162. int face_side_sharpness;
  163. int verso_side_sharpness;
  164. int papers_sharpness;
  165. int face_side_exposure;
  166. int verso_side_exposure;
  167. int papers_stability;
  168. };
  169. #endif
  170. struct TGLivenessConfig
  171. {
  172. std::string liveness_combinations = "NoLiveness"; //`#` seperated liveness methods<fpp_blink#yunqi_blink#dragonfly#gemini>
  173. std::string collection; //`#` seperated liveness methods<fpp_blink#yunqi_blink#dragonfly#gemini>
  174. float bat_min_score;
  175. float dragonfly_min;
  176. float dragonfly_max = 0.5;
  177. float gemini_min;
  178. float gemini_max;
  179. float eye_occlusion_threshold = 0.8;
  180. float eye_blink_threshold = 0.2;
  181. float lip_motion_threshold = 0.15;
  182. bool fpp_pre_processed = false;
  183. int faceActionType;
  184. bool detect_before_nano;
  185. unsigned int dragonfly_retry_limit = 0;
  186. };
  187. struct TGCameraConfig
  188. {
  189. float color_intrin[3 * 3];
  190. float depth_intrin[3 * 3];
  191. float color2depth_extrin[4 * 4];
  192. TGIRect roiRect;
  193. bool isAligned;
  194. bool isMirror;
  195. };
  196. struct TGConfig
  197. {
  198. TGQualityConfig quality_config;
  199. TGLivenessConfig liveness_config;
  200. TGCameraConfig camera_config;
  201. TGCommonConfig common_config;
  202. };
  203. #ifdef ZDOC
  204. enum TGDocType{
  205. TG_Doc_Type_Common = 0,
  206. TG_Doc_Type_ID_Card,
  207. TG_Doc_Type_Driver_License,
  208. TG_Doc_Type_Vehicle_License,
  209. };
  210. enum TGDocPageNo{
  211. TG_Doc_Page_NO_ONE = 1,
  212. TG_Doc_Page_NO_TWO = 2,
  213. };
  214. #endif
  215. enum TGFrameFormat
  216. {
  217. TG_Frame_Format_YUV = 0,
  218. TG_Frame_Format_BGRA = 1,
  219. TG_Frame_Format_BGR = 2,
  220. TG_Frame_Format_RGBA = 3,
  221. TG_Frame_Format_RGB = 4,
  222. TG_Frame_Format_Gray_8 = 5,
  223. TG_Frame_Format_Gray_16 = 6,
  224. TG_Frame_Format_DEPTH = 1024,
  225. TG_Frame_Formate_Unknown = 9999,
  226. };
  227. enum TGFrameType
  228. {
  229. TG_Frame_Light = 0,
  230. TG_Frame_Dark,
  231. TG_Frame_Depth,
  232. TG_Frame_IR,
  233. TG_Frame_Nano,
  234. TG_Frame_Gray_Nano,
  235. TG_Frame_Unknown = 9999,
  236. };
  237. class TGFrame
  238. {
  239. public:
  240. TGFrame();
  241. TGFrame(int w, int h, int angle, TGFrameFormat color_space, TGFrameType brightness);
  242. TGFrame(unsigned char *data, int w, int h, int angle, TGFrameFormat color_space, TGFrameType brightness, bool copy = true);
  243. ~TGFrame();
  244. TGFrame(const TGFrame &src);
  245. TGFrame& operator=(const TGFrame &src);
  246. TGFrame clone() const;
  247. bool empty() const;
  248. unsigned char *image_data;
  249. long data_len;
  250. int width;
  251. int height;
  252. int rotation;
  253. TGFrameFormat format;
  254. TGFrameType frame_type;
  255. private:
  256. int getDataLen() const;
  257. void Ref();
  258. int UnRef();
  259. bool _own;
  260. int *_use = nullptr; // todo: not thread safe
  261. };
  262. #pragma mark - Face
  263. struct TGFaceAttr
  264. {
  265. int face_id = -1;
  266. bool has_face;
  267. bool eye_blink;
  268. TGFRect face_region;
  269. float quality;
  270. float yaw;
  271. float pitch;
  272. float gaussian;
  273. float motion;
  274. float brightness;
  275. float integrity;
  276. // 左眼分数、最大最小值
  277. float left_eye_blink_ratio;
  278. float left_eye_blink_ratio_min;
  279. float left_eye_blink_ratio_max;
  280. // 右眼分数、最大最小值
  281. float right_eye_blink_ratio;
  282. float right_eye_blink_ratio_min;
  283. float right_eye_blink_ratio_max;
  284. short distance;
  285. float keypts[10];
  286. //2018年03月31日 新增属性 - 守逸
  287. bool eye_open;
  288. float confidence;
  289. float deepLiveness;
  290. float iodRatio;
  291. float roll;
  292. float depthQuality;
  293. float left_eye_occlusion_score;
  294. float right_eye_occlusion_score;
  295. int gender;
  296. float age;
  297. //人脸亮度 [82,208]区间内是可通过人脸,下限亮度可以通过ToygerQualityConfig.img_light参数控制
  298. float light;
  299. /* 在执行detect阶段时,同一张图片上发现的总的人脸个数,上传服务端供风控环节使用*/
  300. int faceCount;
  301. /* 上传多人脸位置信息时,最多上传3张人脸的位置,首选的人脸在face_region中已存,这里再存最多2张人脸。
  302. [left, top, right, bottom], [left, top, right, bottom]。如果四个位置都为0,说明该张人脸没有检出,不能上传
  303. */
  304. float otherFacePos[8];
  305. /*
  306. 保存到otherFacePos中的人脸个数。可取值范围为[0,2]
  307. */
  308. int otherFaceCount;
  309. bool yaw_left;
  310. bool yaw_right;
  311. bool mouth_open;
  312. bool lip_movement;
  313. };
  314. enum TGMessage{
  315. TG_Message_Image_Perfect = 0,
  316. TG_Message_No_Face,
  317. TG_Message_Distance_Too_Far,
  318. TG_Message_Distance_Too_Close,
  319. TG_Message_Face_Not_In_Center,
  320. TG_Message_Bad_Pitch,
  321. TG_Message_Bad_Yaw,
  322. TG_Message_Is_Moving,
  323. TG_Message_Bad_Brightness,
  324. TG_Message_Bad_Quality,
  325. TG_Message_Bad_Eye_Openness,
  326. TG_Message_Blink_Openness,
  327. TG_Message_Stack_Time,
  328. TG_Message_Depth_Damage,
  329. TG_Message_Left_Yaw_Guide,
  330. TG_Message_Right_Yaw_Guide,
  331. TG_Message_Mouth_Open_Guide,
  332. TG_Message_Lip_Move_Guide
  333. };
  334. enum TGStaticMessage{
  335. TGStaticMessage_NoLiveness = 0,
  336. TGStaticMessage_BlinkLiveness,
  337. TGStaticMessage_LeftYawLiveness,
  338. TGStaticMessage_RightYawLiveness,
  339. TGStaticMessage_MouthOpenLiveness,
  340. TGStaticMessage_LipMoveLiveness,
  341. };
  342. enum TGActionLiveness{
  343. TG_Action_None = 0,
  344. TG_Action_Blink,
  345. TG_Action_Left_YAW,
  346. TG_Action_Right_YAW,
  347. TG_Action_Mouth_Open,
  348. TG_Action_Lip_Move
  349. };
  350. struct TGFaceState
  351. {
  352. TGMessage tip_message = TG_Message_No_Face;
  353. TGStaticMessage static_message;
  354. bool has_face;
  355. int brightness;
  356. int distance;
  357. bool face_in_center;
  358. bool is_moving;
  359. bool good_quality;
  360. int good_pitch;
  361. int good_yaw;
  362. /**
  363. * 人脸图片的亮度是否在TGQualityConfig的配置范围内. 0表示当前人脸图片亮度合法,非0代表不合法。这种与一般习惯不同的用法是为了和其他good_xxx的判断方法保持一致!
  364. */
  365. int good_img_light;
  366. bool eye_openness;
  367. bool blink_openness;
  368. float current_progress;
  369. };
  370. enum TGEventCode{
  371. TG_Event_Code_Monitor = 2,
  372. TG_Event_Code_Log = 1,
  373. TG_Event_Code_Dark_Screen = -1,
  374. TG_Event_Code_RGB_Frame_Corrupted = -2,
  375. TG_Event_Code_Depth_Frame_Corrupted = -3,
  376. TG_Event_Code_Nano_Generate_Failed = -13,
  377. TG_Event_Code_Nano_Generate_Success = -14,
  378. TG_Event_Code_Dragonfly_Liveness_Failed = -15,
  379. TG_Event_Code_Dragonfly_Liveness_Success = -16,
  380. TG_Event_Code_Select_Face_End = -17,
  381. TG_Event_Code_ReDetect_Face_Start = -30,
  382. TG_Event_Code_ReDetect_Face_End = -31,
  383. TG_Event_Code_Pegasus_Liveness_End = -32,
  384. TG_Event_Code_Action_Liveness_blink_End = -42,
  385. TG_Event_Code_Action_Liveness_Reset = -43,
  386. TG_Event_Code_Action_Liveness_End = -45,
  387. };
  388. struct TGFaceInfo
  389. {
  390. TGFrame frame;
  391. TGFaceAttr attr;
  392. };
  393. struct TGDepthFaceInfo
  394. {
  395. TGFrame frame;
  396. TGFaceAttr rgb_attr;
  397. TGFaceAttr depth_attr;
  398. };
  399. enum TGDataType{
  400. TG_DATA_TYPE_GYRO = 0,
  401. TG_DATA_TYPE_PEGASUS = 1,
  402. };
  403. struct TGDataInfo
  404. {
  405. std::vector<unsigned char> rawData;
  406. TGDataType dataType;
  407. };
  408. #ifdef ZDOC
  409. #pragma mark - Doc
  410. struct TGDocAttr
  411. {
  412. };
  413. struct TGDocState
  414. {
  415. bool has_doc;
  416. bool is_completed;
  417. bool is_reflected;
  418. bool is_blur;
  419. bool is_copy;
  420. };
  421. struct TGDocInfo{
  422. TGFrame frame;
  423. TGDocAttr attr;
  424. };
  425. #endif
  426. struct TGSDKInfo {
  427. std::string code_version;
  428. std::string model_version;
  429. int max_face_count;
  430. };
  431. struct TGFeatureStoreResult {
  432. int error_code;
  433. std::vector<std::string> failed_face_id;
  434. };
  435. struct ToygerCallback{
  436. void (*handleCaptureCompleted)(void *cb_receiver, int result,const std::vector<TGFaceInfo>&info,const std::map<std::string, float> &ext);
  437. void (*handleBestImageUpdate)(void *cb_receiver, int result,const std::vector<TGFaceInfo>&info,const std::map<std::string, float> &ext);
  438. void (*handleFaceInfoReady)(void *cb_receiver, const std::vector<TGFaceInfo> &infos, const TGFaceAttr &attr);
  439. void (*handleFaceStateUpdated)(void *cb_receiver,const TGFaceState &state, const TGFaceAttr &attr);
  440. void (*handleEventTriggered)(void *cb_receiver, int event_code, const char *log, const unsigned char *buffer, int buffer_length);
  441. void (*handleLog)(void *cb_receiver,int logLevel, const std::map<std::string, std::string> &logInfo);
  442. bool (*handleModelLoad)(void *cb_receiver, Capacity capacity_mask);
  443. #if (defined IR) || (defined BAT)
  444. void (*handleDepthInfoReady)(void *cb_receiver, const std::vector<uint8_t> &depthBlobInfo);
  445. void (*handleInfraRedInfoReady)(void *cb_receiver, const TGFrame &infraRedFrame);
  446. TGPoint (*handleAlignDepthPoint)(void *cb_receiver, TGPoint pt);
  447. #endif
  448. #if (defined HUMMINGBIRD) || (defined JASMINE)
  449. void (*handleFeatureGenerated)(void *cb_receiver,int status,std::vector<float> &featureData);
  450. void (*handleFeatureSave)(void *cb_receiver,int status,std::vector<float> &featureData);
  451. #endif
  452. #ifdef ZDOC
  453. void (*handleScanCompleted)(void *cb_receiver, int result,const std::vector<TGDocInfo>&info,const std::map<std::string, float> &ext);
  454. void (*handleDocStateUpdated)(void *cb_receiver, const TGDocState &state, const TGDocAttr &attr);
  455. #endif
  456. };
  457. struct DetectedFaceInfo{
  458. TGFaceAttr face_attr;
  459. TGIRect rgb_face_rect;
  460. TGIRect depth_face_rect;
  461. };
  462. /**
  463. Toyger C++ 初始化接口
  464. @param model 传入的模型
  465. @param license 传入的license
  466. @param bundleid 传入的bundle id
  467. @param tg_instance 传入的需要被赋值的 tg_instance
  468. @param algorithm_mask 需要运行的算法
  469. @param capacity_mask 启动的人脸算法能力
  470. @return 初始化是否成功
  471. */
  472. bool tg_init(const std::vector<uint8_t> &model, std::vector<uint8_t> &moutModel, const std::string &license, const std::string &bundleid,
  473. void **tg_instance, ToygerAlgorithm algorithm_mask, int capacity_mask);
  474. bool tg_load_model(void *tg_instance,const std::vector<uint8_t> &faceModel, std::vector<uint8_t> &moutModel, Capacity modelCapacity);
  475. /**
  476. 配置Toyger算法
  477. @param tg_instance 传入的算法实例
  478. @param callback 回调
  479. @param cb_receiver 接受回调的对象
  480. @param config 传入的配置
  481. */
  482. void tg_config(void *tg_instance, ToygerCallback *callback, void *cb_receiver, const TGConfig &config);
  483. #ifdef ZDOC
  484. /**
  485. 配置Toyger 证件算法
  486. @param tg_instance 传入的算法实例
  487. @param callback 回调
  488. @param cb_receiver 接受回调的对象
  489. @param config 传入的配置
  490. @return 配置是否成功
  491. */
  492. bool tg_config(void *tg_instance, ToygerCallback *callback, void *cb_receiver, const TGDocConfig &config);
  493. #endif
  494. /**
  495. 处理每一帧数据
  496. @param tg_instance 传入的算法对象
  497. @param frames 需要处理的RGB 帧
  498. @param depthFrame 需要处理的深度帧
  499. @param fpp_attr 经过fpp算法检测后的人脸属性
  500. @return 是否数据完整
  501. */
  502. bool tg_process_image(void *tg_instance, const std::vector<TGFrame> &frames, const TGFrame &depthFrame, const TGFaceAttr &fpp_attr);
  503. #if defined JASMINE
  504. /**
  505. * 1:N 人脸比对
  506. * @param tg_instance 传入的算法对象
  507. * @param topN 取前N个满足阈值的人脸库
  508. * @param threshold 比对阈值
  509. * @param faceSearchResult
  510. * @return 成功或失败
  511. */
  512. bool tg_verify_feature(void *tg_instance,
  513. int topN,
  514. float threshold,
  515. std::vector<FaceSearchResult> &faceSearchResult);
  516. /**
  517. * @param tg_instance 传入的算法对象
  518. * @param faceDatabase 人脸库
  519. * @return 成功或失败
  520. */
  521. bool tg_load_feature_database(void *tg_instance,
  522. const std::vector<FaceItem> &faceDatabase);
  523. /**
  524. * @param tg_instance 传入的算法对象
  525. * @return 成功或失败
  526. */
  527. bool tg_release_feature_database(void *tg_instance);
  528. #endif
  529. #ifdef HUMMINGBIRD
  530. std::vector<float> tg_extract_feature(void *tg_instance);
  531. std::vector<float> tg_detect_extra_feature(void *tg_instance);
  532. void tg_verify_score(void *tg_instance, std::vector<float> tempFeature, float &score, double &far);
  533. #endif
  534. /**
  535. Toyger算法重置
  536. @param tg_instance 传入的算法对象
  537. */
  538. void tg_reset(void *tg_instance);
  539. /**
  540. 释放Toyger算法
  541. @param tg_instance 传入的算法实例
  542. */
  543. void tg_release(void *tg_instance);
  544. /**
  545. * 获取sdk版本号
  546. * @return TGSDKVersion
  547. */
  548. TGSDKInfo tg_get_sdk_info();
  549. std::map<std::string, std::string> tg_get_runtime_info(void *tg_instance);
  550. void tg_set_xnn_handle(void *handle);
  551. std::vector<TGDataInfo> tg_get_collection_data(void *tg_instance);