Vision Framework Causes EXC_BREAKPOINT Error in Xcode App Playground (.swiftpm) File

I’m trying to use the Vision framework in a Swift Playground to perform face detection on an image. The following code works perfectly when I run it in a regular Xcode project, but in an App Playground, I get the error:

Thread 12: EXC_BREAKPOINT (code=1, subcode=0x10321c2a8)

Here's the code:

import SwiftUI
import Vision

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Face Detection")
                .font(.largeTitle)
                .padding()
            
            Image("me")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .onAppear {
                    detectFace()
                }
        }
    }
    
    func detectFace() {
        guard let cgImage = UIImage(named: "me")?.cgImage else { return }

        let request = VNDetectFaceRectanglesRequest { request, error in
            if let results = request.results as? [VNFaceObservation] {
                print("Detected \(results.count) face(s).")
                for face in results {
                    print("Bounding Box: \(face.boundingBox)")
                }
            } else {
                print("No faces detected.")
            }
        }
        
        let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
        do {
            try handler.perform([request]) // This line causes the error.
        } catch {
            print("Failed to perform Vision request: \(error)")
        }
    }
}

The error occurs on this line:

try handler.perform([request])

Details:

  • This code runs fine in a normal Xcode project (.xcodeproj).
  • I'm using an App Playground instead (.swiftpm).
  • The image is being included in the .xcassets folder.

Is there any way I can mitigate this issue? Please do not recommend switching to .xcodeproj, as I am making a submission for Apple's Swift Student Challenge, and they require that I use .swiftpm.

I tried your code and it correctly detected 2 faces in the image supplied. This is with iPad Playgrounds 4.5.1.

I didn’t put the source image in a .xcassets folder though, instead I used the Add File > Photo button at the top left.

Vision Framework Causes EXC_BREAKPOINT Error in Xcode App Playground (.swiftpm) File
 
 
Q