我已经使用 Vision Kit 框架扫描了图像,并且能够提取字符串中的数据,但我希望它在键值对中,例如字典( [String: Any] )。
private var ocrRequest = VNRecognizeTextRequest(completionHandler: nil)
ocrRequest = VNRecognizeTextRequest { (request, error) in
guard let observations = request.results as? [VNRecognizedTextObservation] else { return }
var ocrText = ""
for observation in observations {
guard let topCandidate = observation.topCandidates(1).first else { return }
ocrText += topCandidate.string + "\n"
}
DispatchQueue.main.async {
self.ocrTextView.text = ocrText
self.scanButton.isEnabled = true
}
}
观察字符串的输出:
East Repair Inc.
Invoice #
US-001
Invoice Date
11/02/2019
P.O.#
2312/2019
Due Date
26/02/2019
我需要一个像这样的输出:
{
"marchantName": "East Repair Inc.",
"invoiceNumber": "US-001",
"invoiceDate": "11/02/2019"
.
.
.
}