Jump to content

udf help


JohnOne
 Share

Recommended Posts

I'm having dificulty making my first ever function

My script, captures charactors of text on screen (individual letters) reads them with tesseract and corrects the ones it returns wrong, (tesseract returns "iu" for example when the letter is actually "t"), and a number of others

here is my working script (no problems with this, but I figure I could probably use a function to replace the repetative code.

I'm just not sure how

#include <Tesseract.au3>
$coord0 = PixelSearch(0, 0, @DesktopWidth / 2, @DesktopHeight / 2, 0x3F5F60)
$c1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 283, $coord0[1] + 133, $coord0[0] + 296, $coord0[1] + 150, 0)
If StringLeft($c1, 1) = "B" Then $c1 = "8"
If StringLeft($c1, 2) = "IU" Then $c1 = "T"
If StringLeft($c1, 2) = "H]" Then $c1 = "T"
If StringLeft($c1, 2) = "ID" Then $c1 = "T"
If StringLeft($c1, 1) = "0" Then $c1 = "D"
If StringLeft($c1, 1) = "O" Then $c1 = "D"
If StringLeft($c1, 2) = "41" Then $c1 = "D"
$c1 = StringLeft($c1, 1)
$s1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 284, $coord0[1] + 149, $coord0[0] + 296, $coord0[1] + 164, 0)
If StringLeft($s1, 1) = "Q" Then $s1 = "w"
If StringLeft($s1, 1) = "6" Then $s1 = "x"
If StringLeft($s1, 1) = "4" Then $s1 = "y"
If StringLeft($s1, 1) = "V" Then $s1 = "z"

$c2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 323, $coord0[1] + 133, $coord0[0] + 336, $coord0[1] + 150, 0)
If StringLeft($c2, 1) = "B" Then $c2 = "8"
If StringLeft($c2, 2) = "IU" Then $c2 = "T"
If StringLeft($c2, 2) = "H]" Then $c2 = "T"
If StringLeft($c2, 2) = "ID" Then $c2 = "T"
If StringLeft($c2, 1) = "0" Then $c2 = "D"
If StringLeft($c2, 1) = "O" Then $c2 = "D"
If StringLeft($c2, 2) = "41" Then $c2 = "D"
$c2 = StringLeft($c2, 1)
$s2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 324, $coord0[1] + 149, $coord0[0] + 336, $coord0[1] + 164, 0)
If StringLeft($s2, 1) = "Q" Then $s2 = "w"
If StringLeft($s2, 1) = "6" Then $s2 = "x"
If StringLeft($s2, 1) = "4" Then $s2 = "y"
If StringLeft($s2, 1) = "V" Then $s2 = "z"

$c3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 363, $coord0[1] + 133, $coord0[0] + 376, $coord0[1] + 150, 0)
If StringLeft($c3, 1) = "B" Then $c3 = "8"
If StringLeft($c3, 2) = "IU" Then $c3 = "T"
If StringLeft($c3, 2) = "H]" Then $c3 = "T"
If StringLeft($c3, 2) = "ID" Then $c3 = "T"
If StringLeft($c3, 1) = "0" Then $c3 = "D"
If StringLeft($c3, 1) = "O" Then $c3 = "D"
If StringLeft($c3, 2) = "41" Then $c3 = "D"
$c3 = StringLeft($c3, 1)
$s3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 364, $coord0[1] + 149, $coord0[0] + 376, $coord0[1] + 164, 0)
If StringLeft($s3, 1) = "Q" Then $s3 = "w"
If StringLeft($s3, 1) = "6" Then $s3 = "x"
If StringLeft($s3, 1) = "4" Then $s3 = "y"
If StringLeft($s3, 1) = "V" Then $s3 = "z"
MsgBox(0,"Result", $c1 & $s1 & " " & $c2 & $s2 & " " & $c3 & $s3)

So I've tried this, and as usual its all over the place and probably totally wrong

