-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
128 lines (103 loc) · 3.24 KB
/
app.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
import cv2
import numpy as np
import os
import face_recognition
from tkinter import *
import sys
from res import *
import threading
# consts
trainingDir = 'known_file'
testingDir = 'imgs'
tolerance = 0.6
frameThickness = 3
model = 'cnn'
#lists
knownNames = []
knownFaces = []
# global booleans
run = True
def updateResources():
print('[RELOAD] Loading or Reloading Known Faces...')
# reset the names
knownNames = []
knownFaces = []
# iterate through all the names
for name in os.listdir(trainingDir):
# iterate throught the images in the directory with the name before
for image in os.listdir(f'{trainingDir}/{name}'):
image = face_recognition.load_image_file(f'{trainingDir}/{name}/{image}')
encoding = face_recognition.face_encodings(image)[0]
knownFaces.append(encoding)
knownNames.append(image)
def faceRecognise(img):
# load the image
image = face_recognition.load_image(f'{testingDir}/{img}')
# find the locations of the faces
locations = face_recognition.face_locations(image, model = model)
# encode the image to compare it later
encodings = face_recognition.face_encodings(image, locations)
# loop to find similar encodings
for faceEncoding, faceLocation in zip(encodings, locations):
# compare the faces
results = face_recognition.compare_faces(knownFaces, faceEncoding, tolerance)
if True in results:
match = knownNames[results.index(True)]
return match
return None
def userAlert(msg):
return True
def faceCheckLoop():
cam = cv2.videoCapture()
while True:
ret, frame = cam.read()
if not ret:
raise IOError("Error getting camera frame...")
raise IOError('"Mission failed... well get them next time..."')
break
# to exit the program
key = cv2.waitKey(1)
# press f to exit application
if key == ord('f'):
print('[EXIT] Exiting Application...')
break
cv2.imwrite(os.path.join('img.jpg'), frame)
face = faceRecognise('img.jpg')
if not face == None:
if userAlert(f'{face} is trying to enter your home'):
print(f'{face} is let in')
else:
print(f'{face} was not let in')
if os.path.exist('img.png'):
os.remove('img.png')
else:
raise FileNotFoundError('The file was not saved properly...')
break
cam.release()
sys.exit()
def tkWindow():
#make the window
root = Tk()
#make the tkinter objects
#make the gradient frame while pulling from res.py
mainframe = gradientFrame(
root
)
#make a label cause I probably don't have time to make
#commit the tkinter objects
mainframe.pack(fill = 'both', expand = True)
root.protocol('WM_DELETE_WINDOW', onWindowClose)
root.mainloop()
def onWindowClose():
run = False
sys.exit()
def loop():
faceRecogniseThread = threading.Thread(target = faceCheckLoop)
tkWindowThread = threading.Thread(target = tkWindow)
faceRecogniseThread.start()
tkWindowThread.start()
while run:
pass
sys.exit()
updateResources()
loop()