Make writing a habit together! This is the 9th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Welcome to my GitHub

Here classification and summary of xinchen all original (including supporting source code) : github.com/zq2599/blog…

This paper gives an overview of

  • The web interface provided by Baidu open platform can be used to detect the number of faces, location, gender, masks and other scenarios. A Web request can complete the detection and obtain the results. This paper records the whole process from application to actual call, which consists of the following steps:

Register a Baidu account

  • According to your actual situation, register personal or business account, this is not to go into

Log in to Baidu Intelligent Cloud

  • Log in using the account with the same registration number. The address is login.bce.baidu.com

Real-name authentication

  • Open the console of Baidu Intelligent Cloud: console.bce.baidu.com
  • As shown below, click the two buttons in the red box below to complete the activation and real-name authentication:

Create an

  • To be able to use Baidu services, you need to create an application
  • First, select the category and click the red box 4 on the console page.

  • Now that you’ve gone to the Manage References page, click create Application in the red box below

  • To use Baidu’s service for free, click on the red box below to get it:

  • Check face detection on the receiving page:

  • After collecting, I returned to the page of application creation and found that these services had been selected, as shown in the following figure:

  • After filling in the application information, submit the form to create an application

Get the API Key and Secret Key

  • Get the API Key and Secret Key on the application list page, which are Key authorization information for invoking Baidu services, as shown in the red box below:

Get the access_token

  • When using various services provided by Baidu (such as face detection), you need to bring the authorization information to prove that you have the permission to use the service. This authorization information is access_token
  • The simplest way to do this is with the curl command
