Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

What WE are going to do today is a very interesting thing, that is to animate the heads of characters. This article is simple, small white operation is not difficult, can operate directly, take to use. The effect is shown below.

Baidu API address: ai.baidu.com/tech/imagep…

Technical documentation: ai.baidu.com/ai-doc/IMAG…

This is used in baidu intelligent cloud API, first need to register baidu account, and then log in baidu intelligent cloud. Open the realization of portrait animation, create applications. The character animation here is to process the character picture into animation picture.

Baidu API used in this document, there are detailed usage, you can refer to their own.

# encoding: utF-8 import requests # client_id Client_secret is SK host = obtained on the official website 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= & website to obtain the AK 】 【 client_secret =' official website to obtain the SK 】 【 response = requests.get(host) if response: print(response.json())Copy the code

Set up the app first, then check your client_id= AK &client_secret= SK. Just fill in these two things. Response.json () will then carry the required access_token. Access_token is needed for the complete code below.

import requests import base64 host = https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxx & client_secret = xxx2 response = requests.get(host) if response: access_token= response.json()["access_token"] request_url = "Https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime" # binary way open to deal with image file f = open (' 1. JPG ', Img = base64encode (f.read()) params = {"image":img} request_url = request_url + "? Access_token =" +  access_token response = requests.post(request_url, data=params) print(response) if response: # save file f = open('2.jpg', 'wb') img = (response.json()['image']) f.write(base64.b64decode(img)) f.close()Copy the code

Now that the access_token process is complete, it’s time to use the url API below for image processing.

Request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"Copy the code

Remember that all images need to be opened in binary mode. The main function of b64decode is to decode bytes-like objects or ASCII strings encoded in Base64.

The entire image data is then passed as a parameter to the API, which needs to add the access_token obtained above.

Response.json ()[‘image’] returns the binary value in JSON format if the response is ok. Encode this value and write it to 2.jpg

In fact, there are quite a number of interesting functions in baidu intelligent cloud API. There are text recognition, animal recognition, interested in practicing students can actually go up and do some API and source code cases are basically read to understand.

You are welcome to discuss procedural questions with me and answer any questions you may have. Follow the public number: poetic code, make a friend.