Use Python to make animation exclusive avatars ~ learn to no longer have to endure the embarrassment of avatar crash.

Posted by sixdollarshirt on Wed, 05 Jan 2022 05:51:11 +0100

Introduction: Hello, Hello, do you like the iron juice duck with cartoon series as my avatar!!

 

  

 

  

 

I really like this (the original picture can also be a private letter to me, remember to pay attention to roar) but sometimes the avatar hasn't been changed for a long time 🤦‍♀️ You'll find a crash 😳 Alas, everyone seems to use the same Du Niang!

Who won't search for pictures, but I will!! Afraid to hit the head? Then I'll make my own animation avatar in Python~

Next, follow Xiaobian to learn how to use Python to generate a unique and precious avatar for yourself. Let your friends envy you! 👇👇

To get more Python senior information and complete source code, click this line

Display effect:

One click Animation:

You must think, what is so hard about this? Tiktok is a key to achieve. But how can we succumb to this in technical work? Of course, we should explore the root and pursue traceability. Although as a technology, Xiaobai can't develop such a great one 👍 But Baidu has, we can still learn it first. Let's start now!

1, Principle analysis

Here, we launch the website of Baidu AI open platform about portrait animation special effects: http://suo.im/64FNvD .

Here we can upload our own pictures and carry out the operation of portrait animation.

Careful partners will find an important thing on the right side of the above page: the software operator is like an animation interface. This is a Post request. The URL of the request is not complete. You need to provide your own access_token. At the same time, sending a Post request requires not only Headers, but also a Params parameter, in which Headers is fixed and the image parameter is the Base64 encoding format of the picture.

Here, launch the API document website( http://suo.im/64FNZ9 ), can help us how to write code.

As can be seen from the figure below, the API document not only contains the operation of human animation painting, but also a series of operations such as black-and-white image coloring, image style change, sky segmentation and so on.

In this way, there are two steps to realize animation in technology:

  • Get access_token parameter

  • Send post request

access_token parameter acquisition

Get access_ For the token parameter, you need to use Baidu's authentication mechanism. The following is the website of the authentication mechanism. On this page, we introduce in detail how to obtain our own access_token parameter.

Authentication mechanism website: http://suo.im/6rUoTr .

​
1https://aip. baidubce. com/oauth/2.0/token? grant_ type=client_ credentials&client_ Id = [AK obtained on the official website] & client_secret = [SK obtained on the official website]

​

It can be seen that in order to send a Post request, we need to find our own API Key and Secret Key. Let's continue 👇

Acquisition of API Key and Secret Key

First, landing on Baidu intelligent cloud. https://login.bce.baidu.com/ ), this website needs us to scan the code to log in. We can log in according to the prompt.

Next, enter face recognition in the following order:

Click public cloud API → application list → create application:

Note: you can see that there are already applications I have created here (if it is the first time to create an application, click create application directly). Here we can find the API Key and Secret Key we want.

The following shows how to create an application. First, you need to give a name (you can remember it here), and then select portrait animation in image enhancement and special effects, as follows:

After successful creation, you can directly view the application list. The final page is as follows.

Click the expansion on the right side of the API list to find a small surprise. There are many interesting contents that you can ponder. Even we can check the usage times of an API. After the effective usage times are used, you need to pay for it. The figure below also shows that the operation of portrait animation is about 500 times of free use.

2, Code display

Now, let's get to the point! The intimate Baidu has shown you some codes. Let's continue to learn:

​
 1import requests
 2import base64
 3
 4# get_ access_ The token () function is used to obtain access_token parameter
 5def get_access_token():
 6    url = 'https://aip.baidubce.com/oauth/2.0/token'
 7    data = {
 8        'grant_type': 'client_credentials',  #Fixed value
 9        # client_id , is the APIKey and client obtained on the official website_ Secret: the secret key obtained on the official website
10        'client_id': 'gIMB...PtqR0D11fz',  #The API} Key of the application created after registration on the open platform is private and will not be displayed
11        'client_secret': '782...vGRKlmwS'  #The Secret Key of the created application is the same as above
12    }
13    res = requests.post(url, data=data)
14    res = res.json()
15    access_token = res['access_token']
16    return access_token
17
18
19#The following code is the code displayed in Baidu API documents. You can move it directly to use it
20request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
21f = open('D:/yiyi.jpg', 'rb')      #Open picture file in binary mode
22img = base64.b64encode(f.read())   #Here is to convert the image to base64 format, which is required in Baidu API documents
23
24#Simple character animation, no masks
25params = {"image":img}
26
27#Call the function to get} access_token 
28access_token = get_access_token()
29request_url = request_url + "?access_token=" + get_access_token()
30headers = {'content-type': 'application/x-www-form-urlencoded'}
31response = requests.post(request_url, data=params, headers=headers)
32res = response.json()
33
34#Write this image information to get the final effect picture.
35if response:
36    f = open("Animation drawing.jpg", 'wb')
37    after_img = res['image']
38    after_img = base64.b64decode(after_img)
39    f.write(after_img)
40    f.close()

​

Careful partners will find that the above code is only set to ordinary animation. In addition, in params parameter setting, if the parameter type is set to "anime_mask" and "mask_id", a two-dimensional animation diagram with mask can be generated.

The code snippet is shown below:

​
1#The character image is wearing a mask. Set the code params above here to the following style
2params = {
3    "image":img,
4    "type":"anime_mask",
5    "mask_id":"5"            #The id range is 1-8
6}

​

Draw! The following figure shows the mask types with id 1-8 in turn, which is really interesting!

Dear friends, there are so many mask s in the figure above. Which type do you pick?

end

The above is the whole content of this issue ~ move your finger and your wechat avatar can be changed! Like iron juice, remember the third consecutive Oh ~ the support of family members is the biggest driving force for Xiaobian to update 💪💪

Topics: Python Back-end Programmer