Spooke/Login_System_using_Facial_Recognition
0
1"""
2@author: Santhosh R
3"""
4import cv2, os
5import shutil
6import csv
7import numpy as np
8from PIL import Image, ImageTk
9import pandas as pd
10
11
12def getImagesAndLabels(path):
13 # Get the path of all the files in the folder
14 imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
15
16 # Create empth face list
17 faces = []
18 # Create empty ID list
19 Ids = []
20 # Looping through all the image paths and loading the Ids and the images
21 for imagePath in imagePaths:
22 # Loading the image and converting it to gray scale
23 pilImage = Image.open(imagePath).convert('L')
24 # Now we are converting the PIL image into numpy array
25 imageNp = np.array(pilImage, 'uint8')
26 # getting the Id from the image
27 Id = int(os.path.split(imagePath)[-1].split(".")[1])
28 # extract the face from the training image sample
29 faces.append(imageNp)
30
31 Ids.append(Id)
32 return faces, Ids
33
34# Train image using LBPHFFace recognizer
35def TrainImages():
36 recognizer = cv2.face.LBPHFaceRecognizer_create() # recognizer = cv2.face.LBPHFaceRecognizer_create()#$cv2.createLBPHFaceRecognizer()
37 harcascadePath = "hh.xml"
38 detector = cv2.CascadeClassifier(harcascadePath)
39 faces , Id= getImagesAndLabels("TrainingImage")
40 recognizer.train(faces, np.array(Id))
41 #store data in file
42 recognizer.save("TrainData\Trainner.yml")
43 res = "Image Trained and data stored in TrainData\Trainner.yml "
44
45 print(res)
46
47
48
49TrainImages()
50 