图片识别文档:通用物体和场景识别_可识别10万多类常见物体和场景-百度AI开放平台

uni-app获取本地或拍照的图片:

 

问题1:

        转换base64格式使用了image-tools插件,这个插件只能转换本地的图片,在线图片无法处理

        插件地址image-tools - DCloud 插件市场

问题2:

        var [error,res]=await uni.request({})有跨域问题,需要配置反向代理

        manifest.json的源码视图里,添加以下代码

   "h5": {
        "devServer": {
            "proxy": {
                "/baiduApi": {
                    "target": "https://aip.baidubce.com/oauth/2.0/token",
                    "changeOrigin": true,
                    "pathRewrite": {
                        "^/baiduApi": ""
                    }
                }
            }
        }
    },

        

代码:

// 从相机获取一张照片,并展示出来
        takePhoto(){
            uni.chooseImage({
                count:1,
                success:(res)=>{
                    // console.log(res);  //照片的临时路径
                    this.img=res.tempFilePaths[0]
                    console.log(this.img);
                    // 把获取的照片转换成百度图像识别API需要的base64格式
                    pathToBase64(this.img)
                      .then(base64 => {
                        // console.log(base64)
                            // console.log(base64.split(',')[1]);
                            this.imgClassify(base64.split(',')[1])
                      })
                      .catch(error => {
                        console.error(error)
                      })
                }
            })
        },
        async imgClassify(b64){
            // 获取access_token,会有跨域问题,配置在manifest.json的源码视图里添加的"h5"
            var [error,res]=await uni.request({
                //baseURL是/baiduApi,代替原本的https://aip.baidubce.com/oauth/2.0/token     
                url:"/baiduApi/oauth/2.0/token?grant_type=client_credentials&                          client_id=ayCfVQUREYQGQwibLHF5EzNo&                client_secret=CBcxzFAt2j                        ylcoKFAQqvalILqQz9ZjoE&"                
                    })
            console.log(res.data.access_token)
            var access_token=res.data.access_token
            
            // 正式请求图像识别的接口
            var [error,res]=await uni.request({     
                url:"/baiduApi/rest/2.0/image-classify/v2/advanced_general",
                method:'POST',
                data:{
                    image:b64,
                    access_token:access_token
                },
                header:{
                    "Content-Type":"application/x-www-form-urlencoded"
                }
            })
            //如果出现Open api qps request limit reached,是没有领取免费额度,在百度创建应用页//面可以领取
            console.log(res); //res.data.result里就有识别出来的结果
            this.parseResult(res.data.result)
        },
        parseResult(result){
            let itemList=[]
            for(var i=0;i<result.length;i++){
                itemList.push(result[i].keyword+' '+result[i].score)
            }
            uni.showActionSheet({ //从底部向上弹出操作菜单
                itemList:itemList,
                success: (res) => {
                    console.log(res);
                    console.log(result[res.tapIndex].keyword);
                }
            })
        }
    }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