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
Images, that it CANNOT read
My current code is:
JavaScript
x
4
1
tesstr = pytesseract.image_to_string(
2
cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY),
3
config="--psm 7")
4
Advertisement
Answer
This little function (below) will do for any color
ec9Ut.png
Thresh result
x18MN.png
Thresh result
SFr48.png
Thresh result
JavaScript
1
20
20
1
import cv2
2
from pytesseract import image_to_string
3
4
def getText(filename):
5
img = cv2.imread(filename)
6
HSV_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
7
h,s,v = cv2.split(HSV_img)
8
thresh = cv2.threshold(v, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
9
txt = image_to_string(thresh, config="--psm 6 digits")
10
return txt
11
12
13
text = getText('ec9Ut.png')
14
print(text)
15
text = getText('x18MN.png')
16
print(text)
17
text = getText('SFr48.png')
18
print(text)
19
20
Output
JavaScript
1
4
1
46
2
31
3
53
4