-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added image processing example for Node
- Loading branch information
Balearica
committed
Dec 11, 2022
1 parent
028a44f
commit 1ebde35
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env node | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { createWorker } = require('../../'); | ||
|
||
const [,, imagePath] = process.argv; | ||
const image = path.resolve(__dirname, (imagePath || '../data/meditations.jpg')); | ||
|
||
console.log(`Recognizing ${image}`); | ||
|
||
// Tesseract.js returns images (imageColor, imageGrey, imageBinary) as strings | ||
// to be used as source tags. | ||
// This function converts to Uint8Array data for saving to disk. | ||
const convertImage = (imageSrc) => { | ||
const data = atob(imageSrc.split(',')[1]) | ||
.split('') | ||
.map((c) => c.charCodeAt(0)); | ||
|
||
return new Uint8Array(data); | ||
} | ||
|
||
(async () => { | ||
const worker = await createWorker(); | ||
await worker.loadLanguage('eng'); | ||
await worker.initialize('eng'); | ||
const { data: { imageColor, imageGrey, imageBinary } } = await worker.recognize(image, {rotateAuto: true}, {imageColor: true, imageGrey: true, imageBinary: true}); | ||
|
||
console.log('Saving intermediate images: imageColor.png, imageGrey.png, imageBinary.png'); | ||
|
||
fs.writeFileSync('imageColor.png', convertImage(imageColor)); | ||
fs.writeFileSync('imageGrey.png', convertImage(imageGrey)); | ||
fs.writeFileSync('imageBinary.png', convertImage(imageBinary)); | ||
|
||
await worker.terminate(); | ||
})(); |