iOS Development

Swift iOS – Imaginative and prescient doesn’t return any observations from cgImage

Spread the love


I am trying to program an app that enables 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 {
        strive 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 { commentary in
        return commentary.topCandidates(1).first?.string
    }
    
    NSLog("Remark: (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 perform).

Beneath are some examples of my handwriting on the Canvas. NSLog returns ‘Remark: [], Recognised Strings: []’, however as you possibly can see, the Photographs app appears to recognise the inputs wonderful!

View of my app, with the letters drawn onto a Canvas

Screenshot from the Photographs app, displaying the letters being learn clearly (each English and Japanese)

My concept is one thing goes flawed between the conversion from Canvas to cgImage, however if you happen to look within the first picture, the highest ‘APPLE’ is a transformed picture from my backside drawing of ‘APPLE’.

Leave a Reply

Your email address will not be published. Required fields are marked *