Sheldon
Sheldon

Reputation: 4633

Adjusting pytesseract parameters

Note: I am migrating this question from Data Science Stack Exchange, where it received little exposure.

I am trying to implement an OCR solution to identify the numbers read from the picture of a screen.

I am adapting this pyimagesearch tutorial to my problem.

Because I am dealing with a dark background, I first invert the image, before converting it to grayscale and thresholding it:

inverted_cropped_image = cv2.bitwise_not(cropped_image)
gray = get_grayscale(inverted_cropped_image)
thresholded_image = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)[1]

Then I call pytesseract's image_to_data function to output a dictionary containing the different text regions and their confidence intervals:

from pytesseract import Output
results = pytesseract.image_to_data(thresholded_image, output_type=Output.DICT)

Finally I iterate over results and plot them when their confidence exceeds a user defined threshold (70%). What bothers me, is that my script identifies everything in the image except the number that I would like to recognize (1227.938).

enter image description here

My first guess is that the image_to_data parameters are not set properly.

Checking this website, I selected a page segmentation mode (psm) of 11 (sparse text) and tried whitelisting numbers only (tessedit_char_whitelist=0123456789m.'):

results = pytesseract.image_to_data(thresholded_image, config='--psm 11 --oem 3 -c tessedit_char_whitelist=0123456789m.', output_type=Output.DICT)

Alas, this is even worse, and the script now identifies nothing at all!

enter image description here

Do you have any suggestion? Am I missing something obvious here?

EDIT #1:

At Ann Zen's request, here's the code used to obtain the first image:

import imutils
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pytesseract
from pytesseract import Output

def get_grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

filename = "IMAGE.JPG"
cropped_image = cv2.imread(filename)
inverted_cropped_image = cv2.bitwise_not(cropped_image)

gray = get_grayscale(inverted_cropped_image)

thresholded_image = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)[1]

results = pytesseract.image_to_data(thresholded_image, config='--psm 11 --oem 3 -c tessedit_char_whitelist=0123456789m.', output_type=Output.DICT)

color = (255, 255, 255)
for i in range(0, len(results["text"])):
    x = results["left"][i]
    y = results["top"][i]
    w = results["width"][i]
    h = results["height"][i]
    text = results["text"][i]
    conf = int(results["conf"][i])
    print("Confidence: {}".format(conf))
    if conf > 70:
        print("Confidence: {}".format(conf))
        print("Text: {}".format(text))
        print("")
        text = "".join([c if ord(c) < 128 else "" for c in text]).strip()
        cv2.rectangle(cropped_image, (x, y), (x + w, y + h), color, 2)
        cv2.putText(cropped_image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,1.2, color, 3)
cv2.imshow('Image', cropped_image)
cv2.waitKey(0)

EDIT #2:

Rarely have I spent reputation points so well! All three replies posted so far helped me refine my algorithm.

First, I wrote a Tkinter program allowing me to manually crop the image around the number of interest (modifying the one found in this SO post)

Then I used Ann Zen's idea of narrowing down the search area around the fractional part. I am using her nifty process function to prepare my grayscale image for contour extraction: contours, _ = cv2.findContours(process(img_gray), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE). I am using RETR_EXTERNAL to avoid dealing with overlapping bounding rectangles.

I then sorted my contours from left to right. Bounding rectangles exceeding a user-defined threshold are associated with the integral part (white rectangles); otherwise they are associated with the fractional part (black rectangles).

enter image description here

I then extracted the characters using Esraa's approach i.e. applying a Gaussian blur prior to calling Tesseract. I used a much larger kernel (15x15 vs 3x3) to achieve this.

I am not out of the woods yet, but hopefully I will get better results by using Ahx's adaptive thresholding.

Upvotes: 7

Views: 7487

Answers (3)

Red
Red

Reputation: 27547

The Concept

As you have probably heard, pytesseract is not good at detecting text of different sizes on the same line as one piece of text. In your case, you want to detect the 1227.938, where the 1227 is much larger than the .938.

One way to go about solving this is to have the program estimate where the .938 is, and enlarge that part of the image. After that, pytesseract will have no problem in returning the text.

The Code

import cv2
import numpy as np
import pytesseract

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 200, 255, cv2.THRESH_BINARY)
    img_canny = cv2.Canny(thresh, 100, 100)
    kernel = np.ones((3, 3))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=2)
    return cv2.erode(img_dilate, kernel, iterations=2)

img = cv2.imread("image.png")
img_copy = img.copy()
hh = 50

contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    if 20 * hh < cv2.contourArea(cnt) < 30 * hh:
        x, y, w, h = cv2.boundingRect(cnt)
        ww = int(hh / h * w)
        src_seg = img[y: y + h, x: x + w]
        dst_seg = img_copy[y: y + hh, x: x + ww]
        h_seg, w_seg = dst_seg.shape[:2]
        dst_seg[:] = cv2.resize(src_seg, (ww, hh))[:h_seg, :w_seg]

gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)
results = pytesseract.image_to_data(thresh)

for b in map(str.split, results.splitlines()[1:]):
    if len(b) == 12:
        x, y, w, h = map(int, b[6: 10])
        cv2.putText(img, b[11], (x, y + h + 15), cv2.FONT_HERSHEY_COMPLEX, 0.6, 0)

cv2.imshow("Result", img)
cv2.waitKey(0)

The Output

Here is the input image:

enter image description here

And here is the output image:

enter image description here

