简体   繁体   English

在Android上使用OpenCV提高Tesseract性能

[英]improve Tesseract performance with OpenCV on Android

I am working on a Android application using real-time OCR. 我正在使用实时OCR处理Android应用程序。 I using OpenCV and Tesseract Library. 我使用OpenCV和Tesseract Library。 But the performance is very poor, even on my Galaxy SIII. 但即使在我的Galaxy SIII上,性能也很差。 There are any methods to improve the performance? 有什么方法可以改善性能吗? It is my code: 这是我的代码:

    Mat mGray = new Mat();
capture.retrieve(mGray);
Bitmap bmp = Bitmap.createBitmap(mGray.cols(), mGray.rows(), Bitmap.Config.ARGB_8888);
tessBaseApi.setImage(bmp);
String recognizedText = tessBaseApi.getUTF8Text();
Log.i("Reg", recognizedText);

Will the speed of tesseract OCR be reduced by passing bitmap to the Tesseract API? 将位图传递给Tesseract API会降低tesseract OCR的速度吗? What pre-processing should I perform before passing to the Tesseract API? 在传递给Tesseract API之前,我应该执行哪些预处理?

要尝试的一件事是使用自适应阈值(OpenCV中的adaptiveThreshold)对图像进行二值化。

You can have Tesseract only do the recognition pass 1, so that it skips passes 2 through 9, when it calls recog_all_words() . 你可以让Tesseract只进行识别传递1,这样当它调用recog_all_words()时它会跳过2到9的传递。

Change the following line in baseapi.cpp and rebuild your Tesseract library project: 更改baseapi.cpp中的以下行并重建Tesseract库项目:

if (tesseract_->recog_all_words(page_res_, monitor, NULL, NULL, 0)) {

Change it to: 将其更改为:

if (tesseract_->recog_all_words(page_res_, monitor, NULL, NULL, 1)) {

Some things that might make it faster are: 一些可能使它更快的事情是:

  • Select a smaller region from mGray where your text is, before createBitmap - so the more heavy methods that follow process a smaller image. 在createBitmap之前,从文本所在的mGray中选择一个较小的区域 - 因此,后续处理较小图像的方法较为繁重。
  • Changing Bitmap.Config.ARGB_8888 to Bitmap.Config.RGB_565 - your image is grayscale, it will not need a ARGB bitmap. 将Bitmap.Config.ARGB_8888更改为Bitmap.Config.RGB_565 - 您的图像是灰度的,它不需要ARGB位图。

Use multithreading, but be aware to create one instance per thread for TessBaseAPI. 使用多线程,但要注意为TessBaseAPI为每个线程创建一个实例。 Don't share them between different threads. 不要在不同的线程之间共享它们。 Create N threads (N >= number of cores), and java will make sure that you speed up at least the number of cores times. 创建N个线程(N> =核心数),java将确保至少加快核心次数。

What I do is creating N threads which create TessBaseAPI objects in their own context (in the run method) and wait for OCR requests in a loop until interrupted. 我所做的是创建N个线程,它们在自己的上下文中(在run方法中)创建TessBaseAPI对象,并在循环中等待OCR请求,直到被中断。

    ...
    ...
    @Override
    public void run() {

      TessBaseAPI tessBaseApi = new TessBaseAPI();

      tessBaseApi.init(Ocrrrer.DATA_PATH, "eng");

      setTessVariable(tessBaseApi, "load_system_dawg", "0");
      setTessVariable(tessBaseApi, "load_freq_dawg", "0");
      setTessVariable(tessBaseApi, "load_unambig_dawg", "0");
      setTessVariable(tessBaseApi, "load_punc_dawg", "0");
      setTessVariable(tessBaseApi, "load_number_dawg", "0");
      setTessVariable(tessBaseApi, "load_fixed_length_dawgs", "0");
      setTessVariable(tessBaseApi, "load_bigram_dawg", "0");
      setTessVariable(tessBaseApi, "wordrec_enable_assoc", "0");
      setTessVariable(tessBaseApi, "tessedit_enable_bigram_correction", "0");
      setTessVariable(tessBaseApi, "assume_fixed_pitch_char_segment", "1");
      setTessVariable(tessBaseApi, TessBaseAPI.VAR_CHAR_WHITELIST, "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ<");

      Log.d(TAG, "Training file loaded");


      while (!interrupted()) {
        reentrantLock.lock();
        try {
          Log.d(TAG, this.getName() + " wait for OCR");
          jobToDo.await();
          Log.d(TAG, this.getName() + " input arrived. Do OCR");
          this.ocrResult = doOcr(tessBaseApi);
          ocrDone.signalAll();
        } catch (InterruptedException e) {
          return;
        } finally {
          try {
            reentrantLock.unlock();
          } catch (Exception ex) {
          }
        }
      }

    }
    ...
    ...

You can see that the tessBaseApi object is local to the run method, hence absolutely not shared. 您可以看到tessBaseApi对象是run方法的本地对象,因此绝对不会共享。

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

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