【问题标题】:Using Tesseract code in async method在异步方法中使用 Tesseract 代码
【发布时间】:2012-09-19 09:18:53
【问题描述】:

我正在构建一个在异步调用中使用 Tesseract 库的 Android 应用。 执行 OCR 的方法效果很好,直到我在不同的线程上调用它。我得到的例外是:

*1: java.Lang.Unsatisfiedlinkerror: Library opencv_core not found;试过 [/vendor/lib/libopencv_core.so, /system/lib/libopencv_core.so]

2:System.TypeInitializationException:Emgu.CV.CvInvoke* 的类型初始化程序引发了异常

我正在使用的代码:

protected override void OnStart()
{
    base.OnStart();
    ImageView testimage = FindViewById<ImageView>(Resource.Id.TestImage);
    testimage.SetImageBitmap(ScanImage);

    ThreadPool.QueueUserWorkItem(state => {
        PerformOCR();
    });
}

protected void PerformOCR() //object state
{
    //Get the tesseract directory
    Java.IO.File myDir = Android.OS.Environment.ExternalStorageDirectory;
    Tesseract _ocr = new Tesseract(myDir.ToString() + "/tesseract/", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);

    //Image optimization
    Image<Gray, Byte> passport = new Image<Gray, Byte>(ScanImage);
    Image<Gray, float> sobel = passport.Sobel(1, 0, 5);
    sobel.Add(passport.Sobel(0, 1, 5));
    passport = passport.Add(sobel.Convert<Gray, byte>());           

    _ocr.Recognize(passport);
    string text = _ocr.GetText();
    RunOnUiThread(() => SpecificationsText.Text = text); 
}

OCR 中使用的图像存储在静态类中。

【问题讨论】:

  • 错误发生在 tesseract 代码中的某处(即使这是一个引用的库..)因此我开始认为这可能是因为第二个线程没有足够的内存来执行?还是不允许访问sdcard?有人知道android中实际的多线程是如何工作的吗?

标签: android asynchronous xamarin.android tesseract


【解决方案1】:

好的,我已经解决了我自己的问题。

由于某种原因,无法在异步方法中完成 tesseract 初始化,因此通过从方法调用中删除初始化并在其他地方进行,我的问题得到了解决。

protected override void OnStart()
{
    base.OnStart();
    ImageView testimage = FindViewById<ImageView>(Resource.Id.TestImage);
    testimage.SetImageBitmap(ScanImage);

    //Get the tesseract directory
    Java.IO.File myDir = Android.OS.Environment.ExternalStorageDirectory;
    Tesseract _ocr = new Tesseract(myDir.ToString() + "/tesseract/", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);

    ThreadPool.QueueUserWorkItem(state => {
        PerformOCR(_ocr);
    });
}

protected void PerformOCR(Tesseract _ocr)
{
    //Image optimization
    Image<Gray, Byte> passport = new Image<Gray, Byte>(ScanImage);
    Image<Gray, float> sobel = passport.Sobel(1, 0, 5);
    sobel.Add(passport.Sobel(0, 1, 5));
    passport = passport.Add(sobel.Convert<Gray, byte>());           

    _ocr.Recognize(passport);
    string text = _ocr.GetText();
    RunOnUiThread(() => SpecificationsText.Text = text); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多