forked from UV1999/Facial-Recognition-Attendance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train_Image.py
27 lines (23 loc) · 831 Bytes
/
Train_Image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import cv2
import numpy as np
from PIL import Image
def getImagesAndLabels(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faces = []
Ids = []
for imagePath in imagePaths:
pilImage = Image.open(imagePath).convert('L')
imageNp = np.array(pilImage, 'uint8')
Id = int(os.path.split(imagePath)[-1].split(".")[1])
faces.append(imageNp)
Ids.append(Id)
return faces, Ids
def TrainImages():
recognizer = cv2.face_LBPHFaceRecognizer.create()
harcascadePath = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(harcascadePath)
faces, Id = getImagesAndLabels("TrainingImage")
recognizer.train(faces, np.array(Id))
recognizer.save("TrainingImageLabel"+os.sep+"Trainner.yml")
print("Images Trained")