-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_Authorization.py
116 lines (98 loc) · 3.06 KB
/
Main_Authorization.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
import face_recognition
import glob
import cv2
import datetime
known_face_encodings = []
def known_pics_prepare(your_pics_dir, debugging):
# Load Known Faces
path_list = glob.glob(your_pics_dir + "\*")
known_face_imgs = []
for path in path_list:
img = face_recognition.load_image_file(path)
known_face_imgs.append(img)
if debugging:
print("Loaded Known Images:", path_list)
if(len(path_list) == 0): return -2
# face detection
known_face_locs = []
for img in known_face_imgs:
loc = face_recognition.face_locations(img, model="hog")
if len(loc) != 1:
if debugging:
print("Known Face Analyzation Error")
return -2
known_face_locs.append(loc)
# face recognition
global known_face_encodings
for img, loc in zip(known_face_imgs, known_face_locs):
(encoding,) = face_recognition.face_encodings(img, loc)
known_face_encodings.append(encoding)
return 0
def camera_capture(capture_pics_dir):
camera_image = (
capture_pics_dir
+ "\\"
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
+ ".jpg"
)
# Open Camera
cap = cv2.VideoCapture(0)
# Capture Pic
ret, frame = cap.read()
# Save Pic
cv2.imwrite(camera_image, frame)
# Close Camera
cap.release()
return camera_image
def authorization(your_pics_dir, capture_pics_dir, rigidity, threshold, debugging):
# Load Captured Target Face
target_image = camera_capture(capture_pics_dir)
face_img_to_check = face_recognition.load_image_file(target_image)
# face detection
face_loc_to_check = face_recognition.face_locations(face_img_to_check, model="hog")
if len(face_loc_to_check) != 1:
if debugging:
print("Target Face Analyzation Error")
return -2, -1, -1, -1, -1
# face recognition
(face_encoding_to_check,) = face_recognition.face_encodings(
face_img_to_check, face_loc_to_check
)
#check difficulty
global known_face_encodings
dists = face_recognition.face_distance(
known_face_encodings, face_encoding_to_check
) # check similarity level
if debugging:
print(len(dists), "\n", dists) # 3 [True, False, False]?
true_count = 0
ave_threshold = 0
min_threshold = 1
max_threshold = 0
for x in dists:
if x < threshold:
true_count += 1
ave_threshold += x
if x < min_threshold:
min_threshold = x
if x > max_threshold:
max_threshold = x
if debugging:
print("rigidity:", rigidity, "%\n", "threshold:", threshold)
last_rigidity = true_count / len(dists)
if last_rigidity >= rigidity * 0.01:
return (
0,
last_rigidity * 100,
ave_threshold / len(dists),
min_threshold,
max_threshold,
)
else:
return (
1,
last_rigidity * 100,
ave_threshold / len(dists),
min_threshold,
max_threshold,
)