# Path: _stbt/cv2_compat.py
# def find_contour_boxes(image, mode, method):
# def _fix_pre_3_2_rects(r):
# def find_contour_boxes(image, mode, method):
# FILLED = cv2.FILLED
# LINE_AA = cv2.LINE_AA
# FILLED = cv2.cv.CV_FILLED  # pylint: disable=c-extension-no-member
# LINE_AA = cv2.CV_AA  # pylint: disable=c-extension-no-member
# 
# Path: _stbt/imgutils.py
# def _image_region(image):
#     s = image.shape
#     return Region(0, 0, s[1], s[0])
# 
# Path: _stbt/logging.py
# @contextmanager
# def scoped_debug_level(level):
#     global _debug_level
#     oldlevel = _debug_level
#     _debug_level = level
#     try:
#         yield
#     finally:
#         _debug_level = oldlevel
# 
# Path: _stbt/match.py
# def _merge_regions(regions):
#     """Discard regions that are entirely contained within another region."""
#     regions.sort(key=lambda r: r.width * r.height)
#     i = len(regions) - 1
#     while i > 0:
#         r = regions[i]
#         for j in range(i - 1, -1, -1):
#             if r.contains(regions[j]):
#                 del regions[j]
#                 i -= 1
#         i -= 1
# 
# Path: tests/test_core.py
# def _find_file(path, root=os.path.dirname(os.path.abspath(__file__))):
#     return os.path.join(root, path)
# 
# Path: tests/test_ocr.py
# def requires_tesseract(func):
#     """Decorator for tests that require Tesseract to be installed."""
#     try:
#         _tesseract_version()
#     except:
#         raise SkipTest("tesseract isn't installed")
#     return func


import os
import random
import re
import timeit
import cv2
import numpy
import pytest
import stbt_core as stbt
from _stbt import cv2_compat
from _stbt.imgutils import _image_region
from _stbt.logging import scoped_debug_level
from _stbt.match import _merge_regions
from tests.test_core import _find_file
from tests.test_ocr import requires_tesseract
    from _stbt.match import _build_pyramid
def test_merge_regions():
    regions = [stbt.Region(*x) for x in [
        (153, 156, 16, 4), (121, 155, 25, 5), (14, 117, 131, 32),
        (128, 100, 19, 5), (122, 81, 22, 14), (123, 73, 5, 4),
        (0, 71, 12, 75), (146, 64, 1, 1), (111, 64, 10, 2), (22, 62, 9, 4),
        (0, 60, 17, 10), (111, 54, 2, 2), (138, 47, 5, 2), (132, 47, 3, 1),
        (130, 46, 1, 2), (55, 32, 11, 1), (52, 32, 1, 1), (0, 29, 50, 28),
        (0, 20, 57, 4), (33, 0, 233, 139)]]
    _merge_regions(regions)
    assert len(regions) == 9
    assert sorted(regions) == (
        [stbt.Region(*x) for x in [
            (0, 20, 57, 4), (0, 29, 50, 28), (0, 60, 17, 10), (0, 71, 12, 75),
            (14, 117, 131, 32), (22, 62, 9, 4), (33, 0, 233, 139),
            (121, 155, 25, 5), (153, 156, 16, 4)]])


@pytest.mark.parametrize("n", [20, 200, 2000])
def test_merge_regions_performance(n):
    random.seed(1)
    regions = []
    for _ in range(n):
        x = random.randint(0, 1280)
        y = random.randint(0, 720)
        right = random.randint(0, 1280)
        bottom = random.randint(0, 720)
        x, w = min(x, right), max(x, right) - min(x, right) + 1
        y, h = min(y, bottom), max(y, bottom) - min(y, bottom) + 1
        regions.append(stbt.Region(x, y, w, h))


# Path: _stbt/cv2_compat.py
# def find_contour_boxes(image, mode, method):
# def _fix_pre_3_2_rects(r):
# def find_contour_boxes(image, mode, method):
# FILLED = cv2.FILLED
# LINE_AA = cv2.LINE_AA
# FILLED = cv2.cv.CV_FILLED  # pylint: disable=c-extension-no-member
# LINE_AA = cv2.CV_AA  # pylint: disable=c-extension-no-member
# 
# Path: _stbt/imgutils.py
# def _image_region(image):
#     s = image.shape
#     return Region(0, 0, s[1], s[0])
# 
# Path: _stbt/logging.py
# @contextmanager
# def scoped_debug_level(level):
#     global _debug_level
#     oldlevel = _debug_level
#     _debug_level = level
#     try:
#         yield
#     finally:
#         _debug_level = oldlevel
# 
# Path: _stbt/match.py
# def _merge_regions(regions):
#     """Discard regions that are entirely contained within another region."""
#     regions.sort(key=lambda r: r.width * r.height)
#     i = len(regions) - 1
#     while i > 0:
#         r = regions[i]
#         for j in range(i - 1, -1, -1):
#             if r.contains(regions[j]):
#                 del regions[j]
#                 i -= 1
#         i -= 1
# 
# Path: tests/test_core.py
# def _find_file(path, root=os.path.dirname(os.path.abspath(__file__))):
#     return os.path.join(root, path)
# 
# Path: tests/test_ocr.py
# def requires_tesseract(func):
#     """Decorator for tests that require Tesseract to be installed."""
#     try:
#         _tesseract_version()
#     except:
#         raise SkipTest("tesseract isn't installed")
#     return func


# Path: tests/test_match.py
import os
import random
import re
import timeit
import cv2
import numpy
import pytest
import stbt_core as stbt
from _stbt import cv2_compat
from _stbt.imgutils import _image_region
from _stbt.logging import scoped_debug_level
from _stbt.match import _merge_regions
from tests.test_core import _find_file
from tests.test_ocr import requires_tesseract
    from _stbt.match import _build_pyramid

def test_merge_regions():
    regions = [stbt.Region(*x) for x in [
        (153, 156, 16, 4), (121, 155, 25, 5), (14, 117, 131, 32),
        (128, 100, 19, 5), (122, 81, 22, 14), (123, 73, 5, 4),
        (0, 71, 12, 75), (146, 64, 1, 1), (111, 64, 10, 2), (22, 62, 9, 4),
        (0, 60, 17, 10), (111, 54, 2, 2), (138, 47, 5, 2), (132, 47, 3, 1),
        (130, 46, 1, 2), (55, 32, 11, 1), (52, 32, 1, 1), (0, 29, 50, 28),
        (0, 20, 57, 4), (33, 0, 233, 139)]]
    _merge_regions(regions)
    assert len(regions) == 9
    assert sorted(regions) == (
        [stbt.Region(*x) for x in [
            (0, 20, 57, 4), (0, 29, 50, 28), (0, 60, 17, 10), (0, 71, 12, 75),
            (14, 117, 131, 32), (22, 62, 9, 4), (33, 0, 233, 139),
            (121, 155, 25, 5), (153, 156, 16, 4)]])


@pytest.mark.parametrize("n", [20, 200, 2000])
def test_merge_regions_performance(n):
    random.seed(1)
    regions = []
    for _ in range(n):
        x = random.randint(0, 1280)
        y = random.randint(0, 720)
        right = random.randint(0, 1280)
        bottom = random.randint(0, 720)
        x, w = min(x, right), max(x, right) - min(x, right) + 1
        y, h = min(y, bottom), max(y, bottom) - min(y, bottom) + 1
        regions.append(stbt.Region(x, y, w, h))


    times = timeit.repeat(lambda: _merge_regions(regions[:]),