Template video face fusion

更新时间:
复制 MD 格式

This topic provides code examples for template video face fusion in common programming languages and scenarios.

Note
  • For real-time assistance, start an online consultation.

  • If you have questions about API access or usage for the Alibaba Cloud Vision AI Platform, join our DingTalk group (ID: 23109592) to contact us.

Overview

For an introduction to the template video face fusion feature and a description of its parameters, see Template video face fusion.

Install the SDK package

For information about the SDK package dependencies for common programming languages, see SDK overview.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.

  • Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.

  • Configure environment variables on Linux and macOS

    1. Open a terminal in IntelliJ IDEA.

    2. Run the following commands to configure the environment variables.

      Replace <access_key_id> with the AccessKey ID of your RAM user and <access_key_secret> with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.

      export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Configure environment variables on Windows

    Create a new environment variable file, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.

    1. Open File Explorer, right-click This PC, and then select Properties.

    2. In the left-side navigation pane, click Advanced system settings.

    3. On the Advanced tab of the System Properties dialog box, click Environment Variables.

    4. In the Environment Variables dialog box, click New.

    5. In the New System Variable dialog box, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.

    6. Restart the Windows operating system for the configurations to take effect.

Sample code

File in OSS (Shanghai)

For code examples on how to query asynchronous task results, see Query asynchronous task results.

/*
Add the dependency.
<!-- https://mvnrepository.com/artifact/com.aliyun/videoenhan20200320 -->
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>videoenhan20200320</artifactId>
  <version>${aliyun.videoenhan.version}</version>
</dependency>
*/

import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videoenhan20200320.models.MergeVideoModelFaceResponse;

public class MergeVideoModelFace {
    public static com.aliyun.videoenhan20200320.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          Initialize the configuration object com.aliyun.teaopenapi.models.Config.
          This object stores configuration parameters such as your AccessKey ID, AccessKey Secret, and endpoint.
         */
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Your AccessKey ID.
                .setAccessKeyId(accessKeyId)
                // Your AccessKey Secret.
                .setAccessKeySecret(accessKeySecret);
        // The endpoint. Note: You must change the endpoint to that of the corresponding service. For more information, see https://help.aliyun.com/document_detail/143103.html.
        config.endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
        return new com.aliyun.videoenhan20200320.Client(config);
    }

    public static void main(String[] args) throws Exception {
        // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
        // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
        // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); 
        com.aliyun.videoenhan20200320.Client client = MergeVideoModelFace.createClient(accessKeyId, accessKeySecret);
        com.aliyun.videoenhan20200320.models.MergeVideoModelFaceRequest mergeVideoModelFaceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoModelFaceRequest()
                .setFaceImageURL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png")
                .setTemplateId("26a42352-2991-4713-85dd-6d8f7cd59b58");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            MergeVideoModelFaceResponse mergeVideoModelFaceResponse = client.mergeVideoModelFaceWithOptions(mergeVideoModelFaceRequest, runtime);
            // Obtain the full response.
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(mergeVideoModelFaceResponse)));
            // Obtain a specific field.
            System.out.println(mergeVideoModelFaceResponse.getBody());
        } catch (TeaException teaException) {
            // Obtain the full error message.
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // Obtain a specific field.
            System.out.println(teaException.getCode());
        }
    }
}
# -*- coding: utf-8 -*-
# Install the dependency.
# pip install alibabacloud_videoenhan20200320

import os
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
from alibabacloud_videoenhan20200320.client import Client
from alibabacloud_videoenhan20200320.models import MergeVideoModelFaceRequest

config = Config(
  # To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  # If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  # Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
  access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
  access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
  # The endpoint.
  endpoint='videoenhan.cn-shanghai.aliyuncs.com',
  # The region ID that corresponds to the endpoint.
  region_id='cn-shanghai'
)
merge_video_model_face_request = MergeVideoModelFaceRequest(
   template_id='26a42352-2991-4713-85dd-6d8f7cd59b58',
   face_image_url='https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png'
)
runtime = RuntimeOptions()
try:
  # Initialize the client.
  client = Client(config)
  response = client.merge_video_model_face_with_options(merge_video_model_face_request, runtime)
  # Obtain the full response.
  print(response.body)
except Exception as error:
  # Obtain the full error message.
  print(error)
  # Obtain a specific field.
  print(error.code)
<?php

// Install the dependency.
// composer require alibabacloud/videoenhan-20200320

