Improving Accuracy with Multiple Scans
- iOS: NtimesViewController.swift / NtimesView.swift
- Android: NtimesTextScanActivity.java
To improve OCR accuracy, the SDK provides a feature that determines whether the same text has been read consecutively across multiple frames.
Setting the Number of Reads
Use ModelSettings to configure the number of consecutive reads required. The default is 1.
- iOS (Swift)
- Android (Java)
// Confirm after reading the same content 5 times
let modelSettings = ModelSettings(textNToConfirm: 5)
// Confirm after reading the same content 5 times
ModelSettings settings = new ModelSettings();
settings.setNToConfirm(5);
Checking the Confirmation Status
Use the getStatus() method of Detection to check whether the status is ScanConfirmationStatus.Confirmed.
- iOS (Swift)
- Android (Java)
if status == ScanConfirmationStatus.Confirmed {
let bbox = detection.getBoundingBox()
drawDetection(bbox: bbox, text: text)
}
for (Text detection : scanResult.getTextDetections()) {
String text = detection.getText();
if (detection.getStatus() == ScanConfirmationStatus.Confirmed) {
// Reading confirmed
filteredDetections.add(detection);
}
}
TextMapper
During multiple reads, camera shake or movement can cause slight variations in the recognition results, which resets the confirmation count. You can use a TextMapper to normalize the recognition results and mitigate this issue.
Subclass the TextMapper class and implement the map (iOS) / apply (Android) method. The following example converts letters and symbols to digits when reading postal codes.
- iOS (Swift)
- Android (Java)
class PostcodeTextMapper: TextMapper {
let regex = /^\D*(\d{3})-(\d{4})\D*$/
override func map(_ text: Text) -> String {
var t = text.getText()
t = t.replacingOccurrences(of: "A", with: "4")
t = t.replacingOccurrences(of: "B", with: "8")
t = t.replacingOccurrences(of: "b", with: "6")
t = t.replacingOccurrences(of: "O", with: "0")
t = t.replacingOccurrences(of: "o", with: "0")
t = t.replacingOccurrences(of: "I", with: "1")
t = t.replacingOccurrences(of: "l", with: "1")
t = t.replacingOccurrences(of: "S", with: "5")
t = t.replacingOccurrences(of: "Z", with: "2")
// ... other conversions
if let match = t.wholeMatch(of: regex) {
t = String(match.1) + "-" + String(match.2)
}
return t
}
}
Configuration:
let modelSettings = ModelSettings(textNToConfirm: 5)
modelSettings.setTextMapper(PostcodeTextMapper())
public class PostCodeTextMapper extends TextMapper {
private final Pattern regexPattern;
public PostCodeTextMapper() {
regexPattern = Pattern.compile("^.*((\\d{3})-(\\d{4})).*$");
}
@Override
public String apply(Text text) {
String t = text.getText();
t = t.replace("A", "4");
t = t.replace("B", "8");
t = t.replace("b", "6");
t = t.replace("O", "0");
t = t.replace("o", "0");
t = t.replace("I", "1");
t = t.replace("l", "1");
t = t.replace("S", "5");
t = t.replace("Z", "2");
// ... other conversions
Matcher m = regexPattern.matcher(t);
if (m.find()) {
t = m.group(1);
}
return t;
}
}
Configuration:
ModelSettings settings = new ModelSettings();
settings.setNToConfirm(5);
settings.setTextMapper(new PostCodeTextMapper());