Implementing Hair Color Change in a Tap
Tags: machinelearning, ai, android, hms
Published: 2022-03-10 11:07:31

Users sometimes want to recolor people's hair in their videos. The color hair capability of HMS Core Video Editor Kit makes it possible for users to choose from a rich array of preset colors to recolor a person's hair, with just a simple tap. In this way, the capability helps make users' videos more interesting.
![Hair color change](https://forum.xda-developers.com/attachments/1-2-gif.5539799/)

## Function Overview​
- Processes an image or a video in real time.
- Supports hair color change for multiple people in an image or a video.
- Supports color strength adjustment.

## Integration Procedure​
### Preparations
For details, please check the [official document](https://developer.huawei.com/consumer/en/doc/development/Media-Guides/config-agc-0000001101108580?ha_source=hms1).
### Configuring a Video Editing Project
1.Set the app authentication information.
You can set the information through an API key or access token.Use the [setAccessToken](https://developer.huawei.com/consumer/en/doc/development/Media-References/mediaapplication-0000001149508209#section92907346177?ha_source=hms1) method to set an access token when the app is started. The access token needs to be set only once.
```
MediaApplication.getInstance().setAccessToken("your access token");
```
Use the [setApiKey](https://developer.huawei.com/consumer/en/doc/development/Media-References/mediaapplication-0000001149508209#section171221750121916?ha_source=hms1) method to set an API key when the app is started. The API key needs to be set only once.
```
MediaApplication.getInstance().setApiKey("your ApiKey");
```
2.Set a License ID.
This ID is used to manage your usage quotas, so ensure that the ID is unique.
```
MediaApplication.getInstance().setLicenseId("License ID");
```
3.Initialize the running environment for **HuaweiVideoEditor**.
When creating a video editing project, first create a **HuaweiVideoEditor** object and initialize its running environment. When exiting a video editing project, release the **HuaweiVideoEditor** object.
Create a **HuaweiVideoEditor** object.
```
HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());
```
Specify the position for the preview area.
This area renders video images, which is implemented by creating SurfaceView in the fundamental capability SDK. Ensure that the preview area position on your app is specified before creating this area.
```
<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" />
// Specify the preview area position.
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);

// Set the layout of the preview area.
editor.setDisplay(mSdkPreviewContainer);
```
Initialize the running environment. If the license verification fails, [LicenseException](https://developer.huawei.com/consumer/en/doc/development/Media-References/sdk-licenseexception-0000001164593066?ha_source=hms1) will be thrown.
After the **HuaweiVideoEditor** object is created, it has not occupied any system resource. You need to manually set the time for initializing the running environment of the object. Then, necessary threads and timers will be created in the fundamental capability SDK.
```
try {
        editor.initEnvironment();
   } catch (LicenseException error) {
        SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());   
        finish();
        return;
   }
```
4.Add a video or image.
Create a video lane and add a video or image to the lane using the file path.
```
// Obtain the HVETimeLine object.
HVETimeLine timeline = editor.getTimeLine();

// Create a video lane.
HVEVideoLane videoLane = timeline.appendVideoLane();

// Add a video to the end of the video lane.
HVEVideoAsset videoAsset = videoLane.appendVideoAsset("test.mp4");

// Add an image to the end of the video lane.
HVEImageAsset imageAsset = videoLane.appendImageAsset("test.jpg");
```
### Integrating the Color Hair Capability
```
// Initialize the AI algorithm for the color hair effect.
asset.initHairDyeingEngine(new HVEAIInitialCallback() {
        @Override
        public void onProgress(int progress) {
        // Initialization progress.
        }

        @Override
        public void onSuccess() {
        // The initialization is successful.
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        // The initialization failed.
    }
   });

// Add the color hair effect by specifying a color and the default strength.
asset.addHairDyeingEffect(new HVEAIProcessCallback() {
        @Override
        public void onProgress(int progress) {
            // Handling progress.
        }

        @Override
        public void onSuccess() {
            // The handling is successful.
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            // The handling failed.
        }
    }, colorPath, defaultStrength);

//Remove the color hair effect.
asset.removeHairDyeingEffect();
```
This article presents the hair dyeing capability of Video Editor Kit. For more, check [here](https://developer.huawei.com/consumer/en/hms/huawei-video-editor?ha_source=hms1).