Re: [tesseract-ocr] Increasing RAM while using tesseract

148 views
Skip to first unread message
Message has been deleted

Zdenko Podobny

unread,
Jun 20, 2019, 7:42:49 AM6/20/19
to tesser...@googlegroups.com
IMO you have to call END() to correctly close tesseract instance.

Zdenko


št 20. 6. 2019 o 13:02 _ Flaviu <flavi...@gmail.com> napísal(a):
I am using tesseract 4 on a VC++ (MFC) app, to read text from images (A4 sizes). I noticed that while I using this app on several PCs (Win10 64 bit, *GB RAM), the RAM occupied by my app (that use tesseract) is increasing on and on. If I read 14 images, my app eat 470 MB, and if I didn't close the app and read again all these 14 images, the RAM eaten by my app is increasing until ~800MB, and if continue to read the same images, the RAM is increasing on an on. Why is this happen ?

Here is the code that I am using for tesseract:

BOOL CMyClass::GetTextFromImage()
{
PIX* pix = NULL;
tesseract::TessBaseAPI* pTess = new tesseract::TessBaseAPI;

do
{
if (pTess->Init(...))
{
m_sError.Format(_T("OCRTesseract: Could not initialize tesseract."));
break;
}
// setup
// read image
PIX* pix = pixRead(m_sFileName);
if (! pix)
{
break;
}
// recognize
pTess->SetImage(pix);
}
while (FALSE);

// cleanup
// pTess->Clear();
// pTess->End();
delete pTess;
pTess = NULL;
pixDestroy(&pix);

return TRUE;
}


Is there anything wrong here ? Why is increasing RAM while I am using tesseract ?

--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.
To view this discussion on the web visit https://groups.google.com/d/msgid/tesseract-ocr/8be010ab-c948-47d3-b51e-ac141ce12bbd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

_ Flaviu

unread,
Jun 20, 2019, 10:04:04 AM6/20/19
to tesseract-ocr
I have uncommented lines:

pTess->Clear();

pTess->End();


and I noticed a little improvement, but the RAM eaten by app is still high … when you wrote "END()" you meant pTess->End() ?


To unsubscribe from this group and stop receiving emails from it, send an email to tesser...@googlegroups.com.

_ Flaviu

unread,
Jun 20, 2019, 10:12:27 AM6/20/19
to tesseract-ocr
Moreover, if I read a lot of images at once, the RAM has increased in such a way that my app stopped by an assert:

Untitled.png

_ Flaviu

unread,
Jun 21, 2019, 4:28:33 AM6/21/19
to tesseract-ocr
As far I understand,

pTess->End();

is called on destructor, am I right ?


On Thursday, June 20, 2019 at 2:42:49 PM UTC+3, zdenop wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to tesser...@googlegroups.com.

Zdenko Podobny

unread,
Jun 21, 2019, 9:37:02 AM6/21/19
to tesser...@googlegroups.com
yes - otherwise you will see memoryleak message when you app will finish...ä
Zdenko


pi 21. 6. 2019 o 10:28 _ Flaviu <flavi...@gmail.com> napísal(a):
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.

To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.

_ Flaviu

unread,
Jun 23, 2019, 10:20:16 AM6/23/19
to tesseract-ocr
No one noticed this fact ? If you read several images with tesseract, how is your RAM ?

Zdenko Podobny

unread,
Jun 23, 2019, 12:09:39 PM6/23/19
to tesser...@googlegroups.com
Hello,

if you are really interesting in help/involving others for testing you need to make it as simple as possible. E.g. providing most simple example.

I started to look on this one night, but need to stop cause I do no have much time. So here I was I was able to do:

1. get the test data ;-)
wget https://cdn.cnn.com/cnn/2019/images/04/18/mueller-report.pdf
mkdir mueller
pdfimages mueller-report.pdf -all mueller\report