#include <Tesseract.au3>
$coord0 = PixelSearch(0, 0, @DesktopWidth / 2, @DesktopHeight / 2, 0x3F5F60)
$c1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 283, $coord0[1] + 133, $coord0[0] + 296, $coord0[1] + 150, 0)
_SortText()
$s1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 284, $coord0[1] + 149, $coord0[0] + 296, $coord0[1] + 164, 0)
_SortMarker()
$c2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 323, $coord0[1] + 133, $coord0[0] + 336, $coord0[1] + 150, 0)
_SortText()
$s2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 324, $coord0[1] + 149, $coord0[0] + 336, $coord0[1] + 164, 0)
_SortMarker()
$c3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 363, $coord0[1] + 133, $coord0[0] + 376, $coord0[1] + 150, 0)
_SortText()
$s3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 364, $coord0[1] + 149, $coord0[0] + 376, $coord0[1] + 164, 0)
_SortMarker()
MsgBox(0,"Result", $c1 & $s1 & " " & $c2 & $s2 & " " & $c3 & $s3)


Func _SortText()
    For $s = 1 to UBound($c1, 1) -1
If StringLeft($c1, 1) = "B" Then $c1 = "8"
If StringLeft($c1, 2) = "IU" Then $c1 = "T"
If StringLeft($c1, 2) = "H]" Then $c1 = "T"
If StringLeft($c1, 2) = "ID" Then $c1 = "T"
If StringLeft($c1, 1) = "0" Then $c1 = "D"
If StringLeft($c1, 1) = "O" Then $c1 = "D"
If StringLeft($c1, 2) = "41" Then $c1 = "D"
$c1 = StringLeft($c1, 1)
Next
EndFunc


Func _SortMarker()
    For $s = 1 to Ubound($s1, 1) -1
If StringLeft($s3, 1) = "Q" Then $s3 = "w"
If StringLeft($s3, 1) = "6" Then $s3 = "x"
If StringLeft($s3, 1) = "4" Then $s3 = "y"
If StringLeft($s3, 1) = "V" Then $s3 = "z"
Next
EndFunc

In my working script I get a msgbox containing something like "Fx Py Tw"

In the barmy functioned one I get Randon letters, and not in the correct format, as though its putting @LF into the msgbox

I would appreciate any education regarding my attempt at making a function.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I'm having dificulty making my first ever function

My script, captures charactors of text on screen (individual letters) reads them with tesseract and corrects the ones it returns wrong, (tesseract returns "iu" for example when the letter is actually "t"), and a number of others

here is my working script (no problems with this, but I figure I could probably use a function to replace the repetative code.

I'm just not sure how

#include <Tesseract.au3>
$coord0 = PixelSearch(0, 0, @DesktopWidth / 2, @DesktopHeight / 2, 0x3F5F60)
$c1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 283, $coord0[1] + 133, $coord0[0] + 296, $coord0[1] + 150, 0)
If StringLeft($c1, 1) = "B" Then $c1 = "8"
If StringLeft($c1, 2) = "IU" Then $c1 = "T"
If StringLeft($c1, 2) = "H]" Then $c1 = "T"
If StringLeft($c1, 2) = "ID" Then $c1 = "T"
If StringLeft($c1, 1) = "0" Then $c1 = "D"
If StringLeft($c1, 1) = "O" Then $c1 = "D"
If StringLeft($c1, 2) = "41" Then $c1 = "D"
$c1 = StringLeft($c1, 1)
$s1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 284, $coord0[1] + 149, $coord0[0] + 296, $coord0[1] + 164, 0)
If StringLeft($s1, 1) = "Q" Then $s1 = "w"
If StringLeft($s1, 1) = "6" Then $s1 = "x"
If StringLeft($s1, 1) = "4" Then $s1 = "y"
If StringLeft($s1, 1) = "V" Then $s1 = "z"

$c2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 323, $coord0[1] + 133, $coord0[0] + 336, $coord0[1] + 150, 0)
If StringLeft($c2, 1) = "B" Then $c2 = "8"
If StringLeft($c2, 2) = "IU" Then $c2 = "T"
If StringLeft($c2, 2) = "H]" Then $c2 = "T"
If StringLeft($c2, 2) = "ID" Then $c2 = "T"
If StringLeft($c2, 1) = "0" Then $c2 = "D"
If StringLeft($c2, 1) = "O" Then $c2 = "D"
If StringLeft($c2, 2) = "41" Then $c2 = "D"
$c2 = StringLeft($c2, 1)
$s2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 324, $coord0[1] + 149, $coord0[0] + 336, $coord0[1] + 164, 0)
If StringLeft($s2, 1) = "Q" Then $s2 = "w"
If StringLeft($s2, 1) = "6" Then $s2 = "x"
If StringLeft($s2, 1) = "4" Then $s2 = "y"
If StringLeft($s2, 1) = "V" Then $s2 = "z"

