AutoitMike Posted September 18, 2022 Share Posted September 18, 2022 I found this OCR code a long time ago, it really work great , When there is text to capture. The problem is that when there is no text, or the text is too close to the defined boundary, it fails and the script just stops. The code line that fails is $miDoc.Ocr($miLANG_ENGLISH, True, False) The console says: ==> The requested action with this object has failed.: This returns an array of captured text expandcollapse popup#include <ScreenCapture.au3> #include <Array.au3> $Array=OCR_Region(TLCx,TLCy,BRCx,BRCy) ;Top Left Corner x, Top Left Cornr y, Bottom Right Corner x, Bottom right Corner y Func OCR_Region($x,$y,$width,$height) ;note $height is NOT the height, it is the Y coordinate bottom right corner, width is bottom right corner X. If 1=2 Then Sleep(500) EndIf $OCRstart=TimerInit() Dim $miDoc, $Doc Dim $str Dim $oWord Dim $sArray[200] Dim $oMyError Dim $HexNumber Dim $msg Dim $i ;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 Local $hBitmap1,$hImage1,$temp $temp=@ScriptDir & "\temp.bmp" _GDIPlus_Startup () ; Capture screen region $hBitmap1 = _ScreenCapture_Capture ("",$x, $y, $width, $height, False) $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap1) _GDIPlus_ImageSaveToFile ($hImage1, $temp) _GDIPlus_ImageDispose ($hImage1) _WinAPI_DeleteObject ($hBitmap1) $miDoc = ObjCreate("MODI.Document") $miDoc.Create($temp) ;MsgBox(0,"",$temp);this is the bitmap image which is "temp.bmp" $miDoc.Ocr($miLANG_ENGLISH, True, False) ;THIS LINE FAILS $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 FileDelete($temp) Return $sArray EndFunc I need to trap this error, not sure how. Thanks Link to comment Share on other sites More sharing options...
Developers Jos Posted September 18, 2022 Developers Share Posted September 18, 2022 Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Moderation Team SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Nine Posted September 18, 2022 Share Posted September 18, 2022 What kind of error are you trying to trap ? Have you tried using this : Local $oMyError = ObjEvent("AutoIt.Error", MyErrFunc) BTW you can screen capture directly into a file instead of going thru GDI+ “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Solution AutoitMike Posted September 18, 2022 Author Solution Share Posted September 18, 2022 I really dont know what the type of error it is. The script simply stops at the offending statement. Is this your suggestion? Local $oMyError = ObjEvent("AutoIt.Error", MyErrFunc) func MyErrFunc() msgbox(0,'','There was an error in the OCR_Region function') Exit endfunc Link to comment Share on other sites More sharing options...
Nine Posted September 18, 2022 Share Posted September 18, 2022 (edited) Yes this is what I was talking about. Place the ObjEvent statement at the beginning of the script. You should not get the error message anymore. edit : I am using MODI successfully, so I dont know what could be the problem, unless the file is garbage... Edited September 18, 2022 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
AutoitMike Posted September 18, 2022 Author Share Posted September 18, 2022 I did a test script where I provided a "Box" where the screen was blank, no text. The OCR_Region function always failed. When I provided a "Box" where text exists, it always returns an array of text, and rather quickly. The text is always correct. There are no "1" confused with lower case L, etc..... I'll try the trap idea you suggested. Link to comment Share on other sites More sharing options...
AutoitMike Posted September 18, 2022 Author Share Posted September 18, 2022 Local $oMyError = ObjEvent("AutoIt.Error", MyErrFunc) This produces : ERROR: sytax error Local $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") This works. I put a return 0 so that the script can continue. Thanks a bunch. Link to comment Share on other sites More sharing options...
AutoitMike Posted September 26, 2022 Author Share Posted September 26, 2022 One last question on this. How can i get the line number that created the error? Thanks again Link to comment Share on other sites More sharing options...
argumentum Posted September 26, 2022 Share Posted September 26, 2022 ... you can find that info right out of the CHM help file or searching this site. And don't declare Globals as Locals.Bad coder, bad coder ... sit !, good coder. Have a cookie 😜 ( hmm, I may not be good at joking, it was funny in my head ) Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
AutoitMike Posted September 26, 2022 Author Share Posted September 26, 2022 I searched the help, the closest that I can find is @scriptLineNumber which is not the offending line but the line that executes the command @scriptLineNumber Link to comment Share on other sites More sharing options...
argumentum Posted September 26, 2022 Share Posted September 26, 2022 (edited) 41 minutes ago, AutoitMike said: I searched the help expandcollapse popup#include <ScreenCapture.au3> #include <Array.au3> Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Global $Array = OCR_Region(20, 20, 200, 200) ;Top Left Corner x, Top Left Cornr y, Bottom Right Corner x, Bottom right Corner y Func OCR_Region($x, $y, $width, $height) ;note $height is NOT the height, it is the Y coordinate bottom right corner, width is bottom right corner X. If 1 = 2 Then Sleep(500) EndIf ;~ Local $OCRstart = TimerInit() Local $miDoc, $Doc, $str, $oWord, $sArray[200] ;~ Local $oMyError ;~ Local $HexNumber ;~ Local $msg Local $i ;Const $miLANG_CZECH = 5 ;Const $miLANG_DANISH = 6 ;Const $miLANG_DUTCH = 19 Local 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 Local $hBitmap1, $hImage1, $temp $temp = @ScriptDir & "\temp.bmp" _GDIPlus_Startup() ; Capture screen region $hBitmap1 = _ScreenCapture_Capture("", $x, $y, $width, $height, False) $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap1) _GDIPlus_ImageSaveToFile($hImage1, $temp) _GDIPlus_ImageDispose($hImage1) _WinAPI_DeleteObject($hBitmap1) $miDoc = ObjCreate("MODI.Document") If @error Then Return SetError(1, 0, "oops") ; if this fails then might as well quit. $miDoc.Create($temp) ;MsgBox(0,"",$temp);this is the bitmap image which is "temp.bmp" $miDoc.Ocr($miLANG_ENGLISH, True, False) ;THIS LINE FAILS $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 FileDelete($temp) Return $sArray EndFunc ;==>OCR_Region ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError, $iLine = @ScriptLineNumber) ; Do anything here. <--- right from the help file ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) MsgBox(262144 + 16, StringTrimRight(@ScriptName, 4), "err.scriptline is: " & $oError.scriptline & @CR & "also @ScriptLineNumber is " & $iLine, 30) EndFunc ;==>_ErrFunc Edited September 26, 2022 by argumentum better code Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
AutoitMike Posted September 26, 2022 Author Share Posted September 26, 2022 Thanks for the code, I will try it later tomorrow. argumentum 1 Link to comment Share on other sites More sharing options...
AutoitMike Posted September 26, 2022 Author Share Posted September 26, 2022 $oError.ScriptLine is my solution. Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now