I am using pytesseract to read text from an image..
I am facing the following challenges:
- It is not extracting all the text.
- All the text read is outputting u'\x0c'
- It is not forming proper text
image_to_data was able to convert a few but most of it was not right. I am attaching the code below
fn = 'testimgnum.png'
image = cv2.imread(fn)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
'''
gray = cv2.Canny(gray, 100, 200)
gray = cv2.dilate(gray, None, iterations=1)
gray = cv2.erode(gray, None, iterations=1)
'''
cv2.imwrite('nt1.png', gray)
cnts = cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts)
orig = gray.copy()
arr_values = []
ctr = 0
#filter the boxes
custom_config = r'--oem 3 --psm 10000'
k = 0
for c in cnts:
k=k+1
#print("c:",c)
try:
text_val=''
box = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
box = np.array(box, dtype="int")
tl, tr, br, bl = box
#print(box)
width = abs(br[0]-tl[0])
height = abs(tl[1]-br[1])
x1= min(tl[0],br[0])
x2= max(tl[0],br[0])
y1=max(tl[1],br[1])
y2 = min(tl[1],br[1])
print(x1,x2,y1,y2, height, width)
crop_img = gray[y2-6:y1+6, x1-4:x2+4]
cv2.imwrite('{0}.png'.format(k), crop_img)
cv2.imshow("Image", crop_img)
cv2.waitKey(0)
#crop_img1 = cv2.resize(crop_img, None, fx=6, fy=6, interpolation=cv2.INTER_CUBIC)
text_val = pytesseract.image_to_string(crop_img, lang='eng',config='--oem 3 -c tessedit_char_whitelist=0123456789')#, config=custom_config,
print("text_val:", text_val)
arr_values.append([x1,x2,y1,y2,width,height,text_val])
except Exception as e:
print("e:", e)
ctr+=1
pass
I am using tesseract 4 and python 2.7. I had a working model for a similar problem. I was using an older version of tesseract.