File size: 2,888 Bytes
f90824a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""MTCNN two-pass recall behaviour."""

from PIL import Image

import core.models as models

_LANDMARKS = [20.0, 25.0, 15.0, 20.0, 25.0, 20.0, 20.0, 30.0, 30.0, 30.0]
_BOX = [10.0, 10.0, 50.0, 50.0, 0.5]


class FakeDetector:
    def __init__(self, passes):
        self.min_face_size = 20
        self.thresholds = [0.6, 0.7, 0.7]
        self.nms_thresholds = [0.7, 0.7, 0.7]
        self.factor = 0.85
        self._passes = list(passes)
        self.calls: list[dict] = []

    def detect_faces(self, image, min_face_size, thresholds, nms_thresholds, factor):
        self.calls.append({
            "min_face_size": min_face_size,
            "thresholds": list(thresholds),
            "nms_thresholds": list(nms_thresholds),
            "factor": factor,
        })
        return self._passes.pop(0)


def _image():
    return Image.new("RGB", (64, 64), (128, 128, 128))


def test_detect_faces_retries_with_relaxed_settings(monkeypatch):
    detector = FakeDetector([
        ([], []),
        ([_BOX], [_LANDMARKS]),
    ])
    monkeypatch.setattr(models, "load_face_detector", lambda: detector)
    monkeypatch.setattr(models, "MTCNN_RETRY_MIN_FACE_SIZE", 12)
    monkeypatch.setattr(models, "MTCNN_RETRY_CONFIDENCE", 0.45)

    faces = models.detect_faces(_image(), confidence_threshold=0.6)

    assert len(faces) == 1
    assert faces[0]["score"] == 0.5
    assert len(detector.calls) == 2
    assert detector.calls[1]["min_face_size"] == 12
    assert detector.calls[1]["thresholds"] == [0.45, 0.45, 0.45]


def test_detect_faces_does_not_retry_when_first_pass_matches(monkeypatch):
    detector = FakeDetector([
        ([[10.0, 10.0, 50.0, 50.0, 0.9]], [_LANDMARKS]),
    ])
    monkeypatch.setattr(models, "load_face_detector", lambda: detector)
    monkeypatch.setattr(models, "MTCNN_RETRY_MIN_FACE_SIZE", 12)
    monkeypatch.setattr(models, "MTCNN_RETRY_CONFIDENCE", 0.45)

    faces = models.detect_faces(_image(), confidence_threshold=0.6)

    assert len(faces) == 1
    assert len(detector.calls) == 1


def test_detect_faces_returns_empty_after_two_failed_passes(monkeypatch):
    detector = FakeDetector([([], []), ([], [])])
    monkeypatch.setattr(models, "load_face_detector", lambda: detector)
    monkeypatch.setattr(models, "MTCNN_RETRY_MIN_FACE_SIZE", 12)
    monkeypatch.setattr(models, "MTCNN_RETRY_CONFIDENCE", 0.45)

    assert models.detect_faces(_image(), confidence_threshold=0.6) == []
    assert len(detector.calls) == 2


def test_detect_faces_skips_retry_when_not_more_permissive(monkeypatch):
    detector = FakeDetector([([], [])])
    monkeypatch.setattr(models, "load_face_detector", lambda: detector)
    monkeypatch.setattr(models, "MTCNN_RETRY_MIN_FACE_SIZE", 20)
    monkeypatch.setattr(models, "MTCNN_RETRY_CONFIDENCE", 0.7)

    assert models.detect_faces(_image(), confidence_threshold=0.6) == []
    assert len(detector.calls) == 1