Animation of Python's Avatar. Create your own

1, Foreword

Most of the time, we will worry about the head portrait. The salt fish like me, who hasn't changed the head portrait for thousands of years, has never had such a worry. However, it's very interesting to have a personalized head, because this nose picking head is in line with my temperament, so it's enough. But I'm still going to share this tip with you.

2, Effect display

After many tests, I found that the generation of girl's head portrait is quite good, and it's better to take a positive picture. But the generation of boy cartoon head is a little unsatisfactory. You can try it on your own. Let's take a look at the two pictures I generated with the village girl. Let's look at the first one:

Next is the second picture. I use the image of the bottom Spice Girls:

Both effects are OK, mainly anime the character's head, other parts just tend to anime style. But I think it's OK.

3, Implementation

For people like me who are not solid in basic skills, I can only call API to realize this. I use Baidu API, we can enter Baidu AI open platform I won't talk about the specific process of registering an account. After logging in, we can see the following interface:

We click "open ability" image technology "human animation in turn, and then we click" use now "to create an application. In the application, we can see three parameters: AppID, API Key and Secret Key. We can start our programming by remembering these three parameters.

There are two interfaces needed to realize the animation of portrait, one is to obtain access_token, the other is to obtain the animation portrait. Let's get access_token:

import requests
def get_access_token():
	# Get the API of token
	url = 'https://aip.baidubce.com/oauth/2.0/token'
	# Get the parameters required for access_token
	params = {
		# Fixed parameters
        'grant_type':'client_credentials',
        # Required parameter, pass in your API Key
        'client_id':'Your API Key',
        # Required parameter, pass in your Secret Key
        'client_secret':'Your Secret Key'
    }
    # Send request, get response data
    response = requests.post(url, params)
    # Convert the data of the response to the dictionary type, and then take out the access_token
    access_token = eval(response.text)['access_token']
    # Return access Ou token
    return access_token

Here we use the requests module. If there is no one installed, you can execute the following statements on the command line:

pip install requests

Next, our avatars are animated:

import base64
import requests
def img2Cartoon(img):
	# Anime API for avatars
    url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    # Read the original image as binary
    origin_im = open(img, 'rb')
    # base64 encoding pictures
    img = base64.b64encode(origin_im .read())
    # Close original picture
    origin_im.close()

	# Requested headers, fixed
    headers = {'content-type':'application/x-www-form-urlencoded'}
	
	# Requested parameters
    params = {
    	# access_token started to get
        'access_token':get_access_token(),
        # base64 encoding of pictures
        'image':img,
    }
    # Send request
    response = requests.post(url, data=params, headers=headers)
    # Process response results
    if response:
    	# Open a file
        f = open('result.jpg', 'wb')
        # Get cartoon Avatar
        anime = response.json()['image']
        # Decode the returned head image
        anime = base64.b64decode(anime)
        # Write the head image to the file
        f.write(anime)
        f.close()

We only need to call the img2Cartoon function in main to complete the creation of the animation head. The complete code is as follows:

import base64
import requests

def get_access_token():
	# Get the API of token
	url = 'https://aip.baidubce.com/oauth/2.0/token'
	# Get the parameters required for access_token
	params = {
		# Fixed parameters
        'grant_type':'client_credentials',
        # Required parameter, pass in your API Key
        'client_id':'Your API Key',
        # Required parameter, pass in your Secret Key
        'client_secret':'Your Secret Key'
    }
    # Send request, get response data
    response = requests.post(url, params)
    # Convert the data of the response to the dictionary type, and then take out the access_token
    access_token = eval(response.text)['access_token']
    # Return access Ou token
    return access_token

def img2Cartoon(img):
	# Anime API for avatars
    url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    # Read the original image as binary
    origin_im = open(img, 'rb')
    # base64 encoding pictures
    img = base64.b64encode(origin_im .read())
    # Close original picture
    origin_im.close()

	# Requested headers, fixed
    headers = {'content-type':'application/x-www-form-urlencoded'}
	
	# Requested parameters
    params = {
    	# access_token started to get
        'access_token':get_access_token(),
        # base64 encoding of pictures
        'image':img,
    }
    # Send request
    response = requests.post(url, data=params, headers=headers)
    # Process response results
    if response:
    	# Open a file
        f = open('result.jpg', 'wb')
        # Get cartoon Avatar
        anime = response.json()['image']
        # Decode the returned head image
        anime = base64.b64decode(anime)
        # Write the head image to the file
        f.write(anime)
        f.close()
        
if __name__ == '__main__':
    img2Cartoon('origin.jpg')

In this way, we have realized the animation operation of the avatar, which is very simple, because the main work has been done by Baidu programmers. You can use your own photos to generate some personalized avatars. If it's too ugly, don't run back and scold me.

41 original articles praised 5733 visitors 460000+
follow Private letter

Tags: encoding REST JSON Programming

Posted on Fri, 08 May 2020 20:47:34 -0400 by Tr4mpldUndrfooT