【AI】图像识别-物体检测-百度AI-EasyDL-NodeJS

var https = require('https')
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
var request = require('request')
var fs = require('fs'); // 引入fs模块

//访问端口
var port = 9999

//接受post数据
app.use(bodyParser.json({
    "limit": "100mb"
}));
app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true
}));

//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
var qs = require('querystring');

const param = qs.stringify({
    'grant_type': 'client_credentials',
    'client_id': 'XXXXX',
    'client_secret': 'XXXXXX'
});


function log(data)
{
    fs.writeFile('imgdata.txt', data, {
        'flag': 'a'
    }, function (err) {
        if (err) {
            throw err;
        }

        console.log('Hello.');

    });
}

app.post('/XXXXDetectV1', function (req, res) {

    var imgdata = req.body.imgdata; //body query
    // console.log(imgdata) 
    imgdata = imgdata.replace("data:image/png;base64,","")
    log(imgdata)
    https.get({
            hostname: 'aip.baidubce.com',
            path: '/oauth/2.0/token?' + param,
            agent: false
        },
        function (res2) {
            // 在标准输出中查看运行结果
            // res2.pipe(process.stdout);
            // console.log('statusCode:', res2.statusCode);
            // console.log('headers:', res2.headers);

            res2.on('data', (d) => {
                console.log("认证成功")
                // console.log('' + d); //将buffer转为字符串或者使用d.toString()
                let b = JSON.parse('' + d); //将buffer转成JSON
                // console.log(b)
                var access_token = b["access_token"]
                console.log(access_token)
                // res.end(JSON.stringify(b));
                data = {
                    'image': imgdata
                }
     
                request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/XXXXDetectV1" + "?access_token=" + access_token
 
                request({
                    url: request_url,
                    method: "POST",
                    json: true,
                    headers: {
                        "content-type": "application/json",
                    },
                    body: data
                }, function (error, response, body) {
                    console.log(error)
                    // console.log(response)
                    // console.log(body)
                    if (!error && response.statusCode == 200) {
                        console.log(body) // 请求成功的处理逻辑
                        res.end(JSON.stringify(body));
                    }
                });


            });

        }
    );

})


//启动服务---------------------------------------------------------------------------------------------
var server = app.listen(port, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例,访问地址为 http://%s:%s", host, port)

})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值