image_to_string doesn't work in Mac - python

I'm trying to follow this example of pytesser (link) in a Mac Maverick.
>>> from pytesser import *
>>> im = Image.open('phototest.tif')
>>> text = image_to_string(im)
But, in the last line I get this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pytesser.py", line 31, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "pytesser.py", line 21, in call_tesseract
proc = subprocess.Popen(args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
But, I don't understand what I should do. The file phototest is in the same folder I'm running the script. How to fix this?
UPDATE:
When I try
brew install tesseract
I get this error:
Warning: It appears you have MacPorts or Fink installed.
Software installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
Error: You must `brew link libtiff libpng jpeg' before tesseract can be installed

I actually had the same error as you, which is how I found this post. I also have the solution to my problem, because you gave it to me!
I was seeing:
ryan.davis$ python tesseract.py
Traceback (most recent call last):
File "tesseract.py", line 52, in <module>
print (image_to_string(big))
File "/usr/local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 161, in image_to_string
config=config)
File "/usr/local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Want to know what I had to do to fix this? Exactly what you tried: brew install tesseract I had installed the tesseract python library, but hadn't installed it at the system level. So that solves my problem. How about yours?
I think you might have been distracted by this:
Warning: It appears you have MacPorts or Fink installed. Software
installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try
again.
And not noticed your answer was already provided in the brew response:
You must brew link libtiff libpng jpeg before tesseract can be
installed.
So do:
brew link libtiff
brew link libpng
brew link jpeg
Then:
brew install tesseract
Finally:
:)

Related

Error installing tesseract Mac OS

I've tried numerous methods to install Tesseract but I just can't seem to get it working. I'm on a mac and this is the error I keep getting
txt = pytesseract.image_to_string(image, lang='eng')
File "/Users/user/anaconda/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 161, in image_to_string
config=config)
File "/Users/user/anaconda/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "/Users/user/anaconda/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/Users/user/anaconda/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Does anyone know how I can solve this issue with tesseract?
Anaconda does have conda Tesseract package available. Use below command to install it and try again.
conda install -c brown-data-science tesseract=3.05.00
If you need the latest vesrion Tesseract 4.00.00alpha, you may refer to github on the installation instructions. If you don't have Xcode or Homebrew installed, check this to install it.

How to install OpenCV for Python 3.x on Windows 8.1?

I am trying to install and run OpenCV 3.0.0 for python 3.4.2, in Windows 8.1.
I have downloaded the OpenCV file from http://opencv.org/.
Extracted the folder in C:.
I went to System → Advanced System Settings → Advanced → Environment variables
Add a new variable in "User Variables frame". Name it "OPENCV_DIR" with value "c:\opencv\build\x64\vc12".
Add this at the end of Path: ";C:\opencv;C:\opencv\build;%OPENCV_DIR%\bin"
I took the archive "cv2.pyd" from "C:\opencv\build\python\2.7\x64" and copied it.
I pasted it in C:\Python34\Lib\site-packages.
Finally I went to the IDLE of Python 3.4.2 and wrote (this happened before copying the fine into the Python directory):
import cv2
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cv2
ImportError: No module named 'cv2'
And this happened after I copied the file into the Python directory:
import cv2
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import cv2
ImportError: DLL load failed: The specified module could not be found.
What am I doing wrong? I not sure whether I should install CMake and NumPy.
Also I am not sure how to use this webpage (recommended by so many people),
http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv, where many Python libraries are available, including PIL, OpenCV, VideoCapture, etc...
I want to try to run the examples from:
Face Detection in Python Using a Webcam
1: Introduction to Python and OpenCV
Face Detection With OS X and Python
OK, I should not use the file from Python 2.7 in Python 3.4. It won't work. I deleted it, so as otterb suggested, I just installed the opencv_python‑3.0.0‑cp34‑none‑win_amd64.whl package from http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv, but this file is an .whl not an EXE file, and according to How do I install a Python package with a .whl file? I followed these steps:
Go to the command prompt
Change directory with "cd" to where I downloaded the file
And write "pip install opencv_python-3.0.0-cp34-none-win_amd64.whl"
But I got the following message in red:
Installing collected packages: opencv-python
Cleaning up...
Exception:
Traceback (most recent call last):
File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is
being used by another process: 'C:\\Users\\JOSEFG~1\\AppData\\Local\\Temp\\pip-n
kyo0c6u-build\\opencv_python-3.0.0.data\\data\\Lib\\site-packages\\cv2.pyd'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pip\commands\install.py", line 283, in run
requirement_set.install(install_options, global_options, root=options.root_p
ath)
File "C:\Python34\lib\site-packages\pip\req.py", line 1444, in install
requirement.remove_temporary_source()
File "C:\Python34\lib\site-packages\pip\req.py", line 761, in remove_temporary
_source
rmtree(self._temp_build_dir)
File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
onerror=rmtree_errorhandler)
File "C:\Python34\lib\shutil.py", line 477, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
(exctype is PermissionError and value.args[3] == 5) #python3.3
IndexError: tuple index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\JOSEFG~1\\AppData\\Local\\Temp\\pip-nkyo0c6u-build\\opencv_python-3.0.0.data\\data\\Lib\\site-packages\\cv2.pyd'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pip\basecommand.py", line 122, in main
status = self.run(options, args)
File "C:\Python34\lib\site-packages\pip\commands\install.py", line 302, in run
requirement_set.cleanup_files(bundle=self.bundle)
File "C:\Python34\lib\site-packages\pip\req.py", line 1319, in cleanup_files
req.remove_temporary_source()
File "C:\Python34\lib\site-packages\pip\req.py", line 761, in remove_temporary_source
rmtree(self._temp_build_dir)
File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
onerror=rmtree_errorhandler)
File "C:\Python34\lib\shutil.py", line 477, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
(exctype is PermissionError and value.args[3] == 5) #python3.3
IndexError: tuple index out of range
Storing debug log for failure in C:\Users\me\pip\pip.log
Which means after reading it carefully that the file is unsafe to install on my PC.
Also it seems that pip should be the latest one to run the previous command according to How do I install pip on Windows?. Then I just upgrade pip in the command prompt with
python -m pip install -U pip
Now I have version 7.1. I know it by writing:
pip --version
Then I tried again
pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
But it seems that it is working as I am getting this message:
C:\Users\Jose F Gomez D\Downloads>pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
Requirement already satisfied (use --upgrade to upgrade): opencv-python==3.0.0 from file:///C:/Users/Jose%20F%20Gomez%20D/Downloads/opencv_python-3.0.0-cp34-none-win_amd64.whl in c:\python34\lib\site-packages
then I write in the python shell "import cv2" but there is a new error
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cv2
ImportError: numpy.core.multiarray failed to import
so I installed the numpy (and scipy just in case) library with
pip install numpy
pip install scipy
But it is not working as it requires C++
error microsoft visual c++ 10 is required
I tried to find "Visual C++ 2010 Express" from the webpage of Visual Studio at https://www.visualstudio.com/, but there is no link to "Microsoft Visual C++ 2010 express", so I went to page http://microsoft-visual-cpp-express.soft32.com/free-download/, downloaded the file, executed it, and now I got C++.
Again I went to the command prompt and tried to install NumPy and SciPy and it did not work. There is an error in the path:
ValueError: ['path']
OK, now I tried to install the NumPy and SciPy packages from:
http://sourceforge.net/projects/numpy/files/NumPy/
But it did not work, so I went to Gohike, http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy, and installed NumPy and SciPy from the command prompt in administrator mode:
pip install scipy‑0.16.0‑cp34‑none‑win_amd64.whl
pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl
Now I tried to run cv, but nothing. Uups, it's cv2, and it is working. I will write the answer next.
The right way to install OpenCV 3.0.0 for Python 3.4.2 on Windows 8.1:
Go to http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
Download OpenCV, NumPy and SciPy
For me I needed:
opencv_python-3.0.0-cp34-none-win_amd64.whl
scipy‑0.16.0‑cp34‑none‑win_amd64.whl
numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl
Download and install Visual C++ 2010 Express from http://microsoft-visual-cpp-express.soft32.com/free-download/
Open a command prompt as administrator
Change directory like "cd C:\Users\me\Downloads" to where I downloaded the file
Copy and paste this: "python -m pip install -U pip"
Then install the libraries. Copy and paste this:
pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
pip install scipy‑0.16.0‑cp34‑none‑win_amd64.whl
pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl
That is all.
-------------> Tricky Python <------------
You can use pip to install the whl file like:
pip install path/to/whlfile
Yes, you are right. Gohlke's package used to be an EXE file file, but OpenCV is now distributed as whl. Sorry I missed that.
Here is a similar Stack Overflow question: How do I install a Python package with a .whl file?
I have successfully installed OpenCV 3.1.0 using conda (install Anaconda 3 - 64 bit ) with the following command (conda installation of OpenCV 3):
conda install -c menpo opencv3=3.1.0
Please note that the only complete installation is obtained compiling the OpenCV libraries from the GitHub repository. I have found that some features (e.g. SURF detection) are missing from the conda installation and also from the wheels.
I did everything as mentioned by Jose Fernando Gomez Davila except I installed numpy before scipy as numpy is required to install scipy
pip install opencv_python-4.1.2-cp37-cp37m-win_amd64.whl
pip install scipy-1.4.1-cp37-cp37m-win_amd64.whl
pip install numpy-1.16.6+mkl-cp37-cp37m-win_amd64.whl

pytesseract-no such file or directory error

I am using Ubuntu 14.04. I have the following code:
import Image
import pytesseract
im = Image.open('test.png')
print pytesseract.image_to_string(im)
but I keep getting the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/chaitanya/pythonapp/localcopy.py", line 4, in <module>
print pytesseract.image_to_string(im)
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 142, in image_to_string
config=config)
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 75, in run_tesseract
stderr=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Both the python program and the image are in the same location.What could be the problem??
You need to install tesseract-ocr:
sudo apt-get install tesseract-ocr
If you're on windows and have PIP installed go to your project directory and run:
pip install tesseract-ocr
Based off of #padraic cunningham's answer which I tailored to my setting.
If you are on Linux (ubuntu 16, should not matter) and have a conda installation:
First search for what you need to be installing:
$ anaconda search -t conda tesserocr
You will get a few options, you need to look at the platforms and builds to identify what makes sense for you.
As I have python 3.6 and linux-64 I chose mcs07/tesserocr
To install:
$ conda install -c mcs07 tesserocr
That's it. I didn't need a restart of the terminal or anything. I just kept going.

how to make pytesser (Tesseract) work?

I'm trying to make pytesser (downloadable here) work on my mac OS, but I don't succeed.
I installed Tesseract, PIL and all the dependencies.
I unzipped pytesser in my python lib folder and modified the script file into __init__.py
in the init file I modified the path to the tesseract.exe file as suggested here and here
that is:
tesseract_exe_name = 'my lib path/pytesser/tesseract' # Name of executable to be called at command line
that's what I get as error:
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
print image_to_string(picz)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pytesser/__init__.py", line 31, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pytesser/__init__.py", line 21, in call_tesseract
proc = subprocess.Popen(args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
it seems that the module does not manage to run the .exe file. I tried to change the path, add the extension .exe but I always get the same error.
Several solutions for a python tesseract wrapper:
Python-Tesseract:
First of get homebrew and brew install python, then easy_install https://bitbucket.org/3togo/python-tesseract/downloads/python_tesseract-0.9.1-py2.7-macosx-10.10-x86_64.egg
source: https://code.google.com/p/python-tesseract/wiki/HowToCompileForHomebrewMac
pytesseract:
This what I was using previously before getting python-tesseract, pip install pytesseract. Then you have to go to /usr/local/lib/python2.7/site-packages and go to pytesseract then pytesseract.py. Change the file path in the python script to where tesseract is located on your computer.

PyTesser simple usage error

I've downloaded PyTesser and extracted it.
I was in the pytesser_v0.0.1 folder and tried to run the sample usage code in the python interpreter:
from pytesser import *
print image_file_to_string('fnord.tif')
and the output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pytesser.py", line 44, in image_file_to_string
call_tesseract(filename, scratch_text_name_root)
File "pytesser.py", line 21, in call_tesseract
proc = subprocess.Popen(args)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
NOTE: I'm in Ubuntu 12.10 with Python 2.7.3
can anyone help me understand this error, and what can I do to fix it ?
This isn't as well documented as it could be, but if you are not on Windows you need to install the tesseract binary for your platform. On Ubuntu and other Debian based Linux distributions, apt-get install tesseract-ocr. Then you can run:
python pytesser.py
which uses the test files phototest.tif, fnord.tif and fonts_test.png to test the library.
For beginners on windows to use pytesseract:
Open command prompt
Type: pip install pytesseract
(this will install pytesseract last version module on your python easily)
Go to this link and download and install tesseract-ocr engine:
https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-ocr-setup-3.02.02.exe&can=2&q=
Now you are ready to use pytesseract
For more information and see code example check this link:
http://www.manejandodatos.es/2014/11/ocr-python-easy/

Categories

Resources