UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

VNImageRequestHandler crash

Forums > Swift

I was playing around with the Vision framework to detect text from images based on this article: https://www.hackingwithswift.com/example-code/vision/how-to-use-vnrecognizetextrequests-optical-character-recognition-to-detect-text-in-an-image

However I'm running into some issues. In this line:

let handler = VNImageRequestHandler(cgImage: img, options: [:])

XCode is telling me that I need to explicitly cast the img as cgimage like this instead

let handler = VNImageRequestHandler(cgImage: img as! CGImage, options: [:]) 

But after this I keep getting a crash at the same line:

Any ideas why this might happen? The image is in the app bundle, so this shouldn't be the issue, also casting should work fine...

2      

I've tried a few alternative solutions but haven't found a working one yet. Currently it seems that casting it to cgImage, which wasn't part of the sample code, but which the compiler requires, is causing the issue.

If I put a breakpoint and look at the NSImage (not UIImage as it's a macOS project), then the image is shown correctly, however later when it's cast to cgImage, then it seems to break, but not sure what's causing this.

Any ideas are welcome :)

2      

Hi.

I could get it running in a Playground with the following code:

DispatchQueue.global(qos: .userInitiated).async {
    guard let nsImg = NSImage(named: "testImage.png") else {
        fatalError("missing image!")
    }
    guard let cgImg = nsImg.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
        fatalError("can't convert image")
    }

    let handler = VNImageRequestHandler(cgImage: cgImg, options: [:])
    try? handler.perform(requests)
}

2      

Wow, a few people stuck on this problem... today even! I'm having trouble with the CVImageBuffer and CGImage initiliazers too, will try to convert to a CMBuffer next, but that doesn't look too easy. I'm using the lastest Xcode after a full update to both OS, iOS and Xcode. I however get a seg fault and no better descrition except that it's While cross-referencing conformance for 'VNRecognizedPointKey' (in module 'Vision'). Hope some solutions are out there soon!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.