use AlibabaCloud\SDK\Videoenhan\V20200320\Videoenhan;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Videoenhan\V20200320\Models\MergeVideoModelFaceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class MergeVideoModelFace {

    /**
     * Use an AccessKey pair to initialize the client.
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Videoenhan Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        // Initialize the configuration object Darabonba\OpenApi\Models\Config.
        // This object stores configuration parameters such as your accessKeyId, accessKeySecret, and endpoint.
        $config = new Config([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret
        ]);
        // The endpoint.
        $config->endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
        return new Videoenhan($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
        // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
        // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
        $accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
        $accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'); 
        $client = self::createClient($accessKeyId, $accessKeySecret);
        $mergeVideoModelFaceRequest = new MergeVideoModelFaceRequest([
            "templateId" => "26a42352-2991-4713-85dd-6d8f7cd59b58",
            "faceImageURL" => "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png"
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $resp = $client->mergeVideoModelFaceWithOptions($mergeVideoModelFaceRequest, $runtime);
            # Obtain the full response.
            echo Utils::toJSONString($resp->body);
        } catch (Exception $exception) {
            # Obtain the full error message.
            echo Utils::toJSONString($exception);
            # Obtain a specific field.
            echo $exception->getCode();
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
// $argv is a reserved array for input parameters. It has no practical significance and does not need to be modified.
MergeVideoModelFace::main(array_slice($argv, 1));
// Install the dependency.
// npm install @alicloud/videoenhan20200320
const VideoenhanClient = require('@alicloud/videoenhan20200320');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');

let config = new OpenapiClient.Config({
  // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured. 
  accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,   
  accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The endpoint.
config.endpoint = `videoenhan.cn-shanghai.aliyuncs.com`;
const client = new VideoenhanClient.default(config);
let mergeVideoModelFaceRequest = new VideoenhanClient.MergeVideoModelFaceRequest({
  faceImageURL: "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png",
  templateId: "26a42352-2991-4713-85dd-6d8f7cd59b58",
});
let runtime = new TeaUtil.RuntimeOptions({});
client.mergeVideoModelFaceWithOptions(mergeVideoModelFaceRequest, runtime)
  .then(function (mergeVideoModelFaceResponse) {
    // Obtain the full response.
    console.log(mergeVideoModelFaceResponse);
    // Obtain a specific field.
    console.log(mergeVideoModelFaceResponse.body.data);
  }, function (error) {
    // Obtain the full error message.
    console.log(error);
    // Obtain a specific field.
    console.log(error.data.Code);
  })
/**
This sample code depends on github.com/alibabacloud-go/videoenhan-20200320/v3.
We recommend that you run `go mod tidy` to install the dependency.
*/

package main

import (
	"fmt"
	"os"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	videoenhan20200320 "github.com/alibabacloud-go/videoenhan-20200320/v3/client"
)

func main() {
  // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.  
  accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")  
  accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
  // Initialize the configuration object &openapi.Config. This object stores configuration parameters such as your AccessKey ID, AccessKey Secret, and endpoint.
  config := &openapi.Config{
    AccessKeyId: tea.String(accessKeyId),
    AccessKeySecret: tea.String(accessKeySecret)
	}
	// The endpoint.
	config.Endpoint = tea.String("videoenhan.cn-shanghai.aliyuncs.com")
	client, err := videoenhan20200320.NewClient(config)
	if err != nil {
		panic(err)
	}
	mergeVideoModelFaceRequest := &videoenhan20200320.MergeVideoModelFaceRequest{
		FaceImageURL: tea.String("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png"),
		TemplateId: tea.String("26a42352-2991-4713-85dd-6d8f7cd59b58"),
	}
	runtime := &util.RuntimeOptions{}
	mergeVideoModelFaceResponse, err := client.MergeVideoModelFaceWithOptions(mergeVideoModelFaceRequest, runtime)
	if err != nil {
		// Obtain the full error message.
		fmt.Println(err.Error())
	} else {
		// Obtain the full response.
		fmt.Println(mergeVideoModelFaceResponse)
	}
}
// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Videoenhan20200320
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Videoenhan20200320.Models;
using Tea;
using Tea.Utils;

namespace AlibabaCloud.SDK.Sample
{
    public class Sample
    {
        /**
            * Use an AccessKey pair to initialize the client.
            * @param accessKeyId
            * @param accessKeySecret
            * @return Client
            * @throws Exception
        */
        public static AlibabaCloud.SDK.Videoenhan20200320.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                AccessKeyId = accessKeyId,
                AccessKeySecret = accessKeySecret,
            };
            // The endpoint.
            config.Endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
            return new AlibabaCloud.SDK.Videoenhan20200320.Client(config);
        }
        public static void Main(string[] args)
        {
            // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
            // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
            // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
            AlibabaCloud.SDK.Videoenhan20200320.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            AlibabaCloud.SDK.Videoenhan20200320.Models.MergeVideoModelFaceRequest mergeVideoModelFaceRequest = new AlibabaCloud.SDK.Videoenhan20200320.Models.MergeVideoModelFaceRequest
            {
                FaceImageURL = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png",
                TemplateId = "26a42352-2991-4713-85dd-6d8f7cd59b58",
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                AlibabaCloud.SDK.Videoenhan20200320.Models.MergeVideoModelFaceResponse mergeVideoModelFaceResponse = client.MergeVideoModelFaceWithOptions(mergeVideoModelFaceRequest, runtime);
                // Obtain the full response.
                Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoModelFaceResponse.Body));
                // Obtain a specific field.
                Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoModelFaceResponse.Body.Data));
            }
            catch (TeaException error)
            {
                // Print the error message.
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // Print the error message.
                Console.WriteLine(error.Message);
            }
        }
    }
}

