current position:Home>Python avatar animation, come and generate your own animation avatar

Python avatar animation, come and generate your own animation avatar

2022-01-31 17:15:48 ZackSock

This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge

One 、 Preface

Many times we worry about avatars , A salted fish like me who hasn't changed his head for thousands of years has never had such trouble . But! , Having a personalized avatar is still very interesting , Because this nose picking head except nose picking , Others are in line with my temperament , So it's enough . But I'm going to share this tip with you .

Two 、 Effect display

After many tests , It is found that the generation of girls' avatars is quite good , And it's best to take a front view . The generation of boys' animation avatars is a little unsatisfactory , You can try it yourself , Now let's take a look at the two pictures I generated with sister Youcun , Let's look at the first one :  Insert picture description here Next is the second , I use the image of the bottom Spice Girl :  Insert picture description here Both effects are OK , The main thing is that the head of the character is animated , Other parts just tend to the style of animation . But I think it's ok .

3、 ... and 、 Realization

For people like me who don't have solid basic skills , This implementation can only call API 了 . I use Baidu's API, We can enter Baidu AI Open platform Registered account , I won't talk about the specific process . After logging in, we can see the following interface :  Insert picture description here Let's click on openness in turn 》 Image technology 》 Anime the portrait , Then we click use now , Then create an application . In the application, we can see three parameters AppIDAPI Key and Secret Key Three parameters , We can start our programming by remembering these three parameters .

Two interfaces are needed to realize portrait animation , One to get access_token, The other is used to get animation avatars , Let's get access_token:

import requests
def get_access_token():
	#  obtain token Of API
	url = 'https://aip.baidubce.com/oauth/2.0/token'
	#  obtain access_token Required parameters 
	params = {
		#  Fixed parameter 
        'grant_type':'client_credentials',
        #  Required parameters , Bring in your API Key
        'client_id':' Yours API Key',
        #  Required parameters , Bring in your Secret Key
        'client_secret':' Yours Secret Key'
    }
    #  Send a request , Get response data 
    response = requests.post(url, params)
    #  Convert the data of the response to dictionary type , Then take out. access_token
    access_token = eval(response.text)['access_token']
    #  take access_token return 
    return access_token
 Copy code 

Here we use requests modular , If there is no installed, you can execute the following statement on the command line :

pip install requests
 Copy code 

Next is the animation of our avatars :

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

	#  Requested headers Information , Fixed writing 
    headers = {'content-type':'application/x-www-form-urlencoded'}
	
	#  Requested parameters 
    params = {
    	#  Start getting access_token
        'access_token':get_access_token(),
        #  The image base64 code 
        'image':img,
    }
    #  Send a request 
    response = requests.post(url, data=params, headers=headers)
    #  Process response results 
    if response:
    	#  Open a file 
        f = open('result.jpg', 'wb')
        #  Get anime avatars 
        anime = response.json()['image']
        #  Decode the returned avatar 
        anime = base64.b64decode(anime)
        #  Write the avatar to the file 
        f.write(anime)
        f.close()
 Copy code 

All we need to do is main Call in img2Cartoon Function to complete the generation of animation avatars , The complete code is as follows :

import base64
import requests

def get_access_token():
	#  obtain token Of API
	url = 'https://aip.baidubce.com/oauth/2.0/token'
	#  obtain access_token Required parameters 
	params = {
		#  Fixed parameter 
        'grant_type':'client_credentials',
        #  Required parameters , Bring in your API Key
        'client_id':' Yours API Key',
        #  Required parameters , Bring in your Secret Key
        'client_secret':' Yours Secret Key'
    }
    #  Send a request , Get response data 
    response = requests.post(url, params)
    #  Convert the data of the response to dictionary type , Then take out. access_token
    access_token = eval(response.text)['access_token']
    #  take access_token return 
    return access_token

def img2Cartoon(img):
	#  Avatar animation API
    url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    #  Read the original image in binary 
    origin_im = open(img, 'rb')
    #  Take pictures base64 code 
    img = base64.b64encode(origin_im .read())
    #  Close original picture 
    origin_im.close()

	#  Requested headers Information , Fixed writing 
    headers = {'content-type':'application/x-www-form-urlencoded'}
	
	#  Requested parameters 
    params = {
    	#  Start getting access_token
        'access_token':get_access_token(),
        #  The image base64 code 
        'image':img,
    }
    #  Send a request 
    response = requests.post(url, data=params, headers=headers)
    #  Process response results 
    if response:
    	#  Open a file 
        f = open('result.jpg', 'wb')
        #  Get anime avatars 
        anime = response.json()['image']
        #  Decode the returned avatar 
        anime = base64.b64decode(anime)
        #  Write the avatar to the file 
        f.write(anime)
        f.close()
        
if __name__ == '__main__':
    img2Cartoon('origin.jpg')
 Copy code 

In this way, we have realized the operation of avatar animation , It's simple , Because the main work is realized 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 .

copyright notice
author[ZackSock],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311715475133.html

Random recommended