diff --git a/src/detectface.py b/src/detectface.py index ea52306..caa9c73 100644 --- a/src/detectface.py +++ b/src/detectface.py @@ -10,9 +10,9 @@ def detectface(img): "facedetection/haarcascade_frontalface_default.xml" ) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - faces = face_cascade.detectMultiScale(gray, 1.3, 5) - if faces is (): - return False + faces = face_cascade.detectMultiScale(gray, 1.3, 5) + if faces is None or len(faces) == 0: + return False for x, y, w, h in faces: roi_gray = img[y : y + h, x : x + w] cropped_img = np.expand_dims( diff --git a/tests/test_detectface.py b/tests/test_detectface.py new file mode 100644 index 0000000..c7706f0 --- /dev/null +++ b/tests/test_detectface.py @@ -0,0 +1,24 @@ +import os +import unittest +import numpy as np + +# Ensure src is on the path +import sys +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + +from src.detectface import detectface + +class DetectFaceNoFaceTest(unittest.TestCase): + def test_no_face_returns_false(self): + # Create a blank image with no faces + img = np.zeros((224, 224, 3), dtype=np.uint8) + # Change working directory so cascade path resolves + cwd = os.getcwd() + try: + os.chdir(os.path.join(cwd, 'restapi', 'memegen')) + self.assertFalse(detectface(img)) + finally: + os.chdir(cwd) + +if __name__ == '__main__': + unittest.main()