opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.32k stars 5.74k forks source link

opencv_contrib/ocr_tesseract.cpp #1041

Open seeyourcell opened 7 years ago

seeyourcell commented 7 years ago

opencv_contrib/ (sample) end_to_end_recognition /ocr_tesseract.cpp ,line 203 and 228. When I use tesseract with opencv to recognize character, the project fail. After I comment these two line,the project is ok. So I think mybe it is a bug.

        char *outText;
        outText = tess.GetUTF8Text();
        output = string(outText);
       // delete [] outText;

        if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) )
        {
            tesseract::ResultIterator* ri = tess.GetIterator();
            tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
            if (component_level == OCR_LEVEL_TEXTLINE)
                level = tesseract::RIL_TEXTLINE;

            if (ri != 0) {
                do {
                    const char* word = ri->GetUTF8Text(level);
                    if (word == NULL)
                        continue;
                    float conf = ri->Confidence(level);
                    int x1, y1, x2, y2;
                    ri->BoundingBox(level, &x1, &y1, &x2, &y2);

                    if (component_texts != 0)
                        component_texts->push_back(string(word));
                    if (component_rects != 0)
                        component_rects->push_back(Rect(x1,y1,x2-x1,y2-y1));
                    if (component_confidences != 0)
                        component_confidences->push_back(conf);

              //      delete[] word;
                } while (ri->Next(level));
            }
            delete ri;
        }
StevenPuttemans commented 7 years ago

Overall it is a bad habit of using explicit delete operations in a *.cpp sample, since garbage collector should handle that.

sovrasov commented 7 years ago

Strange error. delete[] is required here according to documentation. @lin65505578 could you attach an input image which causes this bug?

seeyourcell commented 7 years ago

@sovrasov,I read the document and the source code. Yes ,delete[] is required. However, If I donot comment these two lines, the "end_to_end_recognition" still still fail. I attach the image and the running code state.

1 2 scenetext_word01

ps: tesseract=>3.04

===================GetUTF8Text() source code======================== ====from " baseapi.cpp"==== /* Make a text string from the internal data structures. / char TessBaseAPI::GetUTF8Text() { if (tesseract_ == NULL || (!recognitiondone && Recognize(NULL) < 0)) return NULL; STRING text(""); ResultIterator it = GetIterator(); do { if (it->Empty(RIL_PARA)) continue; char para_text = it->GetUTF8Text(RIL_PARA); text += para_text; delete []para_text; } while (it->Next(RIL_PARA)); char result = new char[text.length() + 1]; strncpy(result, text.string(), text.length() + 1); delete it; return result; } ====from "baseapi.h"==== /**