Want to know what the future child looks like? Python face fusion tells you

Compared with face changing algorithm, face fusion algorithm is much more flexible. As the name implies, face fusion is the fusion of two faces, and the generated face has the appearance characteristics of two faces at the same time. What is the practical significance of face fusion? A simple application is to fuse the facial images of both parents to get the possible appearance of children in the future.

This paper makes a simple experiment through the face fusion function of Baidu AI open platform.

preparation in advance

Account registration

To use the functions of Baidu AI development platform, we must first register an account. Visit https://login.bce.baidu.com/ and log in with your baidu account.

After logging in, select "product service - > Artificial Intelligence - > face recognition" submenu in the left menu bar to enter the product interface of face recognition:

Then click "create app", fill in "app name" and "app description" to create app:

Then return to the application list, and you can see the application you created:

After creating the application, you need to write down the API Key and Secret Key, which will be used in the code we will use later.

Read development documents

After the application is created, we need to know how to call Baidu's API to complete our experiment, so we need to read the official documents. We need to do two things: authentication and image fusion.

Authentication documents

If you want to call Baidu's API interface, you must first authenticate and obtain the application token. The document address for obtaining token is https://ai.baidu.com/ai-doc/FACE/5k37c1ti0.

Some useful information in the document is as follows:

Request URL data format:

Send a request to the authorized service address https://aip.baidu.com/oauth/2.0/token (POST is recommended), and bring the following parameters in the URL:

  • Grant? Type: required parameter, fixed to client? Credentials;

  • Client? ID: required parameter, API Key of application;

  • client_secret: required parameter, Secret Key of application;

Request example:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&

The JSON text parameters returned by the server are as follows:

  • access_token: Access Token to obtain;

  • Expires in: the period of validity of Access Token (in seconds, generally 1 month);

  • Other parameters are ignored and not used temporarily;

Return example:

{
  "refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074",
  "expires_in": 2592000,
  "scope": "public wise_adapt",
  "session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI",
  "access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074",
  "session_secret": "dfac94a3489fe9fca7c3221cbf7525ff"
}

Picture fusion document

The address of the image fusion document is https://ai.baidu.com/ai-doc/FACE/5k37c1ti0.

Some useful information in the document is as follows:

Notice for request:

  • Request body format: content type is application/json, and request body is formatted through json.

  • Base64 encoding: the requested image needs to be Base64 encoded. Base64 encoding refers to encoding the image data into a string, using the string instead of the image address. You can get the binary of the picture first, and then encode it in Base64 format. It should be noted that the base64 encoding of a picture does not contain the picture header, such as data:image/jpg;base64.

  • Picture format: now supports PNG, JPG, JPEG, BMP, and does not support GIF pictures.

Example request:

  • HTTP method: POST

  • Request URL: https://aip.baidu.com/rest/2.0/face/v1/merge

  • URL parameter: access Ou token

  • Header: content type is application/json

  • Put request parameter in Body

Return to example:

{
    "error_code": 0,
    "error_msg": "SUCCESS",
    "log_id": 1234567890123,
    "timestamp": 1533094576,
    "cached": 0,
    "result": {
        "merge_image": "iVBORw0KGgoAAAANSUhEUgAAAeoAAAHqCAYAAADLb..."
        }
}

Code

Get token

According to the above document description, we call the authentication interface as follows:

# Get token
def get_token(client_id, client_secret):
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials"
    params = {"client_id": client_id, "client_secret": client_secret}
    res = requests.get(url, params=params)
    result = res.json()
    return result['access_token']

This interface is very simple, and the information we pass in our application can be obtained directly.

Get base64 encoding of pictures

When we call Baidu face fusion interface, we need to input base64 encoding of the picture, so we need to convert the picture to base64 format first, and the conversion method is as follows:

# Read image, convert to base64
def read_pic(name):
    with open('./%s' % name, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        s = base64_data.decode()
        return s

I put the picture in the same level directory of the program. Just pass in the name of the picture file.

Call face fusion interface and save the result

When the base64 encoding of token value and image is ready, we can call the interface to fuse. According to the official API documents, our call methods are as follows:

# Fusion picture
def merge(token, template, target):
    url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
    request_url = url + '?access_token=' + token
    params = {
        "image_template": {
            "image": template,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "image_target": {
            "image": target,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "merge_degree": "HIGH"
    }
    params = json.dumps(params)
    headers = {'content-type': 'application/json'}
    result = requests.post(request_url, data=params, headers=headers).json()
    if result['error_code'] == 0:
        res = result["result"]["merge_image"]
        down_pic(res)
    else:
        print(str(result['error_code'])+result['error_msg'])

The template in the parameter refers to the template image, and the target refers to the fused image. That is to say, the face of target image is fused into the face of template image, and the final output image is template image.

Here is a method to transfer the returned pictures from the interface to local storage, which is shown as follows:

# Download pictures
def down_pic(data):
    imagedata = base64.b64decode(data)
    file = open('./result.jpg', "wb")
    file.write(imagedata)

We named the fused image result.jpg and stored it in the same level directory of the program.

main program

We have completed the main methods. Next, we write the main program to test our fusion effect. The code is as follows:

if __name__ == '__main__':
    girl = read_pic('girl.jpg')
    boy = read_pic('boy.jpg')
    token = get_token(API_KEY, SECRET_KEY)
    merge(token, boy, girl)

Here I use a man's picture and a woman's picture to do the test. I use the man's picture as the template. Both pictures are searched from Baidu pictures.

The man's face image is:

Women's faces are:

The fused face image is:

Is it very handsome? This may be the appearance of their future sons. Next, let's change the template. We use the pictures of women as the template to see their future daughter's appearance. The results are as follows:

summary

In this paper, we call the face fusion interface of Baidu AI open platform to realize the fusion experiment of two front face images. What do you think of the effect of integration? I think if these two people get married, it will look better to have boys! You can also test yourself and your partner's selfie to see what your children will look like in the future? Of course, if you are a single dog, you can have a picture of a beautiful star.

Tags: Python JSON encoding REST

Posted on Sat, 09 May 2020 04:46:47 -0400 by castor_troy