FURenderKitManager.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // FURenderKitManager.m
  3. // FULiveDemo
  4. //
  5. // Created by 项林平 on 2022/7/22.
  6. //
  7. #import "FURenderKitManager.h"
  8. #import "authpack.h"
  9. #import "FULiveDefine.h"
  10. @interface FURenderKitManager ()
  11. @property (nonatomic, assign) FUDevicePerformanceLevel devicePerformanceLevel;
  12. @property (nonatomic, copy) NSDictionary *configurations;
  13. @end
  14. @implementation FURenderKitManager
  15. + (instancetype)sharedManager {
  16. static FURenderKitManager *instance = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. instance = [[FURenderKitManager alloc] init];
  20. });
  21. return instance;
  22. }
  23. - (void)setupRenderKit {
  24. [FURenderKit setLogLevel:FU_LOG_LEVEL_ERROR];
  25. FUSetupConfig *setupConfig = [[FUSetupConfig alloc] init];
  26. setupConfig.authPack = FUAuthPackMake(g_auth_package, sizeof(g_auth_package));
  27. NSString *controllerPath = [[NSBundle mainBundle] pathForResource:@"controller_cpp" ofType:@"bundle"];
  28. if (controllerPath) {
  29. setupConfig.controllerPath = controllerPath;
  30. }
  31. // 初始化 FURenderKit
  32. [FURenderKit setupWithSetupConfig:setupConfig];
  33. // 设置缓存目录
  34. [FURenderKit setCacheDirectory:FUDocumentPath];
  35. // 算法耗时统计
  36. // [FURenderKit setFrameTimeProfileEnable:YES];
  37. // [FURenderKit setFrameTimeProfileReportDetailsEnable:YES];
  38. // // 算法耗时统计输出到控制台
  39. // [FURenderKit setFrameTimeProfileAutoReportToConsole];
  40. // // 算法耗时统计输出到文件
  41. // [FURenderKit setFrameTimeProfileAutoReportToFile:[FUDocumentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"FUFrameTime %@.txt", FUCurrentDateString()]]];
  42. // 舌头
  43. NSString *path = [[NSBundle mainBundle] pathForResource:@"tongue" ofType:@"bundle"];
  44. [FUAIKit loadTongueMode:path];
  45. _loadModelCount ++;
  46. self.devicePerformanceLevel = [FURenderKit devicePerformanceLevel];
  47. }
  48. - (void)destoryRenderKit {
  49. [FURenderKit destroy];
  50. }
  51. - (void)setDevicePerformanceDetails {
  52. // 设置人脸算法质量
  53. [FUAIKit shareKit].faceProcessorFaceLandmarkQuality = self.devicePerformanceLevel == FUDevicePerformanceLevelHigh ? FUFaceProcessorFaceLandmarkQualityHigh : FUFaceProcessorFaceLandmarkQualityMedium;
  54. // 设置小脸检测是否打开
  55. [FUAIKit shareKit].faceProcessorDetectSmallFace = self.devicePerformanceLevel == FUDevicePerformanceLevelHigh;
  56. }
  57. + (void)loadFaceAIModel {
  58. NSString *faceAIPath = [[NSBundle mainBundle] pathForResource:@"ai_face_processor" ofType:@"bundle"];
  59. [FUAIKit loadAIModeWithAIType:FUAITYPE_FACEPROCESSOR dataPath:faceAIPath];
  60. [FURenderKitManager sharedManager].loadModelCount ++;
  61. }
  62. + (void)loadHumanAIModel:(FUHumanSegmentationMode)mode {
  63. NSString *bodyAIPath = [[NSBundle mainBundle] pathForResource:@"ai_human_processor" ofType:@"bundle"];
  64. [FUAIKit loadAIHumanModelWithDataPath:bodyAIPath segmentationMode:mode];
  65. }
  66. + (void)loadHandAIModel {
  67. NSString *handAIPath = [[NSBundle mainBundle] pathForResource:@"ai_hand_processor" ofType:@"bundle"];
  68. [FUAIKit loadAIModeWithAIType:FUAITYPE_HANDGESTURE dataPath:handAIPath];
  69. }
  70. + (BOOL)faceTracked {
  71. return [FUAIKit aiFaceProcessorNums] > 0;
  72. }
  73. + (BOOL)humanTracked {
  74. return [FUAIKit aiHumanProcessorNums] > 0;
  75. }
  76. + (BOOL)handTracked {
  77. return [FUAIKit aiHandDistinguishNums] > 0;
  78. }
  79. + (void)setMaxFaceNumber:(NSInteger)number {
  80. [FUAIKit shareKit].maxTrackFaces = (int)number;
  81. }
  82. + (void)setMaxHumanNumber:(NSInteger)number {
  83. [FUAIKit shareKit].maxTrackBodies = (int)number;
  84. }
  85. + (void)updateBeautyBlurEffect {
  86. if (![FURenderKit shareRenderKit].beauty || ![FURenderKit shareRenderKit].beauty.enable) {
  87. return;
  88. }
  89. if ([FURenderKitManager sharedManager].devicePerformanceLevel == FUDevicePerformanceLevelHigh) {
  90. // 根据人脸置信度设置不同磨皮效果
  91. CGFloat score = [FUAIKit fuFaceProcessorGetConfidenceScore:0];
  92. if (score > 0.95) {
  93. [FURenderKit shareRenderKit].beauty.blurType = 3;
  94. [FURenderKit shareRenderKit].beauty.blurUseMask = YES;
  95. } else {
  96. [FURenderKit shareRenderKit].beauty.blurType = 2;
  97. [FURenderKit shareRenderKit].beauty.blurUseMask = NO;
  98. }
  99. } else {
  100. // 设置精细磨皮效果
  101. [FURenderKit shareRenderKit].beauty.blurType = 2;
  102. [FURenderKit shareRenderKit].beauty.blurUseMask = NO;
  103. }
  104. }
  105. + (void)resetTrackedResult {
  106. [FUAIKit resetTrackedResult];
  107. }
  108. + (void)setFaceProcessorDetectMode:(FUFaceProcessorDetectMode)mode {
  109. [FUAIKit shareKit].faceProcessorDetectMode = mode;
  110. }
  111. + (void)setHumanProcessorDetectMode:(FUHumanProcessorDetectMode)mode {
  112. [FUAIKit shareKit].humanProcessorDetectMode = mode;
  113. }
  114. + (void)clearItems {
  115. // if([FURenderKitManager sharedManager].loadModelCount > 0){
  116. // [FUAIKit unloadAllAIMode];
  117. // }
  118. [FUAIKit unloadAllAIMode];
  119. [FURenderKit clear];
  120. }
  121. - (NSDictionary *)configurations {
  122. if (!_configurations) {
  123. NSString *path = [[NSBundle mainBundle] pathForResource:@"test_configurations" ofType:@"plist"];
  124. _configurations = [NSDictionary dictionaryWithContentsOfFile:path];
  125. }
  126. return _configurations;
  127. }
  128. - (BOOL)showsLandmarks {
  129. return [self.configurations[@"点位开关"] boolValue];
  130. }
  131. @end