repeating findBest question

Asked by Ben Dix on 2021-03-05

Hi,

I try to optimize my script from:

if exists(img1,0) or exists(img2,0) or ....:
     do something

to a better readable version by using a list and findBest.

So I made a list of images and repeat a function to click on different existing images, looking almost the same.
But in my case my function always clicks the same spot, found within the first round, even if it's not existing anymore.

any advice how to tell the function to search again new?

item_list = findBest{img1,img2,img3)

def doIt():
 for i in range(1,4,+1):
  if item_List:
   Lclick(item_List)
   print('found and clicked item')
   sleep(random.uniform(0.4, 0.6))
  else:break

Lclick() is my own def to click somewhere inside the region:

def Lclick(region):
    CoOrdData = (region)
    CoOrdDatax = CoOrdData.x + int(CoOrdData.w * random.random())
    CoOrdDatay = CoOrdData.y + int(CoOrdData.h * random.random())
    click(Location(CoOrdDatax, CoOrdDatay))

I am also wondering, if I can define a wait time like in using if exists(img,WAITTIME): for my list images?

Greetings 😄

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Ben Dix
Solved:
2021-03-06
Last query:
2021-03-06
Last reply:
2021-03-06
RaiMan (raimund-hocke) said : #1

findBest returns only one Match object like exists does (it is the one with the highest match score of all images found).
For each image it only looks once. This is done for all images in parallel.

images = {img1,img2,img3)
match = findBestList{images)
if match:
    print "found: %d" % match.getIndex()
   click(match)

see: https://sikulix-2014.readthedocs.io/en/latest/region.html#find-more-than-one-image-in-a-region-at-the-same-time

With 2.0.5 there are also available: waitBest/waitBestList and waitAny/waitAnyList, that take as first parameter the max time to wait for matches (not yet in the docs :-(

Ben Dix (bendix80) said : #2

thx Rainman,

now I figured out what to do 😄
Your example helps, of course, but now I understood that the moment you call "findBest" must be inside the loop to be able to search again for all images:

Images = [img1, img2,img2]

for i in range (1,5,+1):
    wait(1)
    matchImages = findBestList(Images)
    if matchImages:
        Lclick(matchImages)

It is working only while using a wait command (or sleep) before calling the "matchImages" again.
Otherwise there seems to be not enough time to do a new search 😄

fine for me, so I can define my images somewhere als and call only the function when needed 😄

greetings