I have some images with two numbers separated by /
very close to them. Tesseract doesn't recognize that dash at all, or recognizes it as 1
in most of them (For few images it works).
My Tesseract code:
pytesseract.image_to_string(img,lang='eng',config='--psm 7 --oem 3 -c tessedit_char_whitelist=/0123456789').strip()
I've tried with other psm
and oem
configs. I've been playing with the images a lot, eg with cv2.threshold
, cv2.cvtColor
, resizing.
EDIT:
After
img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV)[1]` <br>
img = cv2.resize(img,(0,0), fx=1.5, fy=1.5)`
most of the images return good values, but some of them add 5
in random place (images after conversion):
and few cases still don't recognize the slash.
Grayscaling, even more enlarging, and a different threshold did the job on my machine:
import cv2
import pytesseract
def extract_stats(img_filepath):
img = cv2.imread(img_filepath, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (0, 0), None, 4.0, 4.0)
img = cv2.threshold(img, 160, 255, cv2.THRESH_BINARY)[1]
config = '--psm 6 -c tessedit_char_whitelist="0123456789/"'
text = pytesseract.image_to_string(img, config=config)
print(text.replace('\n', '').replace('\f', ''))
for filepath in ['Bzh3j.png', 't9gAh.png', 'BBy2P.png']:
extract_stats(filepath)
# 4319/6149
# 943/7114
# 103/6149
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19042-SP0
Python: 3.9.6
PyCharm: 2021.2
OpenCV: 4.5.3
pytesseract: 5.0.0-alpha.20201127
----------------------------------------
With tesseract 5.0.0-alpha-20210401 and the tessdata_best I got correct result with this code:
import cv2
import numpy as np
import pytesseract
from IPython.display import display
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'bin\\tesseract.exe'
tessdata = "tessdata"
img = cv2.imread('t9gAh.png', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img,(0,0), fx=3.0, fy=3.0)
bin_inverted = ~cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
test = pytesseract.image_to_string(bin_inverted, config=f'--psm 6 --tessdata-dir "{tessdata}"')
print(text.replace('\n\f'', ''))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.