步骤一、百度文字识别控制台(地址:https://ai.baidu.com/tech/ocr/general)创建应用并获取appid、appkey、appsecret,领取免费资源包;
步骤二、在node工程中package.json中添加依赖
"baidu-aip-sdk": "4.15.4"
步骤三、node版本调整为 v6.17.1
sudo n 6.17.1
步骤四、安装文字识别库
npm install
步骤五、对应方法内调用ocr库完成文字识别
var AipOcrClient = require("baidu-aip-sdk").ocr;
// 设置APPID/AK/SK
var APP_ID = "步骤一中获取到的APP_ID";
var API_KEY = "步骤一中获取到的API_KEY";
var SECRET_KEY = "步骤一中获取到的SECRET_KEY";
// 新建一个对象,建议只保存一个对象调用服务接口
var client = new AipOcrClient(APP_ID, API_KEY, SECRET_KEY);
var HttpClient = require("baidu-aip-sdk").HttpClient;
// 设置request库的一些参数,例如代理服务地址,超时时间等
// request参数请参考 https://github.com/request/request#requestoptions-callback
HttpClient.setRequestOptions({timeout: 5000});
// 也可以设置拦截每次请求(设置拦截后,调用的setRequestOptions设置的参数将不生效),
// 可以按需修改request参数(无论是否修改,必须返回函数调用参数)
// request参数请参考 https://github.com/request/request#requestoptions-callback
HttpClient.setRequestInterceptor(function(requestOptions) {
// 查看参数
console.log(requestOptions)
// 修改参数
requestOptions.timeout = 5000;
// 返回参数
return requestOptions;
});
var fs = require('fs');
var filePath = "~/Downloads/WechatIMG398.jpeg";
var image = fs.readFileSync(filePath).toString("base64");
// 调用通用文字识别(高精度版)
client.accurateBasic(image).then(function(result) {
console.log(JSON.stringify(result));
}).catch(function(err) {
// 如果发生网络错误
console.log(err);
});
打印结果
{"words_result":[{"words":"韵达"},{"words":"全国服务热线:95546"},{"words":"标准"},{"words":" www.yundaex.com"},{"words":"快递"},{"words":"2021/08/1911:11:15第71/88个"},{"words":"760Z051-0013"},{"words":"460*******53"},{"words":"富兴嘉城3栋"},{"words":"集长沙地区包"},{"words":"ω"},{"words":"*先生177*****226"},{"words":"收湖南省长沙市天心区"},{"words":"已验视"},{"words":"红包"},{"words":"扫一扫领百元"}],"words_result_num":23}
网友评论