From 5c4622bea995b7832befdded6ab3c73a79042ee7 Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Sun, 23 Jun 2024 07:02:19 +0000 Subject: [PATCH] use js plugin system --- .github/workflows/build.yml | 69 +++------- .gitignore | 1 - Cargo.lock | 257 ------------------------------------ Cargo.toml | 12 -- info.json | 2 +- main.js | 24 ++++ src/lib.rs | 70 ---------- 7 files changed, 42 insertions(+), 393 deletions(-) delete mode 100644 .gitignore delete mode 100644 Cargo.lock delete mode 100644 Cargo.toml create mode 100644 main.js delete mode 100644 src/lib.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3a57863..683d299 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,74 +1,39 @@ name: Build on: push +permissions: write-all jobs: - check: + build: runs-on: ubuntu-latest - outputs: - name: ${{ steps.getname.outputs.NAME }} - icon: ${{ steps.getname.outputs.ICON }} steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 1 - - name: Install zip - run: sudo apt-get install -y zip jq + - name: Install jq + run: sudo apt-get install -y jq p7zip-full wget - id: getname run: | - echo "NAME=$(cat info.json | jq '.id' | tr -d '"')" >> "$GITHUB_OUTPUT" - echo "ICON=$(cat info.json | jq '.icon' | tr -d '"')" >> "$GITHUB_OUTPUT" - build-for-windows: - needs: check - strategy: - fail-fast: false - matrix: - platform: [windows-latest] - target: [x86_64-pc-windows-msvc] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 1 + echo "NAME=$(cat info.json | jq '.id' | tr -d '"')" >> "$GITHUB_ENV" + echo "ICON=$(cat info.json | jq '.icon' | tr -d '"')" >> "$GITHUB_ENV" - name: Download PaddleOCR-json run: | - powershell -c "invoke-webrequest -uri https://github.com/hiroi-sora/PaddleOCR-json/releases/download/v1.3.0/PaddleOCR-json_v.1.3.0.7z -outfile PaddleOCR-json_v.1.3.0.7z" - mkdir PPOCR + wget https://github.com/hiroi-sora/PaddleOCR-json/releases/download/v1.3.1/PaddleOCR-json_v.1.3.1.7z - name: extract-7z-action - uses: DuckSoft/extract-7z-action@v1.0 - with: - pathSource: PaddleOCR-json_v.1.3.0.7z - pathTarget: PPOCR - - name: Install Rust Stable - uses: dtolnay/rust-toolchain@stable - - name: install toolchain - run: | - rustup target add ${{ matrix.target }} - rustup toolchain install --force-non-host stable-${{ matrix.target }} - - name: Build - run: cargo build --release --target ${{ matrix.target }} - - name: Rename - run: move target/${{ matrix.target }}/release/plugin.dll ./plugin.dll + run: 7za x PaddleOCR-json_v.1.3.1.7z - uses: vimtor/action-zip@v1 with: - files: info.json ${{needs.check.outputs.icon}} plugin.dll PPOCR - dest: ${{needs.check.outputs.name}}.potext + files: info.json ${{ env.ICON }} main.js PaddleOCR-json_v.1.3.1 + dest: ${{ env.NAME }}.potext - name: Upload Artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: - name: ${{ matrix.target }} - path: "*.potext" + name: ${{ env.NAME }} + path: "${{ env.NAME }}.potext" if-no-files-found: error - - name: Wrap - if: startsWith(github.ref, 'refs/tags') - uses: vimtor/action-zip@v1 - with: - files: ${{needs.check.outputs.name}}.potext - dest: ${{ matrix.target }}.zip - name: Upload Release if: startsWith(github.ref, 'refs/tags') - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 with: - token: ${{ secrets.TOKEN }} - files: ${{ matrix.target }}.zip + token: ${{ secrets.GITHUB_TOKEN }} + files: "${{ env.NAME }}.potext" diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 1de5659..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 91b5911..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,257 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "dirs" -version = "5.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" -dependencies = [ - "dirs-sys", -] - -[[package]] -name = "dirs-sys" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" -dependencies = [ - "libc", - "option-ext", - "redox_users", - "windows-sys", -] - -[[package]] -name = "getrandom" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - -[[package]] -name = "itoa" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" - -[[package]] -name = "libc" -version = "0.2.147" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" - -[[package]] -name = "option-ext" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" - -[[package]] -name = "plugin" -version = "1.0.0" -dependencies = [ - "dirs", - "serde_json", -] - -[[package]] -name = "proc-macro2" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "redox_syscall" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" -dependencies = [ - "bitflags", -] - -[[package]] -name = "redox_users" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" -dependencies = [ - "getrandom", - "redox_syscall", - "thiserror", -] - -[[package]] -name = "ryu" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" - -[[package]] -name = "serde" -version = "1.0.188" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.188" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "syn" -version = "2.0.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "thiserror" -version = "1.0.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "unicode-ident" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.48.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 2fd28f6..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "plugin" # 名称固定为plugin,请勿更改 -version = "1.0.0" -authors = ["pot-app"] -edition = "2021" - -[lib] -crate-type = ["dylib"] # pot只支持dylib类型动态库,请勿修改 - -[dependencies] -dirs = "5.0.1" -serde_json = "1.0" diff --git a/info.json b/info.json index f685327..d79675b 100644 --- a/info.json +++ b/info.json @@ -1,5 +1,5 @@ { - "id": "[plugin].com.pot-app.paddle", + "id": "plugin.com.pot-app.paddle", "plugin_type": "recognize", "icon": "paddle.png", "display": "Paddle OCR", diff --git a/main.js b/main.js new file mode 100644 index 0000000..aa31f6f --- /dev/null +++ b/main.js @@ -0,0 +1,24 @@ +async function recognize(_base64, lang, options) { + const { utils } = options; + const { run, cacheDir, pluginDir } = utils; + + let result = await run(`${pluginDir}/PaddleOCR-json.exe`, [ + "use_angle_cls=true", + "cls=true", + `--image_path=${cacheDir}/pot_screenshot_cut.png`, + `--config_path=models/config_${lang}.txt`, + ]); + if (result.status === 0) { + let out = result.stdout; + out = out.split("OCR init completed."); + out = out[1].trim(); + let json = JSON.parse(out); + let target = ""; + for (let line of json.data) { + target += `${line.text}\n`; + } + return target.trim(); + } else { + throw Error(result.stderr); + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 4ef4843..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,70 +0,0 @@ -use dirs::{cache_dir, config_dir}; -use serde_json::Value; -use std::collections::HashMap; -use std::error::Error; -use std::os::windows::process::CommandExt; -use std::process::Command; - -#[no_mangle] -pub fn recognize( - _base64: &str, - lang: &str, - _needs: HashMap, -) -> Result> { - let config_dir_path = config_dir().unwrap(); - - let plugin_path = config_dir_path - .join("com.pot-app.desktop") - .join("plugins") - .join("recognize") - .join("[plugin].com.pot-app.paddle"); - let paddle_dir_path = plugin_path.join("PaddleOCR-json_v.1.3.0"); - let paddle_exe_path = paddle_dir_path.join("PaddleOCR-json.exe"); - - let cache_dir_path = cache_dir().unwrap(); - let image_path = cache_dir_path - .join("com.pot-app.desktop") - .join("pot_screenshot_cut.png"); - - let mut cmd = Command::new("cmd"); - let cmd = cmd.creation_flags(0x08000000); - let cmd = cmd.args(["/c", &paddle_exe_path.to_str().unwrap()]); - - let output = cmd - .current_dir(paddle_dir_path) - .arg("use_angle_cls=true") - .arg("cls=true") - .arg(&format!("--image_path={}", image_path.to_str().unwrap())) - .arg(&format!("--config_path=models/config_{}.txt", lang)) - .output()?; - - let result = String::from_utf8_lossy(&output.stdout); - let result = result.split("OCR init completed.\r\n").last().unwrap(); - let json = serde_json::from_str(result.trim())?; - - fn parse_json(res: Value) -> Option { - let mut target = String::new(); - let data = res.as_object()?.get("data")?.as_array()?; - for line in data { - let str = line.as_object()?.get("text")?.as_str()?; - target.push_str(str); - target.push_str("\n"); - } - Some(target) - } - match parse_json(json) { - Some(text) => Ok(Value::String(text)), - None => Err("Parse Error".into()), - } -} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn try_request() { - let needs = HashMap::new(); - let result = recognize("", "chinese", needs); - println!("{result:?}"); - } -}