Local file or URL

For code examples on how to query asynchronous task results, see Query asynchronous task results.

/*
Add the dependency.
<!-- https://mvnrepository.com/artifact/com.aliyun/videoenhan20200320 -->
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>videoenhan20200320</artifactId>
  <version>${aliyun.videoenhan.version}</version>
</dependency>
*/

import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videoenhan20200320.models.MergeVideoModelFaceResponse;

import java.io.InputStream;
import java.net.URL;

public class MergeVideoModelFace {
    public static com.aliyun.videoenhan20200320.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          Initialize the configuration object com.aliyun.teaopenapi.models.Config.
          This object stores configuration parameters such as your AccessKey ID, AccessKey Secret, and endpoint.
         */
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Your AccessKey ID.
                .setAccessKeyId(accessKeyId)
                // Your AccessKey Secret.
                .setAccessKeySecret(accessKeySecret);
        // The endpoint. Note: You must change the endpoint to that of the corresponding service. For more information, see https://help.aliyun.com/document_detail/143103.html.
        config.endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
        return new com.aliyun.videoenhan20200320.Client(config);
    }

    public static void main(String[] args) throws Exception {
        // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
        // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
        // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); 
        com.aliyun.videoenhan20200320.Client client = MergeVideoModelFace.createClient(accessKeyId, accessKeySecret);
        // Scenario 1: Use a local file.
        // InputStream inputStream = new FileInputStream(new File("/tmp/MergeVideoModelFace-pic1.png"));
        // Scenario 2: Use a publicly accessible URL.
        URL url = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png");
        InputStream inputStream = url.openConnection().getInputStream();
        com.aliyun.videoenhan20200320.models.MergeVideoModelFaceAdvanceRequest mergeVideoModelFaceAdvanceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoModelFaceAdvanceRequest()
                .setFaceImageURLObject(inputStream)
                .setTemplateId("26a42352-2991-4713-85dd-6d8f7cd59b58");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            MergeVideoModelFaceResponse mergeVideoModelFaceAdvanceResponse = client.mergeVideoModelFaceAdvance(mergeVideoModelFaceAdvanceRequest, runtime);
            // Obtain the full response.
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(mergeVideoModelFaceAdvanceResponse)));
            // Obtain a specific field.
            System.out.println(mergeVideoModelFaceAdvanceResponse.getBody());
        } catch (TeaException teaException) {
            // Obtain the full error message.
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // Obtain a specific field.
            System.out.println(teaException.getCode());
        }
    }
}
# -*- coding: utf-8 -*-
# Install the dependency.
# pip install alibabacloud_videoenhan20200320

import os
import io
from urllib.request import urlopen
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
from alibabacloud_videoenhan20200320.client import Client
from alibabacloud_videoenhan20200320.models import MergeVideoModelFaceAdvanceRequest

config = Config(
  # To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  # If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  # Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
  access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
  access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
  # The endpoint.
  endpoint='videoenhan.cn-shanghai.aliyuncs.com',
  # The region ID that corresponds to the endpoint.
  region_id='cn-shanghai'
)
# Scenario 1: Use a local file.
#img = open(r'/tmp/MergeVideoModelFace-pic1.png', 'rb')
# Scenario 2: Use a publicly accessible URL.
url = 'https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png'
img = io.BytesIO(urlopen(url).read())
merge_video_model_face_request = MergeVideoModelFaceAdvanceRequest()
merge_video_model_face_request.template_id = '26a42352-2991-4713-85dd-6d8f7cd59b58'
merge_video_model_face_request.face_image_urlobject = img
runtime = RuntimeOptions()
try:
  # Initialize the client.
  client = Client(config)
  response = client.merge_video_model_face_advance(merge_video_model_face_request, runtime)
  # Obtain the full response.
  print(response.body)