2. make a simple testing code
  • I started on windows with clang (9 with msvc compatibility), which is my most used setting at these day. testing case is in attachment (test_tesseract.cpp)
  • I limit loop to 15 pages. 
  • I found out that code did not run ocr correctly (while tesseract executable has no problem with it ;-) ) so I need to add detection for page rotation from leptonica.
  • Anyway I saw there was crash after some processing some page (If I remember correctly it crash on api->Clear()). Because I am not familiar wit debugging on windows, I decided to run on linux. And ups c++17 code does not worked... I than I wast time with using different compiler, looking for other versions... and finally rewrite problematic part of code :-( And I need to stopped at this point. At lease I was able to run this code on linux for first 15 pages and it did not crash...
Just based on this it seems there is some problem on windows, because there should not be crash.

Zdenko


ne 23. 6. 2019 o 16:20 _ Flaviu <flavi...@gmail.com> napísal(a):
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.

To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.
test_tesseract.cpp

_ Flaviu

unread,
Jun 23, 2019, 2:29:54 PM6/23/19
to tesseract-ocr
Ok, good idea, I am working right now on a simple testing app that prove the issue. I will come back as soon as I can.

Zdenko Podobny

unread,
Jun 24, 2019, 3:53:40 AM6/24/19
to tesser...@googlegroups.com
AFAIR: there were always problem with windows tesseract debug build (MSVC). Release version works fine, but debug failed. So it would be great if somebody experienced with windows debugging have a look at this.

Zdenko


ne 23. 6. 2019 o 20:29 _ Flaviu <flavi...@gmail.com> napísal(a):
--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.
Message has been deleted

Zdenko Podobny

unread,
Jun 24, 2019, 4:35:55 AM6/24/19
to tesser...@googlegroups.com
IMHO you should call  Clear in loop with  SetImage : https://github.com/tesseract-ocr/tesseract/blob/master/src/api/baseapi.h#L675

Zdenko


po 24. 6. 2019 o 10:25 _ Flaviu <flavi...@gmail.com> napísal(a):
I come in just in time: I have developed a little test application, which have debug and release, source code, and exe.

Here is the code that get text from images:

BOOL CTemp::GetTextFromImage(CString sFileName)
{
PIX* pix = NULL;
tesseract::TessBaseAPI Tess;

do
{
if (Tess.Init(_T("C:\\tessdata-master\\"), _T("eng"), tesseract::OcrEngineMode::OEM_TESSERACT_LSTM_COMBINED))
{
m_sError.Format(_T("OCRTesseract: Could not initialize tesseract."));
break;
}
// read image
PIX* pix = pixRead(sFileName);
if (! pix)
{
m_sError.Format(_T("Cannot open input file: %s."), sFileName);
break;
}
// recognize
Tess.SetImage(pix);
}
while (FALSE);

// cleanup
pixDestroy(&pix);
Tess.Clear();
Tess.End();

return m_sError.IsEmpty();
}

Of course, you have put tessdata-master folder in C:

The test app has an menu item: File->Read images, and from that CFileDialog you can select multiple image file, and you can see the state of reading in the statusbar.

If you intend to run this project on you machine, you have modify yourself the paths in project settings. No big deal. The project is a VS2017 project, MFC.

I run this app on few Win10 64bit machines, with the same results. I have loaded for my tests 14 images, and after all image has been converted, the test app took 466 MB, and after these 14 images has been loaded again, the app took 883 MB, after I loaded again, 1307 MB, and so on ... the only way to free RAM is to stop the app and start again ! Sad :(

Here is the link for attachements: https://1drv.ms/f/s!AtSPCxnvttzegw6QHz8RAOjJq8fe
To unsubscribe from this group and stop receiving emails from it, send an email to tesser...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To post to this group, send email to tesser...@googlegroups.com.
Visit this group at https://groups.google.com/group/tesseract-ocr.

Zdenko Podobny

unread,
Jun 24, 2019, 4:52:11 AM6/24/19
to tesser...@googlegroups.com
And also  pixDestroy should be in loop too. You just create new objects and you did not destroy them, so why you wander you run out of memory?

 
Zdenko


po 24. 6. 2019 o 10:35 Zdenko Podobny <zde...@gmail.com> napísal(a):

_ Flaviu

unread,
Jun 24, 2019, 5:03:42 AM6/24/19
to tesseract-ocr
BTW, is almost no difference between debug and release with RAM occupied (few MB).

_ Flaviu

unread,
Jun 24, 2019, 5:06:51 AM6/24/19
to tesseract-ocr
I think there no loop there, it is just an do/while(FALSE), those Clear and pixDestroy is called every time when an images are read.

Zdenko Podobny

unread,
Jun 24, 2019, 1:40:26 PM6/24/19
to tesser...@googlegroups.com
I am sorry I miss understand your code: I was expecting that in
do/while loop you are iterating over file list. IMO its inefficient to
initialize tesseract for each image.


Zdenko

po 24. 6. 2019 o 11:06 _ Flaviu <flavi...@gmail.com> napísal(a):
> To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
> To post to this group, send email to tesser...@googlegroups.com.
> Visit this group at https://groups.google.com/group/tesseract-ocr.
> To view this discussion on the web visit https://groups.google.com/d/msgid/tesseract-ocr/ddea01e6-dcdd-4f17-932c-363d46d25fc4%40googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages