├── Media ├── Output.mp4 ├── face detection.jpeg └── face deyection 2.png ├── FaceDetection.py └── README.md /Media/Output.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StarDev913/FaceDetect/HEAD/Media/Output.mp4 -------------------------------------------------------------------------------- /Media/face detection.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StarDev913/FaceDetect/HEAD/Media/face detection.jpeg -------------------------------------------------------------------------------- /Media/face deyection 2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StarDev913/FaceDetect/HEAD/Media/face deyection 2.png -------------------------------------------------------------------------------- /FaceDetection.py: -------------------------------------------------------------------------------- 1 | import cv2 # import openCV 2 | 3 | alg = 'haarcascade_frontalface_default.xml' # accessed the model file 4 | 5 | cascade = cv2.CascadeClassifier(alg) # loading the model with cv2 6 | 7 | cam = cv2.VideoCapture(0) # initialization camera 8 | 9 | while True: 10 | 11 | _,img = cam.read() # read the frame from the camera 12 | 13 | grayImg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # converting color into gray scale image 14 | 15 | face = cascade.detectMultiScale(grayImg) # get coordinates of face 16 | 17 | for (x, y, w, h) in face: # segregating x,y,w,h 18 | 19 | cv2.rectangle(img, (x, y), (x + w, y + h),(0,255,0),2) # draw the retangle 20 | 21 | cv2.imshow("FaceDetect",img) 22 | 23 | key = cv2.waitKey(1) 24 | 25 | if key == 81 or key == 113 : 26 | break 27 | 28 | cam.release() 29 | cv2.destroyAllWindows() 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Face Detecton In Python Using OpenCV 2 | 3 | # OpenCV 4 | 5 | OpenCV is an open source computer vision and machine learning software library. It is a BSD-licence product thus free for both business and academic purposes.The Library provides more than 2500 algorithms that include machine learning tools for classification and clustering, image processing and vision algorithm, basic algorithms and drawing functions, GUI and I/O functions for images and videos. Some applications of these algorithms include face detection, object recognition, extracting 3D models, image processing, camera calibration, motion analysis etc. 6 | 7 | OpenCV is written natively in C/C++. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS, and Android. OpenCV was designed for computational efficiency and targeted for real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. 8 | 9 | # Face Detection 10 | 11 | Face detection has gained a lot of attention due to its real-time applications. A lot of research has been done and still going on for improved and fast implementation of the face detection algorithm. Why is face detection difficult for a machine? Face detection is not as easy as it seems due to lots of variations of image appearance, such as pose variation (front, non-front), occlusion, image orientation, illumination changes and facial expression. 12 | 13 | OpenCV contains many pre-trained classifiers for face, eyes, smile etc. The XML files of pre-trained classifiers are stored in opencv/data/. For face detection specifically, there are two pre-trained classifiers: 14 | 15 | 1.Haar Cascade Classifier 16 | 17 | 2.LBP Cascade Classifier 18 | 19 | We will explore both face detectors in this tutorial. 20 | 21 | # Haar Cascade Classifier 22 | 23 | It is a machine learning based approach where a cascade function is trained from a lot of positive (images with face) and negative images (images without face). The algorithm is proposed by Paul Viola and Michael Jones. 24 | 25 | The algorithm has four stages: 26 | 27 | # 1.Haar Feature Selection: 28 | Haar features are calculated in the subsections of the input image. The difference between the sum of pixel intensities of adjacent rectangular regions is calculated to differentiate the subsections of the image. A large number of haar-like features are required for getting facial features. 29 | # 2.Creating an Integral Image: 30 | Too much computation will be done when operations are performed on all pixels, so an integral image is used that reduce the computation to only four pixels. This makes the algorithm quite fast. 31 | # 3.Adaboost: 32 | All the computed features are not relevant for the classification purpose. Adaboost is used to classify the relevant features. 33 | # 4.Cascading Classifiers: 34 | Now we can use the relevant features to classify a face from a non-face but algorithm provides another improvement using the concept of cascades of classifiers. Every region of the image is not a facial region so it is not useful to apply all the features on all the regions of the image. Instead of using all the features at a time, group the features into different stages of the classifier.Apply each stage one-by-one to find a facial region. If on any stage the classifier fails, that region will be discarded from further iterations. Only the facial region will pass all the stages of the classifier. 35 | 36 | # LBP Cascade Classifier 37 | 38 | LBP is a texture descriptor and face is composed of micro texture patterns. So LBP features are extracted to form a feature vector to classify a face from a non-face. Following are the basic steps of LBP Cascade classifier algorithm: 39 | 40 | # 1.LBP Labelling: 41 | A label as a string of binary numbers is assigned to each pixel of an image. 42 | # 2.Feature Vector: 43 | Image is divided into sub-regions and for each sub-region, a histogram of labels is constructed. Then, a feature vector is formed by concatenating the sub-regions histograms into a large histogram. 44 | # 3.AdaBoost Learning: 45 | Strong classifier is constructed using gentle AdaBoost to remove redundant information from feature vector. 46 | # 4.Cascade of Classifier: 47 | The cascades of classifiers are formed from the features obtained by the gentle AdaBoost algorithm. Sub-regions of the image is evaluated starting from simpler classifier to strong classifier. If on any stage classifier fails, that region will be discarded from further iterations. Only the facial region will pass all the stages of the classifier. 48 | 49 | # Steps : 50 | 51 | # 1.Loading HaarCascadeFace Algorithm 52 | 53 | # 2.Initializing Camera 54 | 55 | # 3.Reading Frame from Camera 56 | 57 | # 4.Converting Color image into Grayscale Image 58 | 59 | # 5.Obtaining Face coordinates by passing algorithm 60 | 61 | # 6.Drawing Rectangle on the Face Coordinates 62 | 63 | # 7.Display the output Frame 64 | 65 | # Output : 66 | 67 | To see the output video, go to the media file and check the output video 68 | 69 | 70 | 71 | 72 | 73 | 74 | --------------------------------------------------------------------------------