2019-09-04 20:37:02 +02:00
/ * *
2019-09-13 15:34:08 +02:00
* @ author n1474335 [ n1474335 @ gmail . com ]
2019-09-04 20:37:02 +02:00
* @ author mshwed [ m @ ttshwed . com ]
2020-03-27 14:12:08 +01:00
* @ author Matt C [ me @ mitt . dev ]
2019-09-04 20:37:02 +02:00
* @ copyright Crown Copyright 2019
* @ license Apache - 2.0
* /
2022-09-09 23:45:06 +02:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
import { isImage } from "../lib/FileType.mjs" ;
import { toBase64 } from "../lib/Base64.mjs" ;
import { isWorkerEnvironment } from "../Utils.mjs" ;
2022-09-16 15:15:54 +02:00
import process from "process" ;
2022-09-09 23:45:06 +02:00
import { createWorker } from "tesseract.js" ;
/ * *
* Optical Character Recognition operation
* /
class OpticalCharacterRecognition extends Operation {
/ * *
* OpticalCharacterRecognition constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Optical Character Recognition" ;
this . module = "OCR" ;
this . description = "Optical character recognition or optical character reader (OCR) is the mechanical or electronic conversion of images of typed, handwritten or printed text into machine-encoded text.<br><br>Supported image formats: png, jpg, bmp, pbm." ;
this . infoURL = "https://wikipedia.org/wiki/Optical_character_recognition" ;
this . inputType = "ArrayBuffer" ;
this . outputType = "string" ;
this . args = [
{
name : "Show confidence" ,
type : "boolean" ,
value : true
}
] ;
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
async run ( input , args ) {
2022-09-16 15:15:54 +02:00
const [ showConfidence ] = args ;
2022-09-09 23:45:06 +02:00
if ( ! isWorkerEnvironment ( ) ) throw new OperationError ( "This operation only works in a browser" ) ;
const type = isImage ( input ) ;
if ( ! type ) {
2022-09-16 15:15:54 +02:00
throw new OperationError ( "Unsupported file type (supported: jpg,png,pbm,bmp) or no file provided" ) ;
2022-09-09 23:45:06 +02:00
}
2022-09-16 15:15:54 +02:00
const assetDir = isWorkerEnvironment ( ) ? ` ${ self . docURL } /assets/ ` : ` ${ process . cwd ( ) } /src/core/vendor/ ` ;
2022-09-09 23:45:06 +02:00
try {
self . sendStatusMessage ( "Spinning up Tesseract worker..." ) ;
const image = ` data: ${ type } ;base64, ${ toBase64 ( input ) } ` ;
const worker = createWorker ( {
2022-09-16 15:15:54 +02:00
workerPath : ` ${ assetDir } tesseract/worker.min.js ` ,
langPath : ` ${ assetDir } tesseract/lang-data ` ,
corePath : ` ${ assetDir } tesseract/tesseract-core.wasm.js ` ,
2022-09-09 23:45:06 +02:00
logger : progress => {
if ( isWorkerEnvironment ( ) ) {
self . sendStatusMessage ( ` Status: ${ progress . status } ${ progress . status === "recognizing text" ? ` - ${ ( parseFloat ( progress . progress ) * 100 ) . toFixed ( 2 ) } % ` : "" } ` ) ;
}
}
} ) ;
await worker . load ( ) ;
2022-09-16 15:15:54 +02:00
self . sendStatusMessage ( ` Loading English language pack... ` ) ;
await worker . loadLanguage ( "eng" ) ;
2022-09-09 23:45:06 +02:00
self . sendStatusMessage ( "Intialising Tesseract API..." ) ;
2022-09-16 15:15:54 +02:00
await worker . initialize ( "eng" ) ;
2022-09-09 23:45:06 +02:00
self . sendStatusMessage ( "Finding text..." ) ;
const result = await worker . recognize ( image ) ;
if ( showConfidence ) {
return ` Confidence: ${ result . data . confidence } % \n \n ${ result . data . text } ` ;
} else {
return result . data . text ;
}
} catch ( err ) {
throw new OperationError ( ` Error performing OCR on image. ( ${ err } ) ` ) ;
}
}
}
export default OpticalCharacterRecognition ;