Reading DPM Codes with AI
Sample Code
This section explains how to read DPM (Direct Part Marking) codes using AI.
Required Model Files
Reading DPM codes with AI uses the barcode_dpm model. Make sure your model includes the following files:
composite/barcode_dpm.jsondetector/barcode_dpm.bindetector/barcode_dpm.jsonrecognizer/barcode_dpm.binrecognizer/barcode_dpm.json
Model Selection
The AI for DPM code reading is currently an experimental model. Use availableModelsWithExperimental() to retrieve a list that includes experimental models.
- iOS (Swift)
- Android (Java)
let allModels = edgeOCR.availableModelsWithExperimental()
for candidate in allModels {
if candidate.getUID() == "barcode_dpm" {
model = candidate
}
}
Loading the model:
loadModelAndNavigate(
destination: .DPMView,
uid: "barcode_dpm",
experimental: true)
List<Model> allModels = api.availableModelsWithExperimental();
for (Model candidate : allModels) {
if (candidate.getUID().equals("barcode_dpm")) {
model = candidate;
}
}
Implementing the Scanner
DPM code scanning uses the scan method, just like OCR.
- iOS (Swift)
- Android (Java)
func captureOutput(
_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection
) {
let scanResult: ScanResult
do {
scanResult = try edgeOCR.scan(sampleBuffer, viewBounds: viewBounds)
} catch {
os_log("Failed to scan: %@", type: .debug, error.localizedDescription)
return
}
DispatchQueue.main.async { [weak self] in
self?.drawDetections(result: scanResult)
}
}
imageAnalysis.setAnalyzer(analysisExecutor, image -> {
if (!api.isReady()) {
image.close();
return;
}
try {
ScanResult scanResult = api.scan(image);
// Display results
} catch (EdgeError e) {
Log.e("EdgeOCR", "Failed to scan", e);
} finally {
image.close();
}
});
Scanning DPM Codes and Text Simultaneously
To scan DPM codes and text at the same time, add composite/hybrid_dpm_text.json to the model directory.
{
"type": "Hybrid",
"barcodeDetectorUid": "barcode_dpm",
"barcodeRecognizerUid": "barcode_dpm",
"textDetectorUid": "detector-d320x320",
"textRecognizerUid": "recognizer"
}
Specify hybrid_dpm_text as the UID in useModel.
- iOS (Swift)
- Android (Java)
loadModelAndNavigate(
destination: .DPMView,
uid: "hybrid_dpm_text",
experimental: true)
loadModelAndStartActivity(intent, "hybrid_dpm_text", modelSettings);