Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/detectface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions tests/test_detectface.py
Original file line number Diff line number Diff line change
@@ -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()