Skip to content

tessedit_write_images has no effect in c++ #681

@piotrekep

Description

@piotrekep

I'm trying to get tesseract to output the processed image using the tessedit_write_images variable.
When doing it through command line it works fine, however setting it in c++ has no effect.
I've tried using a config file and SetVariable(commented code lines). the check for setting the var passes, tesseract.conf is loaded without issue( i get errors if i put a typo in)

tesseract.conf is a copy of get.images.

 bool ocrService::initTesseract(){
    ocrService::api = new tesseract::TessBaseAPI();
    //if(ocrService::api->SetVariable("tessedit_write_images", "1") !=1){
    //    std::cout << "write images set fail" << std::endl;
   // }   
   
    char *configs[]={"tesseract.conf"};
    int configs_size = 1;
   // ocrService::api->SetVariable("debug_file", "tesseract_debug.log");
    if (api->Init(NULL, "pol", tesseract::OEM_LSTM_ONLY, configs, configs_size, NULL, NULL, false)) {
    //if (api->Init(NULL, "pol")) {
        fprintf(stderr, "Could not initialize tesseract.\n");
        return false;
    }
    return true;
 }

bool ocrService::loadImage(cv::Mat img){
    if (img.channels() != 1) {
         cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
    }    
    ocrService::api->SetImage(img.data, img.cols, img.rows, img.channels(), img.step);
    return true;
 }

std::string ocrService::returnHOCRText(){
    char* outHOCR = ocrService::api->GetHOCRText(0);
    std::string hocrString(outHOCR);
    delete [] outHOCR; 
    ocrService::api->ClearAdaptiveClassifier();
    return hocrString;
}

I don't think the working dir is an issue. when i enable the debug file it's saved in the directory where the binary is located. I tried disabling the working dir change and it has no effect. I set the working dir to the program location as follows:

int setWorkdir(){
    char buffer[PATH_MAX];
    ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
    if (len == -1) {
        std::cerr << "Failed to read /proc/self/exe" << std::endl;
        return 1;
    }
    buffer[len] = '\0'; 

    fs::path exePath(buffer);
    fs::path exeDir = exePath.parent_path();
    fs::current_path(exeDir);
    
    return 0;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions