Help with detecting a code on an image

102 views
Skip to first unread message

nabil-ak

unread,
Oct 31, 2022, 3:47:10 AM10/31/22
to tesseract-ocr
Hello,

I want to detect a code (combination of characters) on an image in python.

I already tried EasyOCR and Tesseract Open Source OCR Engine but noone could detect the characters.

I also tried to preprocess the picture by inverting the white font to a black font and painting the background white to make it for the engine easier to detect the characters. 

What am i doing wrong?


voucher.png
changed.png

Umanda Dikwatta

unread,
Oct 31, 2022, 5:46:59 AM10/31/22
to tesser...@googlegroups.com
Did you try all the preprocessing steps? 

Rotation / Deskewing ? I think Tesseract finds it difficult to identify skewed images.


--
You received this message because you are subscribed to the Google Groups "tesseract-ocr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tesseract-oc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tesseract-ocr/2e764275-4992-495f-b0ac-ffe668254231n%40googlegroups.com.

nabil-ak

unread,
Oct 31, 2022, 8:55:56 AM10/31/22
to tesseract-ocr
I also tried Rotation but its still not working.

These are the preprocessing steps that i used:

import cv2
import pytesseract
import numpy as np
from scipy import ndimage

img = cv2.imread('voucher.png')

img = cv2.bitwise_not(img)

img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)

img = ndimage.rotate(img, -20)

cv2.imwrite("changed.png",img)

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
print(pytesseract.image_to_string(img))


changed.png

nabil-ak

unread,
Oct 31, 2022, 2:01:36 PM10/31/22
to tesseract-ocr
https://imagetotext.info/

If i use a website like this one it can extract the text perfectly and they also use tesseract.
There has to be some preproccesing/setting that makes tesseract detect the text perfectly.

kwmz...@gmail.com

unread,
Oct 31, 2022, 8:46:29 PM10/31/22
to tesseract-ocr
So I think you could also try using some morphological transformation; I feel like dilation could help a lot too 

Nikhil Fande

unread,
Oct 31, 2022, 11:59:55 PM10/31/22
to tesser...@googlegroups.com
Hi,

Combination of image deskewing and image inversion should work.

Use PSM 11 on processed image, it will detect most of the characters.

Thanks,
Nikhil

nabil-ak

unread,
Nov 1, 2022, 4:05:48 PM11/1/22
to tesseract-ocr
Thanks for all the help.
I tried closing as a morphological transformation and also used a threshold to get a black and white picture.

I got "BYC3 .- A4UFYY.QLBGY -+ " as a answer this is almost right.

Is there anything else that i can do to improve the detection?
i attached the python code that i used and the picture after the preprocessing.


import cv2
import pytesseract
import numpy as np
from scipy import ndimage

img = cv2.imread('voucher.png')



img = cv2.bitwise_not(img)

img = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

_,img = cv2.threshold(img,145,255,cv2.THRESH_BINARY)

kernel = np.ones((2, 2), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)

img = ndimage.rotate(img, -20)

cv2.imwrite("changed.png",img)

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
print(pytesseract.image_to_string(img))
changed.png
Reply all
Reply to author
Forward
0 new messages