|
| 1 | +--- |
| 2 | +title: useOCR |
| 3 | +sidebar_position: 4 |
| 4 | +--- |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +:::caution |
| 9 | +It is recommended to use models provided by us, which are available at our [Hugging Face repository](https://huggingface.co/software-mansion). You can also use [constants](https://github.com/software-mansion/react-native-executorch/blob/765305abc289083787eb9613b899d6fcc0e24126/src/constants/modelUrls.ts#L51) shipped with our library. |
| 10 | +::: |
| 11 | + |
| 12 | +## Reference |
| 13 | + |
| 14 | +```jsx |
| 15 | +import { |
| 16 | + useOCR, |
| 17 | + CRAFT_800, |
| 18 | + RECOGNIZER_EN_CRNN_512, |
| 19 | + RECOGNIZER_EN_CRNN_256, |
| 20 | + RECOGNIZER_EN_CRNN_128 |
| 21 | +} from 'react-native-executorch'; |
| 22 | + |
| 23 | +function App() { |
| 24 | + const model = useOCR({ |
| 25 | + detectorSource: CRAFT_800, |
| 26 | + recognizerSources: { |
| 27 | + recognizerLarge: RECOGNIZER_EN_CRNN_512, |
| 28 | + recognizerMedium: RECOGNIZER_EN_CRNN_256, |
| 29 | + recognizerSmall: RECOGNIZER_EN_CRNN_128 |
| 30 | + }, |
| 31 | + language: "en", |
| 32 | + }); |
| 33 | + |
| 34 | + ... |
| 35 | + for (const ocrDetection of await model.forward("https://url-to-image.jpg")) { |
| 36 | + console.log("Bounding box: ", ocrDetection.bbox); |
| 37 | + console.log("Bounding label: ", ocrDetection.text); |
| 38 | + console.log("Bounding score: ", ocrDetection.score); |
| 39 | + } |
| 40 | + ... |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +<details> |
| 45 | +<summary>Type definitions</summary> |
| 46 | + |
| 47 | +```typescript |
| 48 | +interface RecognizerSources { |
| 49 | + recognizerLarge: string | number; |
| 50 | + recognizerMedium: string | number; |
| 51 | + recognizerSmall: string | number; |
| 52 | +} |
| 53 | + |
| 54 | +type OCRLanguage = 'en'; |
| 55 | + |
| 56 | +interface Point { |
| 57 | + x: number; |
| 58 | + y: number; |
| 59 | +} |
| 60 | + |
| 61 | +interface OCRDetection { |
| 62 | + bbox: Point[]; |
| 63 | + text: string; |
| 64 | + score: number; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +</details> |
| 69 | + |
| 70 | +### Arguments |
| 71 | + |
| 72 | +**`detectorSource`** - A string that specifies the location of the detector binary. For more information, take a look at [loading models](../fundamentals/loading-models.md) section. |
| 73 | + |
| 74 | +**`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. |
| 75 | + |
| 76 | +- `recognizerLarge` - A string that specifies the location of the recognizer binary file which accepts input images with a width of 512 pixels. |
| 77 | +- `recognizerMedium` - A string that specifies the location of the recognizer binary file which accepts input images with a width of 256 pixels. |
| 78 | +- `recognizerSmall` - A string that specifies the location of the recognizer binary file which accepts input images with a width of 128 pixels. |
| 79 | + |
| 80 | +For more information, take a look at [loading models](../fundamentals/loading-models.md) section. |
| 81 | + |
| 82 | +**`language`** - A parameter that specifies the language of the text to be recognized by the OCR. |
| 83 | + |
| 84 | +### Returns |
| 85 | + |
| 86 | +The hook returns an object with the following properties: |
| 87 | + |
| 88 | +| Field | Type | Description | |
| 89 | +| ------------------ | -------------------------------------------- | ------------------------------------------------------------------------------------------- | |
| 90 | +| `forward` | `(input: string) => Promise<OCRDetection[]>` | A function that accepts an image (url, b64) and returns an array of `OCRDetection` objects. | |
| 91 | +| `error` | <code>string | null</code> | Contains the error message if the model loading failed. | |
| 92 | +| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | |
| 93 | +| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | |
| 94 | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1. | |
| 95 | + |
| 96 | +## Running the model |
| 97 | + |
| 98 | +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. |
| 99 | + |
| 100 | +## Detection object |
| 101 | + |
| 102 | +The detection object is specified as follows: |
| 103 | + |
| 104 | +```typescript |
| 105 | +interface Point { |
| 106 | + x: number; |
| 107 | + y: number; |
| 108 | +} |
| 109 | + |
| 110 | +interface OCRDetection { |
| 111 | + bbox: Point[]; |
| 112 | + text: string; |
| 113 | + score: number; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +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. |
| 118 | +The `text` property contains the text recognized within detected text region. The `score` represents the confidence score of the recognized text. |
| 119 | + |
| 120 | +## Example |
| 121 | + |
| 122 | +```tsx |
| 123 | +import { |
| 124 | + useOCR, |
| 125 | + CRAFT_800, |
| 126 | + RECOGNIZER_EN_CRNN_512, |
| 127 | + RECOGNIZER_EN_CRNN_256, |
| 128 | + RECOGNIZER_EN_CRNN_128, |
| 129 | +} from 'react-native-executorch'; |
| 130 | + |
| 131 | +function App() { |
| 132 | + const model = useOCR({ |
| 133 | + detectorSource: CRAFT_800, |
| 134 | + recognizerSources: { |
| 135 | + recognizerLarge: RECOGNIZER_EN_CRNN_512, |
| 136 | + recognizerMedium: RECOGNIZER_EN_CRNN_256, |
| 137 | + recognizerSmall: RECOGNIZER_EN_CRNN_128, |
| 138 | + }, |
| 139 | + language: 'en', |
| 140 | + }); |
| 141 | + |
| 142 | + const runModel = async () => { |
| 143 | + const ocrDetections = await model.forward('https://url-to-image.jpg'); |
| 144 | + |
| 145 | + for (const ocrDetection of ocrDetections) { |
| 146 | + console.log('Bounding box: ', ocrDetection.bbox); |
| 147 | + console.log('Bounding text: ', ocrDetection.text); |
| 148 | + console.log('Bounding score: ', ocrDetection.score); |
| 149 | + } |
| 150 | + }; |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Supported models |
| 155 | + |
| 156 | +| Model | Type | |
| 157 | +| ------------------------------------------------------ | ---------- | |
| 158 | +| [CRAFT_800](https://github.com/clovaai/CRAFT-pytorch) | Detector | |
| 159 | +| [CRNN_EN_512](https://www.jaided.ai/easyocr/modelhub/) | Recognizer | |
| 160 | +| [CRNN_EN_256](https://www.jaided.ai/easyocr/modelhub/) | Recognizer | |
| 161 | +| [CRNN_EN_128](https://www.jaided.ai/easyocr/modelhub/) | Recognizer | |
| 162 | + |
| 163 | +## Benchmarks |
| 164 | + |
| 165 | +### Model size |
| 166 | + |
| 167 | +| Model | XNNPACK [MB] | |
| 168 | +| ----------- | ------------ | |
| 169 | +| CRAFT_800 | 83.1 | |
| 170 | +| CRNN_EN_512 | 547 | |
| 171 | +| CRNN_EN_256 | 277 | |
| 172 | +| CRNN_EN_128 | 142 | |
| 173 | + |
| 174 | +### Memory usage |
| 175 | + |
| 176 | +| Model | Android (XNNPACK) [MB] | iOS (XNNPACK) [MB] | |
| 177 | +| --------------------------------------------------- | ---------------------- | ------------------ | |
| 178 | +| CRAFT_800 + CRNN_EN_512 + CRNN_EN_256 + CRNN_EN_128 | 2100 | 1782 | |
| 179 | + |
| 180 | +### Inference time |
| 181 | + |
| 182 | +:::warning warning |
| 183 | +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. |
| 184 | +::: |
| 185 | + |
| 186 | +| Model | iPhone 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] | |
| 187 | +| ----------- | ---------------------------- | -------------------------------- | -------------------------- | --------------------------------- | --------------------------------- | |
| 188 | +| CRAFT_800 | 2099 | 2227 | ❌ | 2245 | 7108 | |
| 189 | +| CRNN_EN_512 | 70 | 252 | ❌ | 54 | 151 | |
| 190 | +| CRNN_EN_256 | 39 | 123 | ❌ | 24 | 78 | |
| 191 | +| CRNN_EN_128 | 17 | 83 | ❌ | 14 | 39 | |
| 192 | + |
| 193 | +❌ - Insufficient RAM. |
0 commit comments