Jump to content

Problem with my OCR


NS5
 Share

Recommended Posts

My System:

MS Office 2007

WINDOWS XP SP2

AUTOIT V3 Latest version updated last Dec. 24 2008.

Hi everyone, I'm having problems with my code when it sits and wait for 5 minutes or less once the code is activated. Auto IT crashes randomly on the specified time period. Can anyone help me out on this...

CODE

;Code Starts here

#Include <Screencapture.au3>

Global $error = ObjEvent("AutoIt.Error","Error")

HotKeySet("z", "_Do") ;Press to activate code

HotKeySet("x", "_Exit");Press to exit code.

Func _Do()

Do

Capture4()

$i = 0

Sleep(1000)

Until $i= 1

EndFunc

;

Func Capture4()

Local $Image = @TempDir&"\ScreenCapture.tif", $v = 0

_ScreenCapture_Capture($Image, 13, 419, 257, 454, True)

_ImageResize($Image, $Image, 150, 100)

$OCR = OCR($Image)

If @error then Return

ClipPut($OCR)

EndFunc

;

Func OCR($Image)

Local $y = 0, $string

$miDoc = ObjCreate("MODI.Document")

$miDoc.Create($Image)

If @error Then Return SetError(1)

$miDoc.Ocr(9, True, True)

If @error Then

Return SetError(1)

EndIf

For $Word In $miDoc.Images(0).Layout.Words

$delim = ' '

If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y+1).Rects(0).Top Then $delim = @CRLF

$string &= $Word.text & $delim

$y += 1

Next

Return StringTrimRight($string,1)

EndFunc

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)

Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0

Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"

If Not FileExists($sInImage) Then Return SetError(1, 0, 0)

$sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))

If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)

$sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

If Not FileExists($sOP) Then Return SetError(3, 0, 0)

$sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

$Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))

If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)

_GDIPlus_Startup()

$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

$ImageHeight = (_GDIPlus_ImageGetHeight($hImage2)) * 1.25

$ImageWidth = (_GDIPlus_ImageGetWidth($hImage2)) * 1.25

$hBitmap = _WinAPI_CreateBitmap($ImageWidth, $ImageHeight, 1, 32)

$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

$ImageHeight = _GDIPlus_ImageGetHeight($hImage2)

$ImageWidth = _GDIPlus_ImageGetWidth($hImage2)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $ImageWidth * 1.25, $ImageHeight * 1.25)

$CLSID = _GDIPlus_EncodersGetCLSID($Ext)

$sOutImage = @TempDir & '\OCR_ImageResize.jpg'

FileDelete($sOutImage)

_GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

_GDIPlus_ImageDispose($hImage1)

_GDIPlus_ImageDispose($hImage2)

_GDIPlus_GraphicsDispose($hGraphic)

_WinAPI_DeleteObject($hBitmap)

_GDIPlus_Shutdown()

Return SetError(0, 0, 1)

EndFunc ;==>_ImageResize

;

Func Error()

Endfunc

While 1

Sleep(1500)

WEnd

Func _Exit()

Exit

EndFunc

;End of Code

Edited by NS5
Link to comment
Share on other sites

;Code Starts here
#Include <Screencapture.au3>

Global $error = ObjEvent("AutoIt.Error","Error")
HotKeySet("z", "_Do") ;Press to activate code
HotKeySet("x", "_Exit");Press to exit code.


Func _Do()

Do
Capture4()
$i = 0
Sleep(1000)
Until $i= 1

EndFunc

;
Func Capture4()

Local $Image = @TempDir&"\ScreenCapture.tif", $v = 0
_ScreenCapture_Capture($Image, 13, 419, 257, 454, True)
_ImageResize($Image, $Image, 150, 100)
$OCR = OCR($Image)
If @error then Return
ClipPut($OCR)
EndFunc

;
Func OCR($Image)

Local $y = 0, $string
$miDoc = ObjCreate("MODI.Document")
$miDoc.Create($Image)
If @error Then Return SetError(1)
$miDoc.Ocr(9, True, True)
If @error Then
Return SetError(1)
EndIf

For $Word In $miDoc.Images(0).Layout.Words
$delim = ' '
If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y+1).Rects(0).Top Then $delim = @CRLF
$string &= $Word.text & $delim
$y += 1
Next
Return StringTrimRight($string,1)
EndFunc


Func _ImageResize($sInImage, $sOutImage, $iW, $iH)

Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"
If Not FileExists($sInImage) Then Return SetError(1, 0, 0)
$sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))
If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)
$sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
If Not FileExists($sOP) Then Return SetError(3, 0, 0)
$sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
$Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))
If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)
If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)
_GDIPlus_Startup()
$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)
$ImageHeight = (_GDIPlus_ImageGetHeight($hImage2)) * 1.25
$ImageWidth = (_GDIPlus_ImageGetWidth($hImage2)) * 1.25
$hBitmap = _WinAPI_CreateBitmap($ImageWidth, $ImageHeight, 1, 32)
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
$ImageHeight = _GDIPlus_ImageGetHeight($hImage2)
$ImageWidth = _GDIPlus_ImageGetWidth($hImage2)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $ImageWidth * 1.25, $ImageHeight * 1.25)
$CLSID = _GDIPlus_EncodersGetCLSID($Ext)
$sOutImage = @TempDir & '\OCR_ImageResize.jpg'
FileDelete($sOutImage)
_GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_GraphicsDispose($hGraphic)
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_Shutdown()
Return SetError(0, 0, 1)

EndFunc ;==>_ImageResize

;
Func Error()
Endfunc


While 1
Sleep(1500)
WEnd

Func _Exit()
Exit
EndFunc
;End of Code

for much more eligibility

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...