├── README.md ├── facedetection.py └── haarcascade_frontalface_default.xml /README.md: -------------------------------------------------------------------------------- 1 | This is a simple python project that uses OpenCV for face detection. It converts the image into grayscale before processing. A rectangular frame around the face says that the face is detected. 2 | -------------------------------------------------------------------------------- /facedetection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 5 | 6 | def detect_faces(img): 7 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 8 | faces = face_classifier.detectMultiScale(gray, 1.3, 5) 9 | 10 | if faces is (): 11 | return img 12 | 13 | for (x, y, w, h) in faces: 14 | cv2.rectangle(img, (x, y), (x+w, y+h), (255,0,0),2) 15 | return img 16 | 17 | cap = cv2.VideoCapture(0) 18 | 19 | while True: 20 | ret, frame =cap.read() 21 | frame = detect_faces(frame) 22 | 23 | cv2.imshow('Video Face Detection', frame) 24 | 25 | if cv2.waitKey(1) & 0xFF == ord('q'): 26 | break 27 | 28 | cap.release() 29 | cv2.destroyAllWindows() 30 | --------------------------------------------------------------------------------