How to find numbers in images and read them?

Viewed 4456

I have this picture:

Structure with painted number

and this is my Region of Interest:

which is a number that I would like to recognize and "read".

I don't know why I can't detect it using pytesseract. Even though I preprocess it and get this image free of noise:

preprocessed and binary image

Here is the configuration I am using to read it:

  1. Only numbers;

  2. One char;

    text = pytesseract.image_to_string(number_5,  lang='eng',config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
    

And still, I just get \n\x0c as an answer.

I would like to ask for some tips on how to recognize images with unique characters (only numbers in this case);

And also a question about number detection. Is there a model that can search for numbers in a photo and return a bounding box of where they are located?

2 Answers

One way of detecting 5 in the image would be masking the image.

You could use Thresholding Operations using inRange. First, we need to find the upper and lower bounds values for thresholding. After few trials, I figured that the following is suitable for recognition.

msk = cv2.inRange(hsv, np.array([0, 0, 175]), np.array([179, 255, 255]))
  • the lower bound is np.array([0, 0, 175])
  • the upper bound is np.array([179, 255, 255])

The result will be:

enter image description here

Here above we can see the number 5 clearly.

Now we can apply the following processing methods.

krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dlt = cv2.dilate(msk, krn, iterations=1)
thr = 255 - cv2.bitwise_and(dlt, msk)

The result will be:

enter image description here

Now if we apply tesseract

d = pytesseract.image_to_string(thr, config="--psm 10")

The result will be:

5

Code:

import cv2
import numpy as np
import pytesseract

# Load the img
img = cv2.imread("MjfJF.png")

# Cvt to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Get binary-mask
msk = cv2.inRange(hsv, np.array([0, 0, 175]), np.array([179, 255, 255]))
krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dlt = cv2.dilate(msk, krn, iterations=1)
thr = 255 - cv2.bitwise_and(dlt, msk)

# OCR
d = pytesseract.image_to_string(thr, config="--psm 10")
print(d)

I think your image has to be black on white. You could also use another value for the psm argument.

Related