Problems getting Rid of Duplicate Matches

Asked by geo on 2017-07-01

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 minimised 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, and it is essential the search runs more than once since, I am trying to minimise false matches in the area ,so that the right keys are pressed.
The code is also meant to be designed for speed with the operations being done before 10 seconds..
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 ###List from the Find All operation from main Function
    global xcordsFinal
    xcordsFinal = []
    for x,y in itertools.combinations(mylist, 2): #This is supposed to Compare 2 matches together
        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) #Adds it to the list to be finally sorted and put to operation.
            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

###################SORTING###################################################
def DISTANCE(match):
    x = math.fabs(match.getCenter().x - ml.x) #Order of the Matches are found by calculating the distance from a point
    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) # Writes to File For Final Processing
    f.close()
    with open("C:/Users/gracious/Desktop/yolo.sikuli/test.txt") as file:
        for line in file:
            line = line.strip() #preprocess line #Presses Buttons according to the Size of the image(Matches we found & Sorted)
            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[:] #Clears the List to Avoid Problems
    del up[:]
    del down[:]
    del leftt[:]
    del rightt[:]

#Function is Called
Solver()

###############################################################

Any Help would be appreciated.

Thanks

Question information

Language:
English Edit question
Status:
Expired
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2017-07-01
Last reply:
2017-07-18
RaiMan (raimund-hocke) said : #1

Without some visual information it is hard to help in such complicated setups.

Launchpad Janitor (janitor) said : #2

This question was expired because it remained in the 'Needs information' state without activity for the last 15 days.