Skip to content

Commit

Permalink
(face) parameterize test
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsBurgh committed Feb 19, 2025
1 parent a1cd8da commit 0598f62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
1 change: 1 addition & 0 deletions image_recognition_face_recognition/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<exec_depend>rospy</exec_depend>

<test_depend>catkin_lint_cmake</test_depend>
<test_depend>python3-parameterized</test_depend>

<doc_depend>python3-sphinx</doc_depend>
<doc_depend>python-sphinx-autoapi-pip</doc_depend>
Expand Down
33 changes: 15 additions & 18 deletions image_recognition_face_recognition/test/test_face_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cv2
import numpy as np
import torch
from parameterized import parameterized
from image_recognition_face_recognition.face_recognizer import FaceRecognizer, RecognizedFace, ROI


Expand Down Expand Up @@ -57,29 +58,25 @@ def test_update_with_categorical_distribution(self):
updated_face = self.face_recognizer._update_with_categorical_distribution(recognized_face)
self.assertIsInstance(updated_face, RecognizedFace)

def test_detect(self):
@parameterized.expand([
("doc/1.jpg", 1),
("doc/example.png", 6),
])
def test_detect(self, image_name, expected_faces):
"""
Test the detect method with different images (single/multiple faces, no faces)
Test the detect method with different images (single/multiple faces)
"""
# Test with an image containing one face
image_path = Path(__file__).parent / "doc" / "1.jpg"
single_face_image = cv2.imread(str(image_path))
self.assertIsNotNone(single_face_image, f"Image not found at {image_path}")
recognized_faces = self.face_recognizer.detect(single_face_image)
image_path = Path(__file__).parent / image_name
image = cv2.imread(str(image_path))
self.assertIsNotNone(image, f"Image not found at {image_path}")
recognized_faces = self.face_recognizer.detect(image)
self.assertIsInstance(recognized_faces, list)
self.assertEqual(len(recognized_faces), 1) # Expect exactly 1 face

# Test with an image containing multiple faces
image_path = Path(__file__).parent / "doc" / "example.png"
multiple_faces_image = cv2.imread(str(image_path))
self.assertIsNotNone(multiple_faces_image, f"Image not found at {image_path}")
recognized_faces = self.face_recognizer.detect(multiple_faces_image)
self.assertIsInstance(recognized_faces, list)
self.assertGreaterEqual(len(recognized_faces), 6) # Expect at least 2 faces
self.assertEqual(len(recognized_faces), expected_faces)

def test_detect_no_faces(self):
# Test with an image containing no faces
no_face_image = np.zeros((160, 160, 3), dtype=np.uint8)
recognized_faces = self.face_recognizer.detect(no_face_image)
image = np.zeros((160, 160, 3), dtype=np.uint8)
recognized_faces = self.face_recognizer.detect(image)
self.assertEqual(len(recognized_faces), 0)

def test_get_min_l2_distance(self):
Expand Down

0 comments on commit 0598f62

Please sign in to comment.