-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaces-train.py
More file actions
53 lines (45 loc) · 1.73 KB
/
faces-train.py
File metadata and controls
53 lines (45 loc) · 1.73 KB
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
42
43
44
45
46
47
48
49
50
51
52
import os
from PIL import Image
import numpy
import cv2
import pickle
face_Cascade2 = cv2.CascadeClassifier('cascades\data\haarcascade_frontalface_default.xml')
face_Cascade3 = cv2.CascadeClassifier('cascades\data\haarcascade_frontalface_alt.xml')
BASE_directory = os.path.dirname(os.path.abspath(__file__))
images_directory = os.path.join(BASE_directory, 'imgs')
recognizer = cv2.face.LBPHFaceRecognizer_create()
current_id = 0
pictures = []
paths = {}
paths_list = []
for root , dirs, files in os.walk(images_directory):
for file in files:
if file.endswith("png") or file.endswith("jpg"):
path = os.path.join(root, file)
print(path)
label = os.path.basename(os.path.dirname(path)).replace(" ", "-").lower()
pil_img = Image.open(path).convert("L")
image_array = numpy.array(pil_img, "uint8")
if not path in paths:
paths[path] = current_id
current_id += 1
id_ = paths[path]
if path.endswith("vi.jpg"):
pictures.append(image_array)
paths_list.append(id_)
faces = face_Cascade3.detectMultiScale(image_array, scaleFactor = 1.1, minNeighbors = 4)
for (x, y, w, h) in faces:
region_of_interests = image_array[y:y+h, x:x+w]
print(region_of_interests)
pictures.append(region_of_interests)
paths_list.append(id_)
with open("labels.pickle", "wb") as f:
pickle.dump(paths, f)
print(len(pictures))
recognizer.train(pictures, numpy.array(paths_list))
recognizer.save("trainner.yml")
print("Done")
while True:
cv2.imshow('Image', pictures[0])
if cv2.waitKey(1) == ord('q'):
break