Takes long time to find picture - using while and exists

Asked by Danielo

This makes me go crazy, seriosly.

This code stops working proprerly after removing some lines.

The problem is that the function

lucha.luchar() doesn't starts.

Every script works well standalone, but it looks like imposible to make it to work together.

As I said, it works with only one import, but after the second import, it stop working, and i can't understand why.

Here is the code

myScriptPath = "E:\My Dropbox\Sikuli"

# all systems
if not myScriptPath in sys.path: sys.path.append(myScriptPath)

import lucha
#import dofus_login

mapa=
ejecuciones=0
level_up=
lucha.luchar()
while True:
# if exists(dofus_login.passw,2):
# dofus_login.login()
 click( )
 sleep(0.2)
 segar=exists( )
 if segar:
  click( )
 if (ejecuciones % 15)==0:
  sleep(5)
  lucha.mata( )
 else:
  sleep(8)

 if exists(level_up):
  click(level_up)
 ejecuciones+=1

 if mapa.exists( ):
  lucha.luchar()
  mapa.getLastMatch().highlight()

 if exists(lucha.cerrar):
  click(getLastMatch())

All the pictures are perfectly founded, but the scripts does't starts. WHY?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Danielo (rdanielo-gmail) said :
#1

Here is the last version, wich perfectly works despite one picture, that takes a very loooooooong time to find.

from sikuli.Sikuli import *
myScriptPath = "E:\My Dropbox\Sikuli"

# all systems
if not myScriptPath in sys.path: sys.path.append(myScriptPath)

import lucha
ejecuciones=0
level_up=Pattern("Campeslnopas.png").targetOffset(-2,21)

with Region(0,40,1053,841):

  while True:
     click(Pattern("U.png").similar(0.00))
     sleep(0.2)
     segar=exists("Segar.png")
     if segar:
      click("Segar.png")
      if (ejecuciones % 15)==0:
       sleep(5)
       lucha.mata( )
      else:
       sleep(8)

     if exists(level_up):
      click(level_up)
     ejecuciones+=1

     if exists(lucha.pasar):
      lucha.luchar()

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

--- 1. indentation problems
since Sikuli does not have an indentation support like other editors or IDE's it is rather easy, to mix up indentation by blanks and by tabs, without being aware (you do not see them, because there is no option to show "not visible" characters like blank or tab)
Since an indentation only has to be consistent within it's scope, the problem arises only, when differently indented lines come together by e.g. deleting lines or copy/move them elsewhere - and you still do not see it :-(

Conclusion: In the Sikuli IDE use TAB/Shift-TAB to indent/dedent (at least this is supported for a group of lines :-).
NEVER USE BLAKS TO INDENT.
When using external editors in parallel, take care, that indentation is based on "real" tabs and that they are preserved all the time.

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

--- 2. loooooooong time
if you look through your solution, the following aspects might add to "loooooooong time":

--- large region:
you use a relatively large region. If you are running on an average machine, each find might cost you up to one second. So to speed up, restrict your search to a smaller region.
e.g. I guess image "level_up" is in a specific area inside the app window:
buttonArea = Region(....)
if buttonArea.exists(level_up):

--- time cost of not found
you have 3 exists in your loop.
In the case, that none of them are found, it takes you about 10 seconds, before the loop is repeated.
So when using exists to make decisions in the workflow and especially in a case construct like yours, I always use exists(img, 0), since in this case it always (found or not found) takes "only" one search try.

--- sequence of exists()
if you have some exists() on the same level (like in your case), the subsequent exists are only visited, if the ones before fail. So (I do not know your game logic :-) in your case it might last up to 20 seconds, before the second or third exists() are executed successfully.
This normally is the situation, where observe() comes in, since the searches are processed in parallel (wait for X 1.0rc2, before really trying it)
But there is an ugly looking, but working solution with exists():
while True:
 case1 = False
 case2 = False
 case3 = False
 if exists(imgCase1, 0): case1 = True
 if exists(imgCase2, 0): case2 = True
 if exists(imgCase3, 0): case3 = True
 # you get here latest after 3 seconds

now you can play with your 3 cases and make some combined decisions like:
if case1 and not case2:

--- One more thing:
You are using:
with Region(0,40,1053,841):

this might be ok for testing purposes, but not for a "productive script" ;-)

look at the new features in the App class, that gives you the region of an application window.

Revision history for this message
Danielo (rdanielo-gmail) said :
#4

Ok, i will try to use less time for the exists sentences. You're right that are some smaller areas than the main one, but for the main purpose of the i need that whole area.

I've readed that sikuli have problems with images that are more text than image. I have one case where one button is mainly text, i don't know if this is important. The current area for search that button is (IMO) relatively small, and it takes more time to find it than the entire script to run once or twice.

