What is the Best Way to Get Text From Image with Python? - python

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)

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.

What is the difference between Pytesseract and Tesserocr?

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.

how to show image after fetching from firebase

I am trying to fetch image from firebase storage using python into my raspberry pi 3.
import urllib.request
URL=urllib.request.urlretrieve("https://firebasestorage.googleapis.com/v0/b/cameraviewer-32936.appspot.com/o/images%2F49567?alt=media&token=1eded9d0-b9f0-48bf-b869-37756b31a94a")
URL object has a few key value pairs. One of them was 'str' with value of:
'C:\Users\DELL\AppData\Local\Temp\tmp4ki4w7we'
The above value represents the path of image fetched into my device But how can i open image. I don't know where to go or am I confused a lot.
It is pretty simple using Python Imaging Library (PIL). PIL is free and open-source library.
How to Install PIL
If you don't have PILLOW installed, first open a terminal ( CTRL + ALT + T ) and download it using command:
sudo pip install Pillow
Displaying an Image
After you successfully installed it, use the code below to show your downloaded image:
from PIL import Image
import urllib.request
URL=urllib.request.urlretrieve("https://firebasestorage.googleapis.com/v0/b/cameraviewer-32936.appspot.com/o/images%2F49567?alt=media&token=1eded9d0-b9f0-48bf-b869-37756b31a94a")
img = Image.open(URL[0])
img.show()

Python OpenCV imshow fails

I installed opencv on my Ubuntu 14.04 system system with
pip install python-opencv
my Python version is 2.7.14
import cv2
cv2.__version__
tells me that I have the OpenCV version 3.4.0.
After that I wanted to follow the tutorial on the OpenCV website
import numpy as np
import cv2 as cv
img = cv.imread('messi5.jpg',0)
print img
It works fine until this point, but then I am supposed to enter
cv.imshow('image',img)
and I get the following error:
QObject::moveToThread: Current thread (0x233cdb0) is not the object's thread (0x2458430).
Cannot move to target thread (0x233cdb0)
QObject::moveToThread: Current thread (0x233cdb0) is not the object's thread (0x2458430).
Cannot move to target thread (0x233cdb0)
QPixmap: Must construct a QApplication before a QPaintDevice
Does anyone know what the problem is?
Try checking if the image you are reading is loading
image = cv2.imread(filepath,0) #0 for gray scale
if image is None:
print "Cant Load Image"
else:
cv2.imshow("Image", image)
cv2.waitKey(0)
Apparently
pip install python-opencv
is not working at all and should not be used. After I installed Opencv from their website it worked
seems hard to install opencv on ubuntu, I finally get it with a docker image
https://hub.docker.com/r/jjanzic/docker-python3-opencv/
or you can download sources and make install as described on
https://milq.github.io/install-opencv-ubuntu-debian/ using bash script

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.

Categories

Resources