Jump to content

191 + 1 = 1? w...t...h?


Go to solution Solved by Sori,

Recommended Posts

Posted (edited)

I'm sure many of you are aware of the image search library. I wanted to make a slightly more advanced version that checks the last known location of an image before checking the full screen. In essense, making the image search faster... Here is what I have.

 

Test.au3

#include <Advanced Image Search.au3>

AdvancedImageSearch("Example.bmp", 30)

Advanced Image Search.au3

#include <GetFileProperty.au3>
#include <ImageSearch.au3>

Dim $pHeight = 0
Dim $pWidth = 0
Dim $dArray
Dim $lastKnownX = ""
Dim $lastKnownY = ""
Dim $checkXTL = 0
Dim $checkXBR = 0
Dim $checkYTL = 0
Dim $checkYBR = 0

Func AdvancedImageSearch($picName, $transparency)
   Dim $x = 0
   Dim $y = 0
   
   If FileExists("C:\Users\The Lucky Crane\Desktop\AI\Images\" & $picName) = 1 Then
      GetDimensions($picName)
      $lastKnownX = GetLastX($picName)
      $lastKnownY = GetLastY($picName)
      
      ;If no record of File exists, search entire screen and save location found to registry.
      If $lastKnownX = "" or _
         $lastKnownY = "" Then
         If _ImageSearchArea("C:\Users\The Lucky Crane\Desktop\AI\Images\" & $picName,1,0,0,@DesktopWidth,@DesktopHeight,$x, $y, $transparency) = 1 Then
            RegWrite("HKEY_CURRENT_USER\Software\TheLuckyCrane\Image Locations",$picName & " X", "REG_SZ", $x)
            RegWrite("HKEY_CURRENT_USER\Software\TheLuckyCrane\Image Locations",$picName & " Y", "REG_SZ", $y)
            MsgBox(0, $picName, "Found at " & $x & ", " & $y)
         Else
            MsgBox(0, $picName, "Image Not Found")
         EndIf
      Else
         ;Check last known location.
         SetLocationCoordinates()
         If _ImageSearchArea("C:\Users\The Lucky Crane\Desktop\AI\Images\" & $picName,1,$checkXTL,$checkYTL,$checkXBR,$checkYBR,$x, $y, $transparency) = 1 Then
            MsgBox(0, $picName, "Found at last coordinates")
            MouseMove($x, $y, 0)
         Else
            ;If not found at last location, check entire screen.
            If _ImageSearchArea("C:\Users\The Lucky Crane\Desktop\AI\Images\" & $picName,1,0,0,@DesktopWidth,@DesktopHeight,$x, $y, $transparency) = 1 Then
               RegWrite("HKEY_CURRENT_USER\Software\TheLuckyCrane\Image Locations",$picName & " X", "REG_SZ", $x)
               RegWrite("HKEY_CURRENT_USER\Software\TheLuckyCrane\Image Locations",$picName & " Y", "REG_SZ", $y)
               MsgBox(0, $picName, "Found at " & $x & ", " & $y)
            Else
               MsgBox(0, $picName, "Not Found")
            EndIf ;Search total screen
         EndIf ;Found at Last Coordinates
      EndIf ;Last Known X
   Else
      MsgBox(0, "File Path Error", "Picture: '" & $picName & "' does not exist in Images folder.")
   EndIf ;File Exists
EndFunc

Func GetDimensions($picName)
   Dim $prop
   $path = "C:\Users\The Lucky Crane\Desktop\AI\Images\" & $picName
   $prop = _GetFileProperty($path, "Dimensions")
   $dArray = StringSplit($prop, " x ")
   $pWidth = $dArray[1] ;Picture Width
   $pHeight = $dArray[4] ;Picture Height
EndFunc

Func GetLastX($picName)
   Return RegRead("HKEY_CURRENT_USER\Software\TheLuckyCrane\Image Locations",$picName & " X")
EndFunc

Func GetLastY($picName)
   Return RegRead("HKEY_CURRENT_USER\Software\TheLuckyCrane\Image Locations",$picName & " Y")
EndFunc

Func SetLocationCoordinates()
   ;MsgBox(0, "Last Known X", $lastKnownX)
   ;MsgBox(0, "Last Known Y", $lastKnownY)
   MsgBox(0, "pWidth", $pWidth)
   ;MsgBox(0, "pHeight", $pHeight)
   ;MsgBox(0, "XTL", $checkXTL)
   ;MsgBox(0, "YTL", $checkYTL)
   ;MsgBox(0, "XBR", $checkXBR)
   ;MsgBox(0, "YBR", $checkYBR)
   MsgBox(0, "pWidth / 2", $pWidth / 2)
   ;$checkXTL = ($lastKnownX - (round($pWidth / 2) + 1))
   ;$checkXBR = $lastKnownX + (round($pWidth / 2) + 1)
   ;$checkYTL = $lastKnownY - (round($pHeight / 2) + 1)
   ;$checkYBR = $lastKnownY + (round($pHeight / 2) + 1)
   ;MsgBox(0, "XTL", $checkXTL)
   ;MsgBox(0, "YTL", $checkYTL)
   ;MsgBox(0, "XBR", $checkXBR)
   ;MsgBox(0, "YBR", $checkYBR)
EndFunc

Now... During the "SetLocationCoordinates" function....

$pWidth is 191.

Message box shows this to be true.

But doing anything to $pWidth, in relation to mathematics (dividing, adding, subtracting) will cause it to show $pWidth as 0.

Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted

Even if $pWidth i s a string I always get 192.

Which line is in error?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 11/7/2013 at 1:41 PM, mikell said:

Please try using Number($pWidth)

When AutoIt does a numeric operation and one of the operands is a string it is implicitely translated to a number.

So there should be no need for this to solve this problem.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

  On 11/7/2013 at 1:41 PM, mikell said:

Please try using Number($pWidth) and declare $pWidth as Global

 

I tried that, it still made pWidth 0

Let me try the global thing really quick.

-- Still shows 0

  On 11/7/2013 at 1:42 PM, water said:

Even if $pWidth i s a string I always get 192.

Which line is in error?

If I set pWidth manually, like $pWidth = 191, it would work fine.

I'm not entirely sure where it's messing up.

Line 75 says 191

Line 81 says 0

If I change it to + 1, it will instead say 1.

Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted

Can you insert a

MsgBox(0, "", VargetType($pWidth))

before line 81 and post the result?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

  On 11/7/2013 at 1:43 PM, water said:

When AutoIt does a numeric operation and one of the operands is a string it is implicitely translated to a number.

So there should be no need for this to solve this problem.

Number($pWidth) / 2 ; 0

Number($pWidth / 2) ; 0

Number($pWidth) ; 0

It doesn't like changing it into a number at all <.<

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted (edited)

  On 11/7/2013 at 1:49 PM, water said:

Can you insert a

MsgBox(0, "", VargetType($pWidth))

before line 81 and post the result?

 

String

P.S.

I was unaware of that function, very convenient

Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted

Local $width = Number(0)
.
.
.
MsgBox(0, "", VargetType($width)) ; Int32
.
$width = Number($pWidth)
MsgBox(0, "", $width) ; 0

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted

  On 11/7/2013 at 1:55 PM, YogiBear said:

For educational purposes, what would this be used for as far as an application? Curious....

I'm creating an AI that will be able to control the entirety of my computer and hopefully I can have it learn.

The reason I am doing the advanced image recognition is to make image recognition faster for items that are stationary, but can sometimes move. Like an application on the taskbar, or even the quickbar.

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted (edited)

  On 11/7/2013 at 1:57 PM, trancexx said:

 

Maybe it's something like this:

$pWidth = "l91"

ConsoleWrite(Number($pWidth) & @CRLF)

That seems to have worked.

Thank you.

Scratch that...

It still won't do math on it.

It just saves 191 and then adding or dividing still shows 191.

Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted

  On 11/7/2013 at 2:12 PM, trancexx said:

What do you get when you run my two lines?

Func SetLocationCoordinates()
   MsgBox(0, "pWidth", $pWidth)
   MsgBox(0, "", VargetType($pWidth))
   ConsoleWrite(Number(($pWidth)) & @CRLF)
   MsgBox(0, "pWidth", ($pWidth + 1))
EndFunc

shows 1

 

Func SetLocationCoordinates()
   $pWidth = "191"
   MsgBox(0, "pWidth", $pWidth)
   MsgBox(0, "", VargetType($pWidth))
   ConsoleWrite(Number(($pWidth)) & @CRLF)
   MsgBox(0, "pWidth", ($pWidth + 1))
EndFunc

Shows 192

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted (edited)

  On 11/7/2013 at 2:23 PM, trancexx said:

Forget for second your script and make new one with this exact code in it:

$sSori = "l9l"

$nSorry = Number($sSori)

MsgBox(4096, "Sori Math", $nSorry) ; TADA!

What do you get?

 

Created a new script...

 

$sSori = "l9l"
$nSorry = Number($sSori)
MsgBox(4096, "Sori Math", $nSorry) ; TADA!

Gave me 0

eh... are those L?

Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Posted

Logically the only place that it can be going wrong is in one of these 2 lines in the GetDimensions() function

$prop = _GetFileProperty($path, "Dimensions")
   $dArray = StringSplit($prop, " x ")

$dArray[1] probably contains spaces or other chracters before the number repesenting the width.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Posted

  On 11/7/2013 at 2:28 PM, Sori said:

Created a new script...

 

$sSori = "l9l"
$nSorry = Number($sSori)
MsgBox(4096, "Sori Math", $nSorry) ; TADA!

Gave me 0

eh... are those L?

So, how do you explain that?

Now do this:

$sSori = "191"

$nSorry = Number($sSori)

MsgBox(4096, "Sori Math", $nSorry) ; TADA!

What do you get now?

♡♡♡

.

eMyvnE

Posted
  On 11/7/2013 at 2:29 PM, Bowmore said:

Logically the only place that it can be going wrong is in one of these 2 lines in the GetDimensions() function

$prop = _GetFileProperty($path, "Dimensions")
   $dArray = StringSplit($prop, " x ")

$dArray[1] probably contains spaces or other chracters before the number repesenting the width.

 

dArray is showing as a string, trying to force it into an int causes it to give 0.

I'll have to dive into the GetFileProperty au3 and see if I can find anything.

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

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
  • Recently Browsing   0 members

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