The avatar always hits the face, how to customize the avatar? A few lines of Python code to achieve portrait animation

foreword

How much does it cost to change a WeChat avatar? When ordinary people hear this question, they may simply replace it with a photo they like, and the whole process does not cost a cent.

But if your friend recently changed to a rare avatar, it may have cost a fortune. This is the NFT avatar that has been popular all over the Internet recently . Such a unique NFT avatar is known as a symbol of social status in the crypto world.

Just like Sun Yuchen, the founder of TRON, who claims to be himself, spent $10.5 million to take an NFT cyberpunk avatar, and after the event, he even expressed his emotion in the circle of friends: " When everyone doesn't quite understand why a picture is so expensive At times, the question that should be answered is, why is a picture so expensive to take?

It's like we don't have so many w, how can we generate a unique avatar by ourselves?

Think of a good way, that is to use your own real-life photos to realize the animation of characters through the Alibaba Cloud API , and generate a unique and valuable avatar for yourself, so that your friends can envy it! ! !

Display of results!

​​​​Hand-painted style

1. Preparation

1. Login to the website

Enter the Alibaba Cloud Visual Intelligence Open Platform and log in to the Alibaba Cloud platform to register.

2. Ability to open

Enter the Alibaba Cloud Visual Intelligence Open Platform to activate the character animation function.

3. Create AccessKey

  1. Log in to the Visual Intelligence Open Platform Console .
  2. Hover the mouse to the user avatar in the upper right corner and click AccessKey Management. Create AccessKey​​
  3. Click Continue with AccessKey in the Security Prompt dialog box.
  4. On the Security Information Management page, click Create AccessKey in the upper left corner.
  5. In the Phone Verification dialog box, click Click Get.
  6. Enter the verification code and click OK.

Enter the AccessKey Management Center and get

​​

2. Call Alibaba Cloud API through Python

1. Environmental requirements

  • Python 3
  • Install the SDK core library OpenAPI and use pip to install package dependencies:
pip install alibabacloud_tea_openapi

2. Installation method

pip install alibabacloud_facebody20191230==2.0.15

3. Use SDK to generate URL

Since the file URL needs to be passed in when using the Alibaba Cloud Visual Intelligence Open Platform service, and the Visual Intelligence Platform intelligently processes the file according to the incoming file URL, we can use the Python SDK to generate the URL.

Install the library:

pip install oss2
pip install aliyun-python-sdk-viapiutils
pip install aliyun-python-sdk-core
pip install viapi-utils

The generated code is as follows:

from viapi.fileutils import FileUtils
file_utils = FileUtils("your own accessKey","your own accessSecret")
oss_url = file_utils.get_oss_url("5.jpg","jpg",True)
print(oss_url)

4. Step introduction

4.1 Initialize the configuration object alibabacloud_tea_openapi.Config

The Config object stores configuration such as access_key_id, access_key_secret, and endpoint. The endpoint is facebody.cn-shanghai.aliyuncs.com in the example.

from alibabacloud_tea_openapi import models as open_api_models

config = open_api_models.Config(
    # 您的AccessKey ID,
    access_key_id=access_key_id,
    # 您的AccessKey Secret,
    access_key_secret=access_key_secret
)
# 访问的域名
config.endpoint = 'facebody.cn-shanghai.aliyuncs.com'

4.2 Instantiate a client

Generate the object client from the alibabacloud_facebody20191230.Client class. Subsequent requests and responses are obtained from alibabacloud_facebody20191230.models.

from alibabacloud_facebody20191230.client import Client as Client
from alibabacloud_facebody20191230 import models as models

client = Client(config)

 4.3 Create a Request corresponding to the API

The method naming convention is Create followed by the API method name followed by Request. E.g:

request  = models.GenerateHumanAnimeStyleRequest()

4.4 Set the parameters of the request class request

Set the parameters by setting the properties of the request class, that is, the information that must be provided in the API. E.g:

request.image_url='http://img.jpg'
request.algo_type='anime'

 4.5 Obtain the corresponding request response response through the client object

response = client.generate_human_anime_style(request)

4.6 Call the corresponding property in the response to get the returned parameter value

Suppose you need to get requestId:

url=response.body.data.image_url

3. Test effect

Finally, let's look at the hand-painted wind effect generated from real photos.

Miss sister life photo
Anime style
hand-painted wind

The detailed production steps have also been made into a video, you can learn and learn directly through the video below! ! !

The avatar always hits the face? Write an exclusive avatar in python in 3 minutes [produced by Ayun]

The customized avatar is generated in this way. Do you think the hand-painted style is good-looking or the animation style is good-looking?

If the article is interesting, you can give me a like and let me know, I will continue to work hard to create more interesting things! ! !

  

Guess you like

Origin blog.csdn.net/kobepaul123/article/details/120317510