How to know a specific image found on a if exists with an array

Asked by Luiz

Hello, i have this part of my code. I already posted it here a few days ago
(https://answers.launchpad.net/sikuli/+question/239619) and now i have other problems.
I have this array (ha =[]) with some images that i use to find on 2 determined regions (rHa and rHb). The code is working fine, but i want to know if there is any way to know which image my "if (existsAny(rHa, ha)" found. I mean if it found something i want to know what exactly.
Sorry i don't know how to explain it better and engish is not my 1st language.
Hope you guys understand what i mean.
Thanks

rHa = Region(Region(858,644,120,97))
rHb = Region(Region(952,643,105,97))
Settings.waitScanRate = 500 # release brake for the exists
rF= Region(Region(920,890,252,116))#trigger region
Settings.waitScanRate = 3 # reset to normal

mH = (Pattern("mH.png").similar(0.99))
mO = (Pattern("mO.png").similar(0.99))
mP = (Pattern("mP.png").similar(0.99))
mS = (Pattern("mS.png").similar(0.99))
kH = (Pattern("kH.png").similar(0.99))
kO = (Pattern("kO.png").similar(0.99))
kP = (Pattern("kP.png").similar(0.99))
kS = (Pattern("kS.png").similar(0.99))
nH = (Pattern("nH.png").similar(0.99))
nO = (Pattern("nO.png").similar(0.99))
nP = (Pattern("nP.png").similar(0.99))
nS = (Pattern("nS.png").similar(0.99))

ha = [] # <---- this array
ha.append(mH)
ha.append(mO)
ha.append(mS)
ha.append(kH)
ha.append(kO)
ha.append(kS)
ha.append(nH)
ha.append(nO)
ha.append(nS)

nh = [] #m (I use this later)
nh.append(mH)
nh.append(mO)
nh.append(mP)
nh.append(mS)

no = [] #k (I use this later)
no.append(kH)
no.append(kO)
no.append(kP)
no.append(kS)

np = [] #n (I use this later)
np.append(nH)
np.append(nO)
np.append(nP)
np.append(nS)

def existsAny(reg, imageList):
    for img in imageList:
        if (reg.exists(img, 0)):
            return True
    return False

count = 0
while (count < 1000):
    rF.wait(Pattern("trigger.png").similar(0.90), FOREVER)#trigger
    img = Screen(0).capture(Region(959,774,80,33))
    import shutil
    shutil.move (img, 'c:/a/x(%d).png' % count)
    count = count + 1

    if (existsAny(rHa, ha) and existsAny(rHb, ha)):
        #print the img that the above if found <------ HERE
        click(Region(1332,817,74,34))
        click(Region(1391,908,215,83))
    else:
        click(Region(882,849,340,166))#clicking the trigger restarts the loop.

Again, thanks!

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

  • by Luiz
Revision history for this message
akbar (mohammed-akbar-ali) said :
#1

I dont know what existsAny returns whether a boolean or a match. Whatever it returns you can store that in a variable and then use it accordingly.

varA = existsAny(rHa,ha)
varB = existsAny(rHb,ha)

if(varA or varB){

if(varA) print something
if(varB) print something

}

Revision history for this message
RaiMan (raimund-hocke) said :
#2

You have to modify your existsAny, to return the index of the found image, otherwise -1

def existsAny(reg, imageList):
# for img in imageList:
    for n in range(len(imageList)):
        if (reg.exists(imageList[n], 0)):
            return n
    return -1

and check/use the return value accordingly.

Revision history for this message
Luiz (talhante) said :
#3

Yay! thx man!

Revision history for this message
Luiz (talhante) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
Luiz (talhante) said :
#5

Hi,

I just found this old project and I want to go back to work on it.
It is the same project of the question, actually.

So the problem is:

def existsAny(reg, imageList):
# for img in imageList:
    for n in range(len(imageList)):
        if (reg.exists(imageList[n], 0)):
            return n
    return -1

it works, but it only returns the first value.

in the case I wonder how to get the second value:

if (existsAny(rHa, ha) and existsAny(rHb, ha)):

I have searched on some Jython docs, specifically on lists, but since I don't really know nothing about programming, coding, etc. I coudn't solve the problem.

So.. that's it and thank you for always helping.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

with your logic, you get the index (beginning with 0) of the image in your image list, that had the first match (because you leave the loop at that moment stopping the search)

so if you always get 0, then always the first image is found.

So please again try with your own words, what you want to achieve.

Revision history for this message
Luiz (talhante) said :
#7

alright

I have this list with 20 diferent images in it (image01-image20). The images will appear in two diferent regions (regionA-regionB)
If they dont appear, the program will click elsewhere and the loop will restart.

If they appear, I want to know which image appears in which region so if i print the result it would come as something like:
image02, image18. <-thats my problem, after getting the first image, it wont give the second image (or value, etc) it just keep going with the loop. so i just want to know which image appears in which region... ( Im feeling a little dumb right now, I imagine this can be achieved in a simple way that I just don't get)

I could think in two possibilities but the are far from elegant:

1 - search the two regions as one region.
The problem here is that the area now is much bigger and the sequence of images that were only 20 becomes (20*20) so I would need 400 images (they do repeat in both regions)to check in a very large region.

2 - search for one image in one region than search for the second image based on the return of the first image, like if the image on region 1 is image1-10 search for images 5-15 on region 2. Since i have very different tasks for each result, this would become a very large if-elif-else...

basically i just want to know which image appears in which region, depending on the result it would do this or that.

Revision history for this message
RaiMan (raimund-hocke) said :
#8

ok, then the easiest way is to search in both regions one after the other.

na = existsAny(regionA, imagelist)
nb = existsAny(regionB, imagelist)

now you have na and nb to make your decisions.

If you want to get faster:
- make the regions as small as possible
- delegate the search for each regions in threads

--- this would become a very large if-elif-else...
If there are many decisions to make, then the construct gets large by design ;-)

Can you help with this problem?

Provide an answer of your own, or ask Luiz for more information if necessary.

To post a message you must log in.