Issue
I have the exact issue as this question and the sample bitmap numbers even seem to look exactly the same; however, the solution no longer works as I get the error AttributeError: module 'cv2' has no attribute 'text'
on
tesser = cv2.text.OCRTesseract_create('C:/Program Files/Tesseract-OCR/tessdata/','eng','0123456789',3,3)
I'm using OpenCV 4.7.0 installed using opencv-contrib-python
, pytesseract 0.3.10, and Tesseract 4 on Windows 10.
I believe the solution above no longer works with OpenCV 4.7.0.
Below is my script, but I get the result 511235160
rather than 0960051123160
.
Note that the numbers used to be vertical, and I have a script that extracts the numbers and lays them out horizontally, so spacing between numbers can be modified.
I appreciate any help on this as I've spent many hours trying to get it to work.
import numpy as np
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img_orig = cv2.imread('img_bitmap_numbers.png',0) # Load image as gray scale
#Dilate (bloat) image
ret, thresh = cv2.threshold(img_orig, 127, 255, cv2.THRESH_BINARY)
kernel = np.ones((3,3),np.uint8)
img_eroded = cv2.erode(thresh,kernel,iterations = 1)
# cv2.imshow('combined', img_eroded)
# cv2.waitKey(0)
tesseract_config = '--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789'
a = pytesseract.image_to_string(img_eroded, lang='eng', config = tesseract_config)
print(a)
Solution
Her is my try:
import pytesseract
import cv2
image_file = "numbers.png"
# load the input image, convert it from BGR to RGB channel ordering,
image = cv2.imread(image_file, cv2.IMREAD_UNCHANGED)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print('Original Dimensions : ',image.shape)
scale_percent = 27 # percent of original size
width = int(rgb.shape[1] * scale_percent / 100)
height = int(rgb.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized = cv2.resize(rgb, dim, interpolation = cv2.INTER_AREA)
print('Resized Dimensions : ',resized.shape)
grayImage = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 150, 245, cv2.THRESH_BINARY) # 0...255
bw = cv2.imshow('Black white image', blackAndWhiteImage)
# Configuration
options = r'--psm 6 --oem 3 -c tessedit_char_whitelist=0123456789.,:mIndexMtr'
# OCR the input image using Tesseract
text_bw = pytesseract.image_to_string(blackAndWhiteImage, config=options)
print(text_bw)
with open("numbers.txt", 'w') as f:
f.writelines(text_bw)
cv2.imshow('Resized', resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output: 0960051123160
Answered By - Hermann12
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.