| const { json } = require('stream/consumers');
|
|
|
| (async () => {
|
|
|
|
|
|
|
| async function sleep(ms) {
|
| return new Promise((resolve) => {
|
| setTimeout(resolve, ms)
|
| })
|
| }
|
|
|
|
|
| async function ocr(imgData) {
|
|
|
| let fs = require('fs')
|
|
|
| let config = fs.readFileSync("../WpfEditor/config.json", {encoding: "utf-8"})
|
| config = JSON.parse(config)
|
|
|
| const api = config.aliOCR.api
|
|
|
|
|
| const appCode = config.aliOCR.appCode;
|
| const appSecret = config.aliOCR.appSecret;
|
|
|
| let request = require('request')
|
|
|
| const data = await new Promise((resolve, reject) => {
|
| request.post({
|
| url: api,
|
| timeout: 1000 * 60 * 60 * 24,
|
| headers: {
|
| "Authorization": `APPCODE ${appCode}`,
|
| "Content-Type": "application/json;charset=UTF-8"
|
| },
|
| body: JSON.stringify({
|
| img: imgData,
|
| prob: true,
|
| charInfo: true,
|
| table: true,
|
| sortPage: true,
|
| NeedRotate: true
|
| })
|
| }, (error, response, body) => {
|
| if (error) {
|
| console.log('#####ERROR: aliyun ocr fail.');
|
| console.log(error);
|
| resolve([null, error])
|
|
|
|
|
| }
|
| else {
|
| if (response.statusCode != 200) {
|
| console.log('#####ERROR: aliyun ocr fail');
|
| return resolve([null, { "error_code": response.statusCode, "error_msg": body }])
|
| }
|
| let aliResult = null;
|
| try {
|
| aliResult = JSON.parse(body);
|
| } catch (ex) {
|
| console.log('#####ERROR: aliyun ocr fail.');
|
| console.log(ex.message);
|
| console.log(response.statusCode);
|
| console.log(error);
|
| console.log(body);
|
| }
|
| resolve([aliResult, null]);
|
| }
|
| })
|
| });
|
|
|
| if (data[0] != null) {
|
| console.log(`one task done.`)
|
| }
|
|
|
| if (data.error_code !== undefined && data.error_code !== null) {
|
| return [null, { "code": data.error_code, "msg": data.error_msg }]
|
| }
|
|
|
| return [data, null]
|
|
|
|
|
|
|
|
|
|
|
| }
|
|
|
|
|
| let fs = require('fs')
|
|
|
|
|
| let bytes = fs.readFileSync("0170.jpg")
|
| let buf = Buffer.from(bytes)
|
|
|
| let b64 = buf.toString('base64')
|
|
|
|
|
| for (let i = 0; i < 5000; i++) {
|
| console.log(`Send ${i}`)
|
| await ocr(b64);
|
| await sleep(300);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| })() |