I am trying to program an app that permits the person to hint Japanese characters, which is able to then use Imaginative and prescient to find out whether or not they have been traced appropriately. To check this, I am trying to make use of English and Japanese characters in my checks, however neither appear to return any observations and due to this fact no recognised strings.
import UIKit
import Imaginative and prescient
func convertCanvasToImage(view: UIView) -> UIImage {
let renderer = UIGraphicsImageRenderer(measurement: view.bounds.measurement)
return renderer.picture { ctx in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}
}
func runVisionRecognition(canvas: Canvas) {
NSLog("Begin runVisionRecognition")
let uiImage = convertCanvasToImage(view: canvas)
guard let cgImage = uiImage.cgImage else { return }
let requestHandler = VNImageRequestHandler(cgImage: cgImage, choices: [:])
let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
request.recognitionLevel = .correct
request.recognitionLanguages = ["en-US"]
//request.minimumTextHeight = 0.1
request.usesLanguageCorrection = true
// request.maximumRecognitionCandidates = 10
do {
attempt requestHandler.carry out([request])
} catch {
NSLog("Uh oh! (error).")
}
}
func recognizeTextHandler(request: VNRequest, error: Error?) {
guard let observations =
request.outcomes as? [VNRecognizedTextObservation] else {
NSLog("Whoops, observations like (request.outcomes)")
return
}
let recognizedStrings = observations.compactMap { remark in
return remark.topCandidates(1).first?.string
}
NSLog("Statement: (observations)")
NSLog("Recognised Strings: (recognizedStrings)")
}
My characters come from a Canvas that I then translate into a picture to be fed into Imaginative and prescient (see the highest operate).
Beneath are some examples of my handwriting on the Canvas. NSLog returns ‘Statement: [], Recognised Strings: []’, however as you possibly can see, the Images app appears to recognise the inputs superb!
View of my app, with the letters drawn onto a Canvas
My concept is one thing goes fallacious between the conversion from Canvas to cgImage, however in the event you look within the first picture, the highest ‘APPLE’ is a transformed picture from my backside drawing of ‘APPLE’.