Skip to main content

useOCR

Optical character recognition(OCR) is a computer vision technique that detects and recognizes text within the image. It's commonly used to convert different types of documents, such as scanned paper documents, PDF files, or images captured by a digital camera, into editable and searchable data.

caution

It is recommended to use models provided by us, which are available at our Hugging Face repository. You can also use constants shipped with our library.

Reference

import {
useOCR,
CRAFT_800,
RECOGNIZER_EN_CRNN_512,
RECOGNIZER_EN_CRNN_256,
RECOGNIZER_EN_CRNN_128
} from 'react-native-executorch';

function App() {
const model = useOCR({
detectorSource: CRAFT_800,
recognizerSources: {
recognizerLarge: RECOGNIZER_EN_CRNN_512,
recognizerMedium: RECOGNIZER_EN_CRNN_256,
recognizerSmall: RECOGNIZER_EN_CRNN_128
},
language: "en",
});

...
for (const ocrDetection of await model.forward("https://url-to-image.jpg")) {
console.log("Bounding box: ", ocrDetection.bbox);
console.log("Bounding label: ", ocrDetection.text);
console.log("Bounding score: ", ocrDetection.score);
}
...
}

Type definitions

interface RecognizerSources {
recognizerLarge: string | number;
recognizerMedium: string | number;
recognizerSmall: string | number;
}

type OCRLanguage = 'en';

interface Point {
x: number;
y: number;
}

interface OCRDetection {
bbox: Point[];
text: string;
score: number;
}

Arguments

detectorSource - A string that specifies the location of the detector binary. For more information, take a look at loading models section.

recognizerSources - An object that specifies locations of the recognizers binary files. Each recognizer is composed of three models tailored to process images of varying widths.

  • recognizerLarge - A string that specifies the location of the recognizer binary file which accepts input images with a width of 512 pixels.
  • recognizerMedium - A string that specifies the location of the recognizer binary file which accepts input images with a width of 256 pixels.
  • recognizerSmall - A string that specifies the location of the recognizer binary file which accepts input images with a width of 128 pixels.

For more information, take a look at loading models section.

language - A parameter that specifies the language of the text to be recognized by the OCR.

Returns

The hook returns an object with the following properties:

FieldTypeDescription
forward(input: string) => Promise<OCRDetection[]>A function that accepts an image (url, b64) and returns an array of OCRDetection objects.
errorstring | nullContains the error message if the model loading failed.
isGeneratingbooleanIndicates whether the model is currently processing an inference.
isReadybooleanIndicates whether the model has successfully loaded and is ready for inference.
downloadProgressnumberRepresents the download progress as a value between 0 and 1.

Running the model

To run the model, you can use the forward method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns an array of OCRDetection objects. Each object contains coordinates of the bounding box, the text recognized within the box, and the confidence score. For more information, please refer to the reference or type definitions.

Detection object

The detection object is specified as follows:

interface Point {
x: number;
y: number;
}

interface OCRDetection {
bbox: Point[];
text: string;
score: number;
}

The bbox property contains information about the bounding box of detected text regions. It is represented as four points, which are corners of detected bounding box. The text property contains the text recognized within detected text region. The score represents the confidence score of the recognized text.

Example

import {
useOCR,
CRAFT_800,
RECOGNIZER_EN_CRNN_512,
RECOGNIZER_EN_CRNN_256,
RECOGNIZER_EN_CRNN_128,
} from 'react-native-executorch';

function App() {
const model = useOCR({
detectorSource: CRAFT_800,
recognizerSources: {
recognizerLarge: RECOGNIZER_EN_CRNN_512,
recognizerMedium: RECOGNIZER_EN_CRNN_256,
recognizerSmall: RECOGNIZER_EN_CRNN_128,
},
language: 'en',
});

const runModel = async () => {
const ocrDetections = await model.forward('https://url-to-image.jpg');

for (const ocrDetection of ocrDetections) {
console.log('Bounding box: ', ocrDetection.bbox);
console.log('Bounding text: ', ocrDetection.text);
console.log('Bounding score: ', ocrDetection.score);
}
};
}

Supported models

ModelType
CRAFT_800Detector
CRNN_EN_512Recognizer
CRNN_EN_256Recognizer
CRNN_EN_128Recognizer

Benchmarks

Model size

ModelXNNPACK [MB]
CRAFT_80083.1
CRNN_EN_512547
CRNN_EN_256277
CRNN_EN_128142

Memory usage

ModelAndroid (XNNPACK) [MB]iOS (XNNPACK) [MB]
CRAFT_800 + CRNN_EN_512 + CRNN_EN_256 + CRNN_EN_12821001782

Inference time

warning

Times presented in the tables are measured as consecutive runs of the model. Initial run times may be up to 2x longer due to model loading and initialization.

ModeliPhone 16 Pro (XNNPACK) [ms]iPhone 14 Pro Max (XNNPACK) [ms]iPhone SE 3 (XNNPACK) [ms]Samsung Galaxy S24 (XNNPACK) [ms]Samsung Galaxy S21 (XNNPACK) [ms]
CRAFT_8002099222722457108
CRNN_EN_5127025254151
CRNN_EN_256391232478
CRNN_EN_12817831439

❌ - Insufficient RAM.