I tried to put boxes in an image file around the texts in it, using pytesseract function image_to_data, but encounters the following error on colab:
KeyError Traceback (most recent call last)
<ipython-input-10-a92a28892aac> in <module>()
6 img = cv2.imread("a.jpg")
7
----> 8 d = pytesseract.image_to_data(img, output_type=Output.DICT)
9 print(d.keys())
5 frames
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in save(self, fp, format, **params)
2121 expand=0,
2122 center=None,
-> 2123 translate=None,
2124 fillcolor=None,
2125 ):
KeyError: 'PNG'
The code I am using is:
import cv2
import pytesseract
from pytesseract import Output
from PIL import Image
img = cv2.imread("a.jpg")
d = pytesseract.image_to_data(img, output_type=Output.DICT)
print(d.keys())
Thinking that may be that image_to_data can only work with PNG and not jpeg (which is odd), I added few lines to change the jpeg to png before feeding to image_to_data, but the same error persists. Why is this happening?
In colab, install tesseract by below code
!sudo apt-get install tesseract-ocr
Next install pytesseract library and restart the runtime.
!pip install pytesseract==0.3.9
Make sure you restart the runtime, after restarting the runtime, execute the code. Upload the image in workspace or get the path from drive and make use of it instead of the path declared here.
import pytesseract
from pytesseract import Output
from PIL import Image
import cv2
imag = cv2.imread('/content/1.jpg')
d = pytesseract.image_to_data(imag, output_type=Output.DICT)
print(d.keys())
This will help you. For reference colab notebook
The output seems like,
dict_keys(['level', 'page_num', 'block_num', 'par_num', 'line_num', 'word_num', 'left', 'top', 'width', 'height', 'conf', 'text'])
Related
I want to tried using OCR and pytesseract. I have code like this
!sudo apt install tesseract-ocr
!pip install pytesseract
import pytesseract
import shutil
import os
import random
try:
from PIL import Image
except ImportError:
import Image
from google.colab import files
uploaded = files.upload()
extractedInformation = pytesseract.image_to_string(Image.open(1.jpg))
or you can check it out here : Google Colab
But when I try to upload some image and try to extract the file,it show error like this
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-37-44411f3825fa> in <module>
----> 1 extractedInformation = pytesseract.image_to_string(Image.open('1.jpg'))
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2894
2895 Note that this function decodes pixel data only, not entire images.
-> 2896 If you have an entire image file in a string, wrap it in a
2897 :py:class:`~io.BytesIO` object, and use :py:func:`~PIL.Image.open` to load it.
2898
UnidentifiedImageError: cannot identify image file '1.jpg'
I have asked my friend to try the code but why that he has no error like me? The code is run smoothly with no error. is that something missing in my computer? or something that I should Install first?
Long shot but I am stuck. Running this test code in Amazon Sagemaker with a Python3 kernel and the function doesn't recognize the web location of the image (media.ci.org). I can't find anything in the documentation from facenet_pytorch about this, anyone know if this is possible? I can do this in R, but much slower.
pip install facenet-pytorch
from facenet_pytorch import MTCNN, InceptionResnetV1
# If required, create a face detection pipeline using MTCNN:
mtcnn = MTCNN()
# Create an inception resnet (in eval mode):
resnet = InceptionResnetV1(pretrained='vggface2').eval()
from PIL import Image
img = Image.open("https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg")
FileNotFoundError Traceback (most recent call last)
in
----> 1 img = Image.open("https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg")
/opt/conda/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode, formats)
3090
3091 if filename:
-> 3092 fp = builtins.open(filename, "rb")
3093 exclusive_fp = True
3094
FileNotFoundError: [Errno 2] No such file or directory: 'https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg'
You should ideally use requests to download the image locally and then using PIL.Image to open something like this.
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
On an Anaconda set up, I want to run following simple code:
from PIL import Image
img = Image.open('test.webp')
This works on my Linux machine, but on Windows:
UserWarning: image file could not be identified because WEBP support not installed
warnings.warn(message)
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-3-79ee787a81b3> in <module>
----> 1 img = Image.open('test.webp')
~\miniconda3\lib\site-packages\PIL\Image.py in open(fp, mode, formats)
3028 for message in accept_warnings:
3029 warnings.warn(message)
-> 3030 raise UnidentifiedImageError(
3031 "cannot identify image file %r" % (filename if filename else fp)
3032 )
UnidentifiedImageError: cannot identify image file 'test.webp'
I do have the libwebp package installed, and libwebp.dll is present in the Library\bin directory of my Anaconda set up.
Any idea?
Looks like no solution is in sight. Assuming dwebp from the libwepb library is available in the system, this works for me to generate a Pillow Image object from a WEBP file:
import os
import subprocess
from PIL import Image, UnidentifiedImageError
from io import BytesIO
class dwebpException(Exception):
pass
def dwebp(file: str):
webp = subprocess.run(
f"dwebp {file} -quiet -o -", shell=True, capture_output=True
)
if webp.returncode != 0:
raise dwebpException(webp.stderr.decode())
else:
return Image.open(BytesIO(webp.stdout))
filename = 'test.webp'
try:
img = Image.open(filename)
except UnidentifiedImageError:
if os.path.splitext(filename)[1].lower() == ".webp":
img = dwebp(filename)
else:
raise
The question is whether Anaconda actually builds Pillow with webp support. Looking on the anaconda website I couldn't determine this.
However, conda-forge does build Pillow with webp support, see here. So you might want to consider using that.
I want to import an image on Google Colab from PIL. By doing so:
from PIL import Image
img = Image.open('smallhouse.jpg')
img
However, I get this error.
FileNotFoundError Traceback (most recent call last)
<ipython-input-20-8ab03a99a9db> in <module>()
1
2 from PIL import Image
----> 3 img = Image.open('smallhouse.jpg')
4 img
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2807
2808 if filename:
-> 2809 fp = builtins.open(filename, "rb")
2810 exclusive_fp = True
2811
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
Not sure why. Thanks!
1. You must upload the smallhouse.jpg image to your Google Drive.
2. In your .ipynb file, you need to include this code at the beginning of your application: drive.mount('/content/gdrive').
Go to the URL provided. Then, you will be prompted to give permission to access your Google Drive. Copy the code provided, access your application and paste the code there.
3. Finally, you can pass your image's path, which now is: /content/gdrive/My Drive/smallhouse.jpg.
Your code will look something like this:
# Imports
from google.colab import drive
from PIL import Image
# Mount Google Drive for fast, responsible access to files
drive.mount('/content/gdrive')
# Open image
img = Image.open('/content/gdrive/My Drive/smallhouse.jpg')
img
I have a multiple page .tif file, I am trying to extract text from it using Tesseract OCR but I am getting this error
TypeError: Unsupported image object
Code
from PIL import Image
import pytesseract
img = Image.open('Group 1/1_CHE_MDC_1.tif')
text = pytesseract.image_to_string(img.seek(0)) # OCR on 1st Page
text = ' '.join(text.split())
print(text)
ERROR
Any idea why its happening
Image.seek does not have a return value so you're essentially running:
pytesseract.image_to_string(None)
Instead do:
img.seek(0)
text = pytesseract.image_to_string(img)
I had a same question and i have tried below code and it worked for me :-
import glob
import pytesseract
import os
os.chdir("Set your Tesseract-OCR .exe file path")
b = ''
for i in glob.glob('Fullpath of your image directory/*.tif'): <-- you can give *.jpg extension in case of jpg image
if glob.glob('*.tif'):
b = b + (pytesseract.image_to_string(i))
print(b)
Happy learning !