Skip to content

Commit

Permalink
feat: Add Tesseract as Linux System OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Jul 27, 2023
1 parent e9a8ff2 commit e0549b7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 355 deletions.
285 changes: 0 additions & 285 deletions public/tesseract-core.wasm.js

This file was deleted.

3 changes: 0 additions & 3 deletions public/worker.min.js

This file was deleted.

26 changes: 21 additions & 5 deletions src-tauri/src/ocr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dirs::cache_dir;

#[tauri::command(async)]
#[cfg(target_os = "windows")]
pub fn system_ocr(app_handle: tauri::AppHandle) -> Result<String, String> {
use dirs::cache_dir;
use win_ocr::ocr;
let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);
Expand All @@ -16,16 +17,31 @@ pub fn system_ocr(app_handle: tauri::AppHandle) -> Result<String, String> {
#[tauri::command]
#[cfg(target_os = "macos")]
pub fn system_ocr(app_handle: tauri::AppHandle) -> Result<String, String> {
use dirs::cache_dir;

let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);
app_cache_dir_path.push("pot_screenshot_cut.png");
Ok(app_cache_dir_path.to_str().unwrap().to_string())
}

#[tauri::command]
#[tauri::command(async)]
#[cfg(target_os = "linux")]
pub fn system_ocr(app_handle: tauri::AppHandle, lang: &str) -> Result<String, String> {
Err("Not supported yet".to_string())
let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);
app_cache_dir_path.push("pot_screenshot_cut.png");
let output = match std::process::Command::new("tesseract")
.arg(app_cache_dir_path.to_str().unwrap())
.arg("stdout")
.arg("-l")
.arg(lang)
.output()
{
Ok(v) => v,
Err(e) => return Err(e.to_string()),
};

match String::from_utf8(output.stdout) {
Ok(v) => Ok(v),
Err(e) => Err(e.to_string()),
}
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"category": "Utility",
"copyright": "GPLv3",
"deb": {
"depends": ["libxdo-dev", "libxcb1", "libxrandr2"]
"depends": ["libxdo-dev", "libxcb1", "libxrandr2", "tesseract-ocr-all"]
},
"externalBin": [],
"icon": [
Expand Down
1 change: 0 additions & 1 deletion src/interfaces_ocr/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * as tesseract from './tesseract';
export * as baidu from './baidu';
export * as baidu_accurate from './baidu_accurate';
export * as baidu_img from './baidu_img';
Expand Down
34 changes: 29 additions & 5 deletions src/interfaces_ocr/system.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import { ocrID } from '../windows/Ocr/components/TextArea';
import { ocr as tesseract } from './tesseract';
import { invoke } from '@tauri-apps/api';
import { Command } from '@tauri-apps/api/shell';

export const info = {
name: 'system',
supportLanguage: {},
supportLanguage: {
auto: 'chi_sim+eng+chi_tra+jpn+kor+fra+spa+rus+deu+ita+tur+por+vie+ind+tha+msa+ara+hin',
zh_cn: 'chi_sim',
zh_tw: 'chi_tra',
en: 'eng',
yue: 'chi_sim',
ja: 'jpn',
ko: 'kor',
fr: 'fra',
es: 'spa',
ru: 'rus',
de: 'deu',
it: 'ita',
tr: 'tur',
pt: 'por',
vi: 'vie',
id: 'ind',
th: 'tha',
ms: 'msa',
ar: 'ara',
hi: 'hin',
},
needs: [],
};

export async function ocr(base64, lang, setText, id) {
export async function ocr(_, lang, setText, id) {
const is_linux = await invoke('is_linux');
const is_macos = await invoke('is_macos');
// Linux 没法做系统OCR,用Tesseract代替

if (is_linux) {
await tesseract(base64, lang, setText, id);
const { supportLanguage } = info;
const result = await invoke('system_ocr', { lang: supportLanguage[lang] });
if (ocrID === id || id === 'translate') {
setText(result);
}
} else if (is_macos) {
const img_path = await invoke('system_ocr');
const command = Command.sidecar('sidecar/ocr', img_path);
Expand Down
55 changes: 0 additions & 55 deletions src/interfaces_ocr/tesseract.js

This file was deleted.

0 comments on commit e0549b7

Please sign in to comment.