└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Face-Detection 2 | # OpenCV program to detect face in real time 3 | # import libraries of python OpenCV 4 | import cv2 5 | 6 | # Trained XML classifiers describes some features of some 7 | # object we want to detect a cascade function is trained 8 | # from a lot of positive(faces) and negative(non-faces) 9 | # images. 10 | face_cascade = cv2.CascadeClassifier('face_.xml') 11 | 12 | # Trained XML file for detecting eyes 13 | eye_cascade = cv2.CascadeClassifier('normal_eye.xml') 14 | 15 | # capture frames from a camera 16 | cap = cv2.VideoCapture(0) 17 | 18 | # loop runs if capturing has been initialized. 19 | while 1: 20 | 21 | # reads frames from a camera 22 | ret, img = cap.read() 23 | 24 | # convert to gray scale of each frames 25 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 26 | 27 | # Detects faces of different sizes in the input image 28 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 29 | 30 | for (x,y,w,h) in faces: 31 | # To draw a rectangle in a face 32 | cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2) 33 | roi_gray = gray[y:y+h, x:x+w] 34 | roi_color = img[y:y+h, x:x+w] 35 | 36 | # Detects eyes of different sizes in the input image 37 | eyes = eye_cascade.detectMultiScale(roi_gray) 38 | 39 | #To draw a rectangle in eyes 40 | for (ex,ey,ew,eh) in eyes: 41 | cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,127,255),2) 42 | 43 | # Display an image in a window 44 | cv2.imshow('img',img) 45 | 46 | # Wait for Esc key to stop 47 | k = cv2.waitKey(30) & 0xff 48 | if k == 27: 49 | break 50 | 51 | # Close the window 52 | cap.release() 53 | 54 | # De-allocate any associated memory usage 55 | cv2.destroyAllWindows() 56 | --------------------------------------------------------------------------------