a function to find with lower similarity (code snippet)

Asked by eliteSKL

Basically a wrapper for an auto de-focus method. I wanted to pass in an image and have a method automatically adjust it's percent of recognition. figured i would share it and see what comments and suggestions i would get. One i have though of right now just stitting here i would like to pass the gui's own region method as is with out having to key in the actual numbers.

class unFocus():
 def target(self, image, percentmatch=0.70, regionlist=[0,0,0,0], offsetclick=[0,0]):
  """This method aquires an object. It gives four chances to find and image starting with 70 percent match and degrading by 10 percent each loop.
  The regionlist variable is optional, if you don't supply cordinates it will default with the screen cordinates. The off center click is optional too"""
  if regionlist==[0,0,0,0]:
   regionlist=[getBounds()]
  cannotfind = 0
  while True:
   if Region(*regionlist).exists(Pattern(image).similar(percentmatch).targetOffset(*offsetclick)):
    return Region(*regionlist).exists(Pattern(image).similar(percentmatch).targetOffset(*offsetclick))
   else:
    if cannotfind ==4:
     return False
     #return popup("Error:\nImage "+image+" not found!")
    percentmatch = percentmatch-10
    cannotfind = cannotfind+1

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
Best RaiMan (raimund-hocke) said :
#1

For this solution a class is not really needed (only if you want to extend it further with more behavior).

so a function would be sufficient:

def unfocus(obj, sim=0.7, reg=SCREEN):
 to = Location(0,0)
 if obj.__class__.__name__ == "Pattern":
  to = obj.getTargetOffset()
 for i in range(4):
  if reg.exists(Pattern(obj).similar(sim).targetOffset(to.x, to.y),0):
   return reg.getLastMatch()
  sim -= 0.1
 return false

this solution:
- takes any Region-like object, if omitted searches in SCREEN
- the search object can either be an image or a Pattern
- does not wait 3 seconds with every search (back latest after 3-4 seconds instead of 12 -15)
- does not search again if found

comment:
the first 3 lines in the def() are only necessary, because Pattern(some_other_pattern) does not preserve a targetOffset.

one more thing (for the developers ;-)
Having a class Pattern, that preserves the targetOffset, the def() would be:

def unfocus(obj, sim=0.7, reg=SCREEN):
 for i in range(4):
  if reg.exists(Pattern(obj).similar(sim)):
   return reg.getLastMatch()
  sim -= 0.1
 return false

class Pattern is really poor currently:
- should preserve the attributes when based on another Pattern
- should accept a Location object as targetOffset

Revision history for this message
eliteSKL (camaro70s) said :
#2

thanks RaiMan! i knew if i posted up my code i could get a review and get a better answer. I hope others will find use for this unfocus code snippet. i needed this method for a task i was working on. I'm trying to get better at writing code. LOL!l my snippet is scary compared to yours.

thanks again!

Revision history for this message
eliteSKL (camaro70s) said :
#3

Thanks RaiMan, that solved my question.

Revision history for this message
eliteSKL (camaro70s) said :
#4

i found out during debugging that you where missing an "else:" in the second from bottom line. it works great though.

def unfocus(obj, sim=0.7, reg=SCREEN):
 to = Location(0,0)
 if obj.__class__.__name__ == "Pattern":
  to = obj.getTargetOffset()
 for i in range(4):
  if reg.exists(Pattern(obj).similar(sim).targetOffset(to.x, to.y),0):
   return reg.getLastMatch()
  sim -= 0.1
else:
 return false

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

No, this additional else should give a syntax error.

The <return False> happens in my script, when the loop is finished (nothing found). If it is found, the function returns from inside the loop. So there is no else needed.

Again my original script with 4 blanks per indentation level:

def unfocus(obj, sim=0.7, reg=SCREEN):
    to = Location(0,0)
    if obj.__class__.__name__ == "Pattern":
        to = obj.getTargetOffset()
    for i in range(4):
        if reg.exists(Pattern(obj).similar(sim).targetOffset(to.x, to.y),0):
            return reg.getLastMatch()
        sim -= 0.1
    return false

Revision history for this message
eliteSKL (camaro70s) said :
#6

your right. I had it incorrectly tabbed.