简体   繁体   English

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

[英]xcode/tesseract, use image from photo library

Im using tesseract for text recognition. 我正在使用tesseract进行文本识别。 My problem is to get the photo from the photo library and then use tesseract. 我的问题是从照片库中获取照片,然后使用tesseract。

My code: 我的代码:

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) %")
    }

}

in the line: 在行中:

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

it says: 它说:

Cannot convert value of type UIImagePickerController 无法转换类型为UIImagePickerController的值

How do I fix that? 我该如何解决?

You declare your image object as type UIImagePickerController : 您将image对象声明为UIImagePickerController类型:

let image = UIImagePickerController()

Yet, you're passing it as a string in UIImage(named: image)?... . 但是,您正在将它作为字符串传递给UIImage(named: image)?...

You need to put a string for the initializer UIImage(named: String) like UIImage(named: "myImage.png") . 您需要为初始化器UIImage(named: String)放置一个字符串,例如UIImage(named: "myImage.png")
If you want the user to be able to pick an image and then process it, you need to get the image picked from the UIImagePickerController and then process that one. 如果希望用户能够拾取图像然后对其进行处理,则需要从UIImagePickerController拾取图像,然后对其进行处理。
There are many tutorials on this topic available online, like this one . 有关于这一主题提供许多在线教程,像这一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM