From c0ee7474d63e47508048661b684efbb6968d14fe Mon Sep 17 00:00:00 2001
From: Tiago Freitas Pereira <tiagofrepereira@gmail.com>
Date: Fri, 11 Jun 2021 10:54:15 +0200
Subject: [PATCH] Fixed tinyface annotator

---
 bob/bio/face/annotator/bobiptinyface.py |  5 +-
 bob/bio/face/test/test_annotators.py    | 85 +++++++++++++++++--------
 2 files changed, 61 insertions(+), 29 deletions(-)

diff --git a/bob/bio/face/annotator/bobiptinyface.py b/bob/bio/face/annotator/bobiptinyface.py
index 9430985b..16e4a502 100644
--- a/bob/bio/face/annotator/bobiptinyface.py
+++ b/bob/bio/face/annotator/bobiptinyface.py
@@ -7,7 +7,7 @@ class BobIpTinyface(Base):
 
     def __init__(self, **kwargs):
         super(BobIpTinyface, self).__init__(**kwargs)
-        self.tinyface = bob.ip.facedetect.tinyface.TinyFacesDetector(prob_thresh=0.5)
+        self.detector = bob.ip.facedetect.tinyface.TinyFacesDetector(prob_thresh=0.5)
 
     def annotate(self, image, **kwargs):
         """Annotates an image using tinyface
@@ -25,6 +25,9 @@ class BobIpTinyface(Base):
             Annotations with (topleft, bottomright) keys (or None).
         """
 
+        # return the annotations for the first/largest face
+        annotations = self.detector.detect(image)
+
         if annotations is not None:
             return annotations[0]
         else:
diff --git a/bob/bio/face/test/test_annotators.py b/bob/bio/face/test/test_annotators.py
index f70d22b8..668c3ccc 100644
--- a/bob/bio/face/test/test_annotators.py
+++ b/bob/bio/face/test/test_annotators.py
@@ -4,34 +4,49 @@ import bob.io.image
 from bob.bio.face.annotator import (
     BobIpFacedetect,
     BobIpFlandmark,
-    min_face_size_validator)
+    min_face_size_validator,
+)
 from bob.bio.base.annotator import FailSafe
 from bob.bio.face.annotator import BobIpMTCNN
+from bob.bio.face.annotator import BobIpTinyface
 import numpy
 
 from bob.bio.base.test.utils import is_library_available
 
 face_image = bob.io.base.load(
-    bob.io.base.test_utils.datafile(
-        'testimage.jpg', 'bob.ip.facedetect'
-    )
+    bob.io.base.test_utils.datafile("testimage.jpg", "bob.ip.facedetect")
 )
 
+
 def _assert_mtcnn(annot):
     """
     Verifies that the MTCNN annotations are correct for ``faceimage.jpg``
     """
     assert type(annot) is dict, annot
-    assert [int(x) for x in annot['topleft']] == [68, 76], annot
-    assert [int(x) for x in annot['bottomright']] == [344, 274], annot
-    assert [int(x) for x in annot['reye']] == [180, 129], annot
-    assert [int(x) for x in annot['leye']] == [175, 220], annot
-    assert numpy.allclose(annot['quality'], 0.9998975), annot
+    assert [int(x) for x in annot["topleft"]] == [68, 76], annot
+    assert [int(x) for x in annot["bottomright"]] == [344, 274], annot
+    assert [int(x) for x in annot["reye"]] == [180, 129], annot
+    assert [int(x) for x in annot["leye"]] == [175, 220], annot
+    assert numpy.allclose(annot["quality"], 0.9998975), annot
+
+
+def _assert_tinyface(annot):
+    """
+    Verifies that the Tinyface annotations are correct for ``faceimage.jpg``
+    """
+
+    assert type(annot) is dict, annot
+    assert [int(x) for x in annot["topleft"]] == [59, 57], annot
+    assert [int(x) for x in annot["bottomright"]] == [338, 284], annot
+    assert [int(x) for x in annot["reye"]] == [162, 125], annot
+    assert [int(x) for x in annot["leye"]] == [162, 216], annot
+
 
 def _assert_bob_ip_facedetect(annot):
