赞
踩
百度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可以得到百度相关的返回值。如图:
百度相关链接为https://ai.baidu.com/docs#/Face-Detect-V3/5875a6ec
在加完参数后,
我们可以看到百度的返回值如下:
返回值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);
}
对于百度返回的混合类型,就需要一步步慢慢来了,需要足够的耐心。
我们可以先把百度的返回值复制粘贴到记事本,自己先看一下,用隔行来分开。
分析清楚后再解析。
相关百度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);
效果如下,达到解析的目的。
然后自己就可以得到自己想用的值了。
第一次解析json。。。
博客内容如有错误,麻烦指正。
标签:人脸识别,AI,list1,face,json,result,解析,百度
来源: https://blog.csdn.net/qq_44807642/article/details/97767696
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。