diff --git a/snipplets/code/Python/opencv/.gitignore b/snipplets/code/Python/opencv/.gitignore index a7e9c88..7506696 100644 --- a/snipplets/code/Python/opencv/.gitignore +++ b/snipplets/code/Python/opencv/.gitignore @@ -1,2 +1,2 @@ *.png -haarcascade_frontalface_default.xml +haarcascade_*.xml diff --git a/snipplets/code/Python/opencv/face-detect-realtime.py b/snipplets/code/Python/opencv/face-detect-realtime.py index 9f74bac..b5fc3fd 100755 --- a/snipplets/code/Python/opencv/face-detect-realtime.py +++ b/snipplets/code/Python/opencv/face-detect-realtime.py @@ -2,28 +2,33 @@ import cv2 -camera = cv2.VideoCapture(2) -while True: - if cv2.waitKey(1) & 0xFF == ord('q'): - break +def face_detect(capture): + gray_image = cv2.cvtColor(capture, cv2.COLOR_BGR2GRAY) + faces = face_classifier.detectMultiScale( + gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40) + ) - result, image = camera.read() + for x, y, w, h in faces: + cv2.rectangle(capture, (x, y), (x + w, y + h), (0, 255, 0), 2) - gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + return faces + +if __name__ == '__main__': + camera = cv2.VideoCapture(2) face_classifier = cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' ) - face = face_classifier.detectMultiScale( - gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40) - ) + while True: + if cv2.waitKey(1) & 0xFF == ord('q'): + break - for x, y, w, h in face: - cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) + result, image = camera.read() - cv2.imshow('Face Realtime Detector', image) + faces = face_detect(image) + cv2.imshow('Face Realtime Detector', image) -camera.release() -cv2.destroyAllWindows() + camera.release() + cv2.destroyAllWindows()