Sign in to follow this  
Followers 0

Real OCR in AU3 - in a few lines.

141 posts in this topic

#1 ·  Posted (edited)

Real OCR in AU3

In the support forum I saw serveral posts about OCR.

And this has been discussed over and over again in a rather complex way.

This gives you OCR in no time and no effort.

MODI automation provides powerful document management and OCR features; however, it does not automate the document scanning process itself or support image annotation.

; http://msdn2.microsoft.com/en-us/library/aa202819(office.11).aspx
#include <GUIConstants.au3>
#include <Array.au3>

Dim $miDoc, $Doc
Dim $str
Dim $oWord
Dim $sArray[500]

Const $miLANG_CZECH = 5
Const $miLANG_DANISH = 6
Const $miLANG_DUTCH = 19
Const $miLANG_ENGLISH = 9
Const $miLANG_FINNISH = 11
Const $miLANG_FRENCH = 12
Const $miLANG_GERMAN = 7
Const $miLANG_GREEK = 8
Const $miLANG_HUNGARIAN = 14
Const $miLANG_ITALIAN = 16
Const $miLANG_JAPANESE = 17
Const $miLANG_KOREAN = 18
Const $miLANG_NORWEGIAN = 20
Const $miLANG_POLISH = 21
Const $miLANG_PORTUGUESE = 22
Const $miLANG_RUSSIAN = 25
Const $miLANG_SPANISH = 10
Const $miLANG_SWEDISH = 29
Const $miLANG_TURKISH = 31
Const $miLANG_SYSDEFAULT = 2048
Const $miLANG_CHINESE_SIMPLIFIED = 2052
Const $miLANG_CHINESE_TRADITIONAL = 1028

; Initialize error handler
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$miDoc = ObjCreate("MODI.Document")
$miDocView = ObjCreate("MiDocViewer.MiDocView")

$Viewer = GUICreate ( "Embedded MODI Viewer", 640, 580,(@DesktopWidth-640)/2, (@DesktopHeight-580)/2 , _
                    $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)


;Creates an ActiveX Control in the GUI.
$GUIActiveX = GUICtrlCreateObj ($miDocView, -1, -1, 640, 580)
GUICtrlSetResizing ($Viewer, $GUI_DOCKAUTO)


$miDoc.Create("C:_AppsAutoIT3COMMODIdeclaration.tif")
$miDoc.Ocr($miLANG_ENGLISH, True, False)

; Show GUI
GUISetState ()

$MiDocView.Document = $miDoc
$MiDocView.SetScale (0.75, 0.75)


While 1
$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend



$i = 0

For $oWord in $miDoc.Images(0).Layout.Words

    $str = $str & $oWord.text & @CrLf
        ConsoleWrite($oWord.text & @CRLF)
    $sArray [$i] = $oWord.text
    $i += 1
Next

_ArrayDisplay($sArray,"OCR Result")



;MsgBox(0,"",$miDocView.FileName)

;------------------------------ This is a COM Error handler --------------------------------
Func MyErrFunc()
$HexNumber=hex($oMyError.number,8)
Msgbox(0,"COM Error Test","We intercepted a COM Error !" & @CRLF & @CRLF & _
             "err.description is: " & @TAB & $oMyError.description & @CRLF & _
             "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: " & @TAB & $HexNumber & @CRLF & _
             "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
             "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
             "err.source is: " & @TAB & $oMyError.source & @CRLF & _
             "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
             "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
            )
SetError(1) ; to check for after this function returns
Endfunc

Forgot to tell that Office 2003 is needed.

PS : Added all available languages.

Enjoy !!

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites



#4 ·  Posted

Awesome!! Works with Office 2007 also. My hopes one day are to have an OCR Setup in autoit tht we can use the rubberband method and copy text from anythingon screen... Great Job ptrex! Cannot wait to see where this goes.

Share this post


Link to post
Share on other sites

#5 ·  Posted

Very nice find! I will find a great use for this in the near future :)


