Ocr not always working

Asked by matteoa on 2018-11-12

since I have to use Sikulix on a 32bit pc I'm stuck with version 1.1.3.
I have a bar in a web app where I have to read with ocr two items, so the font, the background the size etc are the same.
What I do in the script is to select the two regions and then do the OCr one after the other(but changing the order doesn't affect the (bad) results.
The bar I' working on is something like (the "yes" is actually what I have to Ocr):
Everything is ok? Yes
The OCR of the left phrase works perfectly, while in the right part it read nothing (instead of "Yes").
I tried with this single line script(question 674899):
print selectRegion().text()
And the results are consistent, the ocr on the right of the bar is always found with no problem, while in the left part found nothing.
The real images are available, if needed.
I tried also the advises in question 271593 but I do not manage to make it run in Sikulix IDE, with these lines:
img = capture(rValAV) # get the image from the screen (in memory)
img1 = Image.create(img) # create an with the new Image class (still in memory)
imgGrey = img1.convertImageToGrayscale(img1.get()) # does what it says (still in memory)
imgGreyResized = imgGrey.resize(3) # resize the image to about 300dpi (usually 3 as factor is sufficient) (still in memory)
text = imgGreyResized.text() # using the SikuliX builtin OCR implementation
print text
the error given is:
line 113, in <module> imgGreyResized = imgGrey.resize(3) # resize the image to about 300dpi (usually 3 as factor is sufficient) (still in memory) AttributeError: 'java.awt.image.BufferedImage' object has no attribute 'resize'
Since in the right part the Tesseract works perfectly I suppose there is something wrong with what I do.
I'm sorry if I did some beginner mistake...
Thanks for any help

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

imgGreyResized = imgGrey.resize(3)

must be

imgGreyResized = imgGrey.resize(imgGrey, 3)

Since both methods are static methods of class Image, correct usage would be:

imgGrey = Image.convertImageToGrayscale(img1.get())
imgGreyResized = Image.resize(imgGrey, 3)

or short:

img1 = Image.create(capture(rValAV))
imgGreyResized = Image.resize(Image.convertImageToGrayscale(img1.get()), 3)

matteoa (matteoa) said : #2

Thanks RaiMan,
I've tried this, merging together the two above mentioned answers :
img1 = Image.create(capture(selectRegion()))
imgGrey = Image.convertImageToGrayscale(img1.get())
imgGreyResized = Image.resize(imgGrey, 3)
print imgGreyResized().text()
But I have had this error:
[error] Error caused by: Traceback (most recent call last): File "C:\Projects\DataPurchase\REACT\SikuliX\Test_h20\interactTextBetter.sikuli\interactTextBetter.py", line 3, in <module> imgGreyResized = Image.resize(imgGrey, 3) TypeError: resize(): self arg can't be coerced to org.sikuli.script.Image

It is not clear to me if when you say that these are static methods you mean of Java or python:
if Java I've checked this:
and I did not find the image.resize method.
If Python I found here:
The resize method, but it says that there are two calls possible:
im.resize(size) => image
im.resize(size, filter) => image
In your 271593 response do you mean that what is needed is to do something like:
imgGreyResized = Image.resize(float(imgGrey[0])*3.0, float(imgGrey[1])*3.0))
Because I tried and have had this error:
line 4, in <module> imgGreyResized = Image.resize(imgGrey[0]*3, imgGrey[1]*3) TypeError: 'java.awt.image.BufferedImage' object is unsubscriptable
then I tried also this line:
imgGreyResized = Image.resize(imgGrey.getHeight()*3, imgGrey.getWidth()*3)
With this error:
line 5, in <module> imgGreyResized = Image.resize(imgGrey.getHeight()*3, imgGrey.getWidth()*3) TypeError: resize(): self arg can't be coerced to org.sikuli.script.Image
It is probably a stupid error from my side...thanks for any help!

RaiMan (raimund-hocke) said : #3

I am talking about the class

static means static in the Java class

... but there seems a confusion when using from the Jython level.

this works:
img1 = Image.create(capture(rValAV))
imgGreyResized = Image(Image(Image.convertImageToGrayscale(img1.get())).resize(3))

the resize() has to be applied to an Image instance.

matteoa (matteoa) said : #4

Thanks for clarification!
I managed to switch to Sikulix 1.1.4, using VNC and screenshot taken by sikulix 1.1.3 and moving the images on a more modern pc, but with not a lot of better results.
The complete testing script I'm using is this one:

img1 = Image.create(capture(selectRegion()))
imgGreyResized = Image(Image(Image.convertImageToGrayscale(img1.get())).resize(3))
print imgGreyResized.text().encode('utf-8')

But the OCR results are not so good, the "Yes" is found as "YE!" and "NO" as "IND", or something like, sometimes it also works.
I also tried with resize(5) or 8, but saw no differences.
I was thinking about binarizing the image to 2 colors only with nearest neighbour algorithm, any suggestion?
Tesseract does it internally, I think, but maybe I could use a more specific algorithm for web pages.
There is the possibility to add a vocabulary of words to be used in a particular situation to the tesseract engine?
I'm thinking about a command to tell to tesseract "here there are numbers", "here there could be only yes or no" and so on..
Thanks for your, as usual, great support

RaiMan (raimund-hocke) said : #5

With 1.1.4 the image is already internally resized to about 300 Dpi.
The resize factor is evaluated based on the Dpi of the current monitor.

So at least the resize is not necessary in your case with 1.1.4.

You should check, wether there are differences between using the original image (not gray-scaled) or the gray-scaled.

Helpful would be, to send me some of the offending image samples to sikulix---at---outlook---dot---com

Sorry, but I will only accept complaints about version 1.1.4 in the future.
Prior versions up to 1.1.3 I will not support any more with such internal implementation/feature problems.

matteoa (matteoa) said : #6

I fully understand that you cannot support old versions, no problem at all.
But, since now I'm using 1.1.4 with VNC connection to the old 32 bit pc...I make some more question...there are experiences of using syntax correction and/or specialized vocabulary with the ocr feature of Sikulix 1.1.4?
if so, which is the libraries tested?

RaiMan (raimund-hocke) said : #7

I only test with 1.1.4 - especially with the text features.

Can you help with this problem?

Provide an answer of your own, or ask matteoa for more information if necessary.

To post a message you must log in.