How to Animate a Picture by Leveraging the AI Algorithms

Have you ever wondered how to animate a still image? Wouldn’t it be fun to animate facial expressions in photos? This function adds original facial expressions to the image of faces by making use of artificial intelligence algorithms such as face detection, facial key point detection, facial expression feature extraction and facial expression animation. Fabulous stuff, isn’t it? Let’s move on and see how this ability can be incorporated.

Integration procedures

Preparations:

  • Configure app information in AppGallery Connect
  • HMS Core SDK Integration
  • Configure opacity scripts
  • add permission

For more details, we can check this document.

Configure a video editing project

1. Set the app authentication information

You can set the information through an API key or an access token.

Use the setAccessToken method to set an access token during initialization at application startup. The access token must only be set once.

MediaApplication.getInstance().setAccessToken("your access token");

Use the setApiKey method to set an API key during initialization at application startup. The API key must only be set once.

MediaApplication.getInstance().setApiKey("your ApiKey");

2. Set License ID

This identifier is used to manage your usage quotas, so make sure the identifier is unique.

MediaApplication.getInstance().setLicenseId("License ID");

2.1 Configuring the operating environment for Huaweivideo references

When creating a video editing project, first create a file HuaweiVideo Editor object and initialize its operating environment. When you exit a video editing project, edit a file HuaweiVideo Editor opposed.

construction HuaweiVideo Editor opposed.

HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());

Specify the position of the preview area.

This area displays video images, which are performed by creation surface width Its basic capability SDK. Make sure to locate the preview area on your app 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);

Configure the operating environment. If the license validation fails, a LicenseException is thrown.

after HuaweiVideo Editor The object was created, it did not occupy any system resource. You need to set the time manually to initialize the running environment of the object. After that, the necessary threads and timers will be created in the Basic Capacity SDK.

try {
        editor.initEnvironment();
   } catch (LicenseException error) { 
        SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());    
        finish();
        return;
   }

2.2 Add a video or photo

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 = vidoeLane.appendVideoAsset("test.mp4");

// Add an image to the end of the video lane.
HVEImageAsset imageAsset = vidoeLane.appendImageAsset("test.jpg");

2.3 Integration of motion picture capability

// Add the moving picture effect.
videoAsset.addFaceReenactAIEffect(new HVEAIProcessCallback() {
        @Override
        public void onProgress(int progress) {
        // Handling progress.
        }

        @Override
        public void onSuccess() {
        // Handling success.
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        // Handling failure.
        }
    });

// Remove the moving picture effect.
videoAsset.removeFaceReenactAIEffect();

.

Leave a Comment