当前位置:   article > 正文

python 百度ai json解析,百度AI 人脸识别 解析返回类型为json的result

ai翻译json

百度AI人脸识别

参考第一条链接微博可以进行百度ai人脸识别前面的布置步骤。

在前篇中,然后百度会把你的请求值通过json的形式返回result给你。

在百度的FaceDect.java文件中

String imgstr = Base64Util.encode(fileContent);

map.put("image", imgstr);

map.put("face_field", "faceshape,facetype,beauty,age");

map.put("image_type", "BASE64");

通过第二行的map.put()里面加百度提供的参数如beauty,int可以得到百度相关的返回值。如图:

0d9be4759bd4ab32dedb02d95f0f2414.png

百度相关链接为https://ai.baidu.com/docs#/Face-Detect-V3/5875a6ec

在加完参数后,

我们可以看到百度的返回值如下:

21b215235d510b66aba9d01de01fc1a4.png

返回值result为json类型。如果我们想用到result里面的值的话,就必须解析json

json解析分为纯对象,和数组类型,和混合类型。百度这种属于混合类型的解析。

纯对象属于JSONObejct解析;如下:

JSONObject json1 = new JSONObject(result1);

String face_list1 = json1.getString("face_list");

System.out.println("face_list1="+face_list1);

数组型属于JSONArray解析,如下:

JSONArray json2 = new JSONArray(face_list1);

int length1 = json2.length();

for(int n=0;n

string = json2.getString(n);

System.out.println("string="+string);

}

对于百度返回的混合类型,就需要一步步慢慢来了,需要足够的耐心。

我们可以先把百度的返回值复制粘贴到记事本,自己先看一下,用隔行来分开。

3228dcdfcda88d464597d58d9f2d73b0.png

分析清楚后再解析。

相关百度ai的json解析的代码如下:

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

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);

效果如下,达到解析的目的。

afed10d356d8fdec82eccaeb1a8c9bcc.png

然后自己就可以得到自己想用的值了。

第一次解析json。。。

博客内容如有错误,麻烦指正。

标签:人脸识别,AI,list1,face,json,result,解析,百度

来源: https://blog.csdn.net/qq_44807642/article/details/97767696

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/708909
推荐阅读
相关标签
  

闽ICP备14008679号