-    assert annot['topleft'] == (110, 82), annot
-    assert annot['bottomright'] == (334, 268), annot
-    assert numpy.allclose(annot['quality'], 39.209601948013685), annot
+    assert annot["topleft"] == (110, 82), annot
+    assert annot["bottomright"] == (334, 268), annot
+    assert numpy.allclose(annot["quality"], 39.209601948013685), annot
+
 
 @is_library_available("tensorflow")
 def test_mtcnn_annotator():
@@ -43,57 +58,71 @@ def test_mtcnn_annotator():
     annot_batch = mtcnn_annotator(batch)
     _assert_mtcnn(annot_batch[0])
 
+
+@is_library_available("mxnet")
+def test_tinyface_annotator():
+    """
+    The Tiny face annotator should return the correct annotations.
+    """
+    tinyface_annotator = BobIpTinyface()
+    batch = [face_image]
+    annot_batch = tinyface_annotator(batch)
+    _assert_tinyface(annot_batch[0])
+
+
 def test_bob_ip_facedetect():
     batch = [face_image]
     annot = BobIpFacedetect()(batch)
     _assert_bob_ip_facedetect(annot[0])
 
+
 def test_bob_ip_facedetect_eyes():
     batch = [face_image]
     annot = BobIpFacedetect(eye_estimate=True)(batch)
     _assert_bob_ip_facedetect(annot[0])
-    assert [int(x) for x in annot[0]['reye']] == [175, 128], annot
-    assert [int(x) for x in annot[0]['leye']] == [175, 221], annot
+    assert [int(x) for x in annot[0]["reye"]] == [175, 128], annot
+    assert [int(x) for x in annot[0]["leye"]] == [175, 221], annot
+
 
 def test_fail_safe():
     annotator = FailSafe(
-        [BobIpFacedetect(eye_estimate=True)],
-        required_keys=('reye', 'leye'),
+        [BobIpFacedetect(eye_estimate=True)], required_keys=("reye", "leye"),
     )
     batch = [face_image]
     annot = annotator(batch)
     _assert_bob_ip_facedetect(annot[0])
-    assert [int(x) for x in annot[0]['reye']] == [175, 128], annot
-    assert [int(x) for x in annot[0]['leye']] == [175, 221], annot
+    assert [int(x) for x in annot[0]["reye"]] == [175, 128], annot
+    assert [int(x) for x in annot[0]["leye"]] == [175, 221], annot
+
 
 def test_bob_ip_flandmark():
     annotator = FailSafe(
-        [BobIpFacedetect(), BobIpFlandmark()],
-        required_keys=('reye', 'leye'),
+        [BobIpFacedetect(), BobIpFlandmark()], required_keys=("reye", "leye"),
     )
     batch = [face_image]
     annot = annotator(batch)
     print(annot)
     _assert_bob_ip_facedetect(annot[0])
-    assert [int(x) for x in annot[0]['reye']] == [183, 127], annot
-    assert [int(x) for x in annot[0]['leye']] == [174, 223], annot
+    assert [int(x) for x in annot[0]["reye"]] == [183, 127], annot
+    assert [int(x) for x in annot[0]["leye"]] == [174, 223], annot
+
 
 def test_min_face_size_validator():
     valid = {
-        'topleft': (0, 0),
-        'bottomright': (32, 32),
+        "topleft": (0, 0),
+        "bottomright": (32, 32),
     }
     assert min_face_size_validator(valid)
 
     not_valid = {
-        'topleft': (0, 0),
-        'bottomright': (28, 33),
+        "topleft": (0, 0),
+        "bottomright": (28, 33),
     }
     assert not min_face_size_validator(not_valid)
 
     not_valid = {
-        'topleft': (0, 0),
-        'bottomright': (33, 28),
+        "topleft": (0, 0),
+        "bottomright": (33, 28),
     }
     assert not min_face_size_validator(not_valid)
 
-- 
GitLab