def match_image(image,element): try: if lambda: match(image,match_parameters=stbt.MatchParameters(match_method="ccoeff-normed", confirm_method="none")): return match(image,match_parameters=stbt.MatchParameters(match_method="ccoeff-normed", confirm_method="none")) else: assert False, elementName+" is not visible on the Screen" except Exception,e: failMessage(e.args) raise e
def check(self, picture, picture_two=None): i = 0 while i < 5: if picture_two is None: if stbt.match(picture, None, stbt.MatchParameters(None, None, None, 0.7)): return True else: if (stbt.match(picture, None, stbt.MatchParameters(None, None, None, 0.7)) and stbt.match(picture_two, None, stbt.MatchParameters(None, None, None, 0.7))): return True i += 1 return False
def wait_for_element(image,elementName,timeout_secs): try: if wait_until(lambda: match(image,match_parameters=stbt.MatchParameters(match_method="ccoeff-normed", confirm_method="none")),timeout_secs=timeout_secs): passMessageWith_Screenshots(elementName+" is visible on the Screen") else: failMessageWith_Screenshots(elementName+" is not visible on the Screen") assert False, elementName+" is not visible on the Screen" except Exception,e: failMessage(e.args) raise e
def test_pyramid_roi_too_small(frame, image, match_method, match_threshold): # This is a regression test for an error that was seen with a particular # frame from a single test-run, with SQDIFF_NORMED: # cv2.error: (-215) _img.size().height <= _templ.size().height && # _img.size().width <= _templ.size().width in function matchTemplate with scoped_debug_level(2): stbt.match(image, frame=stbt.load_image(frame), match_parameters=stbt.MatchParameters( match_method=match_method, match_threshold=match_threshold))
def validate(video, driver, validate_match=True): import stbt driver.show(video) colours = STANDARD_COLOURS[video] pristine = svg_to_array(generate_letters_svg(*colours)) # Attempt to wait until correct video is showing. try: # environment variable controls the timeout here as with the # fake-video-source with the parallel test runner 10s can not be enough timeout = int(os.environ.get('STBT_TEST_VALIDATION_WAIT_TIMEOUT', 10)) stbt.wait_for_match(pristine[80 * 3:80 * 6, 80 * 6:80 * 10], timeout_secs=timeout) except stbt.MatchTimeout: pass res = [] for square, char in zip(SQUARES, GLYPHS): pos = square_to_pos(square) template = pristine[pos.y:pos.y + 80, pos.x:pos.x + 80] result = (stbt.detect_match( template, match_parameters=stbt.MatchParameters(match_method="ccoeff-normed")) .next()) sys.stdout.write( "%s%s" % ([FAIL, WARNING, OKGREEN][rate(square, result)], char)) if square.x == 15: sys.stdout.write('\n') sys.stdout.flush() res.append((square, char, result)) sys.stdout.write(ENDC) sys.stdout.write('\n\n') for square, char, result in res: expected = square_to_pos(square) off = Coord(result.position[0] - expected.x, result.position[1] - expected.y) sys.stdout.write( "%s%s" % ([FAIL, WARNING, OKGREEN][rate(square, result)], off_to_arrow(off))) if square.x == 15: sys.stdout.write('\n') sys.stdout.write(ENDC) if validate_match: sys.stdout.write('\n\n') for square, char, result in res: if result.match: rating = 2 else: rating = 1 if result.first_pass_result > 0.9 else 0 quality = "0123456789"[int(result.first_pass_result * 10)] sys.stdout.write( "%s%s" % ([FAIL, WARNING, OKGREEN][rating], quality)) if square.x == 15: sys.stdout.write('\n') sys.stdout.write(ENDC) is_bad = 0 for square, char, result in res: good = rate(square, result) == 2 if validate_match: good = good and result.match if not good: if is_bad == 0: is_bad = 1 sys.stdout.write('\nChar\tPos\tOffset\tDist\tMatch\n' + '-' * 40 + '\n') expected = square_to_pos(square) off = Coord(result.position[0] - expected.x, result.position[1] - expected.y) sys.stdout.write('%s%s\t(%i, %i)\t(%i, %i)\t%02f\t%s\n' % ([FAIL, WARNING, OKGREEN][rate(square, result)], char, square.x, square.y, off.x, off.y, distance(expected, result.position), "MATCH" if result.match else "NO MATCH")) sys.stdout.write(ENDC) sys.stdout.flush() return is_bad
def mp(match_method=stbt.MatchMethod.SQDIFF, match_threshold=None, **kwargs): if match_threshold is None and match_method != stbt.MatchMethod.SQDIFF: match_threshold = 0.8 return stbt.MatchParameters(match_method, match_threshold, **kwargs)
def main(): parser = argparse.ArgumentParser() parser.prog = "stbt match" parser.description = """Run stbt's image-matching algorithm against a single frame (which you can capture using `stbt screenshot`).""" parser.add_argument( "-v", "--verbose", action="store_true", help="Dump image processing debug images to ./stbt-debug directory") parser.add_argument("--all", action="store_true", help='Use "stbt.match_all" instead of "stbt.match"') parser.add_argument("source_file", help="""The screenshot to compare against (you can capture it using 'stbt screenshot')""") parser.add_argument("reference_file", help="The image to search for") parser.add_argument( "match_parameters", nargs="*", help="""Parameters for the image processing algorithm. See 'MatchParameters' in the stbt API documentation. For example: 'confirm_threshold=0.70')""") args = parser.parse_args(sys.argv[1:]) mp = {} try: for p in args.match_parameters: name, value = p.split("=") if name == "match_method": mp["match_method"] = value elif name == "match_threshold": mp["match_threshold"] = float(value) elif name == "confirm_method": mp["confirm_method"] = value elif name == "confirm_threshold": mp["confirm_threshold"] = float(value) elif name == "erode_passes": mp["erode_passes"] = int(value) else: raise Exception("Unknown match_parameter argument '%s'" % p) except Exception: # pylint:disable=broad-except error("Invalid argument '%s'" % p) source_image = cv2.imread(args.source_file) if source_image is None: error("Invalid image '%s'" % args.source_file) with (_stbt.logging.scoped_debug_level(2) if args.verbose else noop_contextmanager()): match_found = False for result in stbt.match_all( args.reference_file, frame=source_image, match_parameters=stbt.MatchParameters(**mp)): print( "%s: %s" % ("Match found" if result else "No match found. Closest match", result)) if result: match_found = True if not args.all: break sys.exit(0 if match_found else 1)
def is_visible(self): ignore_text = stbt.MatchParameters(confirm_method="none") return stbt.match("images/roku/menu-selection-background.png", frame=self._frame, region=self.selection_region, match_parameters=ignore_text)