HomeiOS DevelopmentSwift iOS - Imaginative and prescient doesn't return any observations from cgImage

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


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 accurately. To check this, I am trying to make use of English and Japanese characters in my exams, however neither appear to return any observations and subsequently no recognised strings.

import UIKit
import Imaginative and prescient

func convertCanvasToImage(view: UIView) -> UIImage {
    let renderer = UIGraphicsImageRenderer(dimension: view.bounds.dimension)
    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 { remark in
        return remark.topCandidates(1).first?.string
    }
    
    NSLog("Commentary: (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).

Under are some examples of my handwriting on the Canvas. NSLog returns ‘Commentary: [], Recognised Strings: []’, however as you may see, the Images app appears to recognise the inputs high quality!

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

Screenshot from the Images app, exhibiting the letters being learn clearly (each English and Japanese)

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments