-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_data.py
41 lines (28 loc) · 1.02 KB
/
train_data.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import cv2
import numpy as np
from PIL import Image
# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.LBPHFaceRecognizer_create()
#recognizer = cv2.face.EigenFaceRecognizer_create()
#recognizer = cv2.face.FisherFaceRecognizer_create()
#This is the path you have to change to the dataset path you have created
path = 'E:\\Summer\\Face detection\\Dataset'
def Images_ID(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
face_img = Image.open(imagePath).convert('L')
face = np.array(face_img,'uint8')
ID = int(os.path.split(imagePath) [-1].split('.') [1])
faces.append(face)
print(ID)
IDs.append(ID)
cv2.imshow("Training_Images",face)
cv2.waitKey(15)
return np.array(IDs), faces
IDs, faces = Images_ID(path)
recognizer.train(faces,IDs)
recognizer.save('Trainer.yml')
cv2.destroyAllWindows()