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!!!