Importing pytesseract - python

I have trying to use pytesseract for OCR (extracting text from the image). I have successfully installed pytessearct by using the command -
pip install pytessearct
When I try to install it again, it clearly says -
Requirement already satisfied (use --upgrade to upgrade):
pytesseract in ./site-packages
This means pytessearct is installed successfully. When i try to import this package in my iPython notebook using -
import pytessearct
It throws an error -
ImportError: No module named pytesseract
Why is that happening?

To use Python-tesseract - requires python 2.5+ or python 3.x - first you have to install PIL and pytesseract packages through pip:
pip install Pillow
pip install pytesseract
Then you have to download and install the tesseract OCR:
https://sourceforge.net/projects/tesseract-ocr-alt/?source=typ_redirect
As far as I know it automatically adds it to your PATH variable.
Then use it like this way:
import pytesseract
from PIL import Image
img = Image.open('Capture.PNG')
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'
print( pytesseract.image_to_string(img) )
I hope it helps :)

Related

Why do I get an error trying to use pytesseract?

import pytesseract
import PIL
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
Im trying that code alone with many other ways to type the pile path as in double '\' etc but I still continue to get the error:
ModuleNotFoundError: No module named 'PIL'
In shell, run:
pip install Pillow
The error means you are missing some modules.
pip install --upgrade --force-reinstall Pillow
resolved my issue

why this error arises though i have install all the required package for opencv?

I have been working on this code for long time , I had run in several ide but all shows same error and i am bit confuse now and i need a proper solution so that code may works
import os
import cv2 as cv
import numpy as np
people=['emily','emma','jim parson']
haar_cas=cv.CascadeClassifier('haar_face.xml')
DIR=r'G:\opencv\faces'
features=[]
labels=[]
def train_face():
for person in people:
paths=os.path.join(DIR,person)
label=people.index(person)
for img in os.listdir(paths):
img_path=os.path.join(paths,img)
img_array=cv.imread(img_path)
gray=cv.cvtColor(img_array,cv.COLOR_BGR2GRAY)
faces_rect=haar_cas.detectMultiScale(gray,1.1,2)
for(x,y,w,h) in faces_rect:
faces_roi= gray[y:y+h,x:x+w]
features.append(faces_roi)
labels.append(label)
train_face()
feature=np.array(features,dtype='object')
label=np.array(labels)
# print(f'length of the feature={len(features)}')
# print(f'lenght of the labels={len(labels)}')
face_reconizer=cv.face.LBPHFaceReconizer_create()
np.save('feature.npy',feature)
np.save('label.py',label)
the output of the code is
File "G:\opencv\face_reconization.py", line 28, in <module>
face_reconizer=cv.face.LBPHFaceReconizer_create()
AttributeError: module 'cv2.face' has no attribute 'LBPHFaceReconizer_create'
If you installed opencv by running pip install opencv-python there are some missing packages there so you need to run pip install opencv-contrib-python to install the full version.
If that doesn't work try reinstalling opencv
pip3 uninstall opencv-contrib-python
python -m pip install opencv-contrib-python

Unable to import PIL into project

The PIL module should be installed because when I run the pip install pillow I get this message: Requirement already satisfied: pillow in [my installation route]. But, when I try to import this into a file, I get this error:
ModuleNotFoundError: No module named 'Pillow'
What am I doing wrong?
this has worked for me if your using vscode try install by typing py -m pip install pillow
from PIL import ImageTk,Image
Pillow is a fork of PIL, and the import is carried over.
import PIL
Should do the trick. The documentation has additional details:
https://pillow.readthedocs.io/en/stable/installation.html

No module named pytesseract error

I am trying to use pytesseract for OCR, on a raspberry pi using Raspbian
I have read several questions on this topic, but can't find an answer that works, they usually say to install pytesseract with pip, and I did it.
my code is very simple:
import pytesseract
from PIL import Image
print(pytesseract.image_to_string(Image.open('test.jpg')))
But it returns error message : "ImportError: No module named 'pytesseract' .
I have installed tesseracrt-ocr (the whereis tesseract-ocr command returns /usr/share/tesseract-ocr)
I have installed pytesseract with pip install tesseract (which returns successfully installed Pillow-4.3.0 olefile-0.44 pytesseract-0.1.7 ... but the whereis pytesseract command does not return anything --> a problem?).
Do you have any idea of the problem I have ?
See after installing pytesseract ,using
<cmd>C:\> pip install pytesseract
Try :
import pytesseract
If above is not working then it has do something with the installation ,
Check if pytesseract folder is available under "\Python27\Lib\site-packages" ,
Try the above command from site packages, Hope this helps ,
Else there is something wrong with installation .
Add this line in your code
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
set your path where tesseract-ocr is installed and also add this path to your window environment variable.Also check this link Tesseract installation for complete installation of tesseract.

No module named Image

Sorry for my grammar, I don't speak English.
After I set filebrowser, tinymce, and grappelli, I get this error: No module named Image
try:
from PIL import Image
except ImportError:
import Image
I set it to PIL but it didn't solve the problem.
my platform windows
If i want: pip install PIL
`c:\Users\Kim\BitNami DjangoStack projects\homex8>pip install PIL
Downloading/unpacking PIL
Running setup.py egg_info for package PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
Installing collected packages: PIL
Running setup.py install for PIL
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
building '_imaging' extension
error: Unable to find vcvarsall.bat`
I do not understand what that means
Solved problem.
reinstall PIL with easy_install, and more movements, here are the details.
You are missing PIL (Python Image Library and Imaging package). To install PIL I used
pip install pillow
For my machine running Mac OSX 10.6.8, I downloaded Imaging package and installed it from source.
http://effbot.org/downloads/Imaging-1.1.6.tar.gz and cd into Download directory. Then run these:
$ gunzip Imaging-1.1.6.tar.gz
$ tar xvf Imaging-1.1.6.tar
$ cd Imaging-1.1.6
$ python setup.py install
Or if you have PIP installed in your Mac
pip install http://effbot.org/downloads/Imaging-1.1.6.tar.gz
then you can use:
from PIL import Image
in your python code.
Did you setup PIL module? Link
You can try to reinstall it on your computer.
It is changed to : from PIL.Image import core as image
for new versions.
You can this query:
pip install image
I had pillow installed, and still, I got the error that you mentioned. But after I executed the above command, the error vanished. And My program worked perfectly.
Problem:
~$ simple-image-reducer
Traceback (most recent call last):
File "/usr/bin/simple-image-reducer", line 28, in <module>
import Image
**ImportError: No module named Image**
Reason:
Image != image
Solution:
1) make sure it is available
python -m pip install Image
2) where is it available?
sudo find ~ -name image -type d
-->> directory /home/MyHomeDir/.local/lib/python2.7/site-packages/image
->> OK
3) make simple-image-reducer understand via link:
ln -s ~/.local/lib/python2.7/site-packages/image
~/.local/lib/python2.7/site-packages/Image
4)
invoke simple-image-reducer again.
Works:-)
If you are trying to just show the image on your notebook, below simply does the job
from IPython.display import Image
Image(url= "Link to the image ending with file format such as, .jpg or .png")

Categories

Resources