-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_auth.py
167 lines (139 loc) · 4.71 KB
/
face_auth.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import cv2
import json
import boto3
import botocore
from datetime import datetime
from dotenv import dotenv_values
def aws_init():
s3 = boto3.client("s3")
reko = boto3.client("rekognition")
return s3, reko
def init_cam():
cam = cv2.VideoCapture(0)
print("Starting camera...")
# cam.set(cv2.CAP_PROP_AUTOFOCUS, 0)
# cam.set(cv2.CAP_PROP_FOCUS, 360)
# cam.set(cv2.CAP_PROP_BRIGHTNESS, 130)
# cam.set(cv2.CAP_PROP_SHARPNESS, 125)
# cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
# cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
face_cascade = cv2.CascadeClassifier(
"./haarcascade/haarcascade_frontalface_default.xml")
return cam, face_cascade
def destory_cam(cam):
cv2.destroyAllWindows()
cam.release()
def variance_of_laplacian(image):
return cv2.Laplacian(image, cv2.CV_64F).var()
def take_pictures():
cam, face_cascade = init_cam()
pic_taken = False
first_loop = True
while (True):
if first_loop:
print("Detecting faces and taking pictures...")
print("Try to stay still...")
first_loop = False
result, image = cam.read()
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(
gray_img,
scaleFactor=1.1,
minNeighbors=5,
minSize=(40, 40)
)
if result:
var = variance_of_laplacian(image)
if var > 30 and len(face) != 0:
capture_filename = datetime.now().strftime(f"%d-%m-%Y_%H-%M-%S.jpg")
cv2.imwrite(f"./captured_images/{capture_filename}", image)
pic_taken = True
print("Picture taken.")
break
destory_cam(cam)
return capture_filename, pic_taken
def get_config_values():
config = dotenv_values(".env")
bucket = config["bucket_name"]
base_face = config["base_face_file"]
return bucket, base_face
def upload_image(s3, bucket, capture_filename, pic_taken):
print(f"Uploading ./captured_images/{capture_filename}...")
is_uploaded = False
if pic_taken:
with open(f"./captured_images/{capture_filename}", "rb") as data:
try:
s3.upload_fileobj(
data,
bucket,
capture_filename
)
is_uploaded = True
except botocore.exceptions.ClientError as error:
print(error)
if is_uploaded:
print("Picture file uploaded.")
def print_output(result):
print("\n")
for match in result["FaceMatches"]:
print(
f"Similarity between compared faces is {result['FaceMatches'][0]['Similarity']}")
for nomatch in result["UnmatchedFaces"]:
print("Faces either don't match or are a poor match")
print("\n")
def log_output(capture_filename):
print(f"Logging to file ./logs/face_capture_time_log.txt...")
with open("./logs/face_capture_time_log.txt", "a") as log_file:
log_file.write(f"{capture_filename.split('.')[0]}\n")
print("Done logging.")
def compare_face(reko, bucket, capture_filename, base_face, threshold=85):
print("Comparing picture...")
success = False
try:
response = reko.compare_faces(
SourceImage={
"S3Object": {
"Bucket": bucket,
"Name": base_face
}
},
TargetImage={
"S3Object": {
"Bucket": bucket,
"Name": capture_filename
}
},
SimilarityThreshold=threshold,
)
success = True
except botocore.exceptions.ClientError as error:
print(error)
if success:
print("Picture succesfully compared.")
result = json.loads(json.dumps(response))
print_output(result)
log_output(capture_filename)
def upload_log_file(s3, bucket):
print("Uploading log file...")
is_uploaded = False
with open("./logs/face_capture_time_log.txt", "rb") as data:
try:
s3.upload_fileobj(
data,
bucket,
"face_capture_time_log.txt"
)
is_uploaded = True
except botocore.exceptions.ClientError as error:
print(error)
if is_uploaded:
print("Log file uploaded.")
if __name__ == "__main__":
print("Starting application...")
capture_filename, pic_taken = take_pictures()
bucket, base_face = get_config_values()
s3, reko = aws_init()
upload_image(s3, bucket, capture_filename, pic_taken)
compare_face(reko, bucket, capture_filename, base_face)
upload_log_file(s3, bucket)
print("Exiting application...")