Best Practices | Use Tencent Cloud AI Portrait Transformation to give yourself a “experience that spans ages”

In the Internet era, rapid technological changes have made the public’s entertainment methods more diversified. More and more online entertainment experiences have become a new trend. Many companies have come up with unique ideas and launched AI portrait special effects, VR tourist attractions, interactive film and television dramas, metaverse, etc.; people are spending more and more time online entertainment experiences. , and are increasingly willing to pay for products and services related to pan-entertainment experience.

Taking AI portrait special effects as an example, the reality is that most novel gameplay methods are faced with difficulties, either because creative companies lack development capabilities, or because small and medium-sized enterprises lack easy-to-use, cost-effective AI products and services. So, is there a smarter and simplified way to empower companies and developers to create pan-entertainment products and services?

By researching the existing capabilities on the market, we found that the portrait transformation launched by Tencent Cloud AI is very interesting. You can choose from API, SDK and other access methods; it includes age transformation, gender transformation, portrait gradient, animation and other capabilities. Each Basic abilities can be used individually or in combination.

Next, I will try to use the age transformation and portrait gradient capabilities to create an "age gradient" visual experience. This type of gameplay can be used in some promotional advertisements suitable for all ages, or special effects experiences for personal growth.

1. Preparation

Before trying a cloud service, you should make some preparations, such as understanding whether the product's effect is satisfactory, whether the cost is affordable, and whether the service needs to be activated.

1.1 Experience effect

In order to decide whether to use this service's capabilities, I wanted to experience the effect. I took a look at the demo experience function on the product homepage, which is quite detailed and supports various parameter adjustments and drag-and-drop real-time effect display.

Demo experience website: Portrait transformation_Facial age transformation_Facial gender transformation_Facial feature editing-Tencent Cloud

 

1.2 Understand billing methods

Before the official trial, I also want to understand his billing rules. If the charge is very expensive, you should be careful when trying it out, but fortunately, the official website has clear purchase and billing methods , and there is a description of the free quota , so it seems that you can use it with confidence.

 

I have also studied the billing model here, and the general meaning is:

(1) All products under Portrait Transformation will have 1,000 free calls per month.

(2) Prepaid calling method, purchase resource packages of different sizes in advance, and they will be deducted when calling. Occasionally there will be promotions, and the resource package is valid for one year. If you can't use it up this month, you can use it next month.

(3) Post-paid calling method. If the resource package is not purchased or the resource package is consumed, it will be billed according to the post-paid rules.

(4) The order of deduction is  free resource package -> prepaid resource package -> postpaid.

 

1.3 Activate portrait transformation service

To officially use the portrait transformation service, it seems that you need to activate the existing service, which is similar to other Tencent Cloud products. Click to activate.

 

1.4 Obtain API call key

In access management , you can obtain the SecretId and SecretKey of the current account to facilitate subsequent verification of cloud service calls. This must be kept well, as there is a risk of it being stolen if it is leaked. Secrets that have been abandoned or are at risk of exposure can also be disabled to minimize the risk.

 

2. API call

2.1 View interface documentation

You can see the parameter descriptions of each interface in the API document options column. The following uses face age transformation and portrait transformation as examples.

2.1.1 Face age transformation

The interface document for face age transformation has two parts: input parameters and output parameters. The input parameters are the content we want to pass in, usually pictures. The output parameter is the return of the interface, usually the result image.

The input parameters also include public parameters, which are generally used for signatures and generally do not need to be included in the request package body.

Remember to print the RequestId in the log. If there are service-related issues later, you can provide this information to locate them.

 

2.1.2 Portrait gradient

The interface for portrait gradient is different from the synchronous interface such as age change. It is an asynchronous processing method.

(1) Portrait gradient : Create a portrait gradient processing task, in which processing parameters can be specified;

(2) Query the portrait gradient task : Query the results of the task processing, which will be used to poll the results later to see if they have been processed;

(3) Cancel the portrait gradient task : Cancel the task.

JobId is the key to identifying the task status. This variable must be recorded in the entire program pipeline.

2.2 Automatically generate code

Most Tencent Cloud interfaces support a tool called API Explorer , which can automatically generate code on it and is also equipped with a signature debugging tool.

Code generation: SDK calling codes in different languages ​​are more practical. Basically, you can just paste them and modify them.

Online call: simulate a real call, you can specify all parameters, and test some parameters that are not supported by the demo page.

Signature string generation: If you plan to splice the API signature yourself to call it, you can compare your signature result with the one here. However, the overall API signature is relatively complex and it is not recommended to use this method.

2.3 Debugging and verification

Taking the python language as an example, you can basically copy the above code and run it. The following takes online SDK calling as an example.

2.3.1 Download SDK

From the official website documentation , you can see that there are many different language versions of the SDK. I chose python here to develop the experience effect.

Click on the git project README, which has more detailed installation methods for the corresponding language.

2.3.2 Paste the generated code and modify it slightly

import json
import base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ft.v20200304 import ft_client, models

# 读一个本地测试文件转换为base64编码
f = open('./test.png', 'rb')
base64_data = base64.b64encode(f.read())

