Код: Выделить всё
func recongizeText()
{
// Create a new image-request handler.
let cgImage = self.nsImage!.cgImage(forProposedRect: nil, context: nil, hints: nil)
let requestHandler = VNImageRequestHandler(cgImage: cgImage!)
// Create a new request to recognize text.
let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
do {
// Perform the text-recognition request.
try requestHandler.perform([request])
} catch {
print("Unable to perform the requests: \(error).")
}
}
func recognizeTextHandler(request: VNRequest, error: Error?) {
guard let observations =
request.results as? [VNRecognizedTextObservation] else {
return
}
let recognizedStrings = observations.compactMap { observation in
// Return the string of the top VNRecognizedText instance.
return observation.topCandidates(1).first?.string
}
let boundingRects: [CGRect] = observations.compactMap { observation in
// Find the top observation.
guard let candidate = observation.topCandidates(1).first else { return .zero }
// Find the bounding-box observation for the string range.
let stringRange = candidate.string.startIndex..
Подробнее здесь: [url]https://stackoverflow.com/questions/78368520/vision-bounding-box-is-offset-in-the-y-coordinates[/url]