nodej,ocr识别图片中的文字

  |  

使用的是阿里云文档

在这里插入图片描述

获取到 对应的 accessKeyId 和 accessKeySecret

新建 test.js 和 green-nodejs-invoker.js 文件 这两个文件要在同一目录下,不然引入报错

test.js 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const uuid = require('node-uuid');
const greenNodejs = require('./green-nodejs-invoker.js');

// 默认参数
const accessKeyId = '<your accessKeyId>';
const accessKeySecret = '<your accessKeySecret>';
const greenVersion = '2017-01-12';
const hostname = 'green.cn-shanghai.aliyuncs.com';
const path = '/green/image/scan'; // 需要请求的接口
const clientInfo = {
'ip': '127.0.0.1',
};


// 测试调用
(async () => {
// 请求体,根据需要调用相应的算法
let requestBody = JSON.stringify({
'bizType': 'Green',
'scenes': [ 'ocr' ], // 指定检测场景 ocr 图片文字识别 //支持多种形式
'tasks': [{ // 指定检测对象,JSON数组中的每个元素是一个OCR图文检测任务结构体(image表)。最多支持10个元素,即对10张图片进行识别
'dataId': uuid.v1(),
'url': 'https://dss2.bdstatic.com/8_V1bjqh_Q23odCf/pacific/1939251838.png',
}, { // 支持多个参数
'dataId': uuid.v1(),
'url': 'https://dss3.baidu.com/-rVXeDTa2gU2pMbgoY3K/it/u=842202544,1222336704&fm=202&mola=new&crop=v1',
}],
});

let bizCfg = {
'accessKeyId': accessKeyId,
'accessKeySecret': accessKeySecret,
'path': path,
'clientInfo': clientInfo,
'requestBody': requestBody,
'hostname': hostname,
'greenVersion': greenVersion,
};

let result = await greenNodejs.getImageInfo(bizCfg);
console.log('result==========>', result);
})();

green-nodejs-invoker.js 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const http = require('http');
const crypto = require('crypto');
const uuid = require('node-uuid');

module.exports = {
async getImageInfo(bizCfg, callback) {
try {
let path = bizCfg['path'];
let clientInfo = bizCfg['clientInfo'];
let requestBody = bizCfg['requestBody'];
let greenVersion = bizCfg['greenVersion'];
let hostname = bizCfg['hostname'];
let gmtCreate = new Date().toUTCString();
let md5 = crypto.createHash('md5');
// 请求头
let requestHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-MD5': md5.update(requestBody).digest().toString('base64'),
'Date': gmtCreate,
'x-acs-version': greenVersion,
'x-acs-signature-nonce': uuid.v1(),
'x-acs-signature-version': '1.0',
'x-acs-signature-method': 'HMAC-SHA1',
};

// 对请求的签名
signature(requestHeaders, bizCfg);

// HTTP请求设置
let options = {
'hostname': hostname,
'port': 80,
'path': encodeURI(path + '?clientInfo=' + JSON.stringify(clientInfo)),
'method': 'POST',
'headers': requestHeaders,
};

let result = await new Promise((resolve, reject) => {
let chunks = [];
let req = http.request(options, function(res) {
// res.setEncoding('utf8');
res.on('error', function(error) {
console.log(error);
});
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
let data = Buffer.concat(chunks).toString();
return resolve(data);
});
});

req.on('error', function(e) {
console.log('err===>', e);
return reject(e);
});
// 将数据写入请求体
req.write(requestBody);
req.end();
});

return result;
} catch (error) {
return error;
}
},
};


// 签名
function signature(requestHeaders, bizCfg) {
let accessKeyId = bizCfg['accessKeyId'];
let accessKeySecret = bizCfg['accessKeySecret'];
let path = bizCfg['path'];
let clientInfo = bizCfg['clientInfo'];

let signature = [];
signature.push('POST\n');
signature.push('application/json\n');
signature.push(requestHeaders['Content-MD5'] + '\n');
signature.push('application/json\n');
signature.push(requestHeaders['Date'] + '\n');
signature.push('x-acs-signature-method:HMAC-SHA1\n');
signature.push('x-acs-signature-nonce:' + requestHeaders['x-acs-signature-nonce'] + '\n');
signature.push('x-acs-signature-version:1.0\n');
signature.push('x-acs-version:2017-01-12\n');
signature.push(path + '?clientInfo=' + JSON.stringify(clientInfo));


let authorization = crypto.createHmac('sha1', accessKeySecret)
.update(signature.join(''))
.digest()
.toString('base64');

requestHeaders.Authorization = 'acs ' + accessKeyId + ':' + authorization;
}

返回结果如下:
在这里插入图片描述

文章目录