将 Vision VNTextObservation 转换为字符串 [英] Converting a Vision VNTextObservation to a String

查看:36
本文介绍了将 Vision VNTextObservation 转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 Apple 的 Vision API 文档,我看到有几个类与UIImages中的文本检测相关:

I'm looking through the Apple's Vision API documentation and I see a couple of classes that relate to text detection in UIImages:

1) class VNDetectTextRectanglesRequest

2) class VNTextObservation

看起来他们可以检测字符,但我看不到对字符执行任何操作的方法.一旦检测到字符,您将如何将它们转换为可以被 <解释的内容代码>NSLinguisticTagger?

It looks like they can detect characters, but I don't see a means to do anything with the characters. Once you've got characters detected, how would you go about turning them into something that can be interpreted by NSLinguisticTagger?

这是一篇文章,简要概述了愿景.

Here's a post that is a brief overview of Vision.

感谢您的阅读.

推荐答案

Apple 终于更新 Vision 做 OCR.打开一个 Playground 并在 Resources 文件夹中转储几个测试图像.就我而言,我称它们为demoDocument.jpg"和demoLicensePlate.jpg".

Apple finally updated Vision to do OCR. Open a playground and dump a couple of test images in the Resources folder. In my case, I called them "demoDocument.jpg" and "demoLicensePlate.jpg".

新类名为VNRecognizeTextRequest.把它扔到操场上试一试:

The new class is called VNRecognizeTextRequest. Dump this in a playground and give it a whirl:

import Vision

enum DemoImage: String {
    case document = "demoDocument"
    case licensePlate = "demoLicensePlate"
}

class OCRReader {
    func performOCR(on url: URL?, recognitionLevel: VNRequestTextRecognitionLevel)  {
        guard let url = url else { return }
        let requestHandler = VNImageRequestHandler(url: url, options: [:])

        let request = VNRecognizeTextRequest  { (request, error) in
            if let error = error {
                print(error)
                return
            }

            guard let observations = request.results as? [VNRecognizedTextObservation] else { return }

            for currentObservation in observations {
                let topCandidate = currentObservation.topCandidates(1)
                if let recognizedText = topCandidate.first {
                    print(recognizedText.string)
                }
            }
        }
        request.recognitionLevel = recognitionLevel

        try? requestHandler.perform([request])
    }
}

func url(for image: DemoImage) -> URL? {
    return Bundle.main.url(forResource: image.rawValue, withExtension: "jpg")
}

let ocrReader = OCRReader()
ocrReader.performOCR(on: url(for: .document), recognitionLevel: .fast)

WWDC19 对此进行了深入讨论

There's an in-depth discussion of this from WWDC19

这篇关于将 Vision VNTextObservation 转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