Recently, "Your background is too fake" has swept the Internet. Because the scenery behind him was too beautiful, netizens questioned that the background was too fake. In order to prove himself, a certain anchor threw the bucket in his hand into the water behind him. In just a few days, billions of views have been played, triggering a frenzy of P-pictures on the entire network. Netizens cut out their portraits in the short video app and replaced them with different backgrounds. All kinds of creative videos quickly rushed to the hot search.
Nowadays, video portrait matting is widely used. In the live broadcast app, you can add various background effects, stickers, etc. to the portrait to provide a richer entertainment atmosphere; in the conference app, you can replace the background to protect user privacy; in the education app, you can replace the background with courseware and other content; The portrait cutout in the short video app can give users more room to play, and can easily shoot shocking blockbusters that travel through time and space without green screen shooting and post-production.
So, how does the App realize the one-click portrait extraction function? The HMS Core video editing service provides portrait and head segmentation capabilities , supports portrait or head segmentation for input videos or pictures, and generates segmented videos, pictures or stickers.
Development steps
1. Development preparation
For detailed preparation steps, please refer to the official website of Huawei Developer Alliance .
2. Edit project integration
2.1 Set the authentication information of the application
Application authentication information can be set through api_key or Access Token.
The Access Token is set through the setAccessToken method, which can be initialized and set once when the application starts, and there is no need to set it multiple times.
MediaApplication.getInstance().setAccessToken("your access token");
The api_key is set by the setApiKey method, which can be initialized and set once when the application starts, and there is no need to set it multiple times.
MediaApplication.getInstance().setApiKey("your ApiKey");
2.2 Set the unique identification ID, that is, the License ID.
The license ID is a valid certificate for management and control. You must ensure that the license ID is unique.
MediaApplication.getInstance().setLicenseId("License ID");
2.2.1 Initialize the Editor runtime environment
To create an editing project, you need to first create an Editor object and initialize its runtime environment. When leaving the editing project, the Editor instance should be released.
(1) Create Editor object
HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());
(2) Specify the layout position of the preview window
The preview window is responsible for the rendering of video images, which is realized by creating SurfaceView inside the video editing atomic capability SDK. Before creating the window, you need to specify the layout position of the preview window in your app.
<LinearLayout
android:id="@+id/video_content_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/video_edit_main_bg_color"
android:gravity="center"
android:orientation="vertical" />
// 指定预览窗口
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);
// 设置预览窗口承载的布局
editor.setDisplay(mSdkPreviewContainer);
(3) Initialize the running environment. If the license authentication fails, a LicenseException will be thrown.
After the Editor object is created, the actual system resources are not occupied at this time, and the timing of its environment initialization needs to be manually selected. At this time, the necessary threads and timers will be created inside the Video Editing Atomic Capability SDK.
try {
editor.initEnvironment();
} catch (LicenseException error) {
SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());
finish();
return;
}
3. Portrait and head segmentation capabilities integration
// 初始化分割AI引擎。segPart为int类型,值为1时,进行人像分割;值为其他时,进行头部分割
visibleAsset.initBodySegEngine(segPart, new HVEAIInitialCallback() {
@Override
public void onProgress(int progress) {
// 初始化进度
}
@Override
public void onSuccess() {
// 初始化成功
}
@Override
public void onError(int errorCode, String errorMessage) {
// 初始化失败
}
});
// 分割的处理结果成功后,添加分割AI特效
visibleAsset.addBodySegEffect(new HVEAIProcessCallback() {
@Override
public void onProgress(int progress) {
// 分割AI特效处理进度
}
@Override
public void onSuccess() {
// 分割AI特效处理成功
}
@Override
public void onError(int errorCode, String errorMsg) {
// 分割AI特效处理失败
}
});
// 中断分割AI特效处理
visibleAsset.interruptBodySegEffect();
// 移除分割AI特效
visibleAsset.removeBodySegEffect();
// 释放分割AI引擎
visibleAsset.releaseBodySegEngine();
Learn more details>>
Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee
Follow us to know the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。