Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,35 @@ Tesseract.js Electron

An example to use tesseract.js in electron.

Please check index.html for details.
1. Loading image in `index.html` and passing the `path` to `main.js`.

## Installation
2. `main.js` recognize the image and returns the result.

3. Then `index.html` displays the result.

# run the example
```bash
# install
$ npm install
```

## Run
# run
$ npm run start
```

# init script (no need to run for example)
```bash
$ npm start
# There is no need for a specific version, just make a record.
# Please try other versions by yourself.

$ node --version
# v16.16.0

$ npm --version
# 9.6.2

# init project
$ npm init -f

# install
$ npm install electron tesseract.js --save-dev
```
40 changes: 26 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,35 @@
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
<img id="image" class="display: none" />
<input id="fileinput" accept="image/jpeg, image/png" type="file" />
<div id="recognize" style="white-space: pre-line"></div>
<script>
const { createWorker } = require('tesseract.js');
const path = require('path');
const image = document.getElementById("image")
const fileinput = document.getElementById("fileinput")
const recognize = document.getElementById("recognize")

const worker = createWorker({
cachePath: path.join(__dirname, 'lang-data'),
logger: m => console.log(m),
});
const changeHandle = async (event) => {
const file = event.target?.files?.[0]
if (!file) return
const path = file.path
if (path) {
image.src = path
image.style.display = "block"
image.style.height = "200px"
image.style.border = "1px solid"
}
recognize.innerText = `file path:\n${path}\n\nresult:\nloading...`
const json = await window.electronAPI.recognize(path)
const result = JSON.parse(json)

(async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize(path.join(__dirname, 'images', 'testocr.png'));
console.log(text);
await worker.terminate();
})();
console.log("file path:")
console.log(path)
console.log("result:")
console.log(result?.data?.text)
recognize.innerText = `file path:\n${path}\n\nresult:\n${result?.data?.text}`
}
fileinput.addEventListener("change", changeHandle)
</script>
</body>
</html>
47 changes: 34 additions & 13 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
const { app, BrowserWindow } = require('electron')
const { app, BrowserWindow, ipcMain } = require('electron')
const { join } = require('path')
const { createWorker } = require('tesseract.js')

const recognize = async (event, path) => {
const worker = await createWorker({
cachePath: join(__dirname, '/lang-data'),
logger: (m) => console.log(JSON.stringify(m)),
errorHandler: (e) => console.log(JSON.stringify(e))
})
await worker.loadLanguage('eng')
await worker.initialize('eng')
const result = await worker.recognize(path)
await worker.terminate()

console.log("image path:")
console.log(path)
console.log("recognize text:")
console.log(result?.data?.text)

return JSON.stringify(result)
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
Expand All @@ -10,6 +31,7 @@ function createWindow () {
width: 800,
height: 600,
webPreferences: {
preload: join(__dirname, "./preload.js"),
nodeIntegration: true
}
})
Expand All @@ -29,10 +51,17 @@ function createWindow () {
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
app.whenReady().then(() => {
ipcMain.handle('recognize', recognize)

createWindow()

app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})

// Quit when all windows are closed.
app.on('window-all-closed', () => {
Expand All @@ -43,13 +72,5 @@ app.on('window-all-closed', () => {
}
})

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
Loading