What is the difference between Pytesseract and Tesserocr? - python

I'm using Python 3.6 in Windows 10 and have Pytesseract already installed but I found in a code Tesserocr which by the way I can't install. What is the difference?

From my experience Tesserocr is much faster than Pytesseract.
Tesserocr is a python wrapper aroung the Tesseract C++ API. Whereas pytesseract is a wrapper the tesseract-ocr CLI.
Therefore with Tesserocr you can load the model in the beginning or your program, and run the model seperately (for example in loops to process videos).
With pytesseract, each time you call image_to_string function, it loads the model and process the image, therefore being slower for video processing.
To install tesserocr I just typed in the terminal pip install tesserocr.
To use tesserocr
import tesserocr
from PIL import Image
api = tesserocr.PyTessBaseAPI()
pil_image = Image.open('sample.jpg')
api.SetImage(pil_image)
text = api.GetUTF8Text()
To install pytesseract : pip install pytesseract.
To run it :
import pytesseract
import cv2
image = cv2.imread('sample.jpg')
text = pytesseract.image_to_string(image)

pytesseract is only a binding for tesseract-ocr for Python. So, if you want to use tesseract-ocr in python code without using subprocess or os module for running command line tesseract-ocr commands, then you use pytesseract. But, in order to use it, you have to have a tesseract-ocr installed.
You can think of it this way. You need a tesseract-ocr installed because it's the program that actually runs and does the OCR. But, if you want to run it from python code as a function, you install pytesseract package that enables you to do that. So when you run pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'), it calls the tesseract-ocr with the provided arguments. The results are the same as running tesseract test-european.jpg -l fra. So, you get the ability to call that from the code, but in the end, it still has to run the tesseract-ocr to do the actual OCR.

Pytesseract is a python "wrapper" for the tesseract binary. It offers only the following functions, along with specifying flags (man page):
get_tesseract_version Returns the Tesseract version installed in the system.
image_to_string Returns the result of a Tesseract OCR run on the image to string
image_to_boxes Returns result containing recognized characters and their box boundaries
image_to_data Returns result containing box boundaries, confidences, and other information. Requires Tesseract 3.05+. For more information, please check the Tesseract TSV documentation
image_to_osd Returns result containing information about orientation and script detection.
See the project description for more information.
On the other hand, tesserocr interfaces directly with Tesseract's C++ API (APIExample) which is much more flexible/complex and offers advanced features.

Related

How do I add and print a image in the console in python with visual studio?

I dont know what to import and how to do this. How do I print and add a image in python.
I have tried nothing im just looking for suggestions.
In the console it's impossible, but you can use OpenCV, PIL, or matplotlib to display images.
With OpenCV you can display the pixel value in the console:
If you are interested in image manipulation check out Pillow. It's an image manipulation/processing library.
You can install it with:
python -m pip install Pillow
To load an image and show it use this:
from PIL import Image
im = Image.open("image.png")
im.show()
For more examples and information check out the official documentation.

How to make the opencv-contrib module work?

I followed this guide to install opencv (version 3.4.4) and the contrib modules because I want to work with the SIFT algorithm.
https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/
When I noticed, it was not working as expected after the installation, I deleted the build folder and tried again, but this did not work as well.
I imported like this:
import cv2 as cv
I tried to use SIFT (in python 3.6) in the following ways:
sift = cv.SIFT_create()
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT()
sift = cv.xfeatures2d.SIFT()
As this usually results in errors like this: Attribute Error: module cv2 has no attribute 'SIFT_create' (same thing happens for the other 3 options), I figure that I am either using it in an incorrect way or the installation process itself did not work properly.
After this, I found the pip install opencv-contrib-python and used it. With no results at all.
I would really appreciate some hints on how I can make opencv with contrib modules work.
You may have to say cv2.xfeatures2d_SIFT or similar. the modules of OpenCV don't necessarily map to python submodules.
Since the patent on SIFT expired in 2019, OpenCV moved it back into features2d (main repo) from xfeatures2d (opencv_contrib repo). Please use the most recent 3.4.x release, or 4.x.

Does any one has idea to install Opencv in spyder?

