Python Tesseract : import tesseract fails on fedora 18

996 views
Skip to first unread message

007

unread,
Nov 20, 2013, 4:30:19 AM11/20/13
to tesser...@googlegroups.com
Please help in resolving import of tesseract

What steps will reproduce the problem?
1. Install tesseract-ocr, leptonica-1.69 and python-tesseract from source
2. import tesseract from script
3. Also readText method from simplecv fails with " SimpleCV:2: 'Please install the correct OCR library required - http://code.google.com/p/tesseract-ocr/ http://code.google.com/p/python-tesseract/

What is the expected output? What do you see instead?
script should be able to import tesseract

What version of the product are you using? On what operating system?
Fedora18
tesseract-ocr, leptonica-1.69 and python-tesseract from source

Please provide any additional information below.
--------------------------------------------------------------------------------------------------
[root@localhost amzn_aiv]# find / -name tesseract.py*
/home/amzn_aiv/python-tesseract/tesseract.pyc
/home/amzn_aiv/python-tesseract/build/lib.linux-x86_64-2.7/tesseract.py
/home/amzn_aiv/python-tesseract/debian/python-tesseract/usr/share/pyshared/tesseract.py
/home/amzn_aiv/python-tesseract/tesseract.py
/home/amzn_aiv/python-tesseract/test-slim/dlls/tesseract.py
/home/amzn_aiv/.local/lib/python2.7/site-packages/python_tesseract-tesseract-py2.7-linux-x86_64.egg/tesseract.pyc
/home/amzn_aiv/.local/lib/python2.7/site-packages/python_tesseract-tesseract-py2.7-linux-x86_64.egg/tesseract.py
===========================================================
[root@localhost amzn_aiv]# export PYTHONPATH=/home/amzn_aiv/.local/lib/python2.7/site-packages/python_tesseract-tesseract-py2.7-linux-x86_64.egg/
==========================================================
[root@localhost amzn_aiv]# cat ex.py
import tesseract
api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz")
api.SetPageSegMode(tesseract.PSM_AUTO)

mImgFile = "eurotext.jpg"
mBuffer=open(mImgFile,"rb").read()
result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
print "result(ProcessPagesBuffer)=",result
=========================================================
[root@localhost amzn_aiv]# python ex.py
Traceback (most recent call last):
  File "ex.py", line 1, in <module>
    import tesseract
  File "/home/amzn_aiv/.local/lib/python2.7/site-packages/python_tesseract-tesseract-py2.7-linux-x86_64.egg/tesseract.py", line 26, in <module>
    _tesseract = swig_import_helper()
  File "/home/amzn_aiv/.local/lib/python2.7/site-packages/python_tesseract-tesseract-py2.7-linux-x86_64.egg/tesseract.py", line 22, in swig_import_helper
    _mod = imp.load_module('_tesseract', fp, pathname, description)
ImportError: /home/amzn_aiv/.local/lib/python2.7/site-packages/python_tesseract-tesseract-py2.7-linux-x86_64.egg/_tesseract.so: undefined symbol: _ZN9tesseract11TessBaseAPI14NormalizeTBLOBEP5TBLOBP3ROWbP6DENORM
====================================================


Installation logs
---------------------------

Install SimpleCV
-----------------
 sudo yum -y install python-ipython opencv-python scipy numpy pygame python-setuptools python-pip
 
Compile leptonica
------------------
tar zxvf leptonica-1.69.tar.gz
cd leptonica-1.69
yum install giflib-devel libtiff-devel zlib-devel libpng-devel libjpeg-turbo-devel python-devel -y 
./configure --prefix=/usr
make
sudo make install

Compile Tesseract OCR
----------------------
./configure --prefix=/usr
make
make install

Compile Python-Tesseract
------------------------
cd python-tesseract
python config.py
python setup.py clean
python setup.py build
python setup.py install --user

zdenko podobny

unread,
Nov 20, 2013, 8:19:07 AM11/20/13
to tesser...@googlegroups.com
python-tesseract is independent project - we can not modified it.

