This time, I spent some time to combine the face recognition function of Baidu intelligent cloud with Python to conduct an experiment. The result is still quite a sense of achievement, the process is also quite simple, not complex, maybe, you can blend out of your child’s appearance!

Prophase preparation

Baidu AI open platform

Account registration

Use your Baidu account to log in ai.baidu.com, baidu AI open platform.

And then create a facial recognition app,

You’ll get an API Key and a Secret Key, which we’re going to use in our code,

Because each account call times are limited, only free 500 times, and play and cherish. So you might as well apply yourself.

Read the interface development documentation…

Document address: ai.baidu.com/docs#/Face-…

Note above, that is, the post should have the header, the image should be base64, and the request address, etc., the document should be carefully read, and there will be fewer errors later.

The image above shows that you want to post past data, such as a template image and a target file. You can also set the image type, image quality, etc

The figure above is a description of what will be returned after you post.

OK, now that we’re done with the documentation, let’s get this straight

Train of thought

Start Coding…

Develop little by little, don’t rush:

The import module

As a rule of thumb, we'll definitely use the Requests module, the JSON module, and then the Base64 module, because we're dealing with images. We already have these modules, so import them directly:Copy the code

Access Token

Token is a Token, and you need to obtain its approval to use the interface, that is, we need to use the previous application of the key to obtain the Token:Copy the code

Get the picture Base64

Base64 is a format for images, so you need to open the image first and then switch to Base64 encoding to use itCopy the code

Save base to the local machine

Since the documentation tells us that the image to be returned is Base64, we also need to convert Base64 to the image you can see. So:

Note: code files are also in this folder.

Image fusion

And then there's the main function,Copy the code

A functional test

At the beginning of debugging, you need to test a little bit, otherwise your 500 calls will not be enough for you to use, when you write the code, sorry, you ran out of times, like the picture below, you also played balls! To test this, I took down two pictures from the Internet and, uh!

Below is my computer folder, pay attention to the format of the picture

Then write a few lines of code and run it

Appear the Done! The code works! Let’s open up the folder and see if there’s a “done.jpg” image

Python complete code

Please send [source], today’s file name is “face.zip”, please find private me oh.