关于百度人脸识别的尝试收获

1.ajax的跨域请求问题

这里直接给出ajax的请求代码,由于ajax认为跨域的post请求不安全,所以再进行项目实现的时候,必须自己写一个中转的接口,我这里是自己写了一个addface这个接口来接受post请求,才能处理。不用get的方式是因为,图片经过base64编码后作为参数去请求域外接口时,请求体会异常庞大,甚至有时候会让浏览器无法无法解析。而且这里注意一个问题,就是在项目发布到服务器后要注意ajax的请求地址应该改成映射的地址。

2.关于json的处理

$.post("https://cac66068.ngrok.io/GasMonitor/addface",
                {
                 "image":data1
                 },
            function(data){
                  //进行判断,json中是否存在error_msg这个key
                if(data.hasOwnProperty("error_msg")) {
                    alert("注册失败");
                } else {
                    alert("注册成功");
                }

             },
             "json");

除了这些问题以外,还要注意json类型数据的处理,特别是嵌套类型的json类型的处理,其中json(回调函数中通常写成data).hasOwnProperty(“key”)可以判断json中是否含有”key”这个属性名,这里可以用“data.属性”的方法来获取属性的值,如果是嵌套的话,还要加后续的属性,比如下面的json:

 {
   "result":[
       {"expression":0,
        "beauty":48.416812896729,
        "gender":"male",
        "gender_probability":0.99925523996353,
        "roll":-1.3796654939651,
        "glasses_probability":0.99953198432922, 
"face_probability":0.71843481063843,
        "yaw":-5.5738277435303,
        "glasses":1,
        "expression_probablity":0.99941623210907,
         "location":{"top":189,"left":100,"width":165,"height":154},
        "rotation_angle":0,
        "pitch":6.1355400085449,
        "age":27.296012878418}
             ],
   "log_id":3239516879,
   "result_num":1
 }

我现在要取result中具体的属性就要这么取:data.result[0].gender、data.result[0].gender、data.result[0].expression

3.图片编码的问题

将图片转成二进制数组,通常用来将图片写入数据库可以这么干,user.setpicture(pic);

String imagePath1 = "C:\\Users\\weiguoLiu\\Desktop\\test.jpg";

        //将图片转成二进制数组进行上传
        byte[] pic = null;
        File f = new File(imagepath);
        FileInputStream fis = new FileInputStream(f);
        pic = new byte[fis.available()];
        fis.read(pic);
        fis.close();

关于base64编码,在拿到后,将头部的前缀去掉,拿到编码的主体:base64.substring(22);js中是imgData.substr(22);到后台进行处理的得到二进制数组直接利用工具类进行解码即可:

byte[] pic = Base64.decode(image);

当然也可以自己写两者的编码互转方式:

import java.io.File;
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  

import sun.misc.BASE64Decoder;  
import sun.misc.BASE64Encoder; 
public class IMGToBase64 {
    //图片转化成base64字符串  
    public static String GetImageStr(String imgPath)  
    {
        if(imgPath!=null && !"".equals(imgPath) && new File(imgPath).exists()){
            //将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
            String imgFile = imgPath;//待处理的图片  
            InputStream in = null;  
            byte[] data = null;  
            //读取图片字节数组  
            try   
            {  
                in = new FileInputStream(imgFile);          
                data = new byte[in.available()];  
                in.read(data);  
                in.close();  
            }   
            catch (IOException e)   
            {  
                e.printStackTrace();  
            }  
            //对字节数组Base64编码  
            BASE64Encoder encoder = new BASE64Encoder();  
            return encoder.encode(data);//返回Base64编码过的字节数组字符串  

        }else{
            return null;
        }
    }  

    //base64字符串转化成图片  
    public static boolean GenerateImage(String imgStr)  
    {   //对字节数组字符串进行Base64解码并生成图片  
        if (imgStr == null) //图像数据为空  
            return false;  
        BASE64Decoder decoder = new BASE64Decoder();  
        try   
        {  
            //Base64解码  
            byte[] b = decoder.decodeBuffer(imgStr);  
            for(int i=0;i<b.length;++i)  
            {  
                if(b[i]<0)  
                {//调整异常数据  
                    b[i]+=256;  
                }  
            }  
            //生成jpeg图片  
            String imgFilePath = "d://222.jpg";//新生成的图片  
            OutputStream out = new FileOutputStream(imgFilePath);      
            out.write(b);  
            out.flush();  
            out.close();  
            return true;  
        }   
        catch (Exception e)   
        {  
            return false;  
        }  
    }  
}
识别结果大概是这样 {'result': {'face_num': 1, 'face_list': [{'quality': {'occlusion': {'right_eye': 0, 'left_cheek': 0.1459853947, 'right_cheek': 0.05144193396, 'left_eye': 0.465408802, 'mouth': 0.02919708006, 'chin_contour': 0.01420217194, 'nose': 0}, 'illumination': 116, 'blur': 7.266304692e-06, 'completeness': 1}, 'age': 22, 'face_token': 'dc6f8f9df5d977ea476e2d04acdf5063', 'race': {'type': 'white', 'probability': 0.6173604727}, 'glasses': {'type': 'common', 'probability': 0.9834988713}, 'gender': {'type': 'male', 'probability': 0.655915916}, 'face_probability': 0.9185044169, 'beauty': 51.21487427, 'angle': {'roll': -2.750922441, 'yaw': 28.97134399, 'pitch': 5.202290535}, 'location': {'height': 65, 'top': 112.0704803, 'width': 76, 'left': 76.20765686, 'rotation': -4}, 'face_type': {'type': 'human', 'probability': 0.9992217422}, 'face_shape': {'type': 'oval', 'probability': 0.4419156313}, 'expression': {'type': 'none', 'probability': 0.9999142885}}]}, 'error_msg': 'SUCCESS', 'timestamp': 1537413754, 'cached': 0, 'error_code': 0, 'log_id': 9465840013520} 年龄:22 颜值:51.21487427 表情-type(none:不笑;smile:微笑;laugh:大笑):none 表情-probability(表情置信度,范围【0~1】,0最小、1最大):0.9999142885 脸型-type(square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形):oval 脸型-probability(置信度,范围【0~1】,代表这是人脸形状判断正确的概率,0最小、1最大):0.4419156313 性别-type(male:男性 female:女性):male 性别-probability(性别置信度,范围【0~1】,0代表概率最小、1代表最大。):0.655915916 是否带眼镜-type(none:无眼镜,common:普通眼镜,sun:墨镜):common 是否带眼镜-probability(眼镜置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9834988713 人种-type(yellow: 黄种人 white: 白种人 black:黑种人 arabs: 阿拉伯人):white 人种-probability(人种置信度,范围【0~1】,0代表概率最小、1代表最大。):0.6173604727 真实人脸/卡通人脸 -type(human: 真实人脸 cartoon: 卡通人脸):human 真实人脸/卡通人脸 -probability(人脸类型判断正确的置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9992217422
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值