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.
Related
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.
I want to get the text out of an image. I tried tesseract but I had issues installing it, so im wondering if I can get some help with that or another way to do it.
When I try to use tesseract it says I have no module names PIL? But I know I have pillow installed and i thought that was in reference to it.
I've provided a Colab solution since that will probably be useful to the most people.
Install tesseract in our Colab environment.
!sudo apt install tesseract-ocr
!pip install PyTesseract
Import libraries and mount our Drive
from google.colab import drive
from google.colab.patches import cv2_imshow
drive.mount('/content/drive/')
Set our pytesseract path, read in source image from our Drive, show our source image, then finally convert the image to text.
import cv2
import pytesseract
import numpy as np
pytesseract.pytesseract.terreract_cmd = {
r'/usr/bin/tesseract'
}
src = cv2.imread('/content/drive/MyDrive/Colab Notebooks/images/macbeth.png')
cv2_imshow(src)
output_txt = pytesseract.image_to_string(src)
print(type(output_txt))
print(output_txt)
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.
I want to use one of the newest versions of Python, (3.5+), and I need to import Image. I use:
from PIL import Image
except this returns an error. I don't have the error on my right now but the important part is there is a problem with PIL (I have already determined that from my Python Discord server)
Can I use pillow? I think I have that installed. I would try it but I want to know how (and if) it's applicable. Thanks
Can I use pillow?
Sure. pillow is a successor project of PIL, as development on the latter was last continued in 2011.
The first thing mentioned in the official pillow documentation is how to import the Image class.
I tried to use Python's Image Module on my Mac (new to mac)and I had trouble setting it up. I have Yosemite and some of the posts that I found online didn't work. I ended up installing Homebrew and I finally got PIL on my computer. However, instead of using import image (which I saw people doing it online), I have to use from PIL import image. Is there key difference between import Image and from PIL import Image. It's the first time that I actually use Image module.
One more question, do I actually need to install third party tools like Homebrew and Macports to set up my environment?
If you are using PIL, you might be able to use import Image.
If you are using Pillow, you must use from PIL import Image.
This is by design.
1-Pillow and PIL cannot co-exist in the same environment. Before installing Pillow, please uninstall PIL.
2-Pillow >= 1.0 no longer supports “import Image”. Please use “from PIL import Image” instead. so be careful with it
3-Pillow >= 2.1.0 no longer supports “import _imaging”. Please use “from PIL.Image import core as _imaging” instead.
In python, modules are represented by *.py files. These files can be imported using import module. That statement will first search for a build-in module, if it fails to find one it will search in a given list of directories (PYTHONPATH).
When you imported your module, you can then access names (aka functions or classes) via module.name.
However, when using from module import myFunc you can reference that function directly without using the modulename. The same goes for from module import *.
Now, to the PIL:
Image is a name (here class) in the PIL module. As, import Image searches for modules called Image. It can't find one, because Image is part of the PIL module.
You can also look at the documentation here: Modules in Python
I hope this helped you understanding modules better.