skeeter
skeeter

Reputation: 39

what is the best way to recognize embossed text with Tesseract OCR?

I am trying to read the text from a U.S. penny to orient the coin.

the original is from https://www.usmint.gov/wordpress/wp-content/uploads/2024/05/2024-lincoln-penny-uncirculated-obverse-philadelphia.jpg

I have extracted the word liberty, and am having difficulty with tesseract.

liberty.jpg

the code I am using is

def GetLibertyCroppedArea(img):
    # ll = 10,242
    # ur = 130,190
    top = 190
    bot = 240
    lft = 10
    rgt = 130
    cropped_area = img[top:bot, lft:rgt]
    cropped_area = cv2.resize(cropped_area, (360,150))
return cropped_area

def LoadImage(fn):
    img = cv2.imread(fn)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.resize(img, (400,400))
    return img

img = LoadImage(fn)
liberty = GetLibertyCroppedArea(rotated_image)
text = pytesseract.image_to_string(liberty, config='--psm 8')
text = text.rstrip()
print(text)

I have tried various psm values without success

what preprocessing steps are necessary for tesseract to have a better chance of recognition ?

tesseract was installed from https://github.com/UB-Mannheim/tesseract/releases/download/v5.4.0.20240606/tesseract-ocr-w64-setup-5.4.0.20240606.exe

this is the actual image I am passing to tesseract image_passed_to_tesseract.jpg

pytesseract.image_to_string(liberty, config='--psm 8') returns "LOOGATY,"

Upvotes: 2

Views: 109

Answers (1)

Lisan Al Gaib
Lisan Al Gaib

Reputation: 419

The issue is not in the code, but the tessdata file. As core of tesseract is in the tessdata, I would suggest you should train your own tessdata with the respective font on your input and use it.

You can refer use this tessdata file: tessdata

Rename it as eng.traineddata amd replace that with the default one in tessdata folder

with your code and the attached file, I got the below output,

LIBERTY

NOTE: If you are using windows, then tessdata would be at C:\Program Files (x86)\Tesseract OCR\tessdata or C:\Program Files\Tesseract OCR\tessdata depending upon your installation. replace the default eng.traineddata with the file mentioned the link mrz.traineddata. The rename mrz.traineddata to eng.traineddata

After using the code provided in question with the traineddata mentioned in link(follow the rename steps in NOTES), it is working fine.

import pytesseract
import cv2

tesseract_path=r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
pytesseract.pytesseract.tesseract_cmd = tesseract_path


def GetLibertyCroppedArea(img):
    # ll = 10,242
    # ur = 130,190
    top = 190
    bot = 240
    lft = 10
    rgt = 130
    cropped_area = img[top:bot, lft:rgt]
    cropped_area = cv2.resize(cropped_area, (360,150))

    return cropped_area

def LoadImage(fn):
    img = cv2.imread(fn)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img = cv2.resize(img, (400,400))
    return img

img = LoadImage('2024-lincoln-penny-uncirculated-obverse-philadelphia.jpg')
liberty = GetLibertyCroppedArea(img)
text = pytesseract.image_to_string(liberty, config='--psm 8')
text = text.rstrip()
print(text)

OUTPUT:

LIBERTY

Upvotes: 1

Related Questions