As you have said in your post, the only part you need the the decimal 1227.938. If you want to filter out the rest of the detected text, you can try tweaking some parameters. For example, replacing the 180 from _, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY) with 230 will result in the output image:

enter image description here

The Explanation

  1. Import the necessary libraries:
import cv2
import numpy as np
import pytesseract
  1. Define a function, process(), that will take in an image array, and return a binary image array that is the processed version of the image that will allow proper contour detection:
def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(img_gray, 200, 255, cv2.THRESH_BINARY)
    img_canny = cv2.Canny(thresh, 100, 100)
    kernel = np.ones((3, 3))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=2)
    return cv2.erode(img_dilate, kernel, iterations=2)
  1. I'm sure that you don't have to do this, but due to a problem in my environment, I have to add pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' before I can call the pytesseract.image_to_data() method, or it throws an error:
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
  1. Read in the original image, make a copy of it, and define the rough height of the large part of the decimal:
img = cv2.imread("image.png")
img_copy = img.copy()
hh = 50
  1. Detect the contours of the processed version of the image, and add a filter that roughly filters out the contours so that the small text remains:
contours, _ = cv2.findContours(process(img), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
    if 20 * hh < cv2.contourArea(cnt) < 30 * hh:
  1. Define the bounding box of each contour that didn't get filtered out, and use the properties to enlarge those parts of the image to the height defined for the large text (making sure to also scale the width accordingly):
        x, y, w, h = cv2.boundingRect(cnt)
        ww = int(hh / h * w)
        src_seg = img[y: y + h, x: x + w]
        dst_seg = img_copy[y: y + hh, x: x + ww]
        h_seg, w_seg = dst_seg.shape[:2]
        dst_seg[:] = cv2.resize(src_seg, (ww, hh))[:h_seg, :w_seg]
  1. Finally, we can use the pytesseract.image_to_data() method to detect the text. Of course, we'll need to threshold the image again:
gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)
results = pytesseract.image_to_data(thresh)

for b in map(str.split, results.splitlines()[1:]):
    if len(b) == 12:
        x, y, w, h = map(int, b[6: 10])
        cv2.putText(img, b[11], (x, y + h + 15), cv2.FONT_HERSHEY_COMPLEX, 0.6, 0)

cv2.imshow("Result", img)
cv2.waitKey(0)

Upvotes: 2

Ahx
Ahx

Reputation: 7985

I would like to recommend applying another image processing method.

Because I am dealing with a dark background, I first invert the image, before converting it to grayscale and thresholding it:

You applied global thresholding and couldn't achieve the desired result.

Then you can apply either adaptive-thresholding or inRange

For the given image, if we apply the inRange threshold:

enter image description here

To be able to recognize the image as accurately as possible we can add a border to the top of the image and resize the image (Optional)

enter image description here

In the OCR section, check if the detected region contains a digit

if text.isdigit():

Then display on the image:

enter image description here

The result is nearly the desired value. Now you can try with the other suggested methods to find the exact value.

The problem is .938 recognized as 235, maybe resizing using different values might improve the result.

Code:

from cv2 import imread, cvtColor, COLOR_BGR2HSV as HSV, inRange, getStructuringElement, resize
from cv2 import imshow, waitKey, MORPH_RECT, dilate, bitwise_and, rectangle, putText
from cv2 import copyMakeBorder as addBorder, BORDER_CONSTANT as CONSTANT, FONT_HERSHEY_SIMPLEX
from numpy import array
from pytesseract import image_to_data, Output

bgr = imread("Iwzrg.png")
resized = resize(bgr, (800, 600), fx=0.75, fy=0.75)
bordered = addBorder(resized, 200, 0, 0, 0, CONSTANT, value=0)
hsv = cvtColor(bordered, HSV)
mask = inRange(hsv, array([0, 0, 250]), array([179, 255, 255]))
kernel = getStructuringElement(MORPH_RECT, (50, 30))
dilated = dilate(mask, kernel, iterations=1)
thresh = 255 - bitwise_and(dilated, mask)

data = image_to_data(thresh, output_type=Output.DICT)

for i in range(0, len(data["text"])):
    x = data["left"][i]
    y = data["top"][i]
    w = data["width"][i]
    h = data["height"][i]
    text = data["text"][i]

    if text.isdigit():
        print("Text: {}".format(text))
        print("")
        text = "".join([c if ord(c) < 128 else "" for c in text]).strip()
        rectangle(thresh, (x, y), (x + w, y + h), (0, 255, 0), 2)
        putText(thresh, text, (x, y - 10), FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3)
        imshow("", thresh)
        waitKey(0)

Upvotes: 1

Esraa Abdelmaksoud
Esraa Abdelmaksoud

Reputation: 1679

I have been working with Tesseract for quite some time, so let me clarify something for you. Tesseract is extremely helpful if you're trying to recognize text in documents more than any other computer vision projects. It usually needs a binarized image to get a good output. Therefore, you will always need some image pre-processing.

However, after several trials in the past with all page segmentation modes, I realized that it fails when font size differs on the same line without having a space. Sometimes PSM 6 is helpful if the difference is low, but in your condition, you may try an alternative. If you don't care about the decimals, you may try the following solution:

img = cv2.imread(r'E:\Downloads\Iwzrg.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(gray, (3,3),0)
_,thresh = cv2.threshold(img_blur,200,255,cv2.THRESH_BINARY_INV)

# If using a fixed camera
new_img = thresh[0:100, 80:320]

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

OUTPUT: 1227

enter image description here

enter image description here

Upvotes: 1

Related Questions