Skip to main content

Model Package Structure and Shrinking

An EdgeOCR model package contains multiple OCR and barcode models. If your application uses only some of them, you can reduce the application distribution size by removing unused files.

caution

Keep the original downloaded model package unchanged and shrink a copy of it. Shrinking on this page means removing unused files; it does not include quantizing or retraining the models.

Model Package Structure​

A model package has the following structure:

models/
├── models.json
├── composite/
│ ├── <composite UID>.json
│ └── ...
├── detectors/
│ ├── <detector UID>.json
│ ├── <detector UID>.bin
│ └── ...
└── recognizers/
├── <recognizer UID>.json
├── <recognizer UID>.bin
└── ...
PathPurpose
models.jsonDefines the model configuration format version and the Composite model UIDs that can be selected by the application.
composite/<UID>.jsonDefines the combination of Detectors and Recognizers used for OCR processing.
detectors/<UID>.jsonDefines the Detector type and settings.
detectors/<UID>.binContains the model used by the Detector. Some Detector types do not require this file.
recognizers/<UID>.jsonDefines the Recognizer type, alphabet, and other settings.
recognizers/<UID>.binContains the model used by the Recognizer. Some Recognizer types do not require this file.

The model management repository may also contain .lnk files. These files are metadata used to retrieve and distribute model files; the EdgeOCR SDK does not read them at runtime. The model directory bundled with an application must contain the actual .bin files.

Model Dependencies​

EdgeOCR resolves the required files in the following order:

models.json
└── composite/<composite UID>.json
├── detectors/<detector UID>.json + .bin
└── recognizers/<recognizer UID>.json + .bin

The following Composite types are available:

typePurposeMain references
TextOnlyText recognitiontextDetectorUid, textRecognizerUid
BarcodeOnlyBarcode recognitionbarcodeDetectorUid, barcodeRecognizerUid
HybridSimultaneous text and barcode recognitionText and barcode Detectors and Recognizers, or a shared Detector

For example, if the Composite configuration for model-d320x320 is as follows, detector-d320x320 and recognizer-tiny are required:

{
"type": "TextOnly",
"textDetectorUid": "detector-d320x320",
"textRecognizerUid": "recognizer-tiny"
}
Built-in SDK Models

edgeocr_barcode_default, edgeocr_barcode_advanced, and edgeocr_dummy are provided by the SDK. You do not need to add JSON or BIN files with these names when a Composite references these UIDs.

Shrinking a Model Package​

The following example keeps only model-d320x320.

1. Identify the Composite UID You Use​

Check the UID that your application selects with useModel. This example uses model-d320x320.

2. Edit models.json​

Keep only the Composite UIDs used by your application in models. Preserve the version value from the model package without changing it.

{
"version": 4,
"models": [
"model-d320x320"
]
}

3. Identify the Composite Dependencies​

Open composite/model-d320x320.json and check the Detector and Recognizer UIDs. In this example, it references the following files:

  • detectors/detector-d320x320.json
  • detectors/detector-d320x320.bin
  • recognizers/recognizer-tiny.json
  • recognizers/recognizer-tiny.bin

If you keep multiple Composites, keep the union of the files referenced by all of them. A Detector or Recognizer shared by multiple Composites needs to be included only once.

4. Remove Unreferenced Files​

Remove files other than the UIDs retained in models.json and the files referenced by those Composites. The resulting structure is:

models/
├── models.json
├── composite/
│ └── model-d320x320.json
├── detectors/
│ ├── detector-d320x320.json
│ └── detector-d320x320.bin
└── recognizers/
├── recognizer-tiny.json
└── recognizer-tiny.bin
warning
  • The UIDs in each Composite JSON file must match the corresponding filenames.
  • Do not leave a UID in models.json while removing its Composite or dependency files.
  • Dependency UIDs may differ between model package versions. Inspect the Composite JSON in your package instead of relying on the filenames in this example.

Where to Replace the Package in Your Application​

Replace the existing model directory bundled with your application with the shrunk models directory.

Replace the models folder added to the application bundle in Xcode. Also verify that the replacement folder is included in the application target.

If you change the directory name, update the name passed to Bundle.main.path or the sample application's modelPath value.

let modelPath = Bundle.main.path(forResource: "models", ofType: "")!
let edgeOCR = try ModelBuilder().fromPath(modelPath).build()

Verification​

After shrinking the package:

  1. Clean-build the application.
  2. Verify that availableModels() includes every UID used by the application.
  3. Load each model with useModel and verify that no error occurs.
  4. Test every OCR and barcode recognition path used by the application on a physical device.