Jump to content
Sign in to follow this  
DNnlee

Can i Speed up imagesearch & lower cpu usage

Recommended Posts

DNnlee

Hi, I am using a function from ImageSearch

in a previous post I've received help to create an ImageSearch script, and it is able to do what i want it to.

the goal of the script is to scan for a an image, and assign a value like 10 to $value

however it takes 15+/- 5 seconds to match an image from a choice of 120 images, and my CPU runs up to 90%+

i am running this script via VMware, and my laptop's specs are T9300 @ 2.49ghz / 4gb ram

so the host cpu is @ 45% and the Virtual CPU is @ 90%+, btw it "is" dangerous to have a constant high CPU usage right?

Need advice on how to Improve / Speed up / Lower cpu use / or scan multiple image at once instead of one by one.

#include <ImageSearch.au3>
$ImageLocation = IniReadSection(@ScriptDir & '\Image.ini', 'Images')

    $x1=0
    $y1=0
For $s = 1 to $ImageLocation[0][0]   
    $result = _ImageSearch($ImageLocation[$s][0],1,$x1,$y1,101)
    Sleep(30)
    if $result = 1 Then
        $value = $ImageLocation[$s][1]
        ExitLoop
    Elseif $result = 0 Then
        $value2 = 0
    EndIf
Next

INI FILE

[Images]
One.bmp = 10
Two.bmp = 10
Three.bmp = 10
Four.bmp = 10
Edited by DNnlee

Share this post


Link to post
Share on other sites
jaberwacky

Hi, I am using a function from ImageSearch

in a previous post I've received help to create an ImageSearch script, and it is able to do what i want it to.

the goal of the script is to scan for a an image, and assign a value like 10 to $value

however it takes 15+/- 5 seconds to match an image from a choice of 120 images, and my CPU runs up to 90%+

i am running this script via VMware, and my laptop's specs are T9300 @ 2.49ghz / 4gb ram

so the host cpu is @ 45% and the Virtual CPU is @ 90%+, btw it "is" dangerous to have a constant high CPU usage right?

Need advice on how to Improve / Speed up / Lower cpu use / or scan multiple image at once instead of one by one.

Is there a need for the sleep() statement? Also, it looks to me like _ImageSearch() actually just calls another function: _ImageSearchArea(). You might be able to call _ImageSearchArea() directly in order to avoid function overhead.

Share this post


Link to post
Share on other sites
DNnlee

Is there a need for the sleep() statement? Also, it looks to me like _ImageSearch() actually just calls another function: _ImageSearchArea(). You might be able to call _ImageSearchArea() directly in order to avoid function overhead.

well i added the SLeep(30) because i was scared i would damage the cpu if i don't let it rest a bit.

i'm not sure whether the Imagesearch already takes a break per image search or not, so i added it just in case.

but i will try to use _ImageSearchArea() to skip the overhead.

Share this post


Link to post
Share on other sites
jaberwacky

well i added the SLeep(30) because i was scared i would damage the cpu if i don't let it rest a bit.

i'm not sure whether the Imagesearch already takes a break per image search or not, so i added it just in case.

but i will try to use _ImageSearchArea() to skip the overhead.

Also, in your other post that you mentioned, Spiff59 said that it wouldn't hurt your CPU as long as certain prerequisites were met. So you'll be fine taking out the sleeps.

Share this post


Link to post
Share on other sites
jaberwacky

Give this a try. I completely eliminated calling the imageSearchArea function and put it right into your For loop. If you know your screen resolution then you can substitute them directly for the @DesktopHeight and width macros. Also, do you know if your tolerence and your resultPosition values will always be the same?

$ImageLocation = IniReadSection( @ScriptDir & '\Image.ini' ,  'Images' )

$tolerance = 101
$resultPosition = 1

For $s = 1 To $ImageLocation[0][0]  
    
    If $tolerance > 0 Then $ImageLocation[$s][0] = "*" & $tolerance & " " & $ImageLocation[$s][0]

    $result = DllCall( _
                "ImageSearchDLL.dll" ,             _
                "str" , "ImageSearch" ,             _
                "int" , 0 ,                               _
                "int" , 0 ,                               _
                "int" , @DesktopWidth ,            _
                "int" , @DesktopHeight ,            _
                    "str" , $ImageLocation[ $s ][ 0 ] _
                 )

    If $result[ 0 ]= "0" Then
        $value2 = 0 ; If error
    Else
        ; Otherwise get the x,y location of the match and the size of the image to
        ; compute the centre of search
        $array = StringSplit( $result[ 0 ] , "|" )

        $x=Int( Number( $array[ 2 ] ) )
        $y=Int( Number( $array[ 3 ] ) )

        If $resultPosition=1 Then
          $x = $x + Int( Number( $array[ 4 ] ) / 2 )
          $y = $y + Int( Number( $array[ 5 ] ) / 2 )
        EndIf
      
        $value = $ImageLocation[ $s ][ 1 ]
        ExitLoop
      
    EndIf
    
Next
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
jaberwacky

do you know if your tolerence and your resultPosition values will always be the same?

I assume that they will remain the same. Thus:

Local $ImageLocation = IniReadSection( @ScriptDir & '\Image.ini' ,  'Images' )  

For $s = 1 To $ImageLocation[ 0 ][ 0 ]    
    
    $findImage = $ImageLocation[ $s ][ 0 ]

    $result = DllCall( "ImageSearchDLL.dll"   , _
                       "str" , "ImageSearch"  , _
                       "int" , 0              , _
                       "int" , 0              , _
                       "int" , @DesktopWidth  , _
                       "int" , @DesktopHeight , _
                       "str" , "*101 " & $findImage )

    ; If error exit
    If $result[ 0 ]= "0" Then
        $value2 = 0
    Else
        ; Otherwise get the x,y location of the match and the size of the image to
        ; compute the centre of search
        $array = StringSplit( $result[ 0 ] , "|" )

        $x = Int( Number( $array[ 2 ] ) + Number( $array[ 4 ] ) / 2 ) ; is Number() even necesary?
        $y = Int( Number( $array[ 3 ] ) + Number( $array[ 5 ] ) / 2 )

         ; Maybe like this instead?
         ; $x = Int( $array[ 2 ] + $array[ 4 ] / 2 )
         ; $y = Int( $array[ 3 ] + $array[ 5 ] / 2 )
      
        $value = $ImageLocation[ $s ][ 1 ]
        
        ExitLoop      
    EndIf    
Next

I think I screwed this whole thang up so don't pay me no mind!

Edited by jaberwocky6669

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.