Skip to main content

Model Selection and Loading

Sample Code

Since the model's aspect ratio is used to determine the drawing area for OCR results, we recommend loading the model before navigating to the OCR screen.

1. Initializing EdgeVisionAPI​

Initialize EdgeVisionAPI by specifying the path to the model directory.

import EdgeOCRSwift

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

2. Selecting a Model​

Use availableModels() to retrieve the list of available models, then select a model by its UID.

var model: Model?
for candidate in edgeOCR.availableModels() {
if candidate.getUID() == "model-d320x320" {
model = candidate
}
}

Available Models​

Refer to models.json in the model folder for the available models. The models you can specify vary depending on the EdgeOCR engine version.

For details about the model file layout and reducing application size by excluding unused models, see Model Package Structure and Shrinking.

General-purpose model names include the model resolution in the format model-d{width}x{height}. Higher resolution improves accuracy but reduces processing speed. Choose according to your use case and device specifications.

Model UIDPurpose
model-d256x64Text recognition (low resolution, fast)
model-d256x128Text recognition
model-d320x160Text recognition
model-d320x320Text recognition (standard)
model-d640x640Text recognition (high resolution, high accuracy)
model-d{width}x{height}_with_barcodeText recognition + barcode recognition
edgeocr_barcode_defaultBarcode recognition
Using OCR and Barcode Recognition Simultaneously

To perform text recognition and barcode recognition simultaneously, specify model-d{width}x{height}_with_barcode (e.g., model-d320x320_with_barcode). Text-only models and edgeocr_barcode_default only recognize one type.

Custom models are also selected by specifying their UID in the same way.

3. Loading the Model​

Use useModel to load the model. You can pass ModelSettings as the second argument to configure detection filters, TextMapper, and other settings.

let modelSettings = ModelSettings()
let modelInfo = try await edgeOCR.useModel(model, settings: modelSettings)

// Get the model's aspect ratio (used for drawing OCR results)
let aspectRatio = modelInfo.getAspectRatio()

Implementation Example​

This pattern loads a model on app launch or button tap, then navigates to the OCR screen upon completion.

func loadModelAndNavigate(
destination: EdgeOCRSampleKind,
uid: String = "model-d320x320",
modelSettings: ModelSettings = ModelSettings()
) {
Task {
isLoading = true
do {
let info = try await loadModel(
path: modelPath, uid: uid, modelSettings: modelSettings)
aspectRatio = info!.getAspectRatio()
path.append(destination)
} catch {
showModelAlert = true
modelAlertContent = "Failed\n" + error.localizedDescription
}
isLoading = false
}
}

In the sample app, this is implemented in EdgeOCRSample/Models/LoadModel/LoadModel.swift and EdgeOCRSample/Views/Main/MainView.swift.

GPU Model Loading Time​

Models that use the GPU take several seconds to load only on the first load. Lower-end devices tend to take longer. From the second load onward, loading is fast, and it will not take several seconds unless the app is deleted.