メインコンテンツまでスキップ

AI を用いた DPM コードの読み取り

サンプルコード

AI を使用して DPM(Direct Part Marking)コードを読み取る方法を説明します。

必要なモデルファイル

AI を用いた DPM コードの読み取りには barcode_dpm モデルを使用します。お使いのモデルに以下のファイルが含まれていることを確認してください。

  • composite/barcode_dpm.json
  • detector/barcode_dpm.bin
  • detector/barcode_dpm.json
  • recognizer/barcode_dpm.bin
  • recognizer/barcode_dpm.json

モデルの選択

DPM コード読み取り用の AI は現在 experimental なモデルです。availableModelsWithExperimental() を使用して、experimental なモデルも含めた一覧を取得してください。

let allModels = edgeOCR.availableModelsWithExperimental()
for candidate in allModels {
if candidate.getUID() == "barcode_dpm" {
model = candidate
}
}

モデルのロード:

loadModelAndNavigate(
destination: .DPMView,
uid: "barcode_dpm",
experimental: true)

スキャンの実装

DPM コードのスキャンは、OCR と同様に scan メソッドを使用します。

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)
}
}

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 を指定してください。

loadModelAndNavigate(
destination: .DPMView,
uid: "hybrid_dpm_text",
experimental: true)