Coordinate Transforms in iOS using Swift, Part 2: Automatically Finding the Coordinate Frame

A test target of four QR codes surrounding a thick-lined square. Inside of the square are four texts: Ant, Bee, Can, and Dog.
Test target with QR codes a, b, c, d; a thick-bordered rectangle suitable for rectangle detection; and four three-letter words suitable for OCR (optical character recognition).
extension CameraViewController: AVCaptureMetadataOutputObjectsDelegate {
/// Data stream for QR Codes and/or DataMatrix codes.
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
let objects = metadataObjects.compactMap( { $0 as? AVMetadataMachineReadableCodeObject } )
for obj in objects {
if let s = obj.stringValue {
//check for texts starting with "A," "B,"...
//format according to personal taste
if let c = obj.centroid() {
print("\(s) @ (\(c.x), \(c.y))")
}
}
}
}
}
var request = VNRecognizeTextRequest { ...   //...
request.recognitionLevel = .accurate
request.recognitionLanguages = ["en-US", "en-GB"]
request.usesLanguageCorrection = true
request.minimumTextHeight = 0.005 //pick some minimum
}
var request = VNRecognizeTextRequest {observation in observations {
guard let topCandidate = observation.topCandidates(1).first else {
return
}

ocrLoop: for observation in observations {
guard let topCandidate = observation.topCandidates(1).first else {
return
}

// exclude OCR text that falls inside a QR code (
if checkObjectOverlap {
overlapCheck: for sample in self.qrCodes {
let q = sample.value.bounds
if q.intersects(observation.boundingBox) {
if let s = sample.value.text {
print("QR code '\(s)' found as text")
}
else {
print("QR code overlap")
}

continue ocrLoop
}
}
}
let b = observation.boundingBox//...
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store