zoukankan      html  css  js  c++  java
  • Build OpenCV text(OCR) module on windows (Old and Obsolete)

    Background.

    AOI software needs to use the OCR feature to recognize the texts on the chips. Because our vision software is based on OpenCV, so the first choice is text module in opencv_contrib. 

    Procedures.

    1. OCR module is not in standard OpenCV package. It is in text module of OpenCV_Contrib. It can be downloaded from opencv_contrib.

    2. The core of OCR is using Tesseract, and Tesseract depends on Leptonica, so need to build Leptonica and Tesseract first.

    3. Get the Leptonica from https://github.com/charlesw/tesseract-vs2012. This solution can directly build. The output is liblept171d.dll and liblept171d.lib.

    4. Get the Tesseract from https://github.com/tesseract-ocr/tesseract. Create the .includeleptonica folder, copy all the header file from . esseract-vs2012libleptinclude (The root folder is in step 3) to it. Create the .lib folder, copy all the  step 3 build generated files(The .dll and .lib) in . esseract-vs2012uildlib to it.  Set the project property of tesseract, change the include folder path "......include" and "......includeleptonica" to "....include" and "....includeleptonica". Then can build the Tessrect project, the output is libtesseract304d.dll and libtesseract304d.lib.

    The AddIdTohOCR has compile error, change the content as below can solve the problem.

    static void AddIdTohOCR(STRING* hocr_str, const std::string base, int num1, int num2) {
        unsigned long bufsize = base.length() + 2 * kMaxIntSize;
        char *id_buffer = new char[bufsize];
        if (num2 >= 0) {
            snprintf(id_buffer, bufsize - 1, "%s_%d_%d", base.c_str(), num1, num2);
        }
        else {
            snprintf(id_buffer, bufsize - 1, "%s_%d", base.c_str(), num1);
        }
        id_buffer[bufsize - 1] = '';
        *hocr_str += " id='";
        *hocr_str += id_buffer;
        *hocr_str += "'";
        delete[]id_buffer;
    }

    5. Use CMake to config the OpenCV solution. Copy the text module from opencv_contrib to .OpenCVsourcesmodules. Run Cmake_Gui, there are 3 options need to set. Lept_library, Tesseract_Include_Dir, Tesseract_Library. Tesseract_Include_Dir set to ../.../tesseract/API. After set, can run CMake to config and generate the solution.

    6. Open the OpenCV solution. Open the .modules extsrcprecomp.hpp file, change the include path to as below.

    #ifdef HAVE_TESSERACT
    //#include <tesseract/baseapi.h>
    //#include <tesseract/resultiterator.h>
    #include "baseapi.h"
    #include "resultiterator.h"
    #endif
    

    If there are header files can not find errors, find and copy them from tesseract to the tesseract/API folder. As i test, below files need to be copied.

    #include ""resultiterator.h""
    #include "platform.h"
    #include "apitypes.h"
    #include "thresholder.h"
    #include "unichar.h"
    #include "tesscallback.h"
    #include "publictypes.h"
    #include "pageiterator.h"
    #include "resultiterator.h"
    #include "host.h" 
    #include "ltrresultiterator.h"

    There may be a compile error in function static double NFA(int n, int k, double p, double logNT) with std::numeric_limits<double>::min(); Add below code before the function to fix it.

    #undef max 
    #undef min

    7. Download the language test data from https://github.com/tesseract-ocr/tessdata. What i use is the eng.traineddata. Put it to . esseract essdata.

    8. After build OpenCV successfully, then you can create the TestOpenCV project with the below function, before running it, need to copy the liblept171d.dll and libtesseract304d.dll to the output folder(where the exe file is put).

    using OCRTesseract =  cv::text::OCRTesseract;
    void TestOCR()
    {    
        cv::Mat mat = cv::imread(".\data\OCRTest.png");
        if ( mat.empty() )
            return;
    
        std::string output_text;
        char *dataPath = "C:/tesseract-build/tesseract/tessdata";
        cv::Ptr<OCRTesseract> ptrOcr = OCRTesseract::create(dataPath);
        ptrOcr->run(mat, output_text );
        cout << output_text << endl;
    }

     9. The Tesseract is sensitive to the text orientation. So need to make the text face up to be better recognized.

  • 相关阅读:
    搭建VirtoCommerce2.6开发环境,同官方dev分支保持同步(上)
    VirtoCommerce中文博客站,2015年11月18日上线了!
    C#编程连接数据库,通过更改配置文件切换数据库功能。
    浅谈dataGridView使用,以及画面布局使用属性,对datagridview进行增删改查操作,以及委托使用技巧
    将listBox中信息显示在dataGridview中,操作datagridview后删除listBox信息和SQL数据库信息 续(浅谈listBox..)
    for语句嵌套使用 实现9*9乘法表
    浅谈ListBox控件,将对象封装在listBox中,在ListBox中显示对象中某个属性,在ListBox中移除和移动信息
    使用SignalR为FineUI/Webform打造消息总线
    封装一个错误重试类
    封装一个锁处理类
  • 原文地址:https://www.cnblogs.com/shengguang/p/5743890.html
Copyright © 2011-2022 走看看