-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_student_data.py
executable file
·56 lines (47 loc) · 1.85 KB
/
encode_student_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from imutils import paths
import face_recognition
import pickle
import cv2
import os
def encode_student_data(student_id):
# Grabbing the input images in our dataset
# print("[INFO] quantifying faces..")
student_image_path = os.path.join("dataset", student_id)
imagePaths = list(paths.list_images(student_image_path))
encodings_file = "encodings.pickle"
# initialise the list of known encodings and known names
knownEncodings = []
knownNames = []
# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
# Extract person name from the image path
# print("[INFO] processing image {}/{}".format(i+1,len(imagePaths)))
name = imagePath.split(os.path.sep)[-2]
# load the image and convert it frim RGM (OpenCV ordering)
# to dlib ordering (RGB)
image = cv2.imread(imagePath)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# detect the coordinates of face
box = face_recognition.face_locations(rgb, model='hog')
# Now as we have face coordinates now compute the encodings
encodings = face_recognition.face_encodings(rgb, box)
# Loop over the encodings :
for encoding in encodings:
# add the values and names in the empty list
knownEncodings.append(encoding)
knownNames.append(name)
# now save the list
# print("[INFO] Serializing encodings..")
old_data = {
"encodings": [],
"names": [],
}
# Load the known face and encodings
if os.path.getsize(encodings_file) > 0:
with open(encodings_file, "rb") as f:
old_data = pickle.loads(f.read(), encoding="latin1")
old_data['encodings'] += knownEncodings
old_data['names'] += knownNames
# print(old_data)
with open("encodings.pickle", "wb") as f:
f.write(pickle.dumps(old_data))