func processImageWithVision(_ image: NSImage) {
guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
print("Failed to convert NSImage to CGImage.")
return
}
let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:])
let request = VNRecognizeTextRequest { (request, error) in
guard let observations = request.results as? [VNRecognizedTextObservation] else {
print("Unexpected result type from VNRecognizeTextRequest.")
return
}
for observation in observations {
guard let topCandidate = observation.topCandidates(1).first else { continue }
print("Recognized text: \(topCandidate.string)")
}
}
do {
try requestHandler.perform([request])
} catch {
print("Failed to perform text recognition: \(error)")
}
}
func foo() {
// Example usage:
if let svgURL = URL(fileURLWithPath: "/Users/hboon/Documents/workspace/playground/remarkable-rmc-test/test.svg")
let rasterImage = convertSVGToImage(svgURL: svgURL) {
processImageWithVision(rasterImage)
}
}
import SVGKit import AppKit import Vision
func convertSVGToImage(svgURL: URL) -> NSImage? { guard let svgImage = SVGKImage(contentsOf: svgURL) else { print("Failed to load SVG file.") return nil }
}
func processImageWithVision(_ image: NSImage) { guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else { print("Failed to convert NSImage to CGImage.") return }
}
func foo() { // Example usage: if let svgURL = URL(fileURLWithPath: "/Users/hboon/Documents/workspace/playground/remarkable-rmc-test/test.svg") let rasterImage = convertSVGToImage(svgURL: svgURL) { processImageWithVision(rasterImage) } }