add ocr_aliyun.js
Browse files- src/WeChatOcrCpp/ocr_aliyun.js +113 -0
src/WeChatOcrCpp/ocr_aliyun.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
(async () => {
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
async function ocr(imgData) {
|
| 5 |
+
// const api = 'https://gjbsb.market.alicloudapi.com/ocrservice/advanced';
|
| 6 |
+
const api = 'http://127.0.0.1:7788/wechatocr';
|
| 7 |
+
const appCode = ``;
|
| 8 |
+
const appSecret = ``;
|
| 9 |
+
|
| 10 |
+
let request = require('request')
|
| 11 |
+
|
| 12 |
+
const data = await new Promise((resolve, reject) => {
|
| 13 |
+
request.post({
|
| 14 |
+
url: api,
|
| 15 |
+
timeout: 1000 * 60 * 2,
|
| 16 |
+
headers: {
|
| 17 |
+
"Authorization": `APPCODE ${appCode}`,
|
| 18 |
+
"Content-Type": "application/json;charset=UTF-8"
|
| 19 |
+
},
|
| 20 |
+
body: JSON.stringify({
|
| 21 |
+
img: imgData,
|
| 22 |
+
prob: true,
|
| 23 |
+
charInfo: true,
|
| 24 |
+
table: true,
|
| 25 |
+
sortPage: true,
|
| 26 |
+
NeedRotate:true
|
| 27 |
+
})
|
| 28 |
+
}, (error, response, body) => {
|
| 29 |
+
if (error) {
|
| 30 |
+
console.log('#####ERROR: aliyun ocr fail.');
|
| 31 |
+
console.log(error);
|
| 32 |
+
resolve( [null, error] )
|
| 33 |
+
|
| 34 |
+
// reject(error);
|
| 35 |
+
}
|
| 36 |
+
else {
|
| 37 |
+
if (response.statusCode != 200) {
|
| 38 |
+
return resolve( [null, {"error_code":response.statusCode, "error_msg":body}] )
|
| 39 |
+
}
|
| 40 |
+
let aliResult = null;
|
| 41 |
+
try {
|
| 42 |
+
aliResult = JSON.parse(body);
|
| 43 |
+
} catch (ex) {
|
| 44 |
+
console.log(ex.message);
|
| 45 |
+
console.log(response.statusCode);
|
| 46 |
+
console.log(error);
|
| 47 |
+
console.log(body);
|
| 48 |
+
}
|
| 49 |
+
resolve([aliResult, null]);
|
| 50 |
+
}
|
| 51 |
+
})
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
if (data.error_code !== undefined && data.error_code !== null) {
|
| 55 |
+
return [ null, {"code":data.error_code, "msg":data.error_msg} ]
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return [ data, null ]
|
| 59 |
+
|
| 60 |
+
// //JSON写入
|
| 61 |
+
// fs.writeFileSync(jsonPath, JSON.stringify(data));
|
| 62 |
+
// //图片写入
|
| 63 |
+
// fs.writeFileSync(imgPath, imgData);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
let fs = require('fs')
|
| 70 |
+
let md5 = require('md5')
|
| 71 |
+
|
| 72 |
+
let bytes = fs.readFileSync("no_think_more.png") // 1032.jpg 'binary'
|
| 73 |
+
let buf = Buffer.from(bytes)
|
| 74 |
+
let m5 = md5(buf)
|
| 75 |
+
let b64 = buf.toString('base64')
|
| 76 |
+
|
| 77 |
+
let [ re, err ] = await ocr(b64)
|
| 78 |
+
|
| 79 |
+
let json = {
|
| 80 |
+
md5: m5,
|
| 81 |
+
imgData: b64,
|
| 82 |
+
guid: '73ffc72d-bfce-43bc-9e90-0007605c6fdb',
|
| 83 |
+
bookNO: 'lrx333',
|
| 84 |
+
imgName: '1032.jpg',
|
| 85 |
+
originImgData: b64
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// json = {
|
| 89 |
+
// md5: '7468efae7d7ab7333d0197a8ca1bf32c',
|
| 90 |
+
// imgData: 'hasTest',
|
| 91 |
+
// guid: 'abc346eb-aeb1-4759-b885-68052ec34810',
|
| 92 |
+
// bookNO: 'lrx333',
|
| 93 |
+
// imgName: '1032.jpg'
|
| 94 |
+
// }
|
| 95 |
+
|
| 96 |
+
let bent = require('bent')
|
| 97 |
+
let formurlencoded = require('form-urlencoded')
|
| 98 |
+
|
| 99 |
+
let formurlencoded_body = formurlencoded(json)
|
| 100 |
+
|
| 101 |
+
let post = bent('http://webapi.xfyun.cn', 'POST', 'json', 200)
|
| 102 |
+
// let post = bent('http://127.0.0.1:11112', 'POST', 'json', 200)
|
| 103 |
+
let response = await post('/v1/service/v1/ocr/general', json)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
data = JSON.parse(response).data.test
|
| 108 |
+
|
| 109 |
+
let s = JSON.stringify(response)
|
| 110 |
+
|
| 111 |
+
console.log(response)
|
| 112 |
+
|
| 113 |
+
})()
|