Anyway you can use tesseract 3.02.02 in python via ctypes - see example[1]


Zdenko


--
--
You received this message because you are subscribed to the Google
Groups "tesseract-ocr" group.
To post to this group, send email to tesser...@googlegroups.com
To unsubscribe from this group, send email to
tesseract-oc...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/tesseract-ocr?hl=en
 
---
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.
For more options, visit https://groups.google.com/groups/opt_out.

Nick White

unread,
Nov 20, 2013, 8:29:56 AM11/20/13
to tesser...@googlegroups.com
Hi Zdenko,

On Wed, Nov 20, 2013 at 02:19:07PM +0100, zdenko podobny wrote:
> Anyway you can use tesseract 3.02.02 in python via ctypes - see example[1]
>
> [1] https://code.google.com/p/tesseract-ocr/source/browse/trunk/contrib/
> tesseract-c_api-demo.py?r=903

That's neat, I hadn't seen python's ctypes before! Any chance you
could add that (or a link to it) to the APIExample wiki page?

Nick

Sneha Murganoor

unread,
Dec 4, 2013, 12:48:43 AM12/4/13
to tesser...@googlegroups.com
Zdenko,

It is just excellent. It works very well. Thank you.

Thanks,
Sneha


--
--
You received this message because you are subscribed to the Google
Groups "tesseract-ocr" group.
To post to this group, send email to tesser...@googlegroups.com
To unsubscribe from this group, send email to
tesseract-oc...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/tesseract-ocr?hl=en

---
You received this message because you are subscribed to a topic in the Google Groups "tesseract-ocr" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tesseract-ocr/tfi9RrPk3_E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tesseract-oc...@googlegroups.com.

Sneha Murganoor

unread,
Dec 5, 2013, 4:34:44 AM12/5/13
to tesser...@googlegroups.com
Hi Zdenko,

how to get size of image and set recognition to a sub-rectangle of the image in this?

Thanks,
Sneha

zdenko podobny

unread,
Dec 5, 2013, 1:56:36 PM12/5/13
to tesser...@googlegroups.com

You need to use leptonica (e.g. via ctypes, but there is also python module pylepthonica).

Modify relevant part of script with something like this:


lep = ctypes.cdll.LoadLibrary("liblept.so")
pix_image = lep.pixRead(filename)
print "image width:", lep.pixGetWidth(pix_image)
print "image height:", lep.pixGetHeight(pix_image)

tesseract.TessBaseAPISetImage2(api, pix_image)
tesseract.TessBaseAPISetRectangle(api, 30, 60, 400, 100)
text_out = tesseract.TessBaseAPIGetUTF8Text(api)
result_text = ctypes.string_at(text_out)
print result_text


Zdenko


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.

zdenko podobny

unread,
Dec 7, 2013, 6:15:30 PM12/7/13
to tesser...@googlegroups.com

Sneha Murganoor

unread,
Dec 10, 2013, 4:51:48 AM12/10/13
to tesser...@googlegroups.com
Hi Zdenko,

"More complex solution" is giving better results in detecting text than earlier one. In addition it points out the layout of where the text is.

But I don't have clear idea as to what this script is doing better compared to earlier one.

Please help me in understanding what the following methods are for:

TessBaseAPISetPageSegMode
TessBaseAPIGetComponentImages
TessBaseAPISetVariable

Also,
What are  Page Segmentation Modes?
What are Page Iterator Levels?
What is OSD?

Thanks,
Sneha


zdenko podobny

unread,
Dec 11, 2013, 2:58:13 AM12/11/13
to tesser...@googlegroups.com
On Tue, Dec 10, 2013 at 10:51 AM, Sneha Murganoor <sneh...@gmail.com> wrote:
Hi Zdenko,

"More complex solution" is giving better results in detecting text than earlier one. In addition it points out the layout of where the text is.

But I don't have clear idea as to what this script is doing better compared to earlier one.

Please help me in understanding what the following methods are for:

