Skip to content

Commit

Permalink
fix: 优化 OCR 数据加载问题
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 27, 2022
1 parent 6bb3a29 commit ed958a9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/node-fetch": "^2.6.1",
"axios": "^0.25.0",
"chalk": "4.1.0",
"dom-to-image": "^2.6.0",
"html2canvas": "^1.4.1",
"lodash": "^4.17.21",
"node-fetch": "^2.6.7",
Expand Down Expand Up @@ -44,6 +45,7 @@
},
"homepage": "https://github.com/enncy/online-course-script#readme",
"devDependencies": {
"@types/dom-to-image": "^2.6.4",
"@types/string-similarity": "^4.0.0",
"@types/tampermonkey": "^4.0.5",
"@vitejs/plugin-vue": "^1.6.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/cx/ExamSettingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ExamSettingPanel = defineComponent({
<input
type="number"
value={settings.period}
min="0"
min="3"
step="1"
onChange={(e: any) => {
settings.period = e.target.valueAsNumber;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/cx/StudySettingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const StudySettingPanel = defineComponent({
<input
type="number"
value={store.setting.cx.work.period}
min="0"
min="3"
step="1"
onChange={(e: any) => {
store.setting.cx.work.period = e.target.valueAsNumber;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/cx/WorkSettingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const WorkSettingPanel = defineComponent({
<input
type="number"
value={settings.period}
min="0"
min="3"
step="1"
onChange={(e: any) => {
settings.period = e.target.valueAsNumber;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/zhs/WorkSettingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const WorkSettingPanel = defineComponent({
type="number"
onChange={(e: any) => (settings.period = e.target.valueAsNumber)}
value={settings.period}
min="0"
min="3"
step="1"
></input>
</div>
Expand Down
18 changes: 8 additions & 10 deletions packages/core/src/core/ocr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import html2canvas from 'html2canvas';
import domtoimage from 'dom-to-image';

import Tesseract, { createWorker } from 'tesseract.js';

/**
Expand All @@ -7,8 +8,6 @@ import Tesseract, { createWorker } from 'tesseract.js';
* @see https://github.com/naptha/tesseract.js
*/
export class OCR {
/** 初始化语言 */
lang: string;
worker: Tesseract.Worker;
/**
* 默认加载语言
Expand All @@ -31,9 +30,8 @@ export class OCR {
letterSpacing: '8px'
}

constructor(lang?: string) {
this.lang = lang || OCR.DEFAULT_LANG;
this.worker = createWorker();
constructor(options: Partial<Tesseract.WorkerOptions> = {}) {
this.worker = createWorker(options);
}

/**
Expand All @@ -43,8 +41,8 @@ export class OCR {
async load(lang?: string) {
await this.worker.load();
// 加载语言
await this.worker.loadLanguage(lang || this.lang || OCR.DEFAULT_LANG);
await this.worker.initialize(lang || this.lang || OCR.DEFAULT_LANG);
await this.worker.loadLanguage(lang || OCR.DEFAULT_LANG);
await this.worker.initialize(lang || OCR.DEFAULT_LANG);
}

/**
Expand Down Expand Up @@ -78,9 +76,9 @@ export class OCR {
*/
async recognize(el: HTMLElement): Promise<string> {
// 图片转base64
const canvas = await html2canvas(el);
const base64 = await domtoimage.toPng(el);
// 识别
const { data: { text } } = await this.worker.recognize(canvas.toDataURL());
const { data: { text } } = await this.worker.recognize(base64);
return text.replace(/ /g, '');
}

Expand Down
19 changes: 9 additions & 10 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { store } from './script/index';
import { CommonScript } from './script/common';
import { ZHSScript } from './script/zhs';
import { CXScript } from './script/cx';
import { store } from './script/index';
import { ZHSScript } from './script/zhs';
import { start } from './start';

export * from './core/index';

/** 默认脚本列表 */
export const definedScripts = [CommonScript, ZHSScript, CXScript];

// @ts-ignore vite.define
const VERSION = process.env._VERSION_;

/**
* ocsjs
*/
export { store, start, VERSION };
export { store, start };

// @ts-ignore vite.define
export const VERSION = process.env._VERSION_;

/** 默认脚本列表 */
export const definedScripts = [CommonScript, CXScript, ZHSScript];
10 changes: 7 additions & 3 deletions packages/core/src/script/cx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ export const CXScript = defineScript({
/** 文字识别 */
const fonts = CXAnalyses.getSecretFont();
if (fonts.length) {
logger('debug', '文字识别启动');
const ocr = new OCR();
await ocr.load();
const ocr = new OCR({
langPath: 'https://cdn.ocs.enncy.cn/resources/tessdata'

});
logger('debug', '加载文字识别功能, 如果是初始化请耐心等待...');
await ocr.load();
logger('info', '文字识别功能加载成功');
for (const font of fonts) {
const text = await ocr.recognize(OCR.suit(font));
font.innerHTML = text;
}
logger('info', '文字识别完成');
}
}
}
Expand Down

0 comments on commit ed958a9

Please sign in to comment.