xcode/tesseract,使用照片库中的图片

xcode/tesseract, use image from photo library

我正在使用 tesseract 进行文本识别。 我的问题是从照片库中获取照片,然后使用 tesseract。

我的代码:

import UIKit
import TesseractOCR

class ViewController: UIViewController, G8TesseractDelegate, 
UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var TextView: UITextView!
@IBAction func takePhoto(_ sender: UIButton) {

    let image = UIImagePickerController()
    image.delegate = self

    image.sourceType = UIImagePickerControllerSourceType.photoLibrary

    image.allowsEditing = false


    self.present(image, animated: true){

    }


    if let tesseract = G8Tesseract(language: "dan+eng") {
        tesseract.delegate = self
        tesseract.image = UIImage(named: image)?.g8_blackAndWhite()
        tesseract.recognize()

        TextView.text = tesseract.recognizedText
    }
    func progressImageRecognition(for tesseract: G8Tesseract!) {
        print("Recognition Progress \(tesseract.progress) %")
    }

}

行中:

tesseract.image = UIImage(named: image)?.g8_blackAndWhite()

它说:

Cannot convert value of type UIImagePickerController

我该如何解决?

您将 image 对象声明为类型 UIImagePickerController:

let image = UIImagePickerController()

然而,您在 UIImage(named: image)?....

中将其作为字符串传递

您需要为初始值设定项 UIImage(named: String) 添加一个字符串,例如 UIImage(named: "myImage.png")
如果您希望用户能够选择图像然后处理它,您需要从 UIImagePickerController 中选择图像然后处理该图像。
网上有很多关于这个主题的教程,比如 this one.