23

我正在使用适用于 iOS 11 的 Vision 框架来检测图像上的文本。

文本已成功检测到,但是我们如何获取检测到的文本?

4

3 回答 3

4

在 Apple Vision 中,您可以使用VNRecognizeTextRequest类轻松地从图像中提取文本,允许您发出图像分析请求以查找和识别图像中的文本。

VNRecognizeTextRequest从 iOS 13.0 和 macOS 10.15 开始工作

这是一个代码片段,向您展示如何做到这一点:

let requestHandler = VNImageRequestHandler(url: imageURL, options: [:])

let request = VNRecognizeTextRequest { (request, error) in

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

    for observation in observations {

        let topCandidate: [VNRecognizedText] = observation.topCandidates(1)

        if let recognizedText: VNRecognizedText = topCandidate.first {
            label.text = recognizedText.string
        }
    }
}

然后你必须为recognitionLevel实例属性分配一个值:

// non-realtime asynchronous but accurate text recognition
request.recognitionLevel = VNRequestTextRecognitionLevel.accurate

// nearly realtime but not-accurate text recognition
request.recognitionLevel = VNRequestTextRecognitionLevel.fast

try? requestHandler.perform([request])
于 2020-03-15T12:01:09.770 回答
0

不完全是骗子,但类似于:Converting a Vision VNTextObservation to a String

您需要使用 CoreML 或其他库来执行 OCR(SwiftOCR 等)

于 2017-06-19T04:29:26.083 回答
-7

这将在检测到的文本上返回带有矩形框的叠加图像

这是完整的 xcode 项目 https://github.com/cyruslok/iOS11-Vision-Framework-Demo

希望对您有所帮助

// Text Detect
func textDetect(dectect_image:UIImage, display_image_view:UIImageView)->UIImage{
    let handler:VNImageRequestHandler = VNImageRequestHandler.init(cgImage: (dectect_image.cgImage)!)
    var result_img:UIImage = UIImage.init();

    let request:VNDetectTextRectanglesRequest = VNDetectTextRectanglesRequest.init(completionHandler: { (request, error) in
        if( (error) != nil){
            print("Got Error In Run Text Dectect Request");

        }else{
            result_img = self.drawRectangleForTextDectect(image: dectect_image,results: request.results as! Array<VNTextObservation>)
        }
    })
    request.reportCharacterBoxes = true
    do {
        try handler.perform([request])
        return result_img;
    } catch {
        return result_img;
    }
}

func drawRectangleForTextDectect(image: UIImage, results:Array<VNTextObservation>) -> UIImage {
    let renderer = UIGraphicsImageRenderer(size: image.size)
    var t:CGAffineTransform = CGAffineTransform.identity;
    t = t.scaledBy( x: image.size.width, y: -image.size.height);
    t = t.translatedBy(x: 0, y: -1 );

    let img = renderer.image { ctx in
        for item in results {
            let TextObservation:VNTextObservation = item
            ctx.cgContext.setFillColor(UIColor.clear.cgColor)
            ctx.cgContext.setStrokeColor(UIColor.green.cgColor)
            ctx.cgContext.setLineWidth(1)
            ctx.cgContext.addRect(item.boundingBox.applying(t))
            ctx.cgContext.drawPath(using: .fillStroke)

            for item_2 in TextObservation.characterBoxes!{
                let RectangleObservation:VNRectangleObservation = item_2
                ctx.cgContext.setFillColor(UIColor.clear.cgColor)
                ctx.cgContext.setStrokeColor(UIColor.red.cgColor)
                ctx.cgContext.setLineWidth(1)
                ctx.cgContext.addRect(RectangleObservation.boundingBox.applying(t))
                ctx.cgContext.drawPath(using: .fillStroke)
            }
        }

    }
    return img
}
于 2017-06-16T02:50:25.500 回答