@anytestforpm/ocr 1.3.8 → 1.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/ocr.js +19 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Install
|
|
2
2
|
npm i @anytestforpm/ocr
|
|
3
3
|
# Language Support
|
|
4
|
-
支持地區為: VN IN ID BR MM PK TH PH NG
|
|
4
|
+
支持地區為: VN IN ID BR MM PK TH PH NG BD TR US
|
|
5
5
|
# Usage Example
|
|
6
6
|
1.初始化OCR的辨識語系及API路由設定,就會自動取得后台設定檔
|
|
7
7
|
```javascript
|
|
@@ -23,6 +23,7 @@ mounted() {
|
|
|
23
23
|
import { recognizeOCR, RESULT_TYPE } from '@anytestforpm/ocr';
|
|
24
24
|
|
|
25
25
|
try {
|
|
26
|
+
//代入要辨識的img url
|
|
26
27
|
let ocrResData = await recognizeOCR(imgUrl);
|
|
27
28
|
//取得后台設定檔失敗
|
|
28
29
|
if (ocrResData.type === RESULT_TYPE.CONFIG_INVALID) {
|
|
@@ -56,5 +57,5 @@ try {
|
|
|
56
57
|
3.recognizeOCR(imgUrl) ,入參imgUrl
|
|
57
58
|
let ocrResData = await recognizeOCR(imgUrl);
|
|
58
59
|
mixins.js
|
|
59
|
-

|
|
60
61
|
|
package/ocr.js
CHANGED
|
@@ -15,7 +15,10 @@ const OCR_LANGUAGE_MAP = {
|
|
|
15
15
|
PK: "eng",
|
|
16
16
|
TH: "tha+eng",
|
|
17
17
|
PH: "eng",
|
|
18
|
-
NG: "eng"
|
|
18
|
+
NG: "eng",
|
|
19
|
+
BD: "ben",
|
|
20
|
+
TR: "tur",
|
|
21
|
+
US: "eng"
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
const setResponseData = (val) => {
|
|
@@ -46,12 +49,12 @@ export const hello = (val) => {
|
|
|
46
49
|
* @description initialization ocr config
|
|
47
50
|
* @param {String} region area of config
|
|
48
51
|
*/
|
|
49
|
-
export const initOCR = (region,apiHost) => {
|
|
52
|
+
export const initOCR = (region, apiHost) => {
|
|
50
53
|
try {
|
|
51
54
|
//sign
|
|
52
|
-
let timestamp = Math.fround(Date.now()/1000);
|
|
53
|
-
let hashString = md5(region+"."+timestamp).toUpperCase();
|
|
54
|
-
|
|
55
|
+
let timestamp = Math.fround(Date.now() / 1000);
|
|
56
|
+
let hashString = md5(region + "." + timestamp).toUpperCase();
|
|
57
|
+
|
|
55
58
|
//get config
|
|
56
59
|
configData = null;
|
|
57
60
|
axios({
|
|
@@ -69,10 +72,10 @@ export const initOCR = (region,apiHost) => {
|
|
|
69
72
|
if (res.status === 200 && res.data.config.enable) {
|
|
70
73
|
configData = res.data;
|
|
71
74
|
console.log("Initialization successful");
|
|
72
|
-
}else
|
|
75
|
+
} else if (res.status === 200 && !res.data.config.enable) {
|
|
73
76
|
configData = res.data;
|
|
74
77
|
console.log("OCR service disabled");
|
|
75
|
-
}else {
|
|
78
|
+
} else {
|
|
76
79
|
console.log("Initialization failed");
|
|
77
80
|
}
|
|
78
81
|
})
|
|
@@ -90,7 +93,7 @@ export const initOCR = (region,apiHost) => {
|
|
|
90
93
|
* @param {String} ImageUrl image url
|
|
91
94
|
*/
|
|
92
95
|
export const recognizeOCR = async (ImageUrl) => {
|
|
93
|
-
|
|
96
|
+
|
|
94
97
|
//初始化失敗
|
|
95
98
|
if (!configData) {
|
|
96
99
|
return new Promise((resolve, reject) => {
|
|
@@ -105,7 +108,7 @@ export const recognizeOCR = async (ImageUrl) => {
|
|
|
105
108
|
resolve(setResponseData(RESULT_TYPE.CONFIG_DISABLED));
|
|
106
109
|
});
|
|
107
110
|
}
|
|
108
|
-
|
|
111
|
+
|
|
109
112
|
let ocr_setTimeout = null;
|
|
110
113
|
let startTime = new Date();
|
|
111
114
|
let worker = await createWorker(
|
|
@@ -129,12 +132,12 @@ export const recognizeOCR = async (ImageUrl) => {
|
|
|
129
132
|
try {
|
|
130
133
|
await worker.loadLanguage(OCR_LANGUAGE_MAP[configData.region]);
|
|
131
134
|
await worker.initialize(OCR_LANGUAGE_MAP[configData.region]);
|
|
132
|
-
|
|
135
|
+
|
|
133
136
|
//OCR辨識結果的內文
|
|
134
137
|
let result = await worker.recognize(ImageUrl);
|
|
135
138
|
let resText = String(result.data.text);
|
|
136
139
|
if (configData.region === "TH") {
|
|
137
|
-
resText = resText.replace(/\s+/g,"");
|
|
140
|
+
resText = resText.replace(/\s+/g, "");
|
|
138
141
|
}
|
|
139
142
|
console.log("OCR Source Text >>> ", resText);
|
|
140
143
|
|
|
@@ -148,11 +151,11 @@ export const recognizeOCR = async (ImageUrl) => {
|
|
|
148
151
|
|
|
149
152
|
//比對合法的關鍵字
|
|
150
153
|
let validKeyWords = configData.keywords.valid.filter((item) => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
if (configData.region === "TH") {
|
|
155
|
+
return resText.search(item.replace(/\s+/g, "")) >= 0
|
|
156
|
+
} else {
|
|
157
|
+
return resText.search(item) >= 0
|
|
158
|
+
}
|
|
156
159
|
});
|
|
157
160
|
console.log("search valid keywords result >>> ", validKeyWords);
|
|
158
161
|
if (validKeyWords.length >= configData.config.verify_in_counts) {
|
|
@@ -174,7 +177,6 @@ export const recognizeOCR = async (ImageUrl) => {
|
|
|
174
177
|
return;
|
|
175
178
|
}
|
|
176
179
|
}
|
|
177
|
-
|
|
178
180
|
|
|
179
181
|
//驗證失敗 沒有找到任何匹配的條件
|
|
180
182
|
console.log("verification failed , keywords not found");
|
|
@@ -184,7 +186,6 @@ export const recognizeOCR = async (ImageUrl) => {
|
|
|
184
186
|
reject(setResponseData(RESULT_TYPE.KEYWORDS_EXCEPTION));
|
|
185
187
|
console.error(error);
|
|
186
188
|
} finally {
|
|
187
|
-
|
|
188
189
|
clearTimeout(ocr_setTimeout);
|
|
189
190
|
await worker.terminate();
|
|
190
191
|
worker = null;
|