Commit 10aa37d0 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add unittest

parent 61310919
Loading
Loading
Loading
Loading
+0 −2803

File deleted.

Preview size limit exceeded, changes collapsed.

+5 −3
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ def _get_bounding_box_of_text(image: ImageTyping, model: str, threshold: float)


def detect_text(image: ImageTyping, model: str = _DEFAULT_MODEL, threshold: float = 0.05,
                max_area_size: Optional[int] = 1200):
                max_area_size: Optional[int] = 640):
    """
    Detect text regions in the given image using the specified model and threshold.

@@ -117,8 +117,8 @@ def detect_text(image: ImageTyping, model: str = _DEFAULT_MODEL, threshold: floa
    :type model: str
    :param threshold: Confidence threshold for text detection.
    :type threshold: float
    :param max_area_size: Max area size when doing inference. Default is ``1200``, which means if
        the image's area is over 1200x1200, it will be resized. When assigned to ``None``,
    :param max_area_size: Max area size when doing inference. Default is ``640``, which means if
        the image's area is over 640x640, it will be resized. When assigned to ``None``,
        it means do not resize in any case.
    :type max_area_size: Optional[int]
    :return: List of detected text bounding boxes, labels, and scores.
@@ -136,4 +136,6 @@ def detect_text(image: ImageTyping, model: str = _DEFAULT_MODEL, threshold: floa
    for (x0, y0, x1, y1), score in _get_bounding_box_of_text(image, model, threshold):
        x0, y0, x1, y1 = int(x0 * r), int(y0 * r), int(x1 * r), int(y1 * r)
        bboxes.append(((x0, y0, x1, y1), 'text', score))

    bboxes = sorted(bboxes, key=lambda x: x[2], reverse=True)
    return bboxes
+49 −0
Original line number Diff line number Diff line
import pytest

from imgutils.detect.text import _open_text_detect_model, detect_text
from test.testings import get_testfile


@pytest.fixture(scope='module', autouse=True)
def _release_model_after_run():
    try:
        yield
    finally:
        _open_text_detect_model.cache_clear()


@pytest.mark.unittest
class TestDetectText:
    def test_detect_text(self):
        detections = detect_text(get_testfile('ml1.png'))
        assert len(detections) == 4

        values = []
        for bbox, label, score in detections:
            assert label in {'text'}
            values.append((bbox, int(score * 1000) / 1000))

        assert values == pytest.approx([
            ((866, 45, 959, 69), 0.543),
            ((222, 68, 313, 102), 0.543),
            ((424, 82, 508, 113), 0.541),
            ((691, 101, 776, 129), 0.471)
        ])

    def test_detect_text_without_resize(self):
        detections = detect_text(get_testfile('ml2.jpg'), max_area_size=None)
        assert len(detections) == 9

        values = []
        for bbox, label, score in detections:
            assert label in {'text'}
            values.append((bbox, int(score * 1000) / 1000))

        assert values == pytest.approx([
            ((360, 218, 474, 250), 0.686), ((119, 218, 203, 240), 0.653), ((392, 47, 466, 76), 0.617),
            ((593, 174, 666, 204), 0.616), ((179, 451, 672, 472), 0.591), ((633, 314, 747, 337), 0.59),
            ((392, 369, 517, 386), 0.589), ((621, 81, 681, 102), 0.566), ((209, 92, 281, 122), 0.423),
        ])

    def test_detect_text_none(self):
        assert detect_text(get_testfile('png_full.png')) == []

test/testfile/ml1.png

0 → 100644
+32.4 KiB
Loading image diff...

test/testfile/ml2.jpg

0 → 100644
+75.2 KiB
Loading image diff...