← Back to team overview

sikuli-driver team mailing list archive

[Question #645818]: Problems getting Rid of Duplicate Matches

 

New question #645818 on Sikuli:
https://answers.launchpad.net/sikuli/+question/645818

I seem to have run into a huge problem in my code :
A mini game requires me to press a certain keys displayed on the screen,the problem is the background is ever changing .
So i minimized the region to a very small area and run multiple searches before sorting them in the right order.The duplicates are found by measuring the x axis distance and if its less then 20 take the match with the most similarity score.
The problem is i am still getting duplicates if the search is run more than once.
Code

import math
import time
import re
import itertools
######################SETTINGS###############################
Settings.ObserveScanRate = 5
#############################################################
up =[]
down =[]
leftt =[]
rightt =[]
ml = Location(724, 386)

def distanceC(x, y):
    if x >= y:
        result = x - y
    else:
        result = y - x
    return result
################################DUPLICATE FINDER#####################################
def DuplicateFinder(C):
    mylist = C
    global xcordsFinal 
    xcordsFinal = []
    for x,y in itertools.combinations(mylist, 2):
        print("Printing Distance\n")
        print((distanceC(x.getX(),y.getX())))
        if(distanceC(x.getX(),y.getX())<20):
            print("Distance is less than 20!\n")
            if (x.getScore() == y.getScore()):
                xcordsFinal.append(x)            
            if (x.getScore() > y.getScore()):
                xcordsFinal.append(x)
            else:
                if(x.getScore() < y.getScore()):
                    xcordsFinal.append(y)
        else:
            xcordsFinal.append(x)
            xcordsFinal.append(y)
    return xcordsFinal
def DISTANCE(match): 
    x = math.fabs(match.getCenter().x - ml.x)
    y = math.fabs(match.getCenter().y - ml.y)  
    distance = int(math.sqrt(x * x + y * y))
    return distance
##############################SOLVER FUNCTION#################################
def Solver():
    global Z1
    global C
    C =[]
    Target= Region(481,112,959,605).find(Pattern("4-2.png").similar(0.46))
    Z1 = Region(Target.getX(),Target.getY(),368,60).offset(78,30)
    Z1.setAutoWaitTimeout(0.2)
    arrows = [Pattern("UP-1.png").similar(0.75),Pattern("DOWN-1.png").similar(0.77),Pattern("LEFT-1.png").similar(0.74),Pattern("RIGHT-1.png").similar(0.74)]
    for xzx in range(0,6):
        print(x, "th Run")
        for y in range(len(arrows)): 
            global up
            global down
            global leftt
            global rightt
            try:
                Z = list(Z1.findAll(arrows[y]))
                if(y==0):
                    up = up+Z
                if y ==1:
                    down = down+Z
                if y==2:
                    leftt = leftt +Z
                if y==3:
                    rightt = rightt+Z
            except FindFailed:
                pass

    up = list(set(up))
    down = list(set(down))
    leftt = list(set(leftt))
    rightt = list(set(rightt))
    C = C+up+down+leftt+rightt
    print("Before Clear:")
    print(C)
    XYYY = DuplicateFinder(C)
    print("Before SET:")
    print(XYYY)
    XYYY = set(XYYY)  
    print("Printing XYY AFTER CLEAR")
    print(XYYY)
    print("Printing Final Values")
    CZ = sorted(XYYY, key=DISTANCE)
    print(CZ)
    #for x in range(len(C)):
    #    C[x].highlight(1)
    #################CONVERSION PART#################
    f = open("C:/Users/gracious/Desktop/yolo.sikuli/test.txt", "w")
    for item in CZ:    
        f.write("%s\n" % item)
    f.close() 
    with open("C:/Users/gracious/Desktop/yolo.sikuli/test.txt") as file:
        for line in file:
            line = line.strip() #preprocess line
            if("10x8" in line):
                type("W")
                print("W")
                wait(.1)
            if("13x15" in line): 
                type("A") 
                print("A")
                wait(.234)
            if("9x8" in line):
                type("S")
                print("S")
            if("16x15" in line): 
                type("D")
                print("D")
                wait(.342)
    del C[:] 
    del up[:]
    del down[:]
    del leftt[:]
    del rightt[:]
Solver()       


-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.