Jump to content

Stuck in Loop


Recommended Posts

Working on OCR for a bot I am making. Reading individual characters works fine and is handled by GetChar(). The problem I am encountering is spaces. Spaces in the font I am reading are a variable number of pixels. I can get the OCR function to recognize spaces, but then it does not realize when it is at the end of the word and endlessly adds spaces. Here is the problem section of my script:

Func GetCardName()
Do
    GetChar()
        If $char = "" Then
            For $xdum = 0 to 10
                For $ydum = 0 To 15
                    $pixCol = PixelGetColor($xstart + $xdum, $ystart - $ydum)
                    If $pixCol = 0xEBE2AF Then
                        If $space = False Then
                            $char = " "
                            $xstart = $xstart + $xdum
                            $space = True
                            ExitLoop(2)
                        Else
                            $wcomplete = True
                            Msgbox(0, "Complete", "Cardname Complete")
                            ExitLoop(2)
                        EndIf
                    EndIf
                Next
            Next
        Else
            $space = False
        EndIf
    $wordString = $wordString & $char
    MsgBox(0, "Reading", $wordString)
Until $wcomplete = True
EndFunc

The method I used here was that when GetChar() encounter blank space after a character, it would search the next 10 pixels (bigger than spaces which are 5-9 pixels) to the right for another character. This works find as far as recognizing spaces and restarting on the next character, but never realizes its at the end of the word ($wcomplete never becomes true).

I have considered a couple problems I might be having, and would appreciate any comments or ideas.

1. I have been staring at the computer screen for way too long and can't see the obvious mistake.

2. I have some flawed logic in the loop.

3. I am overstepping the end of the column while looking to see if there are extra spaces.

Link to comment
Share on other sites

  • Moderators

GoofyGremlin,

This is my attempt to cretae a logical function for you, but of course it is untested. I have tried to simplify it as much as possible and commented almost every line to explain my thinking. Try it and see! >_<

Func GetCardName()
    While 1
        ; Try and read character
        GetChar()   ; Assume that $xstart and $ystart are reset for next read if successful

        ; Was a character read?
        If $char = "" Then  ; No - we have a gap

            ; Is it a space (5-9 pixels wide) or the end of word (10 pixels wide)
            For $xdum = 0 to 10
                For $ydum = 0 To 15
                    ; Search the gap
                    $pixCol = PixelGetColor($xstart + $xdum, $ystart - $ydum)
                    ; If we find "text colour" exit the search
                    If $pixCol = 0xEBE2AF Then ExitLoop 2     ; Break out of 2 For...Next loops
                    ; <<<<<<<<<<<<< Use correct syntax for ExitLoop !!!!!!  >>>>>>>>>>>>>>>>>>>
                Next
            Next
            ; Check to see if we exited early because text colour was found
            If $xdum = 11 Then
                ; No early exit so gap is end of word
                Msgbox(0, "Complete", "Cardname Complete")
                ExitLoop    ; Break out of While...WEnd loop
            Else
                ; Early exit so gap must only be a space
                $char = " "
                ; Reset $xstart for next read
                $xstart = $xstart + $xdum   ; Might be $xdum +/- 1 - you will have to test!
            EndIf
        EndIf

        ; Add char to string
        $wordString = $wordString & $char
        MsgBox(0, "Reading", $wordString)

    WEnd
EndFunc

Two points - both made in the comments but reiterated here:

1. You were using the wrong syntax for ExitLoop - that may have caused your problem in part.

2. You will have to play with resetting $xstart after finding a space - I think I have it set correctly, but it might well be a pixel out either way.

Let me know how you get on.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks a lot that worked. I still don't understand why my original code didn't work thought. Exit 2 and Exit(2) both had the same effect (the help file seems to imply that it has to be in parantheses). But anyway thanks a lot for your help, OCR is now working great.

Link to comment
Share on other sites

Exit and ExitLoop are different. You cannot use ExitLoop like you can Exit.

ExitLoop(2) is the incorrect syntax. See http://www.autoitscript.com/autoit3/docs/keywords/ExitLoop.htm.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

  • Moderators

GoofyGremlin,

Your initial code did not work because of several logic errors within it. That is why I went for a complete rewrite rather than amending your code. Sorry, but I am not going to analyse them in detail. :(

As to the syntax question:

Exit: "Remarks - The parameter, if included, can be enclosed in parentheses. Thus, the following are equivalent: Exit, Exit 0, and Exit(0). However, Exit() is invalid."

ExitLoop: No parentheses allowed.

The two are very different in action: Exit quits the script setting a DOS errorlevel (oh, how that takes me back..... >_< ) while ExitLoop breaks out of a While/Do/For loop.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...