1

I'm in the middle of developing a system that predict numbers from 7Seg LCD and I'm using for the matter tesseract OCR engine and it's wrapper for python pytesseract.

I'm taking pictures with a camera then cropping the Region of Interest and I found out that I have to enhance my Image quality to increase the accuracy of the OCR engine.

I used some Image processing techniques (gray scale --> Gaussian Blur --> threshold) and I got a quiet good image but tesseract still can't detect the numbers in the image.

I use the code:

image = cv2.imread('test.jpg')
image = image[50:200, 300:540]

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (3,3), 0)

_, image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

cv2.imshow('result', image)

cv2.waitKey()
cv2.destroyAllWindows()

cv2.imwrite('enhanced.jpg', image)

tess_dir_config = r'--tessdata-dir "C:\Program Files\Tesseract-OCR\tessdata"'
text = image_to_string(image, lang='letsgodigital', config=tess_dir_config)
print(text)

The Output Image:

1

The Input Image:

2

The engine usually have an empty output and if not it will not detect the number correctly.

Is there some sort of other image processing that I can use to get the potential of the Engine.

Note: I'am using letsgodigital weights

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

1 Answers1

0

This works for me, if I improve the crop a little, and use page segmentation mode 7. (This mode does no page segmentation and assumes a single line of text.)

import cv2
import matplotlib.pyplot as plt
import pytesseract

image = cv2.imread('seven_seg_disp.jpg')
# Strip off top of meter and little percent symbol.
image = image[90:200, 300:520]
# plt.imshow(image)

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (3,3), 0)

_, image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# plt.imshow(image)

tess_dir_config = r'--tessdata-dir "../.tesseract" --psm 7'
text = pytesseract.image_to_string(image, lang='letsgodigital', config=tess_dir_config)
text = text.strip()
print(text)  # prints 75

Note: I changed the value of tessdata-dir because it's in a different place on my computer.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • where to find "letsgodigital" language ? – Yunus Temurlenk Aug 12 '22 at 06:37
  • @YunusTemurlenk I downloaded the version I used here: https://github.com/arturaugusto/display_ocr/blob/master/letsgodigital/letsgodigital.traineddata – Nick ODell Aug 12 '22 at 14:24
  • 1
    As correct detection is critical for me, I've developed this software that uses a more traditional approach: https://github.com/arturaugusto/7seg-ocr – art Oct 07 '22 at 16:38