TessBaseAPISetPageSegMode
TessBaseAPIGetComponentImages
TessBaseAPISetVariable

These are the functions from C-API [1]. C-API wraps C++ tesseract functions API[2]. E.g. TessBaseAPISetPageSegMode[3] is C equivalent for SetPageSegMode[4]. So if you need description of C function you need read comment for it in C++ API ;-)


 
Also,
What are  Page Segmentation Modes?
 
What are Page Iterator Levels?
see description [6].
What is OSD?

Orientation and script detection.

Sneha Murganoor

unread,
Jan 28, 2014, 6:55:49 AM1/28/14
to tesser...@googlegroups.com
Hi Zendko,

I am facing segmentation fault after calling the readImage method hundreds of times in a script.
Please help me resolving this ..

Error:

Error in pixRemoveColormap: pixs must be {1,2,4,8} bpp
Error in pixGetDepth: pix not defined
Error in pixGetWpl: pix not defined
Please call SetImage before attempting recognition.Segmentation fault

Code Snippet:
def readImage(filename,frac=0,xTopFrac=1,yTopFrac=1,xBottomFrac=1,yBottomFrac=1):
    result_text = ""
    #Initialize the Tesseract API
    api = tesseract.TessBaseAPICreate()
    rc = tesseract.TessBaseAPIInit3(api, TESSDATA_PREFIX, lang);
    if (rc):
        tesseract.TessBaseAPIDelete(api)
        logging.error("Could not initialize tesseract.\n")
        raise Exception("Could not initialize tesseract.")

    if(frac == 0):
        logging.debug("No Focusing required")
        #Get the ext from Image
        text_out = tesseract.TessBaseAPIProcessPages(api, filename, None , 0);
        result_text = ctypes.string_at(text_out)
    else :
        logging.debug("Focus Required. Fractions passed")
        logging.debug(xTopFrac+" "+yTopFrac+" "+xBottomFrac+" "+yBottomFrac)
       
        #Get the Image height and Width
        pix_image = leptonica.pixRead(filename)
        x = leptonica.pixGetWidth(pix_image)
        y = leptonica.pixGetHeight(pix_image)
        logging.debug("Image width: " + str(x))
        logging.debug("Image height: " + str(y))
       
        tesseract.TessBaseAPISetImage2(api, pix_image)
        #Setting origin (xPoint,yPoint ) and width and height
        xPoint = int(float(Fraction(xTopFrac)*x))
        yPoint = int(float(Fraction(yTopFrac)*y))
        width = int(float(Fraction(xBottomFrac)*x)) - xPoint
        height = int(float(Fraction(yBottomFrac)*y)) - yPoint
        logging.debug("Origin : ")
        logging.debug(str(xPoint)+" "+str(yPoint))
        logging.debug("Width & Height :")
        logging.debug(str(width)+" "+str(height))
        #Setting the rectangle
        tesseract.TessBaseAPISetRectangle(api,xPoint,yPoint,width-1,height-1)
        #get the text

        text_out = tesseract.TessBaseAPIGetUTF8Text(api)
        result_text = ctypes.string_at(text_out)
    tesseract.TessBaseAPIDelete(api)
    logging.debug("Result Text : "+result_text)
    return result_text


Thanks,
Sneha

zdenko podobny

unread,
Jan 28, 2014, 4:14:55 PM1/28/14
to tesser...@googlegroups.com
Hi Sneha,

first of all - please create test case and not code snippets ;-) . It is quite annoying (and discouraging) for tester to create test case by himself :-( )

Next: "Error in pixRemoveColormap: pixs must be {1,2,4,8} bpp" is a leptonica error (e.g. I guess you would see it when you execute line "text_out = tesseract.TessBaseAPIProcessPages(api, filename, None , 0);" or "pix_image = leptonica.pixRead(filename)". 

Because you did not provide image that failed (and full error message with test case) I can just guess that is problem with input image format...


Zdenko
Reply all
Reply to author
Forward
0 new messages