Extract text from image with pytesseract - python

I tried extract numbers from original image https://imgur.com/a/adMaKGy , but with no luck.
Output from pytesseract is: "[a ]:[4] G2):Go] [7 ):Ce J"
Thank you for advice,
My code:
import pytesseract
import cv2
pytesseract.pytesseract.tesseract_cmd = 'folder /tesseract.exe'
img = cv2.imread("folder /test_image.png")
text = pytesseract.image_to_string(img)
print(text)

The README says that OpenCV images are in BGR format and pytesseract assumes RGB format, so you need to convert it
import cv2
img_cv = cv2.imread(r'/<path_to_image>/digits.png')
# By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
# we need to convert from BGR to RGB format/mode:
img_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(img_rgb))
# OR
img_rgb = Image.frombytes('RGB', img_cv.shape[:2], img_cv, 'raw', 'BGR', 0, 0)
print(pytesseract.image_to_string(img_rgb))

Related

Why Opencv provides RGB instead of BGR?

I am using OpenCV - 3.4.9.31
I was trying to run a very basic code to read an image, but to my surprise, the output was in RGB colorspace instead of BGR.
import cv2
folder = 'C:/Users/xxx/PycharmProjects/Images/'
picture = 'lena.png'
filename = folder + picture
Img = cv2.imread(filename)
cv2.imshow("Image", Img)
cv2.waitKey(0)
Why do I see RGB image instead of BGR ?
If you want to convert it to BGR, you can do this :
imgBGR = cv2.cvtColor(Img, cv2.COLOR_RGB2BGR)

Removing alpha channels from grayscale images

I have some B&W images, but in RGBa. I used skimage rgb2gray(inp_image) to convert them into grayscale. Yet they become grayscale images with alpha channel.
What do I do if I want to have those RGBa converted to grayscale without alpha channel?
You can try out this.
import cv2
image = cv2.imread('path to your image')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
for multiple images
import cv2
from os import listdir,makedirs
from os.path import isfile,join
source = r'path to source folder'
destination = r'path where you want to save'
files = [f for f in listdir(source) if isfile(join(source,f))]
for image in files:
try:
img = cv2.imread(os.path.join(source,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(destination,image)
cv2.imwrite(destination,gray)
except:
print ("{} is not converted".format(image))

An alternative to read image from scipy to cv2

the following python script I want to convert using opencv python, how do I make it converted?
script: scipy.misc.imread(path, mode='RGB').astype(np.float)
I want to convert it using cv2 and what would be alternative for astype(np.float) with this?
import cv2
import scipy.misc
img = scipy.misc.imread(path, mode='RGB').astype(np.float)
You may use cv2.imread, convert color format from BGR to RGB, and convert to float:
path = 'chelsea.png'
ref_img = scipy.misc.imread(path, mode='RGB').astype(np.float)
img = cv2.imread(path) # Read input imgae
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert color format form BGR to RGB (OpenCV default is BGR).
img = img.astype(np.float) # Convert ot float
print(np.all(img == ref_img))
This should do the trick:
img = cv2.imread(path, -1).astype(np.float)

Pytesseract image_to_string empty output

I have this image that was cropped from another image and I want to give this image as an input to image_to_string method:
import pytesseract
import cv2
num_plate = cv2.imread('E:\Images\car_plate222.jpeg' , cv2.IMREAD_GRAYSCALE)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
cv2.dilate(num_plate, (15, 15), num_plate)
pytesseract.image_to_string(num_plate)
Here's the photo:
Car Plate:
I used dilation for better performance, but it doesn't give me desired output (Sometimes gives me empty string and sometimes gives me weird output)
Does anybody know what's wrong?
You must threshold the image before passing it to pytesseract. That increases the accuracy.
Here is a sample:
import cv2
import numpy as np
import pytesseract
from PIL import Image
# Grayscale image
img = Image.open('E:\\WorkDir\\KAVSEE\\Python\\test.jpg').convert('L')
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)
# Older versions of pytesseract need a pillow image
# Convert back if needed
img = Image.fromarray(img.astype(np.uint8))
print(pytesseract.image_to_string(img))
Hope this helps :)

How to check whether my image is RGB format or BGR format in python? How do i convert them and viceversa?

I am doing some preprocessing things on pretrained data in OpenVino model.
It says it only uses the BGR format image.
Here ,
How do i check in python whether my image is in BGR format or RBG format?
my loaded image code is as
import cv2
import numpy as np
from PIL import Image
image = cv2.imread('29101878_988024658021087_5045014614769664000_o.jpg')
print(image.shape)
Gives output of
shape (973,772,3)
How do i check image is RBG or BGR format?
If it is in RBG format How do i convert it to BGR and viceversa?
When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.
Reference :
Note: In the case of color images, the decoded images will have the channels stored in B G R order.
Link : https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#imread)
To convert you can use
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
and vice versa.
To check if the image is in RGB or BGR format we can use:
import cv2
from PIL import Image
image = cv2.imread('image path')
img = Image.fromarray(image)
img.mode

Categories

Resources