背景分割特效 技术文档
1.说明:
- 背景分割特效可以以道具包的形式使用,也可以以SDK内建特技的形式使用,但前提是都需要初始化**“人像背景分割模型”**,本文档以SDK内建特技的形式进行讲述。
- 背景分割特效既可以在**拍摄**中使用,也可以在**编辑**中使用,两者的应用方式不同,文档会详细说明
- CaptureScene只能在**拍摄**中使用
2.背景分割的使用
- 初始化人像背景分割模型。在拍摄中,相机为前置摄像头时可设置使用“半身分割模型”,是后置摄像头时可设置使用“全身分割模型”。在编辑中,设置使用“全身分割模型”。
String segPath = "assets:/facemode/ms/ms_humanseg_v1.0.7.model";
boolean segSuccess = NvsStreamingContext.initHumanDetectionExt(MSApplication.getContext(),
segPath, null, NvsStreamingContext.HUMAN_DETECTION_FEATURE_SEGMENTATION_BACKGROUND);
Logger.e(TAG, "ms segSuccess-->" + segSuccess);
String halfBodyPath = "assets:/facemode/ms/ms_halfbodyseg_v1.0.6.model";
boolean halfBodySuccess = NvsStreamingContext.initHumanDetectionExt(MSApplication.getContext(),
halfBodyPath, null, NvsStreamingContext.HUMAN_DETECTION_FEATURE_SEGMENTATION_HALF_BODY);
Logger.e(TAG, "ms halfBodySuccess-->" + halfBodySuccess);
背景分割是内建特效,创建如下:
拍摄:
NvsCaptureVideoFx mSegmentEffect = mStreamingContext.appendBuiltinCaptureVideoFx("Segmentation Background Fill");
if (mCurrentDeviceIndex == 0) {
mSegmentEffect.setMenuVal("Segment Type", "Background");
} else if (mCurrentDeviceIndex == 1) {
mSegmentEffect.setMenuVal("Segment Type", "Half Body");
}
mSegmentEffect.setIntVal("Stretch Mode", 1);
编辑:
NvsVideoFx mSegmentEffect = clip.appendRawBuiltinFx("Segmentation Background Fill");
mSegmentEffect.setMenuVal("Segment Type", "Background");
mSegmentEffect.setIntVal("Stretch Mode", 1);
设置背景颜色
mSegmentEffect.setColorVal("Background Color", new NvsColor(0.0f, 1.0f, 0.0f, 0.0f));
设置图片背景,目前支持设置图片
mSegmentEffect.setStringVal("Tex File Path", filePath);
移除分割特效
拍摄:
if (null != mSegmentEffect) {
mStreamingContext.removeCaptureVideoFx(mSegmentEffect.getIndex());
}
编辑:
if (null != mSegmentEffect) {
clip.removeRawFx(mSegmentEffect.getIndex());
}
>注:在编辑中应用背景分割特技后,需要在Seek、播放、导出添加对应的flag,即:NvsStreamingContext.STREAMING_ENGINE_XXX_FLAG_BUDDY_ORIGIN_VIDEO_FRAME
>注:该内建分割特技目前不支持设置视频背景,如果需要,则需要使用captureScene素材包
CaptureScene的使用。其背景既支持设置图片,也支持设置视频,使用如下:
安装captureScene素材包
String path = "assets:/capturescene/A2F05F58-87AB-4D1D-9609-6C00EF09E4D1.capturescene";
StringBuilder sb = new StringBuilder();
int inCode = mStreamingContext.getAssetPackageManager().installAssetPackage(path, null
, NvsAssetPackageManager.ASSET_PACKAGE_TYPE_CAPTURESCENE, true, sb);
capturesceneId = sb.toString();
创建背景分割特技(**“Segmentation Background Fill”**),同上创建(**必需**)
调用**applyCaptureScene**添加
NvsCaptureSceneInfo sceneInfo = new NvsCaptureSceneInfo();
List<NvsCaptureSceneInfo.ClipData> clipDataList = new ArrayList<>();
NvsCaptureSceneInfo.ClipData clipData = new NvsCaptureSceneInfo.ClipData();
clipData.scan = 1;
clipData.imageFillMode = NvsCaptureSceneInfo.CAPTURESCENE_INFO_IMAGE_FILLMODE_CROP;
clipData.mediaPath = filePath;
clipDataList.add(clipData);
sceneInfo.foregroundClipArray = clipDataList;
mStreamingContext.applyCaptureScene(mBgSegPackageId, sceneInfo);
移除当前拍摄场景
mStreamingContext.removeCurrentCaptureScene();