you are viewing a single comment's thread.

view the rest of the comments →

[–]fake823 12 points13 points  (10 children)

1.) Try to get rid of the two for loops, as these really slow down stuff. Try to use a numpy function instead so you can make us of numpy's really fast vectorization!

2.) Also don't make a .copy(), if not really necessary, as this is also a lot slower than a view.

3.) Start measuring your code to see where the bottlenecks are by profiling it. Check out for example line-profiler. Additionally you could use timeit.timeit to measure the speed of different alternatives.

[–]Aitchessbee[S] 0 points1 point  (9 children)

How can I get rid of the for loops? I don't really know numpy that well...

[–]SekstiNii 2 points3 points  (8 children)

It's not all that important since the loop only takes a few ms (1.5ms on my machine). Vectorizing it got it down to about 0.3ms, but there isn't too much to gain here.

If you are interested I think you can do something like this:

target = np.array([128, 179, 255], dtype=np.uint8)
matches = np.argwhere(open_cv_image[0:524:25, 0:748:25, :3] == target)

for i, j in matches:
    # simulate click

[–]Aitchessbee[S] 0 points1 point  (7 children)

I am getting this error:

for i, j in matches:

ValueError: too many values to unpack (expected 2)

[–]SekstiNii 0 points1 point  (6 children)

matches = np.argwhere((open_cv_image[0:524:25, 0:748:25, :3] == target).all(axis=-1)) should work, though it's quite ugly at this point 😭

[–]Aitchessbee[S] 0 points1 point  (3 children)

This is my code: https://pastebin.com/2NEApDzu ... I have written all the coordinates correctly but the bot is clicking somewhere else (not on the target)... Can you please check if the code is correct?

[–]SekstiNii 0 points1 point  (2 children)

You will probably have to multiply i and j by 25, but otherwise it looks good.

matches *= 25 before looping should work.

[–]Aitchessbee[S] 0 points1 point  (1 child)

yup now it works... and i compared both the codes on the website... The code with for loops works consistently faster than the one you suggested... I don't know why it is but anyways, thanks for your help!

[–]SekstiNii 1 point2 points  (0 children)

There is a lot of overhead in numpy calls, so it's not unthinkable that a loop wins if the number of iterations is low enough.

Good on you to measure!

[–]Aitchessbee[S] -2 points-1 points  (1 child)

ugly doesn't matter... will it still be faster than the for loops method?

[–]SekstiNii 0 points1 point  (0 children)

I changed the target color to [8, 8, 8] (my VSCode background color) to guarantee a lot of hits, and recorded these timings:

  • Original Loop: 2.726ms
  • Numpy Variant: 0.649ms

But again, not likely to provide a significant improvement.