Skip to content

Commit

Permalink
feat(app): 将OCR识别模块整合打包,修复OCR路径存在空格执行失败的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Mar 13, 2023
1 parent 184e0e9 commit 9609af6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"cSpell.words": [
"jsdom",
"axios",
"historychange",
"isclose",
"isstop",
"jsdom",
"nomove",
"ondefault",
"ondone",
"onhistorychange",
"onprevent",
"onrender",
"onrun",
"sider",
"Tesseract",
"traineddata",
"webrtc",
Expand All @@ -35,5 +36,8 @@
/** 鼠标滚动速度 */
"editor.mouseWheelScrollSensitivity": 2,
/** 不生成 @return 的 jsdoc */
"typescript.suggest.jsdoc.generateReturns": false
"typescript.suggest.jsdoc.generateReturns": false,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
4 changes: 2 additions & 2 deletions packages/app/electron.builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"extraFiles": [
{
"from": "./bin",
"to": "./bin"
"from": "./bin/ocr.zip",
"to": "./bin/ocr.zip"
}
],

Expand Down
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ocsjs/app",
"version": "2.0.4",
"version": "2.0.5",
"description": "app package of ocs",
"main": "./lib/index.js",
"types": "./lib/types.d.ts",
Expand Down
14 changes: 11 additions & 3 deletions packages/app/src/tasks/init.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { existsSync, mkdirSync } from 'fs';
import { appStore, store } from '../store';
import { valid, coerce, clean, gt, SemVer } from 'semver';
import defaultsDeep from 'lodash/defaultsDeep';
import { Logger } from '../logger';

const logger = Logger('store-init');

/**
* 初始化配置
*/
export async function initStore() {
const version = store.store.version;
logger.log('version', version);

// 是否需要初始化
if (typeof version === 'string') {
Expand All @@ -23,13 +27,17 @@ export async function initStore() {
store.store = defaultsDeep(store.store, appStore);
}
} else {
const render = store.store.render || {};
const render = store.store.render ? JSON.parse(JSON.stringify(store.store.render)) : {};
appStore.render = render;
// 初始化设置
store.store = appStore;
// 恢复 render 数据
store.store.render = render;

logger.log('store', store.store);
}

// 强制更新 exe-path
store.store.paths['exe-path'] = app.getPath('exe');

if (!existsSync(store.store.paths.userDataDirsFolder)) {
mkdirSync(store.store.paths.userDataDirsFolder, { recursive: true });
}
Expand Down
15 changes: 10 additions & 5 deletions packages/app/src/tasks/ocr.unzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { Logger } from '../logger';
import { getProjectPath, unzip } from '../utils';
import path from 'path';
import fs from 'fs';
import { app } from 'electron';
import { store } from '../store';

const logger = Logger('ocr-unzip');

export let ocr_path = '';

export async function ocrUnzip() {
logger.log('ocr-unzip', Date.now());
const parentFolder = app.isPackaged ? path.join(app.getPath('exe'), '../') : getProjectPath();
try {
const ocr = path.join(getProjectPath(), './bin/ocr.zip');
const dest = path.join(getProjectPath(), './bin/ocr');
if (!fs.existsSync(dest)) {
await unzip(ocr, path.join(getProjectPath(), './bin/ocr'));
logger.log('ocr-unzip-finish', Date.now());
const ocr = path.join(parentFolder, './bin/ocr.zip');
ocr_path = path.join(store.store.paths.downloadFolder, './ocr');
if (!fs.existsSync(ocr_path)) {
await unzip(ocr, ocr_path);
logger.log('ocr-unzip-finish', Date.now(), ocr_path);
}
} catch (err) {
logger.log('ocr-unzip-error', err);
Expand Down
11 changes: 8 additions & 3 deletions packages/app/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ export function zip(input: string, output: string) {
*/

export function unzip(input: string, output: string) {
return new Promise<void>((resolve) => {
return new Promise<void>((resolve, reject) => {
const zip = new AdmZip(input);
zip.extractAllTo(output, true);
resolve();
zip.extractAllToAsync(output, true, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

Expand Down
19 changes: 13 additions & 6 deletions packages/app/src/utils/ocr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { writeFile, mkdirSync, existsSync, rmSync, writeFileSync } from 'fs';
import path from 'path';
import { randomUUID } from 'crypto';
import child_process from 'child_process';
import { getProjectPath } from '.';
import { ocr_path } from '../tasks/ocr.unzip';
import { Logger } from '../logger';

const logger = Logger('ocr');

/**
* 使用 ddddocr 进行验证码识别
Expand All @@ -12,13 +14,16 @@ import { getProjectPath } from '.';
export function ocr(base64: string) {
return new Promise<string>((resolve, reject) => {
const uuid = randomUUID();
const img_cache = path.join(getProjectPath(), './bin/ocr/img_cache');
const img_cache = path.join(ocr_path, './img_cache');
if (!existsSync(img_cache)) {
mkdirSync(img_cache, { recursive: true });
}
const img = path.join(img_cache, uuid + '.png');
writeFile(img, base64, 'base64', () => {
const cmd = [path.join(getProjectPath(), './bin/ocr/ocr.exe'), '--ocr', img].join(' ');
// 要使用 "" 去包裹路径,防止出现空格
const cmd = [`"${path.join(ocr_path, './ocr.exe')}"`, '--ocr', `"${img}"`].join(' ');
logger.log('cmd', cmd);

child_process.exec(cmd, (err, stdout, stderr) => {
if (err || stderr) {
reject(err || stderr);
Expand Down Expand Up @@ -48,7 +53,7 @@ export function det(det_target_base64: string, det_bg_base64: string) {
target_y: number;
target: [number, number, number, number];
}>((resolve, reject) => {
const img_cache = path.join(getProjectPath(), './bin/ocr/img_cache');
const img_cache = path.join(ocr_path, './img_cache');
if (!existsSync(img_cache)) {
mkdirSync(img_cache, { recursive: true });
}
Expand All @@ -57,7 +62,9 @@ export function det(det_target_base64: string, det_bg_base64: string) {
writeFileSync(img1, det_target_base64, 'base64');
writeFileSync(img2, det_bg_base64, 'base64');

const cmd = [path.join(getProjectPath(), './bin/ocr/ocr.exe'), '--det-target', img1, '--det-bg', img2].join(' ');
const cmd = [`"${path.join(ocr_path, './ocr.exe')}"`, '--det-target', `"${img1}"`, '--det-bg', `"${img2}"`].join(
' '
);

child_process.exec(cmd, (err, stdout, stderr) => {
if (err || stderr) {
Expand All @@ -78,5 +85,5 @@ export function det(det_target_base64: string, det_bg_base64: string) {

/** 判断是否能够进行验证码识别 */
export function canOCR() {
return existsSync(path.join(getProjectPath(), './bin/ocr/ocr.exe'));
return existsSync(path.join(ocr_path, './ocr.exe'));
}

0 comments on commit 9609af6

Please sign in to comment.