Skip to main content

OCR with Master Data (Exact Match)

This section explains how to check whether OCR results match entries in master data (whitelist) and display highlights or dialogs when a match is found.

Display when matching master data

Implementing the Analyzer​

Implement an Analyzer class that compares detection results against master data. It classifies text into those that match the master data and those that do not.

class WhiteListAnalyzer {
let whiteList: Set = [
"090-1234-5678",
"090-0000-1234",
"090-2222-3456",
"090-4444-5555",
"090-6666-7777",
"090-8888-9999",
]

func analyze(_ detections: [Text]) -> AnalyzerResult {
var targetDetections: [Text] = []
var notTargetDetections: [Text] = []

for detection in detections {
let text = detection.getText()
if whiteList.contains(text) {
targetDetections.append(detection)
} else {
notTargetDetections.append(detection)
}
}

return AnalyzerResult(
targetDetections: targetDetections,
notTargetDetections: notTargetDetections)
}
}

Displaying Detection Results​

Text that matches the master data is displayed in green, while non-matching text is displayed in red.

func drawDetections(result: ScanResult) {
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
detectionLayer.sublayers = nil

let detections = result.getTextDetections()
let analyzerResult = analyzer.analyze(detections)

for targetDetection in analyzerResult.getTargetDetections() {
let text = targetDetection.getText()
let bbox = targetDetection.getBoundingBox()
drawDetection(bbox: bbox, text: text) // Green (default)
}

for notTargetDetection in analyzerResult.getNotTargetDetections() {
let text = notTargetDetection.getText()
let bbox = notTargetDetection.getBoundingBox()
drawDetection(
bbox: bbox, text: text,
boxColor: UIColor.red.withAlphaComponent(0.5).cgColor)
}

CATransaction.commit()
}