Finde a fil/path in a function

Hi,

i am new to swift an Xcode, just a little experience in Matlab and python basics.

I will trie a code that reads a picture from file and use the OCR engine from vision.

Here my code:

import Cocoa

var greeting = "Hello, playground"

import Foundation
import Vision
import AppKit



func recognizeText(imageName: String) {
    guard let image = NSImage(named: imageName) else {
        print("Failed to load image-2")
        return
    }

    let request = VNRecognizeTextRequest { (request, error) in
        guard let observations = request.results as? [VNRecognizedTextObservation] else { return }

        let recognizedText = observations.map { observation in
            return observation.topCandidates(1).first?.string ?? ""
        }.joined(separator: "\n")

        print("Recognized Text: \(recognizedText)")
    }

    guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
        print("Failed to get CGImage")
        return
    }

    let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:])

    do {
        try requestHandler.perform([request])
    } catch {
        print("Failed to perform request: \(error)")
    }
}

let filePath = "/Users/XXX/Desktop/OCR/OCR/MyPlayground.playground/Resources/Test.png"
let fileURL = URL(fileURLWithPath: filePath)

recognizeText(imageName: "fileURL")

But i still get the Error defined in the function: " Failed to load image-2"

can anyone help me? how can i load that image. Path and file are double checkt multiple times.

thanks!!!

This is not a question about the Swift language.

However, try to build the code in a real Xcode project, and don't forget to add the image to the project resources.

You can just drag and drop the image file into Playground's "Resources" and then drag & drop it from resources onto your source as in the picture:

Then instead of

guard let image = NSImage(named: imageName) else {
    print("Failed to load image-2")
    return
}

just this:

let image = 🖼
// ... use image as usual NSImage