Exemple #1
0
def wait(targets,
         timeout=...,
         logger=None,
         threshold=None,
         can_trace=True,
         trace_frame=0):
    trace_frame += 1
    if isinstance(targets, str):
        targets = (targets, )
    if timeout is ...:
        timeout = config.get("utils:default-wait-timeout")
    target_names = ", ".join(targets)
    tm = time.time()
    while 1:
        sample = client.get_sample()
        for t in targets:
            match = templates[t].find(sample=sample, threshold=threshold)
            if match:
                if can_trace:
                    trace.trace("<done>",
                                sample,
                                match,
                                trace_frame=trace_frame)
                return match.set_logger(logger)
        if timeout is not None and (time.time() - tm) > timeout:
            match = NoMatch(targets).set_logger(logger)
            if can_trace:
                trace.trace("<timeout>",
                            sample,
                            match,
                            trace_frame=trace_frame)
            return match
        if logger and tm - time.time() > 2.:
            logger.info("waiting [%s]", target_names, extra={"rate": 1 / 5})
        client.new_sample()
Exemple #2
0
 def find_all(self, sample=None, threshold=None):
     threshold = threshold or settings.IMAGE_SEARCH_TRHESHOLD
     if sample is None:
         sample = client.get_sample()
     res = cv2.matchTemplate(sample, self.img, cv2.TM_CCOEFF_NORMED)
     loc = np.where(res >= threshold)
     return self.without_intersections(zip(*loc[::-1]))
Exemple #3
0
 def match(self, sample=None):
     if sample is None:
         sample = client.get_sample()
     res = cv2.matchTemplate(sample, self.img, cv2.TM_CCOEFF_NORMED)
     if len(res):
         _, coef, _, _ = cv2.minMaxLoc(res)
         return coef
     return 0.0
Exemple #4
0
 def find(self, sample=None, threshold=None):
     threshold = threshold or settings.IMAGE_SEARCH_TRHESHOLD
     if sample is None:
         sample = client.get_sample()
     res = cv2.matchTemplate(sample, self.img, cv2.TM_CCOEFF_NORMED)
     if len(res):
         _, coef, _, match = cv2.minMaxLoc(res)
         if coef >= threshold:
             width, height = self.img.shape[::-1]
             left, top = match
             return Match(self.name, left, top, width, height)
Exemple #5
0
def reshaped_sample(left=0, top=0, right=0, bottom=0, sample=None):
    assert 0 <= left <= 1
    assert 0 <= top <= 1
    assert 0 <= right <= 1
    assert 0 <= bottom <= 1
    if sample is None:
        sample = client.get_sample()
    w, h = sample.shape[::-1]
    left = int(w * left)
    right = int(w * (1 - right))
    top = int(h * top)
    bottom = int(h * (1 - bottom))
    return sample[top:bottom, left:right]
Exemple #6
0
def find(targets,
         logger=None,
         sample=None,
         threshold=None,
         can_trace=True,
         trace_frame=0):
    trace_frame += 1
    if isinstance(targets, str):
        targets = (targets, )
    if sample is None:
        sample = client.get_sample()
    for t in targets:
        match = templates[t].find(sample=sample, threshold=threshold)
        if match:
            if can_trace:
                trace.trace("<done>", sample, match, trace_frame=trace_frame)
            return match.set_logger(logger)
    return NoMatch(targets).set_logger(logger)
Exemple #7
0
def wait_while(targets,
               timeout=...,
               logger=None,
               threshold=None,
               can_trace=True,
               trace_frame=0):
    trace_frame += 1
    if isinstance(targets, str):
        targets = (targets, )
    if timeout is ...:
        timeout = config.get("utils:default-wait-timeout")
    tm = time.time()
    attempt = 1
    while 1:
        sample = client.get_sample()
        for t in targets:
            match = templates[t].find(sample=sample, threshold=threshold)
            if match:
                if logger and tm - time.time() > 2.:
                    logger.info("still can find [%s]",
                                match,
                                extra={"rate": 1 / 5})
                break
        else:
            if can_trace:
                match = NoMatch(targets)
                trace.trace("<done>", sample, match, trace_frame=trace_frame)
            return True
        if timeout is not None and (time.time() - tm) > timeout:
            if can_trace:
                trace.trace("<timeout>",
                            sample,
                            match,
                            trace_frame=trace_frame)
            return False
        client.new_sample()
        attempt += 1
Exemple #8
0
def get_sample_part(x, y, width, height, sample=None):
    if sample is None:
        sample = client.get_sample()
    return sample[y:y + height, x:x + width]