curl -i -k 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= [baidu cloud application API Key】&client_secret= [Secret Key] '
Copy the code
  • Here we use Postman to try the above request, as shown in the picture below. The red box is the access_token information we need for this request:

  • After obtaining the access_token, you can start to call Baidu’s services. The official document states that the validity period of the access_token is 30 days:

  • More information about baidu cloud authorization information please check here: cloud.baidu.com/doc/FACE/s/…

coding

  • Baidu on face detection documents: ai.baidu.com/ai-doc/FACE…
  • The face detection service is a Web interface, which can also be done by using curl or postman, but in order to use baidu’s service in the code, here is a code to do face detection
  • Today’s project is a normal Maven project, with no Spring or SpingBoot framework, just a few simple Java classes and main methods
  • Start by introducing the following three libraries into your project:
<! Shortcut Code Helper library -->
 <dependency>
	<groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.18</version>
</dependency>
<! Network Request Library -->
<dependency>
	<groupId>com.squareup.okhttp3</groupId>
	<artifactId>okhttp</artifactId>
	<version>3.10.0</version>
</dependency>
<! -- JSON processing -->
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.11.0</version>
</dependency>
Copy the code
  • Create a new object, faceDetectrequest.java, to hold the request parameters:
package com.bolingcavalry.grabpush.bean.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class FaceDetectRequest {
    // Image information (the total data size should be less than 10M). The image upload mode is determined according to image_type
    String image;

    // Image type
    // BASE64: the BASE64 value of the image. The size of the encoded image does not exceed 2M.
    // URL: the URL of the image (it may take too long to download the image due to network reasons);
    // FACE_TOKEN: the unique identifier of a face image. When the face detection interface is invoked, each face image is given a unique FACE_TOKEN. The same image can be detected multiple times to obtain the same FACE_TOKEN.
    @JsonProperty("image_type")
    String imageType;

    / / including age, expression, face_shape, gender, glasses, landmark, landmark150, quality, eye_status, emotion, face_type, mask, spoofing information
    By default, only face_token, face frame, probability, and rotation Angle are returned
    @JsonProperty("face_field")
    String faceField;

    // The maximum number of faces to process, the default value is 1, according to the face detection sorting type detection picture of the first face (default is the face with the largest face area), the maximum value is 120
    @JsonProperty("max_face_num")
    int maxFaceNum;

    // The type of face
    // LIVE: a portrait taken by a mobile phone or camera, or taken from the Internet
    // IDCARD stands for IDCARD chip photo: portrait photo in the embedded chip of the second-generation IDCARD
    // WATERMARK indicates a small watermarked id photo, such as a police network photo
    // CERT: photo of id card, employee card, passport, student id card, etc
    / / LIVE by default
    @JsonProperty("face_type")
    String faceType;

    // Faces that do not meet the requirements in the liveness control test result will be filtered
    // NONE: no control is performed
    // LOW: LOW live requirement (high pass rate, LOW attack rejection rate)
    // NORMAL: NORMAL living requirements (balanced attack rejection rate, pass rate)
    // HIGH: HIGH live requirement (HIGH attack rejection rate, low pass rate)
    / / the default NONE
    @JsonProperty("liveness_control")
    String livenessControl;
    
    // Face detection sort type
    // 0: indicates that the detected faces are arranged according to the face area from large to small
    // 1: indicates that the detected faces are arranged from near to far from the center of the picture
    // Default is 0
    @JsonProperty("face_sort_type")
    int faceSortType;
}
Copy the code
  • The next response object is faceDetectresponse.java:
package com.bolingcavalry.grabpush.bean.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

@Data
@ToString
public class FaceDetectResponse implements Serializable {
    / / return code
    @JsonProperty("error_code")
    String errorCode;
    // Description
    @JsonProperty("error_msg")
    String errorMsg;
    // What to return
    Result result;

    / * * *@author willzhao
     * @version 1.0
     * @descriptionWhat is returned *@date2022/1/1 16:01 * /
    @Data
    public static class Result {
        // The number of faces
        @JsonProperty("face_num")
        private int faceNum;
        // Information about each face
        @JsonProperty("face_list")
        List<Face> faceList;

        @Data
        public static class Face {
            / / position
            Location location;
            // Is the face confidence
            @JsonProperty("face_probability")
            double face_probability;
            / / face mask
            Mask mask;

            / * * *@author willzhao
             * @version 1.0
             * @descriptionThe position of the face in the picture *@date2022/1/1 16:04 * /
            @Data
            public static class Location {
                double left;
                double top;
                double width;
                double height;
                double rotation;
            }

            / * * *@author willzhao
             * @version 1.0
             * @descriptionMask object *@date2022/1/1 tightly with * /
            @Data
            public static class Mask {
                int type;
                doubleprobability; }}}}Copy the code
  • One thing to note here is that the fields in the FaceDetectResponse object are fewer than the fields returned by the actual response. This is because the demo doesn’t need the full returned content, so just select the field definitions that you want to apply in FaceDetectresponse.java
  • Finally, the complete service class baiducloudservice.java is shown as follows, i.e. read the image -> transfer base64 -> construct the request object -> submit the request -> receive the response -> parse the response:
public class BaiduCloudService {

    / / conversion
    BASE64Encoder encoder = new BASE64Encoder();

    OkHttpClient client = new OkHttpClient();

    static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    static final String URL_TEMPLATE = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=%s";

    String token;

    ObjectMapper mapper = new ObjectMapper();

    public BaiduCloudService(String token) {
        this.token = token;
        
        // Important: When deserializing, if the character field is more than the class field, the following setting will ensure that deserialization is successful
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    /** * converts the image at the specified location to the base64 string *@param imagePath
     * @return* /
    private String img2Base64(String imagePath) {
        InputStream inputStream = null;
        byte[] data = null;

        try {
            inputStream = new FileInputStream(imagePath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        return null==data ? null :encoder.encode(data);
    }

    /** * test the specified image *@param imageBase64
     * @return* /
    public FaceDetectResponse detect(String imageBase64) {
        // Request object
        FaceDetectRequest faceDetectRequest = new FaceDetectRequest();
        faceDetectRequest.setImageType("BASE64");
        faceDetectRequest.setFaceField("mask");
        faceDetectRequest.setMaxFaceNum(6);
        faceDetectRequest.setFaceType("LIVE");
        faceDetectRequest.setLivenessControl("NONE");
        faceDetectRequest.setFaceSortType(0);
        faceDetectRequest.setImage(imageBase64);

        FaceDetectResponse faceDetectResponse = null;

        try {
            // Serialize the request object into a string with Jackson
            String jsonContent = mapper.writeValueAsString(faceDetectRequest);

            //
            RequestBody requestBody = RequestBody.create(JSON, jsonContent);
            Request request = new Request
                    .Builder()
                    .url(String.format(URL_TEMPLATE, token))
                    .post(requestBody)
                    .build();
            Response response = client.newCall(request).execute();
            String rawRlt = response.body().string();
            faceDetectResponse = mapper.readValue(rawRlt, FaceDetectResponse.class);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        return faceDetectResponse;
    }

    public static void main(String[] args) {
        // The image is in the local location
        String imagePath = "E:\\temp\\202201\\01\\pic\\1.jpeg";

        / / token of baidu cloud, is obtained by this interface: https://aip.baidubce.com/oauth/2.0/token
        String token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 24.95. XXXXXXX. XXXXXXXXXX. XXXXXX - XXXXXXXX";

        // instantiate the service object
        BaiduCloudService service = new BaiduCloudService(token);

        // Convert the image to a base64 string
        String imageBase64 = service.img2Base64(imagePath);

        // Send a request to Baidu service to detect the face
        FaceDetectResponse faceDetectResponse = service.detect(imageBase64);

        // Output the detection resultSystem.out.println(faceDetectResponse); }}Copy the code
  • Make sure that the image used for detection is the same as the path in the above code (E:\temp\202201\01\ PIC \1.jpeg). I have chosen a single person with a mask as shown below:

  • Using the BaiduCloudService main method, the console prints out the result returned by Baidu. Note that the following is not JSON, but lombok’s @toString annotation:

  • At this point, through baidu’s Web interface to call the actual combat of face detection has been completed, it can be seen that with the support of the cloud platform, the development process for users has become very simple

Use restrictions

  • Since it is free, it is hard to be perfect. There are QPS limits for such web services, as shown in the picture below. No more than two web services per second can be increased to ten if enterprise authentication is completed.

Welcome to the Nuggets: programmer Chen Chen

Learning on the road, you are not alone, Xinchen original all the way accompanied by…