I am making a model for reading digital numbers on a digital measuring instrument, where the result must be a decimal number. However, my problem is that the model I wrote cannot recognize punctuation marks in decimal numbers. Is there anyone who can help me so that I can read the OCR results more optimally?
Thank You
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pytesseract
# preprocessing image
def preprocess_image(image_path):
image = cv2.imread(image_path)
# Grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Thresholding
_, thresh_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Dilation
dilated_image = dilate(thresh_image)
# Erotion
eroded_image = erode(dilated_image)
# Opening (erosion followed by dilation)
opened_image = opening(eroded_image)
# Noise removal
denoised_image = remove_noise(opened_image)
return denoised_image
# get grayscale image
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#thresholding
def thresholding(image):
return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
#dilation
def dilate(image):
kernel = np.ones((5,5),np.uint8)
return cv2.dilate(image, kernel, iterations = 1)
#erosion
def erode(image):
kernel = np.ones((5,5),np.uint8)
return cv2.erode(image, kernel, iterations = 1)
#opening - erosion followed by dilation
def opening(image):
kernel = np.ones((5,5),np.uint8)
return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
# noise removal
def remove_noise(image):
return cv2.medianBlur(image,5)
# Path to the image
input_image_path = 'images/train7.jpg'
# Preprocessing image
preprocessed_image = preprocess_image(input_image_path)
# Show preprocessing result
plt.imshow(preprocessed_image, cmap='gray')
plt.title('Preprocessed Image')
plt.axis('off')
plt.show()
pytesseract.pytesseract.tesseract_cmd = r"C:/Program Files/Tesseract-OCR/tesseract.exe"
custom_config = r'--oem 3 --psm 7 -l ssd -c tessedit_char_whitelist=0123456789.'
print('-----------------------------------------')
print('TESSERACT OUTPUT')
print('-----------------------------------------')
print(pytesseract.image_to_string(preprocessed_image, config=custom_config))
output:
-----------------------------------------
TESSERACT OUTPUT
-----------------------------------------
1485
1