Want to know what kids will look like in the future ?Python Face fusion tells you

writing | Leisure and joy

source :Python technology 「ID: pythonall」

 Want to know what kids will look like in the future ?Python Face fusion tells you

Compared with the simplicity and rudeness of the face changing algorithm , Face fusion algorithm should be more flexible . seeing the name of a thing one thinks of its function , Face fusion is the fusion of two faces , The generated face has the appearance features of two faces at the same time . What is the practical significance of face fusion ? A simple application is to fuse facial images of both parents , Get what the future child might look like .

This article through Baidu AI Open platform face fusion function to do a simple experiment .

Preparation

Account registration

We want to use Baidu AI The function of the development platform , You have to register an account first . visit https://login.bce.baidu.com/ , Then log in with your baidu account .

 Want to know what kids will look like in the future ?Python Face fusion tells you

After login , In the left menu bar, select “ Product service -> Artificial intelligence -> Face recognition ” Sub menu , Enter the product interface of face recognition :

 Want to know what kids will look like in the future ?Python Face fusion tells you

And then click “ Create an ”, Fill in “ apply name ” and “ Application description ” You can create an app :

 Want to know what kids will look like in the future ?Python Face fusion tells you

Then return to the list of apps , You can see the app you created :

 Want to know what kids will look like in the future ?Python Face fusion tells you

After creating the app , You need to API Key and Secret Key write down , We'll use it in the code later .

Read the development documentation

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

Authentication document

If you want to call Baidu's API Interface , You have to authenticate first , That is to get applications token . obtain token The document address of is :https://ai.baidu.com/ai-doc/FACE/5k37c1ti0.

Several pieces of information in the document that are useful to us are as follows :

request URL data format :

To the authorized service address https://aip.baidubce.com/oauth/2.0/token Send a request ( Recommended POST), And in URL With the following parameters :

  • grant_type: Necessary parameters , Fixed for client_credentials;
  • client_id: Necessary parameters , Applied API Key;
  • client_secret: Necessary parameters , Applied Secret Key;

Request example :

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

Server returned JSON The text parameters are as follows :

  • access_token: To obtain the Access Token;
  • expires_in:Access Token The validity of the ( Seconds per unit , It's usually 1 Months );
  • Other parameters ignored , Not for the time being ;

Back to 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 :https://ai.baidu.com/ai-doc/FACE/5k37c1ti0.

Several pieces of information in the document that are useful to us are as follows :

Ask for attention :

  • Request body format :Content-Type by application/json, adopt json Format request body .
  • Base64 code : The requested image needs to go through Base64 code , The image base64 Encoding refers to encoding the image data into a string , Use this string instead of the image address . You can get the binary image first , And then use Base64 Format coding is enough . It should be noted that , The image base64 The encoding does not contain the picture header , Such as data:image/jpg;base64.
  • Image format : Now support PNG、JPG、JPEG、BMP, I won't support it GIF picture .

Request example :

Return to example :

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

code

obtain token

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

# obtain 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']

The interface is simple , The information we pass in to our application is directly available .

Get the base64 code

We call Baidu face fusion interface , Need to pass in the picture base64 code , So first we need to turn the picture into base64 Format , The conversion method is as follows :

# Read the picture , 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 program directory , Method to pass in the image file name .

Call the face fusion interface and save the results

token Value and picture base64 The code is ready , We can call the interface to fuse . According to the official API file , Our calling method is as follows :

# Merge images
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'])

In the parameter template Refers to the template image ,target It refers to the fused image . That is to say target The face of the image is fused into template In the face of the picture , The final output image is in template The picture is a template .

Here is a method to transfer the image returned by the interface to the local down_pic , It is realized as follows :

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

We name the fused image result.jpg , Stored in the same level directory of the program .

The main program

We've done 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)

I'm going to test it with a picture of a man and a picture of a woman , Using pictures of men as templates , Both images are searched from Baidu pictures .

The picture of a man's face is :

 Want to know what kids will look like in the future ?Python Face fusion tells you

The picture of a woman's face is :

 Want to know what kids will look like in the future ?Python Face fusion tells you

The fused face image is :

 Want to know what kids will look like in the future ?Python Face fusion tells you

Isn't it handsome , This may be the appearance of their future sons . next , Let's change the template , We use pictures of women as templates , Look at their future daughter , give the result as follows :

 Want to know what kids will look like in the future ?Python Face fusion tells you

summary

This article calls Baidu AI Face fusion interface of open platform , To realize the fusion experiment of two frontal face images . What do you think of the effect of integration ? I think if these two people get married , It's better to have boys ! You can also take photos of yourself and your partner to test , Look at the children of the future ? Of course , If you're a single dog , Then you can find a beautiful star picture to imagine .