I have applied the following adjustments to the original image:
This results in the following image
Using tesseract, i'm converting the image to a string but it only seems to recognise the 4.
Code to convert to text -
print (tess.image_to_string(img, config='--psm 6 -c tessedit_char_whitelist="9876543210"'))
4
I then attempted to sharpen using the following code resulting in the next image, but tesseract is still only recognising the 4. Any idea how I can sharpen this further so tesseract recognises this as 40?
kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
sharpened = cv2.filter2D(img,-1,kernel)
print (tess.image_to_string(sharpened, config='--psm 6 -c tessedit_char_whitelist="9876543210"'))
4
Alternatively, the original image is the following without any resizing.
Tesseract does pick this up as 40 but I need it to pick up the larger image. Is there a way I can resize but retain the quality/sharpness?
Resizing code -
img = cv2.resize(img,(0,0),fx=5,fy=5)
If you have the possibility to use ImageMagic
:
import subprocess
import cv2
import pytesseract
# Image manipulation
# Commands https://imagemagick.org/script/convert.php
mag_img = r'D:\Programme\ImageMagic\magick.exe'
con_bw = r"D:\Programme\ImageMagic\convert.exe"
in_file = r'40.png'
out_file = r'40_bw.png'
# Play with black and white and contrast for better results
process = subprocess.run([con_bw, in_file, "-resize", "100%","-threshold","60%", out_file])
# Text ptocessing
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread(out_file)
# Parameters see tesseract doc
custom_config = r'--psm 7 --oem 3 -c tessedit_char_whitelist=01234567890'
tex = pytesseract.image_to_string(img, config=custom_config)
print(tex)
with open("number.txt", 'w') as f:
f.writelines(tex)
cv2.imshow('image',img)
cv2.waitKey(12000)
cv2.destroyAllWindows()
Option 2: With OpenCV
, what you prefer.
import cv2
import pytesseract
img = cv2.imread("40.png")
# Read a grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Convert grayscale image to binary use THRESH_OTSU, named after its creator Nobuyuki Otsu is a good start point of thresholding.
(thresh, im_bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# Optional play around with the thresh value
# thresh = 200
# im_bw = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)[1]
# write image to disk
cv2.imwrite('bw_40.png', im_bw)
# Parameters see tesseract doc
custom_config = r'--psm 7 --oem 3 -c tessedit_char_whitelist=01234567890'
tex = pytesseract.image_to_string(im_bw, config=custom_config)
print(tex)
cv2.imshow('image',im_bw)
cv2.waitKey(12000)
cv2.destroyAllWindows()