Il s'agit du cœur d'un outil simple de clique pour extraire le texte d'une image :
let img = NSImage(byReferencing: url)
guard let imgRef = img.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
fputs("Error: failed to convert NSImage to CGImage for '\(url)'\n", stderr)
return 1
}
let request = VNRecognizeTextRequest { (request, error) in
let observations = request.results as? [VNRecognizedTextObservation] ?? []
let obs : [String] = observations.map { $0.topCandidates(1).first?.string ?? ""}
try? obs.joined(separator: "\n").write(to: url.appendingPathExtension("md"), atomically: true, encoding: String.Encoding.utf8)
fputs("got page obs is \(obs)", stderr)
}
request.recognitionLevel = VNRequestTextRecognitionLevel.accurate // or .fast
request.usesLanguageCorrection = true
request.revision = VNRecognizeTextRequestRevision2
request.recognitionLanguages = ["de"]
request.customWords = ["more", "Worte", "Wort"]
try? VNImageRequestHandler(cgImage: imgRef, options: [:]).perform([request])
de ce projet de travail complet