本文介绍如何在你的项目中集成和使用商汤高级美颜/特效系列插件(以下简称为“商汤美颜插件”),包括 Android 和 iOS 平台。
商汤美颜插件是对商汤 SenseAR Effects SDK 核心 API 的封装。通过声网 SDK v4.x 提供的 setExtensionProperty 或 setExtensionPropertyWithVendor 方法,传入指定的 key
和 value
参数,你可以快速集成商汤的美颜能力。
以 setExtensionProperty
为例,其中的 key
参数与商汤的 API 名称完全对应,value
参数以 JSON 格式包装该 API 的部分或全部参数。因此,调用 setExtensionProperty
时只要传入指定的 key
和 value
,就可以调用对应的商汤 API,实现美颜的有关功能。setExtensionPropertyWithVendor
同理。
在 GitHub 上查看完整的示例代码:
平台 | 语言 | 示例项目 |
---|---|---|
Android | Java | SenseTime/android |
iOS | Objective-C | SenseTime/ios |
参考以下步骤快速跑通示例项目:
Android
克隆仓库:
git clone https://github.com/AgoraIO-Community/AgoraMarketPlace.git
参考仓库文件夹中的 SenseTime/android/README.md
文件完成后续步骤。
iOS
克隆仓库:
git clone https://github.com/AgoraIO-Community/AgoraMarketPlace.git
参考仓库文件夹中的 SenseTime/ios/README.md
文件完成后续步骤。
商汤美颜插件需要与声网视频 SDK v4.x 搭配使用。参考以下文档集成视频 SDK v4.x 并实现基础的视频通话:
Android
进入声网控制台 > 云市场页面下载商汤高级美颜/特效系列的 Android 插件(.aar
),保存到项目文件夹的 /app/libs
路径。
点击下载商汤美颜插件的资源包,将 license 文件以及所需的模型文件和 bundle 文件保存到项目文件夹的 /app/src/main/assets
路径。
打开 app/build.gradle
文件,在 dependencies
中添加如下行:
implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
导入需要用到的类:
import com.sensetime.stmobile.STMobileEffectNative;
import com.sensetime.stmobile.STMobileHumanActionNative;
import io.agora.rtc2.Constants;
import io.agora.rtc2.IMediaExtensionObserver;
import io.agora.rtc2.IRtcEngineEventHandler;
import io.agora.rtc2.RtcEngine;
import io.agora.rtc2.RtcEngineConfig;
import io.agora.rtc2.video.VideoCanvas;
import io.agora.rte.extension.sensetime.ExtensionManager;
iOS
Resource
文件夹,位置如下:.
├── <ProjectName>
├── <ProjectName>.xcodeproj
└── Resource
.framework
),保存到 <ProjectName>
路径下。Resource
路径下。 #import <AgoraRtcKit/AgoraRtcEngineKit.h>
#import <AgoraSenseTimeExtension/st_mobile_human_action.h>
#import <AgoraSenseTimeExtension/st_mobile_effect.h>
下面介绍插件相关接口的调用流程。接口的参数解释详见接口说明。
Android
初始化 RtcEngine
时,调用 enableExtension
启用插件。
RtcEngineConfig config = new RtcEngineConfig();
// 监听插件事件,用于接收 onEvent 回调
config.mExtensionObserver = this;
config.addExtension("AgoraSenseTimeExtension");
mRtcEngine = RtcEngine.create(config);
// 开启插件
mRtcEngine.enableExtension("SenseTime", "Effect", enabled);
iOS
初始化 AgoraRtcEngineKit
时,调用 enableExtensionWithVendor
启用插件。
[self.agoraKit enableExtensionWithVendor:@"SenseTime"
extension:@"Effect"
enabled:self.enable];
初始化插件需要调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS)并传入对应的 key 和 value,具体顺序如下:
st_mobile_check_activecode
,value 为 license 文件的路径。st_mobile_human_action_create
,value 包含模型文件的路径和配置选项。st_mobile_effect_create_handle
,value 为空,传入 {}
即可。Android
private void initExtension() {
// 获取 license 路径
File licensePath = new File(getExternalFilesDir(null), "license/" + io.agora.rte.extension.sensetime.example.Constants.mLicenseName);
// 检查激活码
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("license_path", licensePath.getPath());
setExtensionProperty("st_mobile_check_activecode", jsonObject.toString());
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
// 创建人体行为检测句柄
try {
File modelsPath = new File(getExternalFilesDir(null), "models/M_SenseME_Face_Extra_Advanced_6.0.13.model");
JSONObject jsonObject = new JSONObject();
jsonObject.put("model_path", modelsPath.getPath());
jsonObject.put("config", STMobileHumanActionNative.ST_MOBILE_HUMAN_ACTION_DEFAULT_CONFIG_IMAGE);
setExtensionProperty("st_mobile_human_action_create", jsonObject.toString());
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
// 创建特效句柄
setExtensionProperty("st_mobile_effect_create_handle", "{}");
}
// 方便多次调用 setExtensionProperty
private void setExtensionProperty(String key, String property) {
mRtcEngine.setExtensionProperty("SenseTime", "Effect", key, property);
}
iOS
- (IBAction)initExtension:(id)sender {
{
NSString* fileName = [[license_name lastPathComponent] stringByDeletingPathExtension];
NSString* extension = [license_name pathExtension];
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
@"license_path": [[NSBundle mainBundle]
pathForResource:fileName
ofType:extension]
}
options:NSJSONWritingPrettyPrinted
error:&error];
// 检查激活码
[self.agoraKit
setExtensionPropertyWithVendor:@"SenseTime"
extension:@"Effect"
key:@"st_mobile_check_activecode"
value:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}
{
NSString *model_path = [[[NSBundle mainBundle]
pathForResource:@"model"
ofType:@"bundle"] stringByAppendingFormat:@"/%@", @"M_SenseME_Face_Extra_Advanced_6.0.13.model"];
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
@"model_path": model_path,
@"config": @(ST_MOBILE_HUMAN_ACTION_DEFAULT_CONFIG_IMAGE)
}
options:NSJSONWritingPrettyPrinted
error:&error];
// 创建人体行为检测句柄
[self.agoraKit
setExtensionPropertyWithVendor:@"SenseTime"
extension:@"Effect"
key:@"st_mobile_human_action_create"
value:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}
{
// 创建特效句柄
[self.agoraKit
setExtensionPropertyWithVendor:@"SenseTime"
extension:@"Effect"
key:@"st_mobile_effect_create_handle"
value:@"{}"];
}
}
设置美颜效果需要调用 setExtensionProperty
(Android)或 setExtensionPropertyWithVendor
(iOS)并传入对应的 key 和 value,目前支持以下功能:
st_mobile_effect_set_beauty_strength
,value 包含美颜类型和美颜强度。st_mobile_effect_set_beauty
,value 包含美颜类型和素材文件路径。Android
// 设置美颜的强度
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("param", STEffectBeautyType.EFFECT_BEAUTY_PLASTIC_SHRINK_GODDESS_FACE);
jsonObject.put("val", Double.valueOf(0.8).doubleValue());
setExtensionProperty("st_mobile_effect_set_beauty_strength", jsonObject.toString());
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
// 方便多次调用 setExtensionProperty
private void setExtensionProperty(String key, String property) {
mRtcEngine.setExtensionProperty("SenseTime", "Effect", key, property);
}
iOS
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
@"param": @(EFFECT_BEAUTY_PLASTIC_SHRINK_GODDESS_FACE),
@"val": @0.8f
}
options:NSJSONWritingPrettyPrinted
error:&error];
[self.agoraKit
setExtensionPropertyWithVendor:@"SenseTime"
extension:@"Effect"
key:@"st_mobile_effect_set_beauty_strength"
value:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
插件相关所有接口的参数解释详见接口说明。
本文只实现了最基础的美颜功能。更多的接口调用方式请参考商汤 SenseAR Effects SDK 的文档(位于商汤美颜插件的资源包内)。