百度人脸识别接口分为V2和V3两个版本,本博为V3版本的说明。
参考链接博客(链接在文章后面)可以进行百度ai人脸识别前面的布置步骤。
在前篇中,然后百度会把你的请求值通过json的形式返回result给你。
在百度的FaceDect.java文件中
String imgstr = de(fileContent);map.put("image", imgstr);map.put("face_field", "faceshape,facetype,beauty,age");map.put("image_type", "BASE64");
通过第二行的map.put()里面加百度提供的参数如beauty,int可以得到百度相关的返回值。如图:
百度相关链接为
在加完参数后,
我们可以看到百度的返回值如下:
返回值result为json类型。如果我们想用到result里面的值的话,就必须解析json
json解析分为纯对象,和数组类型,和混合类型。百度这种属于混合类型的解析。
纯对象属于JSONObejct解析;如下:
JSONObject json1 = new JSONObject(result1);String face_list1 = String("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<length1;n++) {string = String(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 = String("result");System.out.println("result1="+result1);JSONObject json1 = new JSONObject(result1);String face_list1 = String("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 = String(n);System.out.println("string="+string);}JSONObject json3 = new JSONObject(string);String face_shape = String("face_shape");System.out.println("face_shape="+face_shape);String face_type = String("face_type");System.out.println("face_type="+face_type);String location = String("location");System.out.println("location="+location);String angle = String("angle");System.out.println("angel="+angle);String beauty = String("beauty");System.out.println("beauty="+beauty);String age = String("age");System.out.println("age="+age);String face_probability = String("face_probability");System.out.println("face_probability="+face_probability);
效果如下,达到解析的目的。
注意:1.如果符号是[ ],则使用JSONObeject解析。
2.如果符号是{ },则用JSONArray解析。
然后自己就可以得到自己想用的值了。
前期步骤为:
第一次解析json。。。
博客内容如有错误,麻烦指正。
本文发布于:2024-02-05 04:38:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170724323263099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |