洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

632 lines
16 KiB

9 months ago
  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的配置范围内. 00good_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. @param tg_instance
  496. @param frames RGB
  497. @param depthFrame
  498. @param fpp_attr fpp算法检测后的人脸属性
  499. @return
  500. */
  501. bool tg_process_image(void *tg_instance, const std::vector<TGFrame> &frames, const TGFrame &depthFrame, const TGFaceAttr &fpp_attr);
  502. #if defined JASMINE
  503. /**
  504. * 1:N
  505. * @param tg_instance
  506. * @param topN N个满足阈值的人脸库
  507. * @param threshold
  508. * @param faceSearchResult
  509. * @return
  510. */
  511. bool tg_verify_feature(void *tg_instance,
  512. int topN,
  513. float threshold,
  514. std::vector<FaceSearchResult> &faceSearchResult);
  515. /**
  516. * @param tg_instance
  517. * @param faceDatabase
  518. * @return
  519. */
  520. bool tg_load_feature_database(void *tg_instance,
  521. const std::vector<FaceItem> &faceDatabase);
  522. /**
  523. * @param tg_instance
  524. * @return
  525. */
  526. bool tg_release_feature_database(void *tg_instance);
  527. #endif
  528. #ifdef HUMMINGBIRD
  529. std::vector<float> tg_extract_feature(void *tg_instance);
  530. std::vector<float> tg_detect_extra_feature(void *tg_instance);
  531. void tg_verify_score(void *tg_instance, std::vector<float> tempFeature, float &score, double &far);
  532. #endif
  533. /**
  534. Toyger算法重置
  535. @param tg_instance
  536. */
  537. void tg_reset(void *tg_instance);
  538. /**
  539. Toyger算法
  540. @param tg_instance
  541. */
  542. void tg_release(void *tg_instance);
  543. /**
  544. * sdk版本号
  545. * @return TGSDKVersion
  546. */
  547. TGSDKInfo tg_get_sdk_info();
  548. std::map<std::string, std::string> tg_get_runtime_info(void *tg_instance);
  549. void tg_set_xnn_handle(void *handle);
  550. std::vector<TGDataInfo> tg_get_collection_data(void *tg_instance);