Model Selection and Loading
- iOS: LoadModel.swift / MainView.swift
- Android: MainActivity.java
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.
- iOS (Swift)
- Android (Java)
import EdgeOCRSwift
let modelPath = Bundle.main.path(forResource: "models", ofType: "")!
let edgeOCR = try ModelBuilder().fromPath(modelPath).build()
import com.nefrock.edgeocr.EdgeVisionAPI;
EdgeVisionAPI api = new EdgeVisionAPI.Builder(context)
.fromAssets("models")
.build();
2. Selecting a Model
Use availableModels() to retrieve the list of available models, then select a model by its UID.
- iOS (Swift)
- Android (Java)
var model: Model?
for candidate in edgeOCR.availableModels() {
if candidate.getUID() == "model-d320x320" {
model = candidate
}
}
Model model = null;
for (Model candidate : api.availableModels()) {
if (candidate.getUID().equals("model-d320x320")) {
model = candidate;
break;
}
}
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 UID | Purpose |
|---|---|
model-d256x64 | Text recognition (low resolution, fast) |
model-d256x128 | Text recognition |
model-d320x160 | Text recognition |
model-d320x320 | Text recognition (standard) |
model-d640x640 | Text recognition (high resolution, high accuracy) |
model-d{width}x{height}_with_barcode | Text recognition + barcode recognition |
edgeocr_barcode_default | Barcode recognition |
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.
- iOS (Swift)
- Android (Java)
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()
ModelSettings modelSettings = new ModelSettings();
api.useModel(model, modelSettings,
modelInformation -> {
// Load successful — get aspect ratio and navigate
double aspectRatio = modelInformation.getAspectRatio();
startActivity(intent);
},
edgeError -> {
// Load failed
Toast.makeText(context, edgeError.getMessage(), Toast.LENGTH_LONG).show();
}
);
Implementation Example
This pattern loads a model on app launch or button tap, then navigates to the OCR screen upon completion.
- iOS (Swift)
- Android (Java)
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.
private void loadModelAndStartActivity(Intent intent, ModelSettings modelSettings) {
runOnUiThread(() ->
findViewById(R.id.progressLayout).setVisibility(View.VISIBLE));
EdgeVisionAPI api;
try {
api = new EdgeVisionAPI.Builder(this).fromAssets("models").build();
} catch (EdgeError e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
Model model = null;
for (Model candidate : api.availableModels()) {
if (candidate.getUID().equals("model-d320x320")) {
model = candidate;
break;
}
}
if (model == null) {
Toast.makeText(this, "Model not found", Toast.LENGTH_LONG).show();
return;
}
api.useModel(model, modelSettings,
modelInformation -> {
intent.putExtra("model_aspect_ratio",
modelInformation.getAspectRatio());
startActivity(intent);
},
edgeError -> Toast.makeText(this,
edgeError.getMessage(), Toast.LENGTH_LONG).show()
);
}
In the sample app, this is implemented in app/src/main/java/com/nefrock/edgeocr_example/MainActivity.java.
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.