www.itoady.com (Go here to download the MacroGamer installer)[list][*]A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding[*]MacroGamer - (Please Like this AutoIt thread on Facebook)[/list]

Share this post


Link to post
Share on other sites

#7 ·  Posted

Does not work with Office 97.


"I've seen your work in the past, and it's novice at best..." SmOke_N

Share this post


Link to post
Share on other sites

#8 ·  Posted

Works with Office 2000. Wow, that's really cool!


[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Share this post


Link to post
Share on other sites

#9 ·  Posted

What exactly is OCR?

Share this post


Link to post
Share on other sites

#10 ·  Posted


[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Thanks!

But not workin for me :P - i getting many COM errors - my Office 2003 is on russian, maby i need to change something? like $miLANG_ENGLISH = ? (what the value for russian?).

And the link in the top of example is not good :)

Edited by MsCreatoR

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#12 ·  Posted

I have Office 2003 installed but dont have that 'MODI.Document' object.

any idea why?


CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Works with Office 2000. Wow, that's really cool!

Are you sure? In wikipedia it says it was introduced in Office XP, and my 2000 Pro certainly doesn't have this. I've never bothered with any service packs though. Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#14 ·  Posted

Are you sure? In wikipedia it says it was introduced in Office XP, and my 2000 Pro certainly doesn't have this. I've never bothered with any service packs though.

I have Office 2000 with SP2 and it works fine.

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

i have 2002 with sp3 and it is not working. I get this error.

C:\Documents and Settings\*******\Desktop\New AutoIt v3 Script.au3 (28) : ==> Object referenced outside a "With" statement.:

For $oWord in $miDoc.Images(0).Layout.Words

For $oWord in $miDoc.Images(0)^ ERROR

I think i might be doing something wrong but im not sure.

Edited by qazwsx

Share this post


Link to post
Share on other sites

#16 ·  Posted

i have 2002 with sp3 and it is not working. I get this error.

C:\Documents and Settings\Steven Pecht\Desktop\New AutoIt v3 Script.au3 (28) : ==> Object referenced outside a "With" statement.:

For $oWord in $miDoc.Images(0).Layout.Words

For $oWord in $miDoc.Images(0)^ ERROR

I think i might be doing something wrong but im not sure.

Exact same thing for me too. :)


www.itoady.com (Go here to download the MacroGamer installer)[list][*]A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding[*]MacroGamer - (Please Like this AutoIt thread on Facebook)[/list]

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

@All

Good to see that it is working in a variaty of MS Office versions !!

Some have still dificulty as I see.

The ones that have can do a little check : Open the registry and check if the MODI component is properly registered.

Go to -> HK_Classes_Root and check if you find an entry for "MODI.Document"

Also check of this exists on your machine : C:\Program Files\Common Files\Microsoft Shared\MODI

If not the COM component is not registered on your system.

Try repairing MS Office or install all components of Office and test again.

@MsCreatoR

Regarding the languages. I will add RUSSIAN too.

See first post.

Regards,

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

#18 ·  Posted

ptrex

Hi, thanks for the languages additions...

Go to -> HK_Classes_Root and check if you find an entry for "MODI.Document"

I have there some values.

Also check of this exists on your machine : C:\Program Files\Common Files\Microsoft Shared\MODI

It's exists (with many stuff in there).

But it still not working (even with my language) :) .

Try repairing MS Office or install all components of Office and test again.

I will try that later, thanks.

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#19 ·  Posted

@MsCreatoR

Just to make sure, In order to have this example work you need to have a TIF file ready in the path specified in the Script :

"C:\Temp\Numbers.tif "

If not it will give errors.

This might be confusing looking at the CREATE statement in the script ?

It does not CREATE anything but OPENS it.

Regards,

ptrex

Share this post


Link to post
Share on other sites

#20 ·  Posted

It does not CREATE anything but OPENS it.

Oh, now i see, thanks... but what do i write to this file? :)

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

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