def test_match_region2(x, y, offset_x, offset_y, width, height, frame_width, frame_height): if x >= 0: x = x + offset_x else: x = frame_width - width - offset_x if y >= 0: y = y + offset_y else: y = frame_height - height - offset_y region = stbt.Region(x, y, width=width, height=height) image = numpy.ones((region.height, region.width, 3), numpy.uint8) * 255 image[5:-5, 5:-5] = (255, 0, 0) frame = black(frame_width, frame_height) frame[region.to_slice()] = image[ 0 if region.y >= 0 else -region.y:region. height if region.bottom <= frame_height else frame_height - region.y, 0 if region.x >= 0 else -region.x:region. width if region.right <= frame_width else frame_width - region.x] * .85 # N.B. .85 is the lowest at which all the tests still passed when I # disabled the pyramid optimisation. should_match = _image_region(frame).contains(region) m = stbt.match(image, frame) if should_match != m.match or os.environ.get("STBT_DEBUG"): with scoped_debug_level(2): stbt.match(image, frame) assert should_match == m.match if should_match: assert m.region == region
def is_screen_black(self, frame=None, mask=None, threshold=None, region=Region.ALL): if threshold is None: threshold = get_config('is_screen_black', 'threshold', type_=int) if frame is None: frame = self.get_frame() if mask is None: mask = _ImageFromUser(None, None, None) else: mask = _load_image(mask, cv2.IMREAD_GRAYSCALE) _region = Region.intersect(_image_region(frame), region) greyframe = cv2.cvtColor(crop(frame, _region), cv2.COLOR_BGR2GRAY) if mask.image is not None: cv2.bitwise_and(greyframe, mask.image, dst=greyframe) maxVal = greyframe.max() if logging.get_debug_level() > 1: imglog = logging.ImageLogger("is_screen_black") imglog.imwrite("source", frame) if mask.image is not None: imglog.imwrite('mask', mask.image) _, thresholded = cv2.threshold(greyframe, threshold, 255, cv2.THRESH_BINARY) imglog.imwrite('non-black-regions-after-masking', thresholded) result = _IsScreenBlackResult(bool(maxVal <= threshold), frame) debug("is_screen_black: {found} black screen using mask={mask}, " "threshold={threshold}, region={region}: " "{result}, maximum_intensity={maxVal}".format( found="Found" if result.black else "Didn't find", mask=mask.friendly_name, threshold=threshold, region=region, result=result, maxVal=maxVal)) return result