An attempt of Tencent Cloud [Face Recognition] service (JAVA)

background

Face recognition is one of the most widely used services in the field of artificial intelligence. I personally believe that face recognition is also one of the most mature technologies in the field of artificial intelligence. All major cloud service vendors have launched facial recognition services.

Then come and taste it. .

Log in to register -> find facial recognition service

I won’t talk about the login process. I had a Tencent Cloud account a long time ago, and now I can log in by scanning the QR code on WeChat. You can find the facial recognition service on the Fujisu product homepage.

 Open face recognition service

Click the Getting Started button to come to the guidance page. Click the connection of the cloud console to jump directly to the activation page and click the activation button. The face recognition service is activated.

Or go find the interface by yourself

 

 Face recognition application

(1) Configure the JAVA environment, the maven environment, and create a new maven project.

Here you need to configure Tencent's SDK, that is, the package, first go to this website to check the version number.

https://search.maven.org/search?q=tencentcloud-sdk-java

It is found that the current version number is 3.1.46, then the corresponding in the maven file is

    <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.46</version>
    </dependency>

(2) Coding

Encode through API Explorer, click to find the API found by the face, and enter your own key in the personal key.

The little friend who just came to use certainly doesn't know where to find the key, but there is a link just above the input box and you can click it directly.

Then take a closer look at what to enter the content of the parameters. Take a closer look, in fact, only regions and pictures are necessary.

For region, just choose one.

But the picture can be a link or a URL, but it should be stored in Tencent Cloud. Here we choose to use Base64 image strings directly.

 If you enter the character string of the picture directly, it will be like this, and it will be exhausting to copy it.

And there will be problems running.

Therefore, do not fill in the picture information for the time being, and modify it in the code. Copy the following code to IDEA

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.iai.v20180301.IaiClient;

import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest;
import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse;

public class DetectFace {
    public static void main(String[] args) {
        try {

            Credential cred = new Credential("XXXXXXXXXXXXXXx", "XXXXXXXXx");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);

            String params = "{}";
            DetectFaceRequest req = DetectFaceRequest.fromJsonString(params, DetectFaceRequest.class);

            DetectFaceResponse resp = client.DetectFace(req);

            System.out.println(DetectFaceRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

    }

}

 (3) Modification

A very critical step, with two goals:

One is to convert the picture to a BASE64 String to construct params

The second is to use the recognition result to label the face.

The json package is used

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68</version>
</dependency>

code show as below.


import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest;
import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse;
import com.tencentcloudapi.iai.v20180301.models.FaceInfo;
import sun.misc.BASE64Encoder;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

public class DetectFace {
    public static void main(String[] args) {
        try {

            String imageUrl = "/Users/yuchk/Desktop/haha.png";
            String markImageUrl = "/Users/yuchk/Desktop/haha_res.png";

            // 替换自己的密钥
            Credential cred = new Credential("XX", "XX");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);

            HashMap map = new HashMap<String, String>(8);
            String image = getBase64Image(imageUrl);
            map.put("Image", image);
            map.put("NeedQualityDetection", "1");
            String params = JSON.toJSONString(map);
            DetectFaceRequest req = DetectFaceRequest.fromJsonString(params, DetectFaceRequest.class);
            DetectFaceResponse resp = client.DetectFace(req);
            System.out.println(DetectFaceRequest.toJsonString(resp));

            FaceInfo[] faceInfos = resp.getFaceInfos();
            long height = faceInfos[0].getHeight();
            long width = faceInfos[0].getWidth();
            long x = faceInfos[0].getX();
            long y = faceInfos[0].getY();
            // 将人脸标注起来

            BufferedImage bufferedImage = ImageIO.read(new File(imageUrl));
            Graphics g = bufferedImage.getGraphics();
            g.setColor(Color.RED);
            //矩形框(原点x坐标,原点y坐标,矩形的长,矩形的宽)
            g.drawRect((int) x, (int) y, (int) width, (int) height);
            g.dispose();
            FileOutputStream out = new FileOutputStream(markImageUrl);
            ImageIO.write(bufferedImage, "png", out);

        } catch (TencentCloudSDKException | IOException e) {
            System.out.println(e.toString());
        }

    }

    private static String getBase64Image(String url) {

        try {
            return getBase64Image(new FileInputStream(url));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getBase64Image(FileInputStream inputStream) {
        try {

            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

}

result

{"ImageWidth":509,"ImageHeight":429,"FaceInfos":[{"X":152,"Y":51,"Width":135,"Height":175,"FaceAttributesInfo":{"Gender":0,"Age":0,"Expression":0,"Glass":false,"Pitch":0,"Yaw":0,"Roll":0,"Beauty":0,"Hat":false,"Mask":false,"Hair":{"Length":0,"Bang":0,"Color":0},"EyeOpen":false},"FaceQualityInfo":{"Score":84,"Sharpness":61,"Brightness":43,"Completeness":{"Eyebrow":87,"Eye":92,"Nose":97,"Cheek":89,"Mouth":99,"Chin":94}}}],"FaceModelVersion":"3.0","RequestId":"xxxxxxxxxx"}
 

 

 

Guess you like

Origin blog.csdn.net/Kangyucheng/article/details/105984419