0

I had scan the image using Vision Kit framework and I was able to extract data in string but I want it in key value pair such like dictionary( [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
}
}

Output of the Observation String:

East Repair Inc.
Invoice #
US-001
Invoice Date
11/02/2019
P.O.#
2312/2019
Due Date
26/02/2019

and I need an output like:

{
  "marchantName": "East Repair Inc.",
  "invoiceNumber": "US-001",
  "invoiceDate": "11/02/2019"
   .
   .
   .
}

devang bhatt
  • 460
  • 2
  • 15
  • A dictionary of what? A key value pair, but do you have an example of what values (`observations`) you have, and what should look like the dictionary? – Larme Sep 17 '20 at 07:49
  • So your questions is more about transforming `[String]` into `[String: String]` where you know "the format/logic/order" behind the array. But could there be other order, or except for the name, the invoiceNumber will always have before "Invoice #", etc.? – Larme Sep 17 '20 at 12:22
  • The format will never be same always for all the Receipt so how would I can able to make a dynamic model for all the receipt? if you have a demo/example then please share. Is there is any other way to extract data from image? – devang bhatt Sep 17 '20 at 12:47
  • Not even the fact that `Invoice Date` will be before the date? If not, how are you supposed to make sense. It's like getting three random pages from 3 different book, and ask you to sort them by the book title. How are you supposed to know from which book it comes from? – Larme Sep 17 '20 at 12:55
  • So is there any way to get JSON from observations? – devang bhatt Sep 18 '20 at 05:27

0 Answers0