Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

clearlylocal/abbyy-ocr-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clearly Local ABBYY Cloud OCR SDK

Important

As of 2024-03-15, this repo is now archived, for reference only. For Clearly Local users, please see:

Usage

OcrSdk class

import { OcrSdk } from 'path/to/OcrSdk.ts'

// credentials obtained from https://cloud.ocrsdk.com/Account/Register
const ocrSdk = new OcrSdk(
	applicationId: Deno.env.get('ABBYY_APPLICATION_ID')!, // e.g. 7ea53f47-8bbc-477b-b17c-989a3184c363
	password: Deno.env.get('ABBYY_PASSWORD')!, // e.g. n6WL0rCFlhU9bDXDri6AQEZV
	serviceUrl: Deno.env.get('ABBYY_SERVICE_URL')!, // e.g. https://cloud-eu.ocrsdk.com/
)

const { txt } = await ocrSdk.ocr(await Deno.readFile('input.jpg'), {
	languages: ['English'],
	exportFormats: ['txt'],
})

await Deno.writeFile('output.txt', new Uint8Array(await txt.arrayBuffer()))

CLI

To run the CLI, note that the relevant ABBYY_APPLICATION_ID, ABBYY_PASSWORD, and ABBYY_SERVICE_URL must be available as environment variables.

# view help
deno task cli --help
# `convert` command, specifying output formats (default "txt")
deno task cli convert path/to/image.jpg -o txt -o xml
# `html`/`json` commands
deno task cli html path/to/image.jpg
deno task cli json path/to/image.jpg
# specify languages (default "English")
deno task cli json path/to/image.jpg -l ChinesePRC -l English

Files

  • src/
    • core/
      • OcrSdk.ts: The OcrSdk class, with various methods for interacting with the ABBYY Cloud OCR API. Loosely based on ABBYY's sample JS code, but with the following changes:
        • Rewritten in modern JS/TS with ES6 classes etc.
        • Promise-based API instead of callbacks
        • Calls the v2 (JSON) Cloud OCR API, not the v1 (XML) API
        • Methods now deal with raw binary data rather than file paths (file reading/writing is left up to calling code)
        • Exposes a single ocr method to OCR an image and return the output file binary in the requested format
        • Zero external dependencies
      • imageMap.ts: The imageMap function, for converting XML output to an image map that can be rendered in HTML etc.
      • prettifyXml.ts: The prettifyXml function, for pretty-printing XML output while preserving significant whitespace
      • types.ts: TypeScript types and lists of values for interacting with OcrSdk
    • cli/
      • functions/
        • convertImage.ts: Use OcrSdk's ocr method to get text and XML files of the OCRed content
        • jsonImageMap.ts: Get simplified JSON content from XML using imageMap.ts
        • htmlImageMap.ts: Get XML content, then convert it to a searchable HTML image map based on the text data and coordinates of each line
      • main.ts: CLI app to run the various functions
  • samples/