Created
May 26, 2021 04:28
-
-
Save anupamchugh/78b3ccf67af0c27e72927759d088fa18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func imageClassifier(image: UIImage, wordNumber: Int, characterNumber: Int, currentObservation : VNTextObservation){ | |
let request = VNCoreMLRequest(model: model) { [weak self] request, error in | |
guard let results = request.results as? [VNClassificationObservation], | |
let topResult = results.first else { | |
fatalError("Unexpected result type from VNCoreMLRequest") | |
} | |
let result = topResult.identifier | |
let classificationInfo: [String: Any] = ["wordNumber" : wordNumber, | |
"characterNumber" : characterNumber, | |
"class" : result] | |
self?.handleResult(classificationInfo, currentObservation: currentObservation) | |
} | |
guard let ciImage = CIImage(image: image) else { | |
fatalError("Could not convert UIImage to CIImage :(") | |
} | |
let handler = VNImageRequestHandler(ciImage: ciImage) | |
DispatchQueue.global(qos: .userInteractive).async { | |
do { | |
try handler.perform([request]) | |
} | |
catch { | |
print(error) | |
} | |
} | |
} | |
func handleResult(_ result: [String: Any], currentObservation : VNTextObservation) { | |
objc_sync_enter(self) | |
guard let wordNumber = result["wordNumber"] as? Int else { | |
return | |
} | |
guard let characterNumber = result["characterNumber"] as? Int else { | |
return | |
} | |
guard let characterClass = result["class"] as? String else { | |
return | |
} | |
if (textMetadata[wordNumber] == nil) { | |
let tmp: [Int: String] = [characterNumber: characterClass] | |
textMetadata[wordNumber] = tmp | |
} else { | |
var tmp = textMetadata[wordNumber]! | |
tmp[characterNumber] = characterClass | |
textMetadata[wordNumber] = tmp | |
} | |
objc_sync_exit(self) | |
DispatchQueue.main.async { | |
self.doTextDetection(currentObservation: currentObservation) | |
} | |
} | |
func doTextDetection(currentObservation : VNTextObservation) { | |
var result: String = "" | |
if (textMetadata.isEmpty) { | |
print("The image does not contain any text.") | |
return | |
} | |
let sortedKeys = textMetadata.keys.sorted() | |
for sortedKey in sortedKeys { | |
result += word(fromDictionary: textMetadata[sortedKey]!) + " " | |
} | |
observationStringLookup[currentObservation] = result | |
} | |
func word(fromDictionary dictionary: [Int : String]) -> String { | |
let sortedKeys = dictionary.keys.sorted() | |
var word: String = "" | |
for sortedKey in sortedKeys { | |
let char: String = dictionary[sortedKey]! | |
word += char | |
} | |
return word | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment