├── Face_Recog.py └── Face_Trainer.py /Face_Recog.py: -------------------------------------------------------------------------------- 1 | #Program to Detect the Face and Recognise the Person based on the data from face-trainner.yml 2 | 3 | import cv2 #For Image processing 4 | import numpy as np #For converting Images to Numerical array 5 | import os #To handle directories 6 | from PIL import Image #Pillow lib for handling images 7 | 8 | labels = ["apurva", "Elon Musk"] 9 | 10 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 11 | recognizer = cv2.face.createLBPHFaceRecognizer() 12 | recognizer.load("face-trainner.yml") 13 | 14 | cap = cv2.VideoCapture(0) #Get vidoe feed from the Camera 15 | 16 | while(True): 17 | 18 | ret, img = cap.read() # Break video into frames 19 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert Video frame to Greyscale 20 | faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) #Recog. faces 21 | for (x, y, w, h) in faces: 22 | roi_gray = gray[y:y+h, x:x+w] #Convert Face to greyscale 23 | 24 | id_, conf = recognizer.predict(roi_gray) #recognize the Face 25 | 26 | if conf>=80: 27 | font = cv2.FONT_HERSHEY_SIMPLEX #Font style for the name 28 | name = labels[id_] #Get the name from the List using ID number 29 | cv2.putText(img, name, (x,y), font, 1, (0,0,255), 2) 30 | 31 | cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) 32 | 33 | cv2.imshow('Preview',img) #Display the Video 34 | if cv2.waitKey(20) & 0xFF == ord('q'): 35 | break 36 | 37 | # When everything done, release the capture 38 | cap.release() 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /Face_Trainer.py: -------------------------------------------------------------------------------- 1 | #Program to train with the faces and create a YAML file 2 | 3 | import cv2 #For Image processing 4 | import numpy as np #For converting Images to Numerical array 5 | import os #To handle directories 6 | from PIL import Image #Pillow lib for handling images 7 | 8 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 9 | recognizer = cv2.face.createLBPHFaceRecognizer() 10 | 11 | Face_ID = -1 12 | pev_person_name = "" 13 | y_ID = [] 14 | x_train = [] 15 | 16 | Face_Images = os.path.join(os.getcwd(), "Face_Images") #Tell the program where we have saved the face images 17 | print (Face_Images) 18 | 19 | for root, dirs, files in os.walk(Face_Images): #go to the face image directory 20 | for file in files: #check every directory in it 21 | if file.endswith("jpeg") or file.endswith("jpg") or file.endswith("png"): #for image files ending with jpeg,jpg or png 22 | path = os.path.join(root, file) 23 | person_name = os.path.basename(root) 24 | print(path, person_name) 25 | 26 | 27 | if pev_person_name!=person_name: #Check if the name of person has changed 28 | Face_ID=Face_ID+1 #If yes increment the ID count 29 | pev_person_name = person_name 30 | 31 | 32 | Gery_Image = Image.open(path).convert("L") # convert the image to greysclae using Pillow 33 | Crop_Image = Gery_Image.resize( (800,800) , Image.ANTIALIAS) #Crop the Grey Image to 550*550 (Make sure your face is in the center in all image) 34 | Final_Image = np.array(Crop_Image, "uint8") 35 | #print(Numpy_Image) 36 | faces = face_cascade.detectMultiScale(Final_Image, scaleFactor=1.5, minNeighbors=5) #Detect The face in all sample image 37 | print (Face_ID,faces) 38 | 39 | for (x,y,w,h) in faces: 40 | roi = Final_Image[y:y+h, x:x+w] #crop the Region of Interest (ROI) 41 | x_train.append(roi) 42 | y_ID.append(Face_ID) 43 | 44 | recognizer.train(x_train, np.array(y_ID)) #Create a Matrix of Training data 45 | recognizer.save("face-trainner.yml") #Save the matrix as YML file 46 | --------------------------------------------------------------------------------