AI を用いた DPM コードの読み取り
サンプルコード
AI を使用して DPM(Direct Part Marking)コードを読み取る方法を説明します。
必要なモデルファイル
AI を用いた DPM コードの読み取りには barcode_dpm モデルを使用します。お使いのモデルに以下のファイルが含まれていることを確認してください。
composite/barcode_dpm.jsondetector/barcode_dpm.bindetector/barcode_dpm.jsonrecognizer/barcode_dpm.binrecognizer/barcode_dpm.json
モデルの選択
DPM コード読み取り用の AI は現在 experimental なモデルです。availableModelsWithExperimental() を使用して、experimental なモデルも含めた一覧を取得してください。
- iOS (Swift)
- Android (Java)
let allModels = edgeOCR.availableModelsWithExperimental()
for candidate in allModels {
if candidate.getUID() == "barcode_dpm" {
model = candidate
}
}
モデルのロード:
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;
}
}
スキャンの実装
DPM コードのスキャンは、OCR と同様に scan メソッドを使用します。
- 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);
// 結果を表示
} catch (EdgeError e) {
Log.e("EdgeOCR", "Failed to scan", e);
} finally {
image.close();
}
});
DPM コードとテキストの同時スキャン
DPM コードとテキストを同時にスキャンしたい場合は、モデルディレクトリに composite/hybrid_dpm_text.json を追加します。
{
"type": "Hybrid",
"barcodeDetectorUid": "barcode_dpm",
"barcodeRecognizerUid": "barcode_dpm",
"textDetectorUid": "detector-d320x320",
"textRecognizerUid": "recognizer"
}
useModel の UID に hybrid_dpm_text を指定してください。
- iOS (Swift)
- Android (Java)
loadModelAndNavigate(
destination: .DPMView,
uid: "hybrid_dpm_text",
experimental: true)
loadModelAndStartActivity(intent, "hybrid_dpm_text", modelSettings);