-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYuz_Egitme.py
35 lines (29 loc) · 1.07 KB
/
Yuz_Egitme.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
import cv2,os
import numpy as np
from PIL import Image
recognizer = cv2.face.LBPHFaceRecognizer_create()
cascadePath = "face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
path = 'resimler'
def get_images_and_labels(path):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
images = []
labels = []
for image_path in image_paths:
image_pil = Image.open(image_path).convert('L')
image = np.array(image_pil, 'uint8')
nbr = int(os.path.split(image_path)[1].split(".")[0].replace("face-", ""))
print(nbr)
faces = faceCascade.detectMultiScale(image)
for (x, y, w, h) in faces:
images.append(image[y: y + h, x: x + w])
labels.append(nbr)
cv2.imshow("Adding faces to traning set...", image[y: y + h, x: x + w])
cv2.waitKey(10)
return images, labels
images, labels = get_images_and_labels(path)
cv2.imshow('test',images[0])
cv2.waitKey(1)
recognizer.train(images, np.array(labels))
recognizer.write('training/trainer.yml')
cv2.destroyAllWindows()