├── test-data └── test.jpg ├── training-data ├── s1 │ ├── 1.jpg │ ├── 10.jpg │ ├── 11.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ └── 9.jpg └── s2 │ ├── 1.jpg │ ├── 10.jpg │ ├── 11.jpg │ ├── 12.jpg │ ├── 13.jpg │ ├── 14.jpg │ ├── 15.jpg │ ├── 16.jpg │ ├── 17.jpg │ ├── 18.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ └── 9.jpg ├── training_data.py ├── facemaster.py ├── face_detect.py └── README.md /test-data/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/test-data/test.jpg -------------------------------------------------------------------------------- /training-data/s1/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/1.jpg -------------------------------------------------------------------------------- /training-data/s1/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/10.jpg -------------------------------------------------------------------------------- /training-data/s1/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/11.jpg -------------------------------------------------------------------------------- /training-data/s1/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/2.jpg -------------------------------------------------------------------------------- /training-data/s1/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/3.jpg -------------------------------------------------------------------------------- /training-data/s1/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/4.jpg -------------------------------------------------------------------------------- /training-data/s1/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/5.jpg -------------------------------------------------------------------------------- /training-data/s1/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/6.jpg -------------------------------------------------------------------------------- /training-data/s1/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/7.jpg -------------------------------------------------------------------------------- /training-data/s1/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/8.jpg -------------------------------------------------------------------------------- /training-data/s1/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s1/9.jpg -------------------------------------------------------------------------------- /training-data/s2/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/1.jpg -------------------------------------------------------------------------------- /training-data/s2/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/10.jpg -------------------------------------------------------------------------------- /training-data/s2/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/11.jpg -------------------------------------------------------------------------------- /training-data/s2/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/12.jpg -------------------------------------------------------------------------------- /training-data/s2/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/13.jpg -------------------------------------------------------------------------------- /training-data/s2/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/14.jpg -------------------------------------------------------------------------------- /training-data/s2/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/15.jpg -------------------------------------------------------------------------------- /training-data/s2/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/16.jpg -------------------------------------------------------------------------------- /training-data/s2/17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/17.jpg -------------------------------------------------------------------------------- /training-data/s2/18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/18.jpg -------------------------------------------------------------------------------- /training-data/s2/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/2.jpg -------------------------------------------------------------------------------- /training-data/s2/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/3.jpg -------------------------------------------------------------------------------- /training-data/s2/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/4.jpg -------------------------------------------------------------------------------- /training-data/s2/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/5.jpg -------------------------------------------------------------------------------- /training-data/s2/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/6.jpg -------------------------------------------------------------------------------- /training-data/s2/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/7.jpg -------------------------------------------------------------------------------- /training-data/s2/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/8.jpg -------------------------------------------------------------------------------- /training-data/s2/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amaljoseph/Python-OpenCV-Multiple-Face-Recognition/HEAD/training-data/s2/9.jpg -------------------------------------------------------------------------------- /training_data.py: -------------------------------------------------------------------------------- 1 | import cv2, os, sys 2 | import numpy as np 3 | import face_detect as face_detect 4 | 5 | def training_data(data_folder): 6 | dirs = os.listdir(data_folder) 7 | faces = [] 8 | labels = [] 9 | for dir_name in dirs: 10 | if not dir_name.startswith("s"): 11 | continue; 12 | label = int(dir_name.replace("s", "")) 13 | subject_dir = data_folder + "/" + dir_name 14 | subject_images_names = os.listdir(subject_dir) 15 | for image_name in subject_images_names: 16 | if image_name.startswith("."): 17 | continue; 18 | image_path = subject_dir + "/" + image_name 19 | face, rect, length = face_detect.face_detect(image_path) 20 | if face is not None: 21 | faces.append(face[0]) 22 | labels.append(label) 23 | 24 | return faces, labels 25 | -------------------------------------------------------------------------------- /facemaster.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import face_detect as face_detect 4 | import training_data as training_data 5 | 6 | label = [] 7 | def predict(test_img): 8 | img = cv2.imread(test_img).copy() 9 | print "\n\n\n" 10 | print "Face Prediction Running -\-" 11 | face, rect, length = face_detect.face_detect(test_img) 12 | print len(face), "faces detected." 13 | for i in range(0, len(face)): 14 | labeltemp, confidence = face_recognizer.predict(face[i]) 15 | label.append(labeltemp) 16 | return img, label 17 | 18 | faces, labels = training_data.training_data("training-data") 19 | face_recognizer = cv2.face.LBPHFaceRecognizer_create() 20 | face_recognizer.train(faces, np.array(labels)) 21 | 22 | 23 | # Read the test image. 24 | test_img = "test-data/test.jpg" 25 | predicted_img , label= predict(test_img) 26 | cv2.destroyAllWindows() 27 | cv2.waitKey(1) 28 | cv2.destroyAllWindows() 29 | print "Recognized faces = ", label 30 | -------------------------------------------------------------------------------- /face_detect.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import sys 3 | 4 | def face_detect(imagePath): 5 | # HaarCascade file, to detect the face. 6 | faceCascade = cv2.CascadeClassifier("opencv-files/haarcascade_frontalface_alt.xml") 7 | image = cv2.imread(imagePath) 8 | #Convert image to grayscale 9 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 10 | grays = [] 11 | faces = faceCascade.detectMultiScale( 12 | gray, 13 | scaleFactor=1.1, 14 | minNeighbors=5, 15 | minSize=(30, 30) 16 | ) 17 | 18 | # For drawing rectangles over multiple faces in the image 19 | for (x, y, w, h) in faces: 20 | cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) 21 | 22 | # Show Detected faces. 23 | cv2.imshow("Faces found", image) 24 | cv2.waitKey(1) 25 | 26 | # Append the detected faces into grays list. 27 | for i in range(0, len(faces)): 28 | (x, y, w, h) = faces[i] 29 | grays.append(gray[y:y+w, x:x+h]) 30 | print "------------------------------------------------------------" 31 | print "Detecting Face -\-" 32 | return grays, faces, len(faces) 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multiple Face Recogniton using Python & OpenCV 2 | OpenCV based face recognition system that can detect and recognize multiple faces in an image. This project is based on [this](https://github.com/informramiz/opencv-face-recognition-python/), which had only single face detection. I have implemented a multiple face recognition system. 3 | 4 | 5 | 6 | There are 2 parts in a face recognition system. 7 | 1. Face Detection - To detect faces in images. 8 | 2. Face Recognition - To recognize face of persons in the images. 9 | 10 | ## 1. Face Detection. 11 | Face detection is acheived in this project using Haar Cascade classifier. It could be used for object detection. Here we are using it for detecting faces. The official documentation of the project is available [here](https://docs.opencv.org/3.2.0/d7/d8b/tutorial_py_face_detection.html). 12 | 13 | ## 2. Face Recognition 14 | We are using LBPH (Local Binary Patterns Histograms ) classifier to recognize the faces from the images. It compares neighboring pixels of a pixel and creates a histogram out of it for comparing faces. We could also use algorithms such as, EigenFaces Face Recognizer and FisherFaces Face Recognizer. The official Documentaion is available [here](https://docs.opencv.org/2.4.13.7/modules/contrib/doc/facerec/facerec_tutorial.html). 15 | 16 | These 3 algorithms could be added to the project by changing a single line. 17 | 18 | EigenFaces Face Recognizer Recognizer - `cv2.face.createEigenFaceRecognizer()` 19 | 20 | FisherFaces Face Recognizer Recognizer - `cv2.face.createFisherFaceRecognizer()` 21 | 22 | Local Binary Patterns Histograms (LBPH) Face Recognizer - `cv2.face.createLBPHFaceRecognizer()` 23 | 24 | ## Requirements 25 | 1. [Python 2.7.x](https://www.python.org/downloads/) 26 | 2. [OpenCV 2](https://opencv.org/releases/) 27 | 3. [Numpy](https://www.numpy.org/) 28 | 29 | ## How to run? 30 | To run the program, in terminal type `python facemaster.py` 31 | 32 | The photos of each individual should be stored in a folder s[i] (s1, s2 etc) inside the training-data folder. 33 | Test images are stored in test-data folder. 34 | 35 | The application is built over 3 files. 36 | 1. facemaster.py - Create the recognizer, train the images using `training_data()` and makes predictions from test data using `predict()` function. 37 | 2. training_data.py - To parse through each images in the training set and call `face_detect()` on each image. 38 | 3. face_detect.py - To detect the face, here we are initializing Haar Cascade classifier to detect multiple faces from the image, draw bounding boxes over face and returns the face bonding box coordinates. 39 | --------------------------------------------------------------------------------