gpt4 book ai didi

android - 在异步方法中使用 Tesseract 代码

转载 作者:行者123 更新时间:2023-11-29 02:01:27 24 4
gpt4 key购买 nike

我正在构建一个在异步调用中使用 Tesseract 库的 Android 应用。执行 OCR 的方法非常有效,直到我在不同的线程上调用它。我得到的异常(exception)是:

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

2: System.TypeInitializationException: An exception was thrown by the type initializer for 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 初始化无法在异步方法中完成,因此通过从方法调用中删除初始化并在其他地方进行初始化,我的问题得到了解决。

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);
}

关于android - 在异步方法中使用 Tesseract 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com