-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaces_trainV2.py
66 lines (51 loc) · 2.03 KB
/
faces_trainV2.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
In this file I train the model using images in the training folder.
If you have new images, add to the training folders under the right name
(or add a folder for a new person), then rerun this training program.
'''
import cv2 as cv
import numpy as np
import os
import face_recognition
from numpy.core.numerictypes import obj2sctype
#people = ['Ben Afflek','Elton John','Jerry Seinfield','Madonna','Mindy Kaling']
DIR = r'Faces\train'
people = []
for name in os.listdir(DIR):
people.append(name)
faceLocations = []
encodingList = []
peopleList = []
def create_train():
for person in people:
path = os.path.join(DIR, person)
for img in os.listdir(path):
print(f'Processing for: {img}')
img_path = os.path.join(path, img)
img_array = cv.imread(img_path)
img_rgb = cv.cvtColor(img_array, cv.COLOR_BGR2RGB)
#detect face
faces_locs0 = face_recognition.face_locations(img_rgb)
if faces_locs0:
faces_locs = faces_locs0[0]
#append faces to lists
faceLocations.append(faces_locs)
print(f'Obtained face locations for: {img}. Number of faces: {len(faces_locs)//4}')
faces_encode = face_recognition.face_encodings(img_rgb,[faces_locs])
print(f'Encoded faces for {person}: {img}')
encodingList.append(faces_encode[0])
peopleList.append(person)
else:
print(f'No faces found for {person:} {img}')
create_train()
print('Training done -----------------')
print(f'Number of faces: {len(faceLocations)}')
print(f'Number of encodings: {len(encodingList)}')
print(f'Number of labels: {len(peopleList)}')
faceLocations = np.array(faceLocations, dtype = 'object')
encodingList = np.array(encodingList, dtype = 'object')
peopleList =np.array(peopleList)
np.save('faceLocations.npy', faceLocations)
np.save('encodingList.npy',encodingList)
np.save('peopleList.npy',peopleList)
print(f'\n Training files saved')