$c3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 363, $coord0[1] + 133, $coord0[0] + 376, $coord0[1] + 150, 0)
If StringLeft($c3, 1) = "B" Then $c3 = "8"
If StringLeft($c3, 2) = "IU" Then $c3 = "T"
If StringLeft($c3, 2) = "H]" Then $c3 = "T"
If StringLeft($c3, 2) = "ID" Then $c3 = "T"
If StringLeft($c3, 1) = "0" Then $c3 = "D"
If StringLeft($c3, 1) = "O" Then $c3 = "D"
If StringLeft($c3, 2) = "41" Then $c3 = "D"
$c3 = StringLeft($c3, 1)
$s3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 364, $coord0[1] + 149, $coord0[0] + 376, $coord0[1] + 164, 0)
If StringLeft($s3, 1) = "Q" Then $s3 = "w"
If StringLeft($s3, 1) = "6" Then $s3 = "x"
If StringLeft($s3, 1) = "4" Then $s3 = "y"
If StringLeft($s3, 1) = "V" Then $s3 = "z"
MsgBox(0,"Result", $c1 & $s1 & " " & $c2 & $s2 & " " & $c3 & $s3)

So I've tried this, and as usual its all over the place and probably totally wrong

#include <Tesseract.au3>
$coord0 = PixelSearch(0, 0, @DesktopWidth / 2, @DesktopHeight / 2, 0x3F5F60)
$c1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 283, $coord0[1] + 133, $coord0[0] + 296, $coord0[1] + 150, 0)
_SortText()
$s1 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 284, $coord0[1] + 149, $coord0[0] + 296, $coord0[1] + 164, 0)
_SortMarker()
$c2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 323, $coord0[1] + 133, $coord0[0] + 336, $coord0[1] + 150, 0)
_SortText()
$s2 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 324, $coord0[1] + 149, $coord0[0] + 336, $coord0[1] + 164, 0)
_SortMarker()
$c3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 363, $coord0[1] + 133, $coord0[0] + 376, $coord0[1] + 150, 0)
_SortText()
$s3 = _TesseractScreenCapture(0, "", 1, 1, $coord0[0] + 364, $coord0[1] + 149, $coord0[0] + 376, $coord0[1] + 164, 0)
_SortMarker()
MsgBox(0,"Result", $c1 & $s1 & " " & $c2 & $s2 & " " & $c3 & $s3)


Func _SortText()
    For $s = 1 to UBound($c1, 1) -1
If StringLeft($c1, 1) = "B" Then $c1 = "8"
If StringLeft($c1, 2) = "IU" Then $c1 = "T"
If StringLeft($c1, 2) = "H]" Then $c1 = "T"
If StringLeft($c1, 2) = "ID" Then $c1 = "T"
If StringLeft($c1, 1) = "0" Then $c1 = "D"
If StringLeft($c1, 1) = "O" Then $c1 = "D"
If StringLeft($c1, 2) = "41" Then $c1 = "D"
$c1 = StringLeft($c1, 1)
Next
EndFunc


Func _SortMarker()
    For $s = 1 to Ubound($s1, 1) -1
If StringLeft($s3, 1) = "Q" Then $s3 = "w"
If StringLeft($s3, 1) = "6" Then $s3 = "x"
If StringLeft($s3, 1) = "4" Then $s3 = "y"
If StringLeft($s3, 1) = "V" Then $s3 = "z"
Next
EndFunc

In my working script I get a msgbox containing something like "Fx Py Tw"

In the barmy functioned one I get Randon letters, and not in the correct format, as though its putting @LF into the msgbox

I would appreciate any education regarding my attempt at making a function.

I don't know anything about Tesseract.au3, but it looks like you are expecting a 1 or 2 character string to be returned. If so then you should not use UBound biut StringLen.

I think it would also be better to pass the string the the function than to use a global variable, and maybe use Select instead of working out the first 1 or 2 characters repeatedly.

.
.
.
_SortMarker($s3)
.
.

Func _SortMarker($sSM)
    if $sSM = '' then return

Local $s1 = StringLeft($s3, 1)
Switch $s1
 Case "Q" 
   $s3 = "w"
 Case "6"
    $s3 = "x"
 Case  "4" 
   $s3 = "y"
  Case  "V" 
     $s3 = "z"
 EndSwitch
EndFunc

If I misunderstood you are trying to parse a complete string then

.
.
.
_SortMarker($s3)
.
.