try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
    # 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
    cred = credential.Credential("YourSecretId", "YourSecretKey")	# 传入自己的秘钥
    # 实例化一个http选项,可选的,没有特殊需求可以跳过
    httpProfile = HttpProfile()
    httpProfile.endpoint = "ft.tencentcloudapi.com"

    # 实例化一个client选项,可选的,没有特殊需求可以跳过
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    # 实例化要请求产品的client对象,clientProfile是可选的
    client = ft_client.FtClient(cred, "ap-guangzhou", clientProfile)

    # 实例化一个请求对象,每个接口都会对应一个request对象
    req = models.ChangeAgePicRequest()
    params = {
        "Image": base64_data.decode("utf-8"),   # python里的bytes类型要转成string才可使用
        "AgeInfos": [
            {
                "Age": 40	# 可以测试不同的年龄看效果
            }
        ],
        "RspImgType": "url"
    }
    req.from_json_string(json.dumps(params))

    # 返回的resp是一个ChangeAgePicResponse的实例,与请求对象对应
    resp = client.ChangeAgePic(req)
    # 输出json格式的字符串回包
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(err)

You can see that the effect is quite good.

 

3. Gameplay upgrade

3.1 Combined age and gradient changes

After learning about it, there are many other interesting abilities under the portrait transformation product. I was thinking that it would be very interesting if these abilities could be combined. For example, face age transformation + portrait gradient should be able to create an age gradient effect.

3.1.1 Wrap cloud service interface calls in functions

Because python is a scripting language, first change the service to the form of a function to facilitate subsequent combination calls.

# 变年龄
def change_age(client, img, age):
    req = models.ChangeAgePicRequest()
    params = {
        "Image": img.decode("utf-8"),   # python里的bytes类型要转成string才可使用
        "AgeInfos": [
            {
                "Age": int(age)
            }
        ],
        "RspImgType": "base64"
    }
    req.from_json_string(json.dumps(params))
    resp = client.ChangeAgePic(req)
    return resp

In the same way, other functions can also be included.

3.1.2 Use of asynchronous class interface

Because portrait gradient is an asynchronous class interface, that is, the result cannot be returned immediately when called. Generally, each call is distinguished by some identification ID. When we want to check the results later, we can use this identification ID to track the results. The polling method is often used , that is, after we obtain the JobId of the overall process, after a fixed period of time, such as 2s, we call the query result interface to see if the results have been processed and then collect them. If not, the cycle continues.

 

3.1.3 Final code

Combine calls to Portrait Gradient and Age Transform.

import json
import sys
import time
import base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ft.v20200304 import ft_client, models

# 读一个本地测试文件转换为base64编码
def read_file(filename):
    f = open(filename, 'rb')
    base64_data = base64.b64encode(f.read())
    return base64_data

# 变年龄
def change_age(client, img, age):
    req = models.ChangeAgePicRequest()
    params = {
        "Image": img.decode("utf-8"),   # python里的bytes类型要转成string才可使用
        "AgeInfos": [
            {
                "Age": int(age)
            }
        ],
        "RspImgType": "base64"
    }
    req.from_json_string(json.dumps(params))
    resp = client.ChangeAgePic(req)
    return resp

# 人像渐变
def morph_face(client, imgs):
    req = models.MorphFaceRequest()
    params = {
        "Images": imgs,
        # 更多细节可通过参数调节
        # "GradientInfos": [
        #     {
        #         "Tempo": 1,
        #         "MorphTime": 1
        #     }
        # ],
        "Fps": 25
    }
    req.from_json_string(json.dumps(params))
    resp = client.MorphFace(req)
    print(resp.to_json_string())
    return resp

# 查询人像渐变结果
def query_morph_face(client, job_id):
    req = models.QueryFaceMorphJobRequest()
    params = {
        "JobId": job_id
    }
    req.from_json_string(json.dumps(params))
    resp = client.QueryFaceMorphJob(req)
    print(resp.to_json_string())
    return resp


try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
    # 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
    cred = credential.Credential("YourSecretId", "YourSecretKey")
    # 实例化一个http选项,可选的,没有特殊需求可以跳过
    httpProfile = HttpProfile()
    httpProfile.endpoint = "ft.tencentcloudapi.com"

    # 实例化一个client选项,可选的,没有特殊需求可以跳过
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    # 实例化要请求产品的client对象,clientProfile是可选的
    client = ft_client.FtClient(cred, "ap-guangzhou", clientProfile)

    # 读取图片
    data = read_file(sys.argv[1])

    ages = [20, 30, 40, 50, 60]

    # 批量变年龄
    imgs = []
    for age in ages:
        resp=change_age(client, data, age)
        imgs.append(resp.ResultImage)

	# 将图片集合起来调人像渐变
    resp = morph_face(client, imgs)
    job_id = resp.JobId

    # 轮询查结果
    resp=query_morph_face(client, job_id)
    while resp.JobStatusCode != 7:
        resp=query_morph_face(client, job_id)
        time.sleep(2)
   

except TencentCloudSDKException as err:
    print(err)

3.2 Parameter optimization effect

Both age changes and gradients support a certain degree of algorithm effect adjustment. I also choose the best combination after trying different effects.

 

 

4. Check the calling status

4.1 Check the amount of interface calls

You can see the corresponding call status on the console page . This call volume includes all calls including prepaid resource packages and postpaid resources.

 

4.2 Check the resource package deduction status

You can see the deduction of prepaid resource packages on the resource package management page . Since I didn’t spend any money on resource packs, there are currently only free resource packs. But it is basically enough for testing effects.

Learn more about Tencent Cloud AI portrait changing capabilities: Portrait transformation_Facial age transformation_Facial gender transformation_Facial feature editing-Tencent Cloud

Guess you like

Origin blog.csdn.net/tencentAI/article/details/127879799