Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

python - Pytesseract read coloured text

I am trying to read coloured (red and orange) text with Pytesseract. I tried to not grayscale the image, but that didn't work either.

Images, that it CAN read IMG 1 IMG 2 IMG 3

Images, that it CANNOT read IMG 1 IMG 2

My current code is:

        tesstr = pytesseract.image_to_string(
                    cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY),
                    config="--psm 7")
question from:https://stackoverflow.com/questions/65881472/pytesseract-read-coloured-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This little function (below) will do for any color

ec9Ut.png

enter image description here

Thresh result

enter image description here

x18MN.png

enter image description here

Thresh result

enter image description here

SFr48.png

enter image description here

Thresh result

enter image description here

import cv2
from pytesseract import image_to_string

def getText(filename):
    img = cv2.imread(filename)
    HSV_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    h,s,v = cv2.split(HSV_img)
    thresh = cv2.threshold(v, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    txt = image_to_string(thresh, config="--psm 6 digits")
    return txt
    

text = getText('ec9Ut.png')
print(text)
text = getText('x18MN.png')
print(text)
text = getText('SFr48.png')
print(text)

Output

46
31
53

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...