import cv2
Traceback (most recent call last):
File "", line 1, in
import cv2
ImportError: DLL load failed: The specified module could not be found.
Install Anaconda
Install OpenCV-Python to Anaconda
The following instruction works for me is inspired by this OpenCV Youtub video. So far I have got it working on both my Desktop and Laptop. Both 64-bit machines and Windows 8.1.
Download OpenCV Package
Firstly, go to the official OpenCV site to download the complete OpenCV package. Pick a version you like (2.x or 3.x). I am on Python 2.x and OpenCV 3.x - mainly because this is how the OpenCV-Python Tutorials are setup/based on.
Copy and Paste the cv2.pyd file
The Anaconda Site-packages directory (e.g. C:\Users\Johnny\Anaconda\Lib\site-packages in my case) contains the Python packages that you may import. Our goal is to copy and paste the cv2.pyd file to this directory (so that we can use the import cv2 in our Python codes.).
To do this, copy the cv2.pyd file...
From this OpenCV directory (the beginning part might be slightly different on your machine):
Python 2.7 and 32-bit machine:
C:\opencv\build\python\2.7\x84
Python 2.7 and 64-bit machine:
C:\opencv\build\python\2.7\x64
To this Anaconda directory (the beginning part might be slightly different on your machine):
C:\Users\Johnny\Anaconda\Lib\site-packages
After performing this step we shall now be able to use import cv2 in Python code. BUT, we still need to do a little bit more work to get FFMPEG (video codec) to work (to enable us to do things like processing videos.)
Set Enviromental Variables
Right-click on "My Computer" (or "This PC" on Windows 8.1) -> left-click Properties -> left-click "Advanced" tab -> left-click "Environment Variables..." button.
Add a new User Variable to point to the OpenCV (either x86 for 32-bit system or x64 for 64-bit system.) I am currently on a 64-bit machine.
32-BIT OR 64 BIT MACHINE? VARIABLE VALUE
32-bit OPENCV_DIR C:\opencv\build\x86\vc12
64-bit OPENCV_DIR C:\opencv\build\x64\vc12
Append %OPENCV_DIR%\bin to the User Variable PATH.
For example, my PATH user variable looks like this...
Before:
C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts
After:
C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts;%OPENCV_DIR%\bin
This is it we are done! FFMPEG is ready to be used!
Test to confirm
We need to test whether we can now do these in Anaconda (via Spyder IDE):
Import OpenCV package
Use the FFMPEG utility (to read/write/process videos)
Test 1: Can we import OpenCV?
To confrim that Anaconda is now able to import the OpenCV-Python package (namely, cv2), issue these in the IPython Console:
import cv2
print cv2.version
If the package cv2 is imported ok with no errors, and the cv2 version is printed out, then we are all good! Here is a snapshot:
import-cv2-ok-in-anaconda-python-2.png
Test 2: Can we Use the FFMPEG codec?
Place a sample input_video.mp4 video file in a directory. We want to test whether we can:
read this .mp4 video file, and
write out a new video file (can be .avi or .mp4 etc.)
To do this we need to have a test python code, call it test.py. Place it in the same directory as the sample input_video.mp4 file.
This is what test.py may look like (Note: many thanks to Pete's and Warren's suggestions in the comment field - I have replaced my original test code with his - please test it yourself and let us know if this works better):
import cv2
cap = cv2.VideoCapture("input_video.mp4")
print cap.isOpened() # True = read video successfully. False - fail to read video.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("output_video.avi", fourcc, 20.0, (640, 360))
print out.isOpened() # True = write out video successfully. False - fail to write out video.
cap.release()
out.release()
This test is VERY IMPORTANT. If you'd like to process video files, you'd need to ensure that Anaconda / Spyder IDE can use the FFMPEG (video codec). It took me days to have got it working. But I hope it would take you much less time! :)
Note: one more very important tip when using the Anaconda Spyder IDE. Make sure you check the Current Working Directory (CWD)!!!
Next step
Now that we have the OpenCV-Python package (cv2), a potential good next step may be to checkout the OpenCV-Python Tutorials.

To detect digits from an image using cv2 and python

I am trying to detect digits located inside a grid and to tell their positions in an image and don't know where to start. So any help is welcome. So far I have used GT Text software but it didn't solve the purpose. Any helper function, libraries, tutorials, links or anything is welcome.
You should check out the pytesseract module:
https://pypi.python.org/pypi/pytesseract/0.1
It has a one-liner for what you're trying to do:
try:
import Image
except ImportError:
from PIL import Image
import pytesseract as tes
results = tes.image_to_string(Image.open('test.png'),boxes=True)
This will give you results, which has each digit and the image coordinates of its bounding box.
You will need to install PIL (python image library, pip install PIL) and the tesseract c library (brew install tesseract if you have homebrew..) so it's not super trivial but once you have it working, this is the most straight forward OCR in python, and requires no training whatsoever.

How to process JPEG-XR files in Pillow (or generally in Python) in linux?

JPEG XR is a Microsoft developed format.
On Ubuntu 14.04, I installed
sudo apt-get install libjxr-dev
sudo apt-get install libjxr-tools
and now I can at least convert the *.jxr files into other more friendly formats like bitmap or tif, with
JxrDecApp -i in.jxr -o output.bmp -c 0
or just using (ImageMagick)
convert in.jxr output.tif
However, Pillow still won't read the jxr image:
from PIL import Image
img = Image.open('in.jxr', 'rb')
gives IOError: cannot identify image file 'in.jxr'.
(it has no problem with the out.tif of course).
I rebuilt Pillow (with --no-cache-dir after installing the libjxr-dev lib too by the way).
I also tried using Wand, but despite the ImageMagick convert command working, Wand won't read the file (possibly because ImageMagick just delegates this task to JxrDecApp anyway).
Finally, I tried imageio, with im = imageio.imread('my.jxr'), and it also failed to read the file.
Is there anyway I can process these JXR images in Pillow? Or any way at all in Python for that matter? I'm stuck calling convert with a subprocess at the moment..

Categories

Resources