← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #207273]: Creating an eitherOr method

 

Question #207273 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/207273

    Status: Open => Answered

j proposed the following answer:
So I made this "waitForAny"-method some time ago. 
It uses an observer and waits for a number of images to appear. If one of the images is found, the observer is stopped and the method returns. If more than one image is found at the same time, the image with the highest score wins. 

Here is a simplified version to show the principle:

# region: region where the images are searched
# observetime: time, how long the images will be searched
# *args: list of images that will be searched
def waitForAny(region, observetime, *args):
    
    # list to save the results
    results = []
    # create a method-local new region to keep the parent region clean of observers because observers cannot be removed
    region = Region(region) 

    # handler that is called when an image is found
    def getHandleFind():
        def handleFind(event):
            event.region.stopObserver()
            results.append(event.match)
        return handleFind

    # iterate over all images
    for img in args:
        # add onAppear for every image
        region.onAppear(img,getHandleFind())
    
    # start observation
    region.observe(observetime)

    if (len(results) == 0):
        return None
    
    results = sorted(results,key=lambda result: result.getScore(), reverse=True) # sort all results by score
    bestMatch = results[0]
    # return image and index of best match and match object
    return bestMatch

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.