Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
tesseract.js
Advanced tools
tesseract.js is a JavaScript library that provides Optical Character Recognition (OCR) capabilities. It allows you to extract text from images and PDFs directly in the browser or in a Node.js environment.
Basic OCR
This feature allows you to perform basic OCR on an image file. The code sample demonstrates how to recognize text from an image using the English language.
const Tesseract = require('tesseract.js');
Tesseract.recognize(
'path/to/image.png',
'eng',
{
logger: m => console.log(m)
}
).then(({ data: { text } }) => {
console.log(text);
});
Handling Multiple Languages
This feature allows you to recognize text in multiple languages. The code sample demonstrates how to recognize text from an image using both English and Spanish languages.
const Tesseract = require('tesseract.js');
Tesseract.recognize(
'path/to/image.png',
'eng+spa',
{
logger: m => console.log(m)
}
).then(({ data: { text } }) => {
console.log(text);
});
Progress Reporting
This feature provides real-time progress updates during the OCR process. The code sample demonstrates how to log progress messages to the console.
const Tesseract = require('tesseract.js');
Tesseract.recognize(
'path/to/image.png',
'eng',
{
logger: m => console.log(m)
}
).then(({ data: { text } }) => {
console.log(text);
});
Using Worker for Performance
This feature allows you to use a worker for better performance, especially for large images or multiple OCR tasks. The code sample demonstrates how to create a worker, load the necessary language, perform OCR, and then terminate the worker.
const { createWorker } = require('tesseract.js');
const worker = createWorker({
logger: m => console.log(m)
});
(async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
const { data: { text } } = await worker.recognize('path/to/image.png');
console.log(text);
await worker.terminate();
})();
ocrad.js is a JavaScript port of the OCRAD OCR library. It is designed to be used in the browser and can recognize text from images. Compared to tesseract.js, ocrad.js is lighter but may not be as accurate or feature-rich.
node-tesseract-ocr is a Node.js wrapper for the Tesseract OCR engine. It provides a simpler interface for performing OCR in Node.js environments. While it offers similar functionalities to tesseract.js, it does not support browser usage.
Tesseract.js is a javascript library that gets words in almost any language out of images. (Demo)
Tesseract.js works with script tags, webpack/browserify, and node. After you install it, using it is as simple as
Tesseract.recognize(myImage)
.progress(function (p) { console.log('progress', p) })
.then(function (result) { console.log('result', result) })
Check out the docs for a full treatment of the API.
Tesseract.js works with a <script>
tag via local copy or cdn, with webpack and browserify via npm
, and on node via npm
. Check out the docs for a full treatment of the API.
You can simply include Tesseract.js with a cdn like this:
<script src='https://cdn.rawgit.com/naptha/tesseract.js/0.2.0/dist/tesseract.js'></script>
After including your scripts, the Tesseract
variable should be defined! You can head to the docs for a full treatment of the API.
First:
> npm install tesseract.js --save
Then
var Tesseract = require('tesseract.js')
or
import Tesseract from 'tesseract.js'
You can head to the docs for a full treatment of the API.
Figures out what words are in image
, where the words are in image
, etc.
image
is any ImageLike object.options
is either absent (in which case it is interpreted as 'eng'
), a string specifing a language short code from the language list, or a flat json object that may:
lang
property with a value from the list of lang parametersReturns a TesseractJob whose then
, progress
, and catch
methods can be used to act on the result.
Tesseract.recognize(myImage)
.then(function(result){
console.log(result)
})
// if we know our image is of spanish words without the letter 'e':
Tesseract.recognize(myImage, {
lang: 'spa',
tessedit_char_blacklist: 'e'
})
.then(function(result){
console.log(result)
})
Figures out what script (e.g. 'Latin', 'Chinese') the words in image are written in.
image
is any ImageLike object.Returns a TesseractJob whose then
, progress
, and error
methods can be used to act on the result of the script.
Tesseract.detect(myImage)
.then(function(result){
console.log(result)
})
The main Tesseract.js functions take an image
parameter, which should be something that is like an image. What's considered "image-like" differs depending on whether it is being run from the browser or through NodeJS.
On a browser, an image can be:
img
, video
, or canvas
elementcanvas.getContext('2d')
)File
object (from a file <input>
or drag-drop event)Blob
objectImageData
instance (an object containing width
, height
and data
properties)In NodeJS, an image can be
Buffer
instance containing a PNG
or JPEG
imageImageData
instance (an object containing width
, height
and data
properties)A TesseractJob is an an object returned by a call to recognize
or detect
. It's inspired by the ES6 Promise interface and provides then
and catch
methods. One important difference is that these methods return the job itself (to enable chaining) rather than new.
Typical use is:
Tesseract.recognize(myImage)
.progress(function(message){console.log(message)})
.catch(function(err){console.error(err)})
.then(function(result){console.log(result)})
Which is equivalent to:
var job1 = Tesseract.recognize(myImage);
job1.progress(function(message){console.log(message)});
job1.catch(function(err){console.error(err)});
job1.then(function(result){console.log(result)})
Sets callback
as the function that will be called every time the job progresses.
callback
is a function with the signature callback(progress)
where progress
is a json object.For example:
Tesseract.recognize(myImage)
.progress(function(message){console.log('progress is: ', message)})
The console will show something like:
progress is: {loaded_lang_model: "eng", from_cache: true}
progress is: {initialized_with_lang: "eng"}
progress is: {set_variable: Object}
progress is: {set_variable: Object}
progress is: {recognized: 0}
progress is: {recognized: 0.3}
progress is: {recognized: 0.6}
progress is: {recognized: 0.9}
progress is: {recognized: 1}
Sets callback
as the function that will be called if and when the job successfully completes.
callback
is a function with the signature callback(result)
where result
is a json object.For example:
Tesseract.recognize(myImage)
.then(function(result){console.log('result is: 'result)})
The console will show something like:
progress is: {
blocks: Array[1]
confidence: 87
html: "<div class='ocr_page' id='page_1' ..."
lines: Array[3]
oem: "DEFAULT"
paragraphs: Array[1]
psm: "SINGLE_BLOCK"
symbols: Array[33]
text: "Hello World↵from beyond↵the Cosmic Void↵↵"
version: "3.04.00"
words: Array[7]
}
Sets callback
as the function that will be called if the job fails.
callback
is a function with the signature callback(erros)
where error
is a json object.window.Tesseract = Tesseract.create({
workerPath: '/path/to/worker.js',
langPath: 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/',
corePath: 'https://cdn.rawgit.com/naptha/tesseract.js-core/0.1.0/index.js',
})
A string specifying the location of the tesseract.js-core library, with default value 'https://cdn.rawgit.com/naptha/tesseract.js-core/master/index.js'. Set this string before calling Tesseract.recognize
and Tesseract.detect
if you want Tesseract.js to use a different file.
A string specifying the location of the tesseract.worker.js file, with default value 'https://cdn.rawgit.com/naptha/tesseract.js/8b915dc/dist/tesseract.worker.js'. Set this string before calling Tesseract.recognize
and Tesseract.detect
if you want Tesseract.js to use a different file.
A string specifying the location of the tesseract language files, with default value 'https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/'. Language file urls are calculated according to the formula langPath + langCode + '.traineddata.gz'
. Set this string before calling Tesseract.recognize
and Tesseract.detect
if you want Tesseract.js to use different language files.
To run a development copy of tesseract.js, first clone this repo.
> git clone https://github.com/naptha/tesseract.js.git
Then, cd in to the folder, npm install
, and npm start
> cd tesseract.js
> npm install && npm start
... a bunch of npm stuff ...
Starting up http-server, serving ./
Available on:
http://127.0.0.1:7355
http://[your ip]:7355
Then open http://localhost:7355/examples/file-input/demo.html
in your favorite browser. The devServer automatically rebuilds tesseract.js and tesseract.worker.js when you change files in the src folder.
After you've cloned the repo and run npm install
as described in the Development Section, you can build static library files in the dist folder with
> npm run build
Thanks :)
FAQs
Pure Javascript Multilingual OCR
The npm package tesseract.js receives a total of 124,219 weekly downloads. As such, tesseract.js popularity was classified as popular.
We found that tesseract.js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.