Jump to content

Speeding this script up...


Recommended Posts

Well, I've been working on a 'motion detection' type script. It works as good as I expected it too, but it's no where near fast enough. The only way I've been able to fix it is to open the script 10 times at once.

Right now, all the code does is search a square, 250x250 (Top left corner of desktop, try dragging an icon over there while program is running) for a change in color. If a color change happens, it sets a little red marker thing. There's a 20 pixel jump, just to save time, so it isn't very sensitive.

I know that the sleep(500) will slow it down, but that's just so I could see where the marker would appear. There's bound to be a faster alternative.

How the heck would I be able to speed this up?

#include <GUIConstants.au3>
#include <Array.au3>

Global $size = 5
Global $Left = MouseGetPos (0)
Global $Top = MouseGetPos (1)

$X = 0
$Y = 0

While 1                                                                       ; loop
$p1 = PixelGetColor ( $X , $Y )                                               ; Get pixel color at $X, $Y
sleep(1)                                                                      ; Delay for 1 ms
$p2 = PixelGetColor ( $X , $Y )                                               ; Get pixel color again
$X = $X+20                                                                    ; Add 20 to $X
if $x > 250 then $Y = $Y+20                                                   ; If $X = 250, add 20 to Y. This basically checks if X has reached the end of it's row, and if it has, start searching 20 pixels down
if $x > 250 then $x = 0                                                       ; Same as above, except it set's $X back to 0
if $p1 - $p2  > 500000 then                                                   ; If  The two pixel color checks are not the same, Create the little red GUI motion indicator thing.
$MouseShadow = GUICreate ("motionX", 3, 100, $Left, $Top, $WS_POPUP, _
BitOR ($WS_EX_TRANSPARENT, $WS_EX_LAYERED, $WS_EX_TOPMOST))
WinSetTrans ($MouseShadow, "", 190)
GUISetBkColor(0xFF0000, $MouseShadow)
GUISetState ()
WinMove ($MouseShadow, "", $X, $Y)

$MouseShadow2 = GUICreate ("motionY", 100, 3, $Left, $Top, $WS_POPUP, _
BitOR ($WS_EX_TRANSPARENT, $WS_EX_LAYERED, $WS_EX_TOPMOST))
WinSetTrans ($MouseShadow2, "", 190)
GUISetBkColor(0xFF0000, $MouseShadow2)
GUISetState ()
WinMove ($MouseShadow2, "", $X, $Y)
sleep(500)                                                                     ; Display the GUI's for 500 ms
guidelete($mouseshadow)                                                        ; Delete GUI 1
guidelete($mouseshadow2)                                                       ; Delete GUI 2
endif                                                                          
if $y > 250 then $y = 0                                                        ; If $Y Has reached the end, go back to searching from 0.
Wend                                                                           ; End loop
Link to comment
Share on other sites

I'm not sure what you are going to do with this.. So this may not help at all, but it is some insight..

PixelChecksum is able to create a checksum of a region. By making a second checksum a few milliseconds later, you are able to detect a change in the area..

You will know nothing about the change, just that there is one, but it may be a good way to detect if a change has been made...

Edit: If you want to loop through a certain region containing X and Y coordinates, it is easier to use two For loops. Like this:

For $x = 0 to 200 Step 20
For $y = 0 to 250 Step 20
; Do things
Next
Next
Edited by Manadar
Link to comment
Share on other sites

I'm not sure what you are going to do with this.. So this may not help at all, but it is some insight..

PixelChecksum is able to create a checksum of a region. By making a second checksum a few milliseconds later, you are able to detect a change in the area..

You will know nothing about the change, just that there is one, but it may be a good way to detect if a change has been made...

Yeah, I need the detected movement to return the coordinates (or at least have the coordinates it was found at available, like in my script)

I like that For Loop thing, thanks.

Link to comment
Share on other sites

Yeah, I need the detected movement to return the coordinates (or at least have the coordinates it was found at available, like in my script)

I like that For Loop thing, thanks.

Use both, then. The much faster PixelChecksum() to detect if there is any change, then go in with your PixelGetColor() loops to find out exactly where the change was. Since the slower loops are only done when something changes, it should be faster and more responsive.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Use both, then. The much faster PixelChecksum() to detect if there is any change, then go in with your PixelGetColor() loops to find out exactly where the change was. Since the slower loops are only done when something changes, it should be faster and more responsive.

:)

So instead of it constantly searching the rows in order, I just wait until something changes and THEN send the PixelGetColor()?

That honestly doesn't make too much sense to me, as it would be nearly the same thing. PixelGetColor() Will still take the same amount of time to see what has changed.

With a step of 20 pixels.. 12,500 pixels need to be pixelgetcolor()'d in a 500x500 square..

Searching approximately 735 pixels per second..

17 seconds to loop through ONCE.

As in, if I waited for the pixelchecksum() to be set off, and the movement was in the bottom right corner, it would be 17 seconds before it reached that corner and detected movement at all, if the movement is still there.

The only solution I got out of that post was MULTIPLE pixelchecksum()'s all searching MUCH smaller areas at the same time. If one of the very small squares is tripped, it sends the pixelgetcolor() in that small square, resulting in a helluva lot faster searching speeds.

Each pixelchecksum() checking a 75x75 square.. I like it. 0.7 seconds to detect movement from the very bottom right corner.

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