except Exception as error:
  # Obtain the full error message.
  print(error)
  # Obtain a specific field.
  print(error.code)
<?php

// Install the dependency.
// composer require alibabacloud/videoenhan-20200320

use AlibabaCloud\SDK\Videoenhan\V20200320\Videoenhan;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Videoenhan\V20200320\Models\MergeVideoModelFaceAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;

class MergeVideoModelFaceAdvance {

    /**
     * Use an AccessKey pair to initialize the client.
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Videoenhan Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        // Initialize the configuration object Darabonba\OpenApi\Models\Config.
        // This object stores configuration parameters such as your accessKeyId, accessKeySecret, and endpoint.
        $config = new Config([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret
        ]);
        // The endpoint.
        $config->endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
        return new Videoenhan($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
        // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
        // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
        $accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
        $accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'); 
        $client = self::createClient($accessKeyId, $accessKeySecret);
        // Scenario 1: Use a local file.
        //$file = fopen('/tmp/MergeVideoModelFace-pic1.png', 'rb');
        //$stream = new Stream($file);
        // Scenario 2: Use a publicly accessible URL.
        $file = fopen('https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png', 'rb');
        $stream = new Stream($file);
        $mergeVideoModelFaceAdvanceRequest = new MergeVideoModelFaceAdvanceRequest([
            "templateId" => "26a42352-2991-4713-85dd-6d8f7cd59b58",
            "faceImageURLObject" => $stream
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $resp = $client->mergeVideoModelFaceAdvance($mergeVideoModelFaceAdvanceRequest, $runtime);
            # Obtain the full response.
            echo Utils::toJSONString($resp->body);
        } catch (Exception $exception) {
            # Obtain the full error message.
            echo Utils::toJSONString($exception);
            # Obtain a specific field.
            echo $exception->getCode();
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
// $argv is a reserved array for input parameters. It has no practical significance and does not need to be modified.
MergeVideoModelFaceAdvance::main(array_slice($argv, 1));
// Install the dependency.
// npm install @alicloud/videoenhan20200320
const VideoenhanClient = require('@alicloud/videoenhan20200320');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');

let config = new OpenapiClient.Config({
  // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured. 
  accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,   
  accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The endpoint.
config.endpoint = `videoenhan.cn-shanghai.aliyuncs.com`;
const client = new VideoenhanClient.default(config);
const getResponse = function (httpClient, url) {
  return new Promise((resolve, reject) => {
    httpClient.get(url, function (response) {
      resolve(response);
    })
  })
}
const request = async function () {
  try {
    let mergeVideoModelFaceAdvanceRequest = new VideoenhanClient.MergeVideoModelFaceAdvanceRequest();
    // Scenario 1: Use a local file.
    // const fileStream = fs.createReadStream('/tmp/MergeVideoFace-pic1.png');
    // mergeVideoModelFaceAdvanceRequest.faceImageURLObject = fileStream;
    // mergeVideoModelFaceAdvanceRequest.templateId = "743e544a-aca2-4411-bb8f-a13723c841cd";
    // Scenario 2: Use a publicly accessible URL.
    const url = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png");
    const httpClient = (url.protocol == "https:") ? https : http;
    mergeVideoModelFaceAdvanceRequest.faceImageURLObject = await getResponse(httpClient, url);
    mergeVideoModelFaceAdvanceRequest.templateId = "26a42352-2991-4713-85dd-6d8f7cd59b58";
    let runtime = new TeaUtil.RuntimeOptions({ });
    client.mergeVideoModelFaceAdvance(mergeVideoModelFaceAdvanceRequest, runtime)
      .then(function(mergeVideoModelFaceResponse) {
        // Obtain the full response.
        console.log(mergeVideoModelFaceResponse);
        // Obtain a specific field.
        console.log(mergeVideoModelFaceResponse.body.data);
      }, function(error) {
        // Obtain the full error message.
        console.log(error);
        // Obtain a specific field.
        console.log(error.data.Code);
      })
  } catch (error) {
    console.log(error);
  }
}();
/**
This sample code depends on github.com/alibabacloud-go/videoenhan-20200320/v3.
We recommend that you run `go mod tidy` to install the dependency.
*/

package main

import (
	"fmt"
	"os"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	videoenhan20200320 "github.com/alibabacloud-go/videoenhan-20200320/v3/client"
	"net/http"
)

func main() {
  // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
  // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
  // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.  
  accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")  
  accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
  // Initialize the configuration object &openapi.Config. This object stores configuration parameters such as your AccessKey ID, AccessKey Secret, and endpoint.
  config := &openapi.Config{
    AccessKeyId: tea.String(accessKeyId),
    AccessKeySecret: tea.String(accessKeySecret)
	}
	// The endpoint.
	config.Endpoint = tea.String("videoenhan.cn-shanghai.aliyuncs.com")
	client, err := videoenhan20200320.NewClient(config)
	if err != nil {
		panic(err)
	}
	// Scenario 1: Use a local file.
	//file, err := os.Open("/tmp/MergeVideoModelFace-pic1.png")
	//if err != nil {
	//	fmt.Println("can not open file", err)
	//	panic(err)
	//}
	//mergeVideoModelFaceAdvanceRequest := &videoenhan20200320.MergeVideoModelFaceAdvanceRequest{
	//	FaceImageURLObject: file,
	//	TemplateId:         tea.String("26a42352-2991-4713-85dd-6d8f7cd59b58"),
	//}
	// Scenario 2: Use a publicly accessible URL.
	httpClient := http.Client{}
	file, _ := httpClient.Get("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png")
	mergeVideoModelFaceAdvanceRequest := &videoenhan20200320.MergeVideoModelFaceAdvanceRequest{
		FaceImageURLObject: file.Body,
		TemplateId:         tea.String("26a42352-2991-4713-85dd-6d8f7cd59b58"),
	}
	runtime := &util.RuntimeOptions{}
	mergeVideoModelFaceAdvanceResponse, err := client.MergeVideoModelFaceAdvance(mergeVideoModelFaceAdvanceRequest, runtime)
	if err != nil {
		// Obtain the full error message.
		fmt.Println(err.Error())
	} else {
		// Obtain the full response.
		fmt.Println(mergeVideoModelFaceResponse)
	}
}
// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Videoenhan20200320
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Videoenhan20200320.Models;
using Tea;
using Tea.Utils;

namespace AlibabaCloud.SDK.Sample
{
    public class Sample
    {
        /**
            * Use an AccessKey pair to initialize the client.
            * @param accessKeyId
            * @param accessKeySecret
            * @return Client
            * @throws Exception
        */
        public static AlibabaCloud.SDK.Videoenhan20200320.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                AccessKeyId = accessKeyId,
                AccessKeySecret = accessKeySecret,
            };
            // The endpoint.
            config.Endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
            return new AlibabaCloud.SDK.Videoenhan20200320.Client(config);
        }
        public static void Main(string[] args)
        {
            // To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
            // If you are using a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
            // Load credentials from environment variables. Before you run the code, make sure that the environment variables are configured.
            AlibabaCloud.SDK.Videoenhan20200320.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            AlibabaCloud.SDK.Videoenhan20200320.Models.MergeVideoModelFaceAdvanceRequest mergeVideoModelFaceAdvanceRequest = new AlibabaCloud.SDK.Videoenhan20200320.Models.MergeVideoModelFaceAdvanceRequest
            ();
            // Scenario 1: Use a local file.
            // System.IO.StreamReader file = new System.IO.StreamReader(@"/tmp/MergeVideoFace-pic1.png");
            // mergeVideoModelFaceAdvanceRequest.FaceImageURLObject = file.BaseStream;
            // mergeVideoModelFaceAdvanceRequest.TemplateId = "743e544a-aca2-4411-bb8f-a13723c841cd";

            // Scenario 2: Use a publicly accessible URL.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoModelFace/MergeVideoModelFace-pic1.png");
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            mergeVideoModelFaceAdvanceRequest.FaceImageURLObject = stream;
            mergeVideoModelFaceAdvanceRequest.TemplateId = "26a42352-2991-4713-85dd-6d8f7cd59b58";
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                AlibabaCloud.SDK.Videoenhan20200320.Models.MergeVideoModelFaceResponse mergeVideoModelFaceResponse = client.MergeVideoModelFaceAdvance(mergeVideoModelFaceAdvanceRequest, runtime);
                // Obtain the full response.
                Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoModelFaceResponse.Body));
                // Obtain a specific field.
                Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoModelFaceResponse.Body.Data));
            }
            catch (TeaException error)
            {
                // Print the error message.
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // Print the error message.
                Console.WriteLine(error.Message);
            }
        }
    }
}