Commit 3e89216d authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add unittest for the 2 detections

parent 336a42c4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -14,3 +14,4 @@ testtools>=2
where>=1.0.2
responses>=0.20.0
pytest-image-diff>=0.0.11
matplotlib
+0 −0

Empty file added.

+34 −0
Original line number Diff line number Diff line
import pytest

from imgutils.detect.face import _open_face_detect_model, detect_faces
from test.testings import get_testfile


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


@pytest.mark.unittest
class TestDetectFace:
    def test_detect_faces(self):
        detections = detect_faces(get_testfile('genshin_post.jpg'))
        assert len(detections) == 4

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

        assert values == pytest.approx([
            ((202, 155, 356, 294), 0.878),
            ((938, 87, 1121, 262), 0.846),
            ((652, 440, 725, 514), 0.839),
            ((464, 250, 535, 326), 0.765)
        ])

    def test_detect_faces_none(self):
        assert detect_faces(get_testfile('png_full.png')) == []
+35 −0
Original line number Diff line number Diff line
import pytest

from imgutils.detect.person import _open_person_detect_model, detect_person
from test.testings import get_testfile


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


@pytest.mark.unittest
class TestDetectPerson:

    def test_detect_person(self):
        detections = detect_person(get_testfile('genshin_post.jpg'))
        assert len(detections) == 4

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

        assert values == pytest.approx([
            ((371, 232, 564, 690), 0.753),
            ((30, 135, 451, 716), 0.678),
            ((614, 393, 830, 686), 0.561),
            ((614, 3, 1275, 654), 0.404)
        ])

    def test_detect_person_none(self):
        assert detect_person(get_testfile('png_full.png')) == []
+23 −0
Original line number Diff line number Diff line
import pytest

from imgutils.data import load_image
from imgutils.detect import detection_visualize
from test.testings import get_testfile


@pytest.mark.unittest
class TestDetectVisual:
    def test_detection_visualize(self, image_diff):
        image = get_testfile('genshin_post.jpg')
        visual = detection_visualize(image, [
            ((202, 155, 356, 294), 'first', 0.878),
            ((938, 87, 1121, 262), 'second', 0.846),
            ((652, 440, 725, 514), 'third', 0.839),
            ((464, 250, 535, 326), 'fourth', 0.765)
        ], fontsize=24)

        assert image_diff(
            load_image(get_testfile('genshin_post_face_visual.jpg'), mode='RGB'),
            visual.convert('RGB'),
            throw_exception=False
        ) < 1e-2
Loading