JAVA face recognition (face comparison)

Face contrast

Enter Baidu face recognition technical document
https://ai.baidu.com/ai-doc/FACE/Lk37c1tpf

Copy the AuthService class and FaceMatch class
Insert picture description here
Insert picture description here

After copying it, you need to import the packages required by the AuthService class and the FaceMatch class


 /**
    * 重要提示代码中所需工具类
    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
    * 下载
    */

Download the 4 java files and put them together with the AuthService class and FaceMatch class.

Import in maven xml

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>


        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

Need to modify AK and SK in the AuthService class
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
After creating the project, click on the project and you can see it in the project.
Note: The validity period of access_token is 30 days. Remember to change regularly every 30 days, or pull a new token every time you request;

The specific operations are as follows:

The AuthService class only needs to replace AK and SK. In the
FaceMatch class, you need to pass in two pictures and convert the pictures to BASE64 type, and then convert them to JSON. Then
you can use the post method of the HttpUtil class to compare the similarity of the two pictures.
There is a readFileByBytes method in the FileUtil tool class. It passes in the image address and outputs a byte[] array. The byte[] array contains the image converted to bytes.
Insert picture description here
Insert picture description here
We only need image and image_type
. The json format of the above picture can be regarded as map, image It is the key, and the following BASE64 is the value.
The whole is like an array to load multiple maps,
but we need to change the byte[] setting to BASE64, and there is an encode method in the Base64Util tool class to convert the byte[] array to BASE64
Insert picture description here
Then we need to convert them to JSON, how to convert?
In the GsonUtils tool class, there is a method toJson
that can help us convert JSON.
Insert picture description here
Finally, we only need to use the post method of the HttpUtil tool class to get the comparison value.
Insert picture description here
Output
Insert picture description here
We can also create a JSON object to directly retrieve the comparison value.
Insert picture description here
Output
Insert picture description here
if it is still The error may be here. The
Insert picture description here
copied address may be wrong

Guess you like

Origin blog.csdn.net/JL_Java/article/details/109306912