Search code examples
c++tesseract

Error with Tesseract-OCR relating tesseract::TessBaseApi() (expected type-specifier)


I did a program using openCV 2.4.9 and tesseract 3.04, both using the C API.

As the C API of openCV is deprecated, I decided to modify it to use the C++ APIs of both libraries.

This part of the code in C (works):

    #include <cv.h>
    #include <tesseract/capi.h>

void    foo (struct _IplImage  *imgptr)
{
    struct TessBaseAPI  *handle_ocr;
    handle_ocr  = TessBaseAPICreate();
    // Do something
}

should be equivalent to this code in C++ (doesn't compile):

    #include <opencv2/opencv.hpp>
    #include <tesseract/baseapi.h>
    #include <leptonica/allheaders.h>

void    foo (class cv::Mat  *imgptr)
{
    class tesseract::TessBaseAPI    *handle_ocr;
    handle_ocr  = new tesseract::TessBaseApi();
    // Do something
}

g++ 6 (on debian stretch) gives the following error:

error: expected type-specifier
  handle_ocr = new tesseract::TessBaseApi();
                   ^~~~~~~~~

What does it mean? And what is the solution?

EDIT: Whole source file:

/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
 /* Standard C ----------------------------------------------------------------*/
        /* snprintf() */
    #include <cstdio>

/* Packages ------------------------------------------------------------------*/
        /* opencv */
    #include <opencv2/opencv.hpp>
        /* OCR Tesseract */
    #include <tesseract/baseapi.h>
    #include <leptonica/allheaders.h>

/* Module --------------------------------------------------------------------*/
        /* img_ocr_text & OCR_TEXT_MAX */
    #include "some_other_file.hpp"

    #include "this_file.hpp"


/******************************************************************************
 ******* func *****************************************************************
 ******************************************************************************/
void    foo (class cv::Mat  *imgptr)
{
    class tesseract::TessBaseAPI    *handle_ocr;

    /* Language */
    char    *lang_str   = "eng";

    /* Config file */
    char    *conf_str   = "/home/user/ocr/price";

    /* init OCR */
    handle_ocr  = new tesseract::TessBaseApi();
    handle_ocr->Init(NULL, lang_str, tesseract::OEM_TESSERACT_CUBE_COMBINED);
    if (conf) {
        /* Configure OCR (whitelist chars) */
        handle_ocr->ReadConfigFile(conf_str);
    }

    /* scan image for text */
    handle_ocr->SetImage(imgptr->data,
                imgptr->size().width, imgptr->size().height,
                imgptr->channels(), imgptr->step1());
    char    *txt;
    txt = handle_ocr->GetUTF8Text();

    /* Copy text to global variable */
    snprintf(img_ocr_text, OCR_TEXT_MAX, "%s", txt);

    /* cleanup */
    delete []   txt;
    handle_ocr->Clear();
    handle_ocr->End();
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/

Solution

  • Replace this:

    handle_ocr  = new tesseract::TessBaseApi();
    

    with this:

    handle_ocr  = new tesseract::TessBaseAPI();
    

    It was a mistake with the upper/lower cases.