The secuence of exist have this purpose:
 Only the next image should be found if the first one doesn't exist. That are spells, and when you use one, it dissapears from the screen, and then the next spell shoud be used. But i will try your solution instead.

Here is the second script. The button called cerrar is what have problems founding it, and cerr is the region where it should be founded.

from sikuli.Sikuli import *
reg = Region(6,28,1061,850)
poderes=Region(584,762,467,87)
cerr=Region(36,412,998,297)

bichos=[Picture,picture,picture,picture]#
turno=Pattern("1294957943970.png").similar(0.68)
garra="1294666099451.png"
bicho="1294667073611.png"
jalato=Pattern("Il.png").similar(0.91)
bendicion=Pattern("1294681487072.png").similar(0.84)
listo="5I.png"
pasar="4.png"
cerrar=Pattern("Cerrar.png").similar(0.96)
casilla=Pattern("1294669287674.png").similar(0.76)

def dale(obj):
 if reg.exists(obj,1):click(reg.getLastMatch())

def mata( ):
 sleep(4)
 for bug in bichos:
  if reg.exists(bug,0):
   print "aparecio uno"
   sleep(7)
   click(reg.getLastMatch())
   sleep(4)
   if reg.exists(listo):
    luchar()
    break

def luchar():
 dale(listo)
 while reg.exists(pasar):
  if exists(jalato):
   click(getLastMatch())
   dale(casilla)
  else:
   if exists(bendicion):
    click(getLastMatch())
    dale(turno)
   else:
    if exists(bicho):
     click(getLastMatch())
     dale(casilla)
    else:
     for b in bichos:
      if exists(b):
       target=getLastMatch()
       dale(garra)
       dale(target)
       mouseMove(Location(100,100))

  dale(pasar)
 exists(turno,7)
 print "*************************Batalla finalizada"
 cerr.exists(cerrar,FOREVER)
 click(cerr.getLastMatch())

I am using the tab and shift+tab for indent, and the problems appears despite of it.
Some important thing, I think it sould be in the next sikuli ide is the posibility to undo. Is very frustating to make a little mistake and have to rewrite it.

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

--- 1. undo
absolutely right: bug 538775 - add yourself as effected
That and debugging is why I work with NetBeans.

--- 2. regions with much text
this problem is solved with Sikuli X

--- 3. cerr.exists(cerrar,FOREVER)
this is critical, since it might hang your script.
you should make some tests with different captures.
Is there some visual effect in the button?
again: I guess this button has a fixed position to something else in the app window: use a calculated location.

Revision history for this message
Danielo (rdanielo-gmail) said :
#6

Here is the picture of the button

http://dl.dropbox.com/u/3257335/Cerrar.png

In an standalone script with only this instruction, it works perfeclty.

the cerr region is about 1,3 bigger than the button, so I think this not should be a problem.

I think the button is perfectly normal, isn't it?

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

the button image looks perfect ;-)

you could do the following, to be sure it really searches a long time for that button:

 print "*************************Batalla finalizada"
 start = time.time()
 cerr.exists(cerrar,FOREVER)
 print "time needed to find button cerrar:", time.time()-start
 click(cerr.getLastMatch())

Revision history for this message
Danielo (rdanielo-gmail) said :
#8

This is unreal, here is the output

time needed to find button cerrar: 0.0940001010895

But it takes more than that, i thik it took like 5 or 8 seconds.

What could be happening?

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

no, not at all. you have a speedy machine, isn't it?

On my MacPro
 Prozessortyp: Dual-Core Intel Xeon
  Speed: 2,66 GHz
  Anzahl der Prozessoren: 2
  Cores: 4
  L2-Cache (pro Prozessor): 4 MB
  RAM: 7 GB
  Busspeed: 1,33 GHz

e.g.
# searching a desktop icon in
Region[1070,76 833x749]@Screen(0) E:Y, T:3,0
0.221999883652

Conclusion:
Your "long time problem" is somewhere else.

Revision history for this message
Danielo (rdanielo-gmail) said :
#10

Yes, i have a speedy machine.
At least i used a fixed location for the button, and some really small areas to find most of the pictures. Thanks to your tips now is fast has hell, Is fasther than any human can be. Some times i can't see what is happening.

But of course i found new problems.

As is wrote before, when I import another script (currently I use one import) every call to the other scripts functions stop working. But is more than that, even if i delete the new import, and all the related functions, it still not working. But, (and here cames the x-files thing) if I restore the previous version with dropbox (thanks to God) it works perfectly again.

If you want to, or as you consider, I can post here the code of the 3 includes of i want to use, and i will tell you what combinations works, and what combinations doesn't.

Of course, every scrits works perfect one by one (standalone).

Thank for all the helped given since this moment.

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

pls. set to solved

Revision history for this message
Danielo (rdanielo-gmail) said :
#12

Thanks RaiMan, that solved my question.