Func _SortMarker($sSM)
    Local $s1, $n
    If $sSM = '' Then Return
    For $n = 1 To StringLen($sSM)
        $s1 = StringMid($s3, $n 1)
        Switch $s1
            Case "Q"
                $s3 = "w"
            Case "6"
                $s3 = "x"
            Case "4"
                $s3 = "y"
            Case "V"
                $s3 = "z"
        EndSwitch
    Next
EndFunc   ;==>_SortMarker
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thanks for your response Martin

I get the same result with the func you posted

The tesseract aspect is working fine, and I can change the incorrect returned letters, but I just have absolutely

no idead how to turn the two if segments into just two functions

I suspect your offering does that, but I cannot understand what it is doing :)

There are tons more peices to the script you see, here I have just $c1 $c2 $c3 but it goes upto $c16 and it just looks ridiculous and probably using more cpu and memory than it should

EDIT:

the only reason I use stringleft is that I think the od whitespace is being returned by tesseract and If I dont use it, the result is undesirale to me

I only want a single charactor returned you see

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I often dont get my question across correctly, so I will try to rephrase it.

$c1 = "ID" (could be anything)

$c2 = "H" (could be anything)

$c3 = "41"(could be anything)

stuff =

If StringLeft($c1, 1) = "B" Then $c1 = "8"
If StringLeft($c1, 2) = "IU" Then $c1 = "T"
If StringLeft($c1, 2) = "H]" Then $c1 = "T"
If StringLeft($c1, 2) = "ID" Then $c1 = "T"
If StringLeft($c1, 1) = "0" Then $c1 = "D"
If StringLeft($c1, 1) = "O" Then $c1 = "D"
If StringLeft($c1, 2) = "41" Then $c1 = "D"
$c1 = StringLeft($c1, 1)

As you can see, its doing stuff, but only for $c1

I'm hpoing to make stuff into a function so it will perform stuff on $c2, $c3, $c4 etc without having to repeat the code each time.

And I'm totally lost

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Could you be looking for eval and assign? Here is an example:

$c1 = "2"
$c2 = "3"
$c3 = "4"
$c4 = "5"

Function(4)

Func Function($VariableNumber)
    For $i = 1 To $VariableNumber + 1
;~         If StringLeft(Eval("c" & $i), 1) = "B" Then Assign("c" & $i, "8")
;~         If StringLeft(Eval("c" & $i), 2) = "IU" Then Assign("c" & $i, "T")
;~         If StringLeft(Eval("c" & $i), 2) = "H]" Then Assign("c" & $i, "T")
;~         If StringLeft(Eval("c" & $i), 2) = "ID" Then Assign("c" & $i, "T")
;~         If StringLeft(Eval("c" & $i), 1) = "0" Then Assign("c" & $i, "D")
;~         If StringLeft(Eval("c" & $i), 1) = "O" Then Assign("c" & $i, "D")
;~         If StringLeft(Eval("c" & $i), 2) = "41" Then Assign("c" & $i, "D")
;~         Eval("c" & $i) = StringLeft(Eval("c" & $i), 1)
        ConsoleWrite(Eval("c" & $i) & @CRLF)
    Next
EndFunc   ;==>Function

Edit: Maybe this would be a bit easier and more varsatile.

;declare variables
Global $c1 = "2", $c2 = "3", $c3 = "4", $c4 = "5"

;put whatever variables that you want to perform Function on in here
Dim $array[4] = ["c1", "c2", "c3", "c4"]

;call function
Function($array)

Func Function($aArray)
    For $element In $aArray
        ;execute these commands for each variable in the array given
;~         If StringLeft(Eval($element), 1) = "B" Then Assign($element, "8")
;~         If StringLeft(Eval($element), 2) = "IU" Then Assign($element, "T")
;~         If StringLeft(Eval($element), 2) = "H]" Then Assign($element, "T")
;~         If StringLeft(Eval($element), 2) = "ID" Then Assign($element, "T")
;~         If StringLeft(Eval($element), 1) = "0" Then Assign($element, "D")
;~         If StringLeft(Eval($element), 1) = "O" Then Assign($element, "D")
;~         If StringLeft(Eval($element), 2) = "41" Then Assign($element, "D")
;~         Eval($element) = StringLeft(Eval($element), 1)
        ConsoleWrite(Eval($element) & @CRLF)
    Next
EndFunc   ;==>Function
Edited by dantay9
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...