Skip to content

Commit

Permalink
dev(narugo): add unittest for booru_yolo
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Sep 11, 2024
1 parent 16e3887 commit 8b68489
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion imgutils/detect/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _get_bounding_box_of_text(image: ImageTyping, model: str, threshold: float)
return bboxes


@deprecated(deprecated_in="0.2.10", removed_in="0.5", current_version=__VERSION__,
@deprecated(deprecated_in="0.2.10", current_version=__VERSION__,
details="Use the new function :func:`imgutils.ocr.detect_text_with_ocr` instead")
def detect_text(image: ImageTyping, model: str = _DEFAULT_MODEL, threshold: float = 0.05,
max_area_size: Optional[int] = 640):
Expand Down
55 changes: 55 additions & 0 deletions test/detect/test_booru_yolo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
from PIL import Image

from imgutils.detect import detect_with_booru_yolo
from imgutils.detect.booru_yolo import _open_booru_yolo_model, _get_booru_yolo_labels
from ..testings import get_testfile


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


@pytest.fixture()
def nude_girl_file():
return get_testfile('nude_girl.png')


@pytest.fixture()
def nude_girl_image(nude_girl_file):
return Image.open(nude_girl_file)


@pytest.fixture()
def nude_girl_detection():
return [
((236, 1, 452, 247), 'head', 0.9584360718727112),
((211, 236, 431, 346), 'boob', 0.9300149083137512),
((62, 402, 427, 697), 'sprd', 0.8708215951919556)
]


@pytest.mark.unittest
class TestDetectBooruYOLO:
def test_detect_with_booru_yolo_file(self, nude_girl_file, nude_girl_detection):
detection = detect_with_booru_yolo(nude_girl_file)
assert [label for _, label, _ in detection] == \
[label for _, label, _ in nude_girl_detection]
for (actual_box, _, _), (expected_box, _, _) in zip(detection, nude_girl_detection):
assert actual_box == pytest.approx(expected_box)
assert [score for _, _, score in detection] == \
pytest.approx([score for _, _, score in nude_girl_detection], abs=1e-4)

def test_detect_with_booru_yolo_image(self, nude_girl_image, nude_girl_detection):
detection = detect_with_booru_yolo(nude_girl_image)
assert [label for _, label, _ in detection] == \
[label for _, label, _ in nude_girl_detection]
for (actual_box, _, _), (expected_box, _, _) in zip(detection, nude_girl_detection):
assert actual_box == pytest.approx(expected_box)
assert [score for _, _, score in detection] == \
pytest.approx([score for _, _, score in nude_girl_detection], abs=1e-4)

0 comments on commit 8b68489

Please sign in to comment.