Ocr
libraryReads text from images and PDF files with the Tesseract OCR engine (LSTM mode), for scanned documents and screenshots. English (eng) and Dutch (nld) are bundled. Things to know: OCR is slow (seconds per page) and only as good as the scan; a clear, high resolution image works best. The image is converted to black and white before recognition. A call that cannot read the file is retried for about a minute before it fails.
- Kind Library, a global in every script
- Category Files & Documents
- Functions 3
Functions
-
imagestring Recognises the text in an image or PDF file of the project (fetched from the Reflex server when it is not in the workspace) and returns it as one string. -
pagestable Like `Ocr.image`, but returns a list with one text per page: a PDF gives one entry for every page, an image gives a list with one entry. -
timeoutnil Sets how long the recognition of one image, or of one PDF page, may take, in seconds, for later Ocr.image and Ocr.pages calls.
image()
Section titled “image()”Ocr.image(image: string)→stringOcr.image(image: string, lang: string)→string Recognises the text in an image or PDF file of the project (fetched from the Reflex server when it is not in the workspace) and returns it as one string. Images in any format Java can read (PNG, JPEG, GIF, BMP) are handled directly. A file whose name ends in ".pdf" is rendered page by page at 300 dpi and every page is recognised; the page texts are then joined with a form feed character ("\f"). Use `Ocr.pages` to get a list with one text per page. Pages that contain real text (not scans) are rendered as images too, so prefer a PDF text extractor for those. The language is a Tesseract code: "eng" (default) or "nld"; combinations such as "eng+nld" work when every part is installed. An unknown language raises an error that lists the available ones. A non-empty tessdata folder next to the agent replaces the bundled languages, so it must contain the ones you use. Each image or page has a time limit (default 30 seconds, see Ocr.timeout); when it is exceeded an error is raised. Read errors (missing file, not an image) are retried 60 times with a one second pause before the error is raised.
Parameters
-
imagestring - Path of the image or PDF file in the project
-
langstring optional - Tesseract language code: "eng" (default) or "nld"
Returns
Example
local text = Ocr.image("scans/invoice.png", "nld")local invoiceNumber = text:match("Factuurnummer:?%s*(%S+)")Assert.notNil(invoiceNumber)pages()
Section titled “pages()”Ocr.pages(image: string)→tableOcr.pages(image: string, lang: string)→table Like `Ocr.image`, but returns a list with one text per page: a PDF gives one entry for every page, an image gives a list with one entry. Language, time limit and error behaviour are the same.
Parameters
-
imagestring - Path of the image or PDF file in the project
-
langstring optional - Tesseract language code: "eng" (default) or "nld"
Returns
Example
local pages = Ocr.pages("scans/contract.pdf")print(#pages)timeout()
Section titled “timeout()”Ocr.timeout(sec: number)→nil Sets how long the recognition of one image, or of one PDF page, may take, in seconds, for later Ocr.image and Ocr.pages calls. Default 30. Large scans may need more. Raises an error unless the value is from 1 to 300. Returns nothing, so it cannot be chained.
Parameters
-
secnumber - Seconds per image or page, 1 to 300
Returns
Example
Ocr.timeout(120)local text = Ocr.image("scans/large-contract.png")