Python face fusion tells you what your next generation looks like



Compared with the simple and rough face change algorithm, face fusion algorithm is much more flexible. As the name suggests, face fusion is the fusion processing of two faces, and the generated faces have two faces at the same time

Prepare two facial features. What is the practical significance of face fusion? A simple application is to fuse the facial images of both parents to get the possible looks of future children.


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

preparation in advance

Account registration

If we want to use the functions of Baidu AI development platform, we must first register an account. visit , and then log in with your baidu account.


After logging in, select the submenu “product service – > Artificial Intelligence – > face recognition” from the menu bar on the left to enter the product interface of face recognition:


Then click “create application”, fill in “application name” and “application description” to create an application:


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 our code later.

Read development documentation

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

Certificate and picture fusion.


Authentication document

If you want to call Baidu’s API interface, you must first authenticate, that is, obtain the application token. The document address to get the token is:


Several useful information in the document are as follows:

Request URL data format:

To the authorized service address Send the request (post is recommended), and take the following parameters in the URL:

•grant_ Type: required parameter, fixed to client_ credentials;

•client_ ID: required parameter, API key applied;

•client_ Secret: required parameter, the applied secret key;

Examples of requests:


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

•access_ Token: access token to get;

•expires_ In: the validity period of the access token (in seconds, usually 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"}


Image fusion document

The address of the image fusion document is: 。

Several useful information in the document are as follows:

Precautions for request:

• request body formatting: the content type is application / JSON, and the request body is formatted through JSON.

• Base64 encoding: the requested picture needs to be Base64 encoded. Base64 encoding of the picture refers to encoding the picture data into a string, which is used to replace the image ground

Address. You can first get the binary of the picture, and then encode it in Base64 format. It should be noted that the base64 encoding of the picture does not include the picture header, such as


• image format: PNG, JPG, JPEG and BMP are supported, but GIF images are not supported.


Request example:

• HTTP method: Post

• request URL:

• URL parameter: access_ token

• header: content type is application / JSON

• put request parameters in the body

Return example:

Python learning exchange Q group: 906715085####

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



Get token

According to the description in the above document, the method of calling the authentication interface is as follows:

#Get token
def get_token(client_id, client_secret):   
 url = ""    
 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. We can get the information we send into our application directly.

Get Base64 encoding of picture

When we call Baidu face fusion interface, we need to input the base64 code of the image, so we first need to convert the image to Base64 format. The conversion method is as follows:

#Read the picture and convert it to Base64
def read_pic(name):   
 with open('./%s' % name, 'rb') as f:       
  base64_data = base64.b64encode(       
   s = base64_data.decode()      
     return s


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

Call the face fusion interface and save the results

When the base64 encoding of the token value and the picture is ready, we can call the interface for fusion. According to the official API document, our calling method is as follows:

#Fusion picture
def merge(token, template, target):    
url = ''    
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 =, data=params, headers=headers).json()   
   if result['error_code'] == 0:      
     res = result["result"]["merge_image"]        



Template in the parameter refers to the template image, and target refers to the fused image. In other words, the face of the target image is fused into the face of the template image, and the final output image takes the template image as the template.

Here is a method to save the image returned by the interface to the local down_ Pic, which is realized as follows:

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


We named the fused image result Jpg, stored in the same level directory of the program.

main program

We have completed the main methods. Let’s test our fusion effect by writing the main program. 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 test. I use a man’s picture as a template. Both pictures are searched from Baidu pictures.

The picture of a man’s face is:

The picture of a woman’s face is:


The fused face image is:

Isn’t it very handsome? This may be the appearance of these two future sons. Then, we change the template. We use the pictures of women as the template to see what their future daughter looks like. The results are as follows:

This paper realizes the fusion experiment of two frontal face images by calling the face fusion interface of Baidu AI open platform. What do you think of the effect of integration? I think if these two people get married, it will look better to have a boy! You can also test the self photos of yourself and your partner to see what children will look like in the future? Of course, if you are a single dog, you can find a beautiful star picture to fantasize about.


Recommended Today

Viewbinding uses view binding in , , adapter, fragment, and activity

Original address The following is the author’s reading record original text Google’s role in 2019 I / O ConferenceWhat’s New in Architecture ComponentsThis paper introduces view binding InWhat’s New in Architecture ComponentsIn, there is a short speech on view binding, which compares view binding with existing solutions and further discusses why view binding is better […]