Baidu AI Face Recognition Resolution Return Type json's result

Baidu AI Face Recognition

Baidu Face Recognition Interface is divided into V2 and V3 versions. This blog is the description of V3 version.
The preliminary steps are as follows: https://blog.csdn.net/weixin_44694178/article/details/97404694
Analytical json values are learned from 0: https://blog.csdn.net/sinat_17775997/article/details/80667381
The above two articles are reproduced.
Referring to the first link micro-blog, Baidu ai face recognition can be done before the layout steps.

In the previous article, Baidu will return your request value to you in the form of json.

In Baidu's FaceDect.java file

String imgstr = Base64Util.encode(fileContent);
            map.put("image", imgstr);
            map.put("face_field", "faceshape,facetype,beauty,age");
            map.put("image_type", "BASE64");

By adding the parameters provided by Baidu such as beauty to map.put() in the second line, the return value related to Baidu can be obtained. As shown in the figure:

Baidu links are https://ai.baidu.com/docs#/Face-Detect-V3/5875a6ec

After adding the parameters,
We can see that Baidu's return value is as follows:

The return value result is of type json. If we want to use the values in result, we have to parse JSON

json parsing is divided into pure object, array type, and mixed type. Baidu is a mixed type of analysis.

Pure objects belong to JSONObejct parsing; as follows:

                JSONObject json1 = new JSONObject(result1);
                String face_list1 = json1.getString("face_list");
                System.out.println("face_list1="+face_list1);

Array type belongs to JSONArray parsing, as follows:

JSONArray json2 = new JSONArray(face_list1);
                int length1 = json2.length();
                for(int n=0;n<length1;n++) {
                	 string = json2.getString(n);
                	System.out.println("string="+string);
                }

For Baidu to return to the mixed type, it needs to take a step by step slowly, need enough patience.
We can first copy and paste Baidu's return value into the notebook, look at it for ourselves, and separate it with interlaces.

Analyse clearly and then analyze.
The code for Baidu ai's json parsing is as follows:

String result = HttpUtil.post(url, accessToken, "application/json", param);
                System.out.println("111result="+result);
                JSONObject jsonObject = new JSONObject(result);
                String result1 = jsonObject.getString("result");
                System.out.println("result1="+result1);
                
                JSONObject json1 = new JSONObject(result1);
                String face_list1 = json1.getString("face_list");
                System.out.println("face_list1="+face_list1);
                
                JSONArray json2 = new JSONArray(face_list1);
                int length1 = json2.length();
                for(int n=0;n<length1;n++) {
                	 string = json2.getString(n);
                	System.out.println("string="+string);
                }
                
                JSONObject json3 = new JSONObject(string);
                String face_shape = json3.getString("face_shape");
                System.out.println("face_shape="+face_shape);
                String face_type = json3.getString("face_type");
                System.out.println("face_type="+face_type);
                String location = json3.getString("location");
                System.out.println("location="+location);
                String angle = json3.getString("angle");
                System.out.println("angel="+angle);
                String beauty = json3.getString("beauty");
                System.out.println("beauty="+beauty);
                String age = json3.getString("age");
                System.out.println("age="+age);
                String face_probability = json3.getString("face_probability");
                System.out.println("face_probability="+face_probability);

The results are as follows to achieve the purpose of analysis.


Then you can get the value you want.
If the content of the blog is wrong, please correct it.

Keywords: JSON Java

Added by xconspirisist on Tue, 30 Jul 2019 07:38:18 +0300