analyse.js 14.1 KB
// packageB/pages/AI-test-skin/analyse/analyse.js
const app = getApp();
const request = app.request;
const os = app.globalData;
const setting = os.setting;
const ut = require('../../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iurl: setting.imghost,
        per:0,
        timer:0,
        img:null,
        sourceimg:null,
        err_map:null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       var th=this;
       //设置错误码分析
       this.set_err_map();
       //进度条加载
       this.timer();
       //接口开始分析
       setTimeout(function () {
           //th.analysis_img();
           //先检验一下图片
           th.img_check(function () {
               
           });
       },800)

    },
    //定时器
    timer:function (func) {
       var per=this.data.per+5;
       var th=this;
       if(per<96){
          th.setData({per:per});
          setTimeout(function () {
              th.timer();
          },400)
       }
    },

    //错误码表
    set_err_map:function () {
       var map={
           "2":"服务暂不可用",
           "4":"集群超限额",
           "6":"没有接口权限",
           "17":"每天流量超限额",
           "18":"QPS超限额",
           "19":"请求总量超限额",
           "100":"无效的access_token参数",
           "110":"Access Token失效",
           "111":"	Access token过期",
           "222001":"必要参数未传入",
           "222002":"参数格式错误",
           "222003":"参数格式错误",
           "222004":"参数格式错误",
           "222005":"参数格式错误",
           "222006":"参数格式错误",
           "222007":"参数格式错误",
           "222008":"参数格式错误",
           "222009":"参数格式错误",
           "222010":"参数格式错误",
           "222011":"参数格式错误",
           "222012":"参数格式错误",
           "222013":"参数格式错误",
           "222014":"参数格式错误",
           "222015":"参数格式错误",
           "222016":"参数格式错误",
           "222017":"参数格式错误",
           "222018":"参数格式错误",
           "222019":"参数格式错误",
           "222020":"参数格式错误",
           "222021":"参数格式错误",
           "222022":"身份证号不符合要求,备注:中国大陆身份证号的每一位数字都有其生成规则,不可随意填写。",
           "222023":"参数格式错误",
           "222024":"参数格式错误",
           "222025":"参数格式错误",
           "222026":"参数格式错误",
           "222027":"验证码长度错误(最小值大于最大值)",
           "222028":"参数格式错误",
           "222029":"参数格式错误",
           "222030":"参数格式错误",
           "222039":"参数格式错误",
           "222200":"该接口需使用application/json的格式进行请求",
           "222201":"服务端请求失败",
           "222202":"图片中没有人脸",
           "222203":"无法解析人脸",
           "222204":"从图片的url下载图片失败",
           "222205":"服务端请求失败",
           "222206":"服务端请求失败",
           "222207":"未找到匹配的用户",
           "222208":"图片的数量错误",
           "222209":"face token不存在",
           "222210":"人脸库中用户下的人脸数目超过限制",
           "222300":"人脸图片添加失败",
           "222301":"获取人脸图片失败",
           "222302":"服务端请求失败",
           "222303":"获取人脸图片失败",
           "222152":"人脸属性编辑,target参数错误",
           "222514":"人脸属性编辑v2调用服务失败,请重试",
           "223100":"操作的用户组不存在",
           "223101":"该用户组已存在",
           "223102":"找不到该用户",
           "223103":"找不到该用户",
           "223104":"group_list包含组数量过多",
           "223105":"该人脸已存在",
           "223106":"该人脸不存在",
           "223107":"人脸库中人脸复制时源组与目标组的scene_type不同",
           "223110":"uid_list包含数量过多",
           "223111":"目标用户组不存在",
           "223112":"quality_conf格式不正确",
           "223113":"人脸有被遮挡",
           "223114":"人脸模糊",
           "223115":"人脸光照不好",
           "223116":"人脸不完整",
           "223117":"app_list包含app数量过多",
           "223118":"质量控制项错误",
           "223119":"活体控制项错误",
           "223120":"活体检测未通过",
           "223121":"质量检测未通过 左眼遮挡程度过高",
           "223122":"质量检测未通过 右眼遮挡程度过高",
           "223123":"质量检测未通过 左脸遮挡程度过高",
           "223124":"质量检测未通过 右脸遮挡程度过高",
           "223125":"质量检测未通过 下巴遮挡程度过高",
           "223126":"质量检测未通过 鼻子遮挡程度过高",
           "223127":"质量检测未通过 嘴巴遮挡程度过高",
           "222901":"参数校验初始化失败",
           "222902":"参数校验初始化失败",
           "222903":"参数校验初始化失败",
           "222904":"参数校验初始化失败",
           "222905":"接口初始化失败",
           "222906":"接口初始化失败",
           "222907":"缓存处理失败",
           "222908":"缓存处理失败",
           "222909":"缓存处理失败",
           "222910":"数据存储处理失败",
           "222911":"数据存储处理失败",
           "222912":"数据存储处理失败",
           "222913":"接口初始化失败",
           "222914":"接口初始化失败",
           "222915":"后端服务连接失败",
           "222916":"后端服务连接失败",
           "222304":"图片尺寸太大",
           "222305":"当前版本不支持图片存储",
           "223128":"正在清理该用户组的数据",
           "223136":"该组内存在关联图片,无法新建相同名称组",
           "222361":"公安服务连接失败",
           "222046":"参数格式错误",
           "222101":"参数格式错误",
           "222102":"参数格式错误",
           "222307":"图片非法 鉴黄未通过",
           "222308":"图片非法 含有政治敏感人物",
           "222211":"人脸融合失败 模板图质量不合格",
           "222212":"人脸融合失败",
           "223129":"人脸未面向正前方(人脸的角度信息大于30度)",
           "223130":"spoofing_control参数格式错误",
           "223131":"合成图检测未通过",
           "223201":"请求参数scene_type 格式错误",
           "223202":"识别时请求的scene_type与group设置的scene_type不匹配",
       }
       this.setData({err_map:map});
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (){ },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    //检查图片有没有OK
    img_check:function (func) {
        var th=this;
        var data={
            "appid": "25521854",
            "image": getApp().globalData.face_SourceImg,
            "face_field": "age,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing",
            "image_type": "URL"
        }
        //开始调用接口,进行对图片进行判断是否符合标准
        this.json_post("/api/weshop/baidubce/face/skindetect",data,function (res) {
            if(res.data.code==0){
                //检查的字段存储
                var check_data=th.data.check_data=res.data.data;
                check_data=check_data.face_list[0];
                console.log(check_data);
                //检查睁眼闭眼
                var eye_status=check_data.eye_status;
                if(eye_status.left_eye>0.1 || eye_status.right_eye>0.1) {
                    var msg="未闭眼拍照";
                    th.go_error(msg);
                    return false;
                }
                //检查是不是有带眼镜
                if(check_data.glasses.type!='none'){
                    var msg="有佩戴眼镜";
                    th.go_error(msg);
                    return false;
                }
                //检查头部的角度
                var angle=check_data.angle;
                if(Math.abs(angle.Pitch)>20) {
                    var msg="头部的俯仰角度过大";
                    th.go_error(msg);
                    return false;
                }
                if(Math.abs(angle.Roll)>20) {
                    var msg="头部的平面角度过大";
                    th.go_error(msg);
                    return false;
                }
                if(Math.abs(angle.Yaw)>20) {
                    var msg="头部的左右角度过大";
                    th.go_error(msg);
                    return false;
                }
                var quality=check_data.quality;
                if(quality.blur>0.7){
                    var msg="图片模糊";
                    th.go_error(msg);
                    return false;
                }
                //开始检查遮挡
                var occlusion=check_data.quality.occlusion;
                if(occlusion.left_eye >0.6){
                    var msg="左眼被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(occlusion.right_eye>0.6){
                    var msg="右眼被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(occlusion.nose>0.7){
                    var msg="鼻子被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(occlusion.mouth>0.7){
                    var msg="嘴巴被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(occlusion.left_cheek >0.8){
                    var msg="左脸颊被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(occlusion.right_cheek  >0.8){
                    var msg="右脸颊被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(occlusion.chin_contour   >0.6){
                    var msg="下巴颊被遮挡";
                    th.go_error(msg);
                    return false;
                }
                if(quality.illumination<40){
                    var msg="图像光照不足";
                    th.go_error(msg);
                    return false;
                }
                if(quality.completeness=0){
                    var msg="人脸未显示完整";
                    th.go_error(msg);
                    return false;
                }
                //-- 开始调用分析的接口 --
                var face_token=check_data.face_token;
                var data={
                    "image": face_token,
                    "face_field": "color,smooth,acnespotmole,wrinkle,eyesattr,blackheadpore,skinface,skinquality",
                    "image_type": "FACE_TOKEN"
                }
                th.analysis_img(data);
            }else{
                var msg=th.data.err_map[res.data.code+""];
                th.go_error(msg);
            }
        })
    },
    
    //跳转到错误页面
    go_error:function (msg) {
        let url = "/packageD/pages/AI-test-skin/fail_result/fail_result?msg="+msg; //跳到分析页
        wx.redirectTo({
            url: url
        });
    },

    analysis_img:function (data) {
        var th=this;
        //开始调用接口,进行分析
        this.json_post("/api/weshop/baidubce/face/skinAnalyze",data,function (res) {
            if(res.data.code==0){
                getApp().globalData.face_result=res.data.data;
                var req_data={
                    store_id:setting.stoid,
                    user_id:getApp().globalData.user_id,
                    img:getApp().globalData.face_img,
                    sourceimg:getApp().globalData.face_SourceImg,
                    addtime:ut.gettimestamp(),
                    resultjson:JSON.stringify(res.data.data),
                    detectjson:JSON.stringify(th.data.check_data)
                }
                //要保存一下分析的结果
                th.json_post("/api/weshop/face/storeSkinface/save",req_data,function (res) {
                    if(res.data.code==0){
                        let url = "/packageD/pages/AI-test-skin/success_result/success_result?id="+res.data.data.id; //跳到分析页
                        wx.redirectTo({
                            url: url
                        })
                    }else{
                        getApp().showWarning("保存失败");
                    }
                })
            }else{
                var msg=th.data.err_map[res.data.code+""];
                let url = "/packageD/pages/AI-test-skin/fail_result/fail_result?msg="+msg; //跳到分析页
                wx.redirectTo({
                  url: url
                })
            }
        })
    },

    /**
     * @param {Object} url    //url地址
     * @param {Object} data   //data入参
     * @param {Object} succ   //成功的回调
     * @param {Object} fail   //失败的回调
     */
    json_post:function(url,data,succ,fail,mathod){
        var Mathod="POST";
        if (mathod) Mathod = mathod;
        if(url.indexOf("http")==-1) url=getApp().globalData.setting.url +url;
        var str = JSON.stringify(data);
        wx.request({
            url: url,
            data: str,
            method: Mathod,
            header: {
                'content-type': 'application/json'
            },// 设置请求的 header
            success: function (res) {succ(res); },
            fail:function(res){fail(res);}
        })
    },


})