-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
303 lines (232 loc) · 8.44 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os, cv2, pyttsx3, pyvirtualcam, multiprocessing, logging, sys, traceback, json, colorama
from cvzone.PoseModule import PoseDetector
from pyvirtualcam import PixelFormat
from datetime import datetime
from os import environ
from dotenv import load_dotenv
from dependencies.Webhook import WebhookBuilder
from dependencies.Facerec import Facerec
colorama.init()
os.makedirs(os.path.join(os.path.dirname(__file__), "logs\\"), exist_ok=True)
logger = logging.getLogger('logger')
fh = logging.FileHandler(os.path.join(os.path.dirname(__file__), "logs\s_cam.log"))
logger.addHandler(fh)
def exc_handler(exctype, value, tb):
logger.exception(''.join(traceback.format_exception(exctype, value, tb)))
sys.excepthook = exc_handler
load_dotenv()
with open(os.path.join(os.path.dirname(__file__), "config.json"), "r") as conf_file:
config = json.load(conf_file)
## SETTINGS:
body_inc = config["camera"]["body_inc"]
face_inc = config["camera"]["face_inc"]
motion_inc = config["camera"]["motion_inc"]
undetected_time = config["camera"]["undetected_time"]
motion_detection = config["settings"]["motion_detection"]
speech = config["settings"]["speech"]
webserver = config["settings"]["webserver"]
notifications = config["settings"]["discord_notifications"]
url = environ["URL"]
cam_n = config["camera"]["main"]
fallback_fps = config["camera"]["fallback_fps"]
# text to speech
engine = pyttsx3.init()
def speak(text):
print(text)
if speech:
engine.say(text)
engine.runAndWait()
# mainloop
if __name__ == '__main__':
multiprocessing.freeze_support()
cap = cv2.VideoCapture(cam_n)
cap.set(3, 1280)
cap.set(4, 720)
detector = PoseDetector(detectionCon=0.5, trackCon=0.5)
webhook = WebhookBuilder(url, os.path.dirname(__file__))
fr = Facerec()
fr.load_encoding_images(os.path.join(os.path.dirname(__file__), r".\images"))
frame_width = int( cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height =int( cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
if fps == 0.0:
fps = fallback_fps
size = (frame_width, frame_height)
# initializing variables
motion_c = 0
face_det = []
face_c = 0
body_c = 0
prev = False
f_reset = False
intruder = True
detected = False
img_thread = None
undetected_c = 0
just_ran = []
v_path = None
check_frame_index = 0
face_list = os.listdir(os.path.join(os.path.dirname(__file__), "images\\"))
def c_face(facelist: list):
faces = {}
for face in facelist:
try:
faces[face]+=1
except KeyError:
faces[face] = 0
highest = 0
highest_n = None
for val in faces:
if faces[val] > highest:
highest = faces[val]
highest_n = val
return highest_n
with pyvirtualcam.Camera(frame_width, frame_height, fps, fmt=PixelFormat.BGR) as cam:
while True:
check_frame_index+=1
if check_frame_index == 50:
check_frame_index = 0
if face_list != os.listdir(os.path.join(os.path.dirname(__file__), "images\\")):
face_list = os.listdir(os.path.join(os.path.dirname(__file__), "images\\"))
fr.load_encoding_images
fr.load_encoding_images(os.path.join(os.path.dirname(__file__), r".\images"))
print("Reloaded faces")
_, frame = cap.read()
## motion
_, frame2 = cap.read()
diff = cv2.absdiff(frame, frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if cv2.contourArea(contour) < 5000:
continue
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 225, 225), 1)
cv2.putText(frame, "Status: {}".format('Movement'), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 225, 225), 2)
cv2.resize(frame, (1280,720))
_, frame2 = cap.read()
if contours != ():
motion_c+=1
motion = True
else:
motion = False
## face
face_locations, face_names = fr.detect_known_faces(frame)
for face_loc, name in zip(face_locations, face_names):
if name == "Unknown":
color = (0, 0, 225)
else:
color = (0, 225, 0)
y1, x2, y2, x1 = face_loc[0], face_loc[1], face_loc[2], face_loc[3]
cv2.putText(frame, name,(x1, y1 - 10), cv2.FONT_HERSHEY_DUPLEX, 1, color, 1)
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 1)
if face_locations.size > 0:
face_c+=1
face = True
face_det.append(name)
else:
face = False
## body
img = detector.findPose(frame, draw=False)
_, bboxInfo = detector.findPosition(img, bboxWithHands=False)
if bboxInfo != {}:
body = True
body_c+=1
else:
body = False
## recorder
now = datetime.now()
file_t = now.strftime("%d-%m-%Y_%H-%M-%S")
path_t = now.strftime("%d-%m-%Y_%H")
path = os.path.join(os.path.dirname(__file__), fr".\clipped\{path_t}")
if body or face:
prev = True
if body_c == 1 or (face_c == 1 and not f_reset) and "recorder" not in just_ran:
result = cv2.VideoWriter(fr"{path}\recording_{file_t}.avi",
cv2.VideoWriter_fourcc(*'MJPG'),
10, size)
v_path = fr"{path}\recording_{file_t}.avi"
just_ran.append("recorder")
if body_c > 1 or face_c > 1:
result.write(frame)
## reset
if not body and not face and not motion and prev:
undetected_c+=1
if undetected_c == undetected_time:
if intruder and body_c > 1 and notifications:
webhook.thread("recording", v_path)
print("Camera reset.")
undetected_c = 0
f_reset = False
detected = False
intruder = True
face_c = 0
face_det = []
prev = False
body_c = 0
motion_c = 0
just_ran = []
## files
if not os.path.exists(path):
os.makedirs(path)
## notification
if motion_detection and motion_c == motion_inc and "motion" not in just_ran:
if not f_reset:
multiprocessing.Process(target=speak, args=["Motion detected"], daemon=True).start()
just_ran.append("motion")
if body_c == body_inc and face_c == 0 and "body_1" not in just_ran:
if not f_reset:
multiprocessing.Process(target=speak, args=["Person detected initiate face detection"], daemon=True).start()
cv2.imwrite(rf"{path}\body_{file_t}.jpg", frame)
just_ran.append("body_1")
elif body_c == body_inc*2 and face_c == 0 and "body_2" not in just_ran:
if not f_reset:
multiprocessing.Process(target=speak, args=["Initiate face detection now you are already on camera"], daemon=True).start()
cv2.imwrite(rf"{path}\body_1_{file_t}.jpg", frame)
just_ran.append("body_2")
elif body_c == body_inc*3 and face_c == 0 and "body_3" not in just_ran:
if not f_reset:
multiprocessing.Process(target=speak, args=["Face not detected"], daemon=True).start()
cv2.imwrite(rf"{path}\body_nf_{file_t}.jpg", frame)
just_ran.append("body_3")
elif body_c == body_inc*4 and face_c == 0 and "body_4" not in just_ran:
cv2.imwrite(rf"{path}\body_nf2_{file_t}.jpg", frame)
if not f_reset:
multiprocessing.Process(target=speak, args=["Intruder detected"], daemon=True).start()
if notifications:
webhook.thread("intruder", rf"{path}\body_nf2_{file_t}.jpg")
just_ran.append("body_4")
elif face_c == 1 and "face_1" not in just_ran:
if not f_reset:
multiprocessing.Process(target=speak, args=["Face detected look into the camera for reconition"], daemon=True).start()
if name == "Unknown":
cv2.imwrite(rf"{path}\unknown_face_{file_t}.jpg", frame)
just_ran.append("face_1")
elif face_c == face_inc:
d_face = c_face(face_det)
if d_face == "Unknown":
cv2.imwrite(rf"{path}\verification_{name}_face_{file_t}.jpg", frame)
if not f_reset:
multiprocessing.Process(target=speak, args=["Unknown face detected"], daemon=True).start()
if notifications:
webhook.thread("unknown", rf"{path}\verification_{name}_face_{file_t}.jpg")
face_c = 0
f_reset = True
elif not detected:
cv2.imwrite(rf"{path}\verification_{name}_face_{file_t}.jpg", frame)
if not f_reset or intruder:
multiprocessing.Process(target=speak, args=[f"Welcome, {name}"], daemon=True).start()
if notifications:
webhook.thread("login", name, rf"{path}\verification_{name}_face_{file_t}.jpg")
face_c = 0
detected = True
intruder = False
f_reset = True
if webserver:
cam.send(img)
cam.sleep_until_next_frame()
cap.release()
cv2.destroyAllWindows()