Search code examples
iosxcodetesseractimage-recognitiontext-recognition

xcode/tesseract, use image from photo library


Im using tesseract for text recognition. My problem is to get the photo from the photo library and then use 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

How do I fix that?


Solution

  • You declare your image object as type UIImagePickerController:

    let image = UIImagePickerController()
    

    Yet, you're passing it as a string in UIImage(named: image)?....

    You need to put a string for the initializer UIImage(named: String) like 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.
    There are many tutorials on this topic available online, like this one.