Sign in to follow this  
Followers 0
Phenom

How to make OCR work in this code

9 posts in this topic

#1 ·  Posted (edited)

This is some AutoIt code that I found.

Local $sArray[1], $oWord

    Local $miDoc = ObjCreate("MODI.Document")
    ConsoleWrite("creating image" & @CRLF)
    $miDoc.Create($Image)
    If @error Then
        ConsoleWrite("failed to create image" & @CRLF)
        Return SetError(1)
    EndIf
    ConsoleWrite("image successfully created" & @CRLF)
    $miDoc.Ocr(9, True, False)

On the last line, I get the following error:

The requested action with this object has failed.:

$miDoc.Ocr(9, True, False)

$miDoc.Ocr(9, True, False)^ ERROR

Edited by Phenom

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello Phenom,

Without seeing the image your trying to process, it's difficult to give you a direct answer. It could be that your not assigning $Image. I don't see anything that does a screen capture, nor do I see anything that would direct $Image to an actual document. If you have assigned a document to $Image, and forgot to show in your example, then it could be a format not supported by MODI. Or the Image could be too small. I have noticed that if attempting to capture a word or two in a small image where the borders are very close to the words, then I would receive the same error as you had.

Realm

Edit: Also to Use these com objects, you must have MODI installed, which comes with Microsoft Office 2003 and up.

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I already tried Tesseract as well as MODI, and can't read the image. I also tried resizing the image.

This is the image.

Posted Image

Edited by Phenom

Share this post


Link to post
Share on other sites

I already tried Tesseract as well as MODI, and can't read the image. I also tried resizing the image.

This is the image.

Posted Image

MODI will have a difficult time reading that image. The characters are too close to the border, try capturing a larger sample. get a sample that is about 15-20 pixels more to the left, the top, and the bottom, and see if that works for you. Don't know why, but MODI needs room to Read.

In example, if your current image is at 30,30,100,50....Then try capturing 15,15,100,65

If that don't work then there is another form of OCR that another member pointed out, however it does not read characters as cleanly as MODI, but with a little work you may be able to train it, the original topic is here:

#794560


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

MODI will have a difficult time reading that image. The characters are too close to the border, try capturing a larger sample. get a sample that is about 15-20 pixels more to the left, the top, and the bottom, and see if that works for you. Don't know why, but MODI needs room to Read.

In example, if your current image is at 30,30,100,50....Then try capturing 15,15,100,65

If that don't work then there is another form of OCR that another member pointed out, however it does not read characters as cleanly as MODI, but with a little work you may be able to train it, the original topic is here:

#794560

When I try that method in the other post I see in the console this error message:

NOT NORMAL, thresholdValue = 160

# thresholdValue out of range 0..0, reset to 0

# no boxes found - stopped

What does that mean?

Also, I expanded the image and put some space around it. Would this work?

Posted Image

Edited by Phenom

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

When I try that method in the other post I see in the console this error message:

What does that mean?

I never ran across that error, but at the same time, It was not reading characters correctly enough for me, so I did'nt use it for very long, I ended up with MODI, and received the best results from there so far.

Also, I expanded the image and put some space around it. Would this work?

Have you tried it? If so, I am curious to your result, I never tried that before.

What I originally meant, was not to make the image larger but rather capture a larger area around the image. When the characters are close to the image borders is when I find difficulties getting MODI to work. what I meant was to grab some white space, above, below and to the left of that particular image to get the characters, '$2', away from the image's borders to increase readability for MODI.

Edit: Actually I took a closer look at your image, and it appears you did grab some whitespace. That should provide a more readable image for MODI. Have you tried to read it?

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

I never ran across that error, but at the same time, It was not reading characters correctly enough for me, so I did'nt use it for very long, I ended up with MODI, and received the best results from there so far.

Have you tried it? If so, I am curious to your result, I never tried that before.

What I originally meant, was not to make the image larger but rather capture a larger area around the image. When the characters are close to the image borders is when I find difficulties getting MODI to work. what I meant was to grab some white space, above, below and to the left of that particular image to get the characters, '$2', away from the image's borders to increase readability for MODI.

Edit: Actually I took a closer look at your image, and it appears you did grab some whitespace. That should provide a more readable image for MODI. Have you tried to read it?

I didn't grab whitespace. I added whitespace using imagemagick. Still can't read it.

It only works if everything is white except the text I want to read. Is there a way to change the image so that everything except the text is white?

Share this post


Link to post
Share on other sites

Try this:

#include <GDIPlus.au3>
Global $sImage = @ScriptDir & '\IrD9C.png'      'Path to the image
$hGui = GUICreate('', 500, 500)

GUISetState()
_GDIPlus_Startup()

$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBitmap = _GDIPlus_BitmapCreateFromFile($sImage)
_GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 20, 20)


$tGDIPBITMAPDATA = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, _GDIPlus_ImageGetWidth($hBitmap), _
_GDIPlus_ImageGetHeight($hBitmap), $GDIP_ILMWRITE, $GDIP_PXF32RGB )

$iWidth = DllStructGetData($tGDIPBITMAPDATA, 'Width')
$iHeight = DllStructGetData($tGDIPBITMAPDATA, 'Height')
$iStride = DllStructGetData($tGDIPBITMAPDATA, 'Stride')
$iScan0 = DllStructGetData($tGDIPBITMAPDATA, 'Scan0')


For $x = 0 To $iWidth-1
    For $y = 0 To $iHeight-1

        $tPixel = DllStructCreate('byte[3]', $iScan0 + ($x * 4) + ($y * $iStride))
        $bPixel = DllStructGetData($tPixel, 1)

        $iRed = BitAND(BitShift($bPixel, 16), 0xFF)
        $iGreen = BitAND(BitShift($bPixel, 8), 0xFF)
        $iBlue = BitAND($bPixel, 0xFF)

        If (($iRed + $iGreen + $iBlue) / 3)  > 100 Then

            DllStructSetData($tPixel, 1, 0xFFFFFF)
        Else
            DllStructSetData($tPixel, 1, 0x000000)
        EndIf
    Next
Next

_GDIPlus_BitmapUnlockBits($hBitmap, $tGDIPBITMAPDATA)
_GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 20, 20 + $iHeight + 10)

While 1
    $iMsg = GUIGetMsg()
    If $iMsg = -3 Then
        _Free()
    EndIf
WEnd

Func _Free()
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

I didn't grab whitespace. I added whitespace using imagemagick. Still can't read it.

I would try to grab a larger parameter around the text, instead of tampering with the image itself.

Is there a way to change the image so that everything except the text is white?

That all depends on what you are attempting to grab the image from. If it's an application installed on your pc, you could probably interchange the graphics behind it with an all white graphic. Which would be found in your Applictions Files

If it's from a website, I am not sure for I have never attempted to play with the displayed HTML grahpics before. If I am correct, there is a way.

UDF Library 'IE.au3', I believe there are couple functions that you could download the source, then change the background graphic before it is displayed in your window.

But again, When I encountered a similar problem with an application installed on my pc, I found that changing the background graphic to all white, did not help any, and I have used MODI to read from all kinds of colorful backgrounds, a light grey should be one of the easiest compared to red and blue, which I have used. MODI tears the image apart pixel by pixel, and tries to recognize a character that matches the pixel map. So I am guessing that being so close to a border would give some irregularities at the associated regions of each character. so tampering with an image may distort the image as well. I would just try capturing a larger region even if it cuts part into other characters, at least MODI will have a better chance at reading your target. I have found that small images such as your example, is where I always have problems with. Again capturing a larger region, in most cases, provided a better result.


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0