Python calls Baidu API for portrait animation

1. About the author

Wu Yanzi , female, School of Electronic Information, Xi'an Polytechnic University, 2021 graduate student, Zhang Hongwei Artificial Intelligence Research Group
Research direction: Pattern Recognition and Artificial Intelligence
Email: [email protected]

2. Call Baidu API to realize portrait animation introduction

2.1 API interface description

Using the world's leading confrontation generation network, combined with face detection, hair segmentation, portrait segmentation and other technologies, tailor-made two-dimensional animation images of thousands of people and thousands of faces for users, and can generate two-dimensional animation portraits wearing masks through parameter settings .

2.2 Request Description

HTTP method: POST. HTTP is the abbreviation of Hyper Text Transfer Protocol, which is a transfer protocol used to transfer hypertext from the server to the local browser. This experiment adopts the POST request method.
Request URL: https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime

2.3 Parameter description

insert image description here

3. Experimental procedure

3.1 Installed packages

Install the following two commands in the terminal and import the libraries required for the experiment:
requests: An easy-to-use HTTP library implemented by python.

pip install requests

base64: An encoding method that converts invisible characters into visible characters. It is a method of encoding pictures required by Baidu API.

pip install pybase64

3.2 Experimental process

1. How to get Baidu API for free
You can get your own API Key and Secret Key by following the steps below. insert image description here
insert image description here
insert image description here

3.3 Experimental code

First import the library required for the experiment, and then use the obtained API key to access the portrait animation address provided by Baidu API to obtain an access token. After obtaining the access token, you can call the portrait animation module, read the input image and convert it Become a picture after animation.

import requests
import base64

# 这个函数的操作是为了获取access_token参数
def get_access_token():
    url = 'https://aip.baidubce.com/oauth/2.0/token'       #访问人像动画漫画地址,以得到访问令牌
    data = {
    
    
        'grant_type': 'client_credentials',  # 固定值
        'client_id': '***',  # 在开放平台注册后所建应用的API Key
        'client_secret': '***'  # 所建应用的Secret Key
    }
    res = requests.post(url, data=data)
    res = res.json()

    access_token = res['access_token']        #获取访问令牌
    return access_token                       #百度返回的读取令牌


# 下面的代码就是API文档中的代码,直接搬过来使用即可。
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
f = open('输入图片.jpg', 'rb')  # 二进制方式打开图片文件

img = base64.b64encode(f.read())  # 图像转为base64的格式,这是百度API文档中要求的

params = {
    
    "image": img}
access_token = '24.11731cd1f0...9f9b3a930f917f3681b.2592000.1596894747.282335-21221990'        #令牌地址
request_url = request_url + "?access_token=" + get_access_token()
headers = {
    
    'content-type': 'application/x-www-form-urlencoded'}      #返回的类型
response = requests.post(request_url, data=params, headers=headers)
res = response.json()          #返回的处理后的数据
# 前面我们讲述了这个请求返回的是一个字典,其中一个键就是image,代表的是处理后的图像信息。
# 将这个图像信息写入,得到最终的效果图。
if res:
    f = open("生成的动漫图.jpg", 'wb')
    after_img = res['image']                         
    after_img = base64.b64decode(after_img)        #将返回的图像数据解码回图片形式
    f.write(after_img)           
    f.close()

In the above code, only one parameter is used: image. At this time, the program has no other requirements except to convert the input image into the corresponding animation image. As can be seen from the parameters given in Table 2.3, whether the output picture is wearing a mask and the type of mask worn can be set, and the corresponding parameter program needs to be changed:

params = {
    
    "image": img, "type": 'anime_mask', "mask_id": "6"}
# 注意:这里就是多了type参数和mask_id参数,都是在源文档中可以查看的参数。
# type的值为anime或者anime_mask。前者生成二次元动漫图,后者生成戴口罩的二次元动漫人像。
# 18之间的整数,用于指定所使用的口罩的编码。大家可以自行下去尝试。

3.4 Running Results

It can be observed that the running results of calling Baidu API to realize portrait animation in single-person pictures and multi-person pictures are relatively good.
insert image description here
It can be observed that the larger the tilt angle, the worse the animation result.
insert image description here

refer to

1. Using Python to realize the "portrait animation" special effect on Douyin, it turns out that it is so simple .

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/123645864