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.
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
└── ...
| Path | Purpose |
|---|---|
models.json | Defines the model configuration format version and the Composite model UIDs that can be selected by the application. |
composite/<UID>.json | Defines the combination of Detectors and Recognizers used for OCR processing. |
detectors/<UID>.json | Defines the Detector type and settings. |
detectors/<UID>.bin | Contains the model used by the Detector. Some Detector types do not require this file. |
recognizers/<UID>.json | Defines the Recognizer type, alphabet, and other settings. |
recognizers/<UID>.bin | Contains 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:
type | Purpose | Main references |
|---|---|---|
TextOnly | Text recognition | textDetectorUid, textRecognizerUid |
BarcodeOnly | Barcode recognition | barcodeDetectorUid, barcodeRecognizerUid |
Hybrid | Simultaneous text and barcode recognition | Text 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"
}
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.jsondetectors/detector-d320x320.binrecognizers/recognizer-tiny.jsonrecognizers/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
- The UIDs in each Composite JSON file must match the corresponding filenames.
- Do not leave a UID in
models.jsonwhile 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.
- iOS (Swift)
- Android (Java)
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()
The directory to replace is usually:
app/src/main/assets/models/
If you change the directory name, update the path passed to fromAssets to match it.
EdgeVisionAPI api = new EdgeVisionAPI.Builder(context)
.fromAssets("models")
.build();
Verification
After shrinking the package:
- Clean-build the application.
- Verify that
availableModels()includes every UID used by the application. - Load each model with
useModeland verify that no error occurs. - Test every OCR and barcode recognition path used by the application on a physical device.