forked from Papi4050/Droid-ComputerVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_face.py
138 lines (104 loc) · 3.1 KB
/
train_face.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'''
This file is a collection of functions used to provide
the image capturing mechanism required for recognizing faces
in LiveTracking.py.
This includes the creation of a standard folder in which the files will be
saved in, and a protocol to collect a predetermined amount of pictures with a
uniqueID.
'''
import os
import cv2
import time
import platform
import system_setup
import face_recognition as fr
def createImageDir(path):
'''
This function creates the necessary local directory for the known face
tracking functionality
Parameters
----------
path : string
File path to the images folder
'''
if os.path.isdir(path) is not True:
os.makedirs(path)
print("Dir created!")
else:
print("Dir already exist")
return 0
def captureFace(path, name):
'''
This functions executes the image capturing process
Parameters
----------
path : string
File path to the images folder
name : string
Contains the name of the person who is being saved
'''
cap = system_setup.configurator()
time.sleep(1)
faceNames = []
images = []
myList = os.listdir(path)
# Get name of faces in the folder
for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
faceNames.append(os.path.splitext(cl)[0])
# Check if name already exists in the files, if name exists, add unique ID
maxVal = []
print(faceNames)
for str in faceNames:
temp = str.split('-')
if temp[0] == name:
maxVal.append(temp[1])
print(temp[1])
maxVal.sort(key=int)
print(f'maxVal = {maxVal}')
if not maxVal:
uniqueID = 0
else:
uniqueID = int(maxVal[-1]) + 1
faceDetected = False
while not faceDetected:
# Prepare for taking the photo
for countdown in range(5, 0, -1):
print(f'Image will be taking in {countdown} seconds...')
time.sleep(1)
# Take the photo
success, image = cap.read()
print("Checking if a face was detected within image")
try:
fr.face_encodings(image)[0]
print("Successful photo with face captured!")
faceDetected = True
except IndexError:
print("Face not detected in image, another photo will be taken")
time.sleep(2)
final_path = f'{path}/{name}-{uniqueID}.png'
cv2.imwrite(final_path, image)
def checkFaceInImage(path, name, image):
'''
This functions checks if a face is present in the current frame
Parameters
----------
path : string
File path to the images folder
name : string
Contains the name of the person who is being saved
'''
try:
fr.face_encodings(image)[0]
print("Successful photo with face captured!")
except IndexError:
print("Face not detected in image, another photo will be taken")
captureFace(path, name)
def main():
path = './Images'
name = 'JohnDoe'
createImageDir(path)
captureFace(path, name)
if __name